none
SerialPort problem usage higth CPU percentage RRS feed

  • Question

  • hi all,

    this methoid implements my thread:

    public override void SPThreadProc(List<byte[]> addressList, int nSizeofBasicType)
    {
    	stopRequest = false;
    
    	eventThreadStopped.Reset();
    
    	myThread = new Thread(this.ThreadProc);
    	myThread.Name = "ThreadProc";
    
    	ThreadParams tParams = new ThreadParams();
    	tParams.AddressList = addressList;
    	tParams.SizeofBasicType = nSizeofBasicType;
    
    	myThread.Start(tParams);
    }


    protected override void ThreadProc(object data)
    {
        ThreadParams tParams = (ThreadParams)data;
        stopRequest = false;
    
        byte[] buffer = new byte[(tParams.AddressList.Count * sizeof(uint))];
        for (int i = 0; i < tParams.AddressList.Count; i++)
        {
            byte[] addr = tParams.AddressList[i];
            Buffer.BlockCopy(addr, 0, buffer, addr.Length * i, addr.Length);
        }
    
        if (InnerReadRND(buffer, (byte)buffer.Length) == true)
        {
            InnerTraceRun();
    
            uint id = 0;
            uint idTrace = 0;
            uint loop = 0;
            byte chPrev = 0;
            int retry = 0;
            do
            {
                if (ReadBuffer() == true)
                {
                    // Indice del frame.
                    //
                    id++;
                    byte[] temp = serprtParameters.OutputFrame;
    
                    // Indice del protocollo.
                    //
                    byte ch = temp[serprtParameters.FrameHeaderSize + tParams.AddressList.Count * tParams.SizeofBasicType];
                    if (ch < chPrev)
                        loop++;
                    chPrev = ch;
                    idTrace = ch + loop * (byte.MaxValue + 1);
    
                    if (temp[5] - 1 >= tParams.AddressList.Count * tParams.SizeofBasicType)
                    {
                        OnTraceProgressChanged(new TraceProgressChangedArgs(id, idTrace, temp.Skip(6).Take(temp[5] - 1).ToArray()));
                        retry = 0;
                    }
                    else
                        retry = 3;
                }
                else
                    retry++;
            }
            while ((stopRequest == false) && (retry < 3));
    
            // Richiesta di interruzione del trace.
            //
            InnerStopTraceRun();
            eventThreadStopped.Set();
    
            OnTraceRunComplete(new TraceRunCompleteArgs(retry != 3));
        }
        else
        {
            eventThreadStopped.Set();
    
            OnTraceRunComplete(new TraceRunCompleteArgs(false));
        }
    }

    The problem that use all percentage of cpu is ReadBuffer method that is implemented in the following code:

    protected virtual bool ReadBuffer()
    {
        long elapsed = 0;
        Stopwatch stopWatch = Stopwatch.StartNew();
        bool res = false;
        do
        {
            if (ReadFromChannel() == true)
                res = SerdebHooker();
    
            elapsed = stopWatch.ElapsedMilliseconds;
        }
        while ((res == false) && (elapsed < channel.ReadTimeout));
        stopWatch.Stop();
    
        return res;
    }
    
    protected virtual bool ReadFromChannel()
    {
        bool res = false;
        int availableBytes = 0;
        try
        {
            availableBytes = serprtParameters.GetAvailableBytes();
            if (availableBytes > (SerprtParameters.BufferSize / 8))
                res = true;
    
            if (res == false)
            {
                byte[] temp = channel.Read();
                if (temp != null)
                {
                    serprtParameters.CopyToBuffer(temp);
                    res = true;
                }
            }
        }
        catch (Exception)
        {
            res = false;
        }
    
        return res;
    }
    
    public override byte[] Read()
    {
        int byteToRead = BytesToRead;
        if (byteToRead > 0)
        {
            byte[] temp = new byte[byteToRead];
            serialPort.Read(temp, 0, byteToRead);
    
            return temp;
        }
    
        return null;
    }

    Is it possible optimize usage of CPU in my ReadBuffer method ?

    thanks


    Tuesday, June 19, 2018 7:05 AM

All replies

  • Hi stefanomi74,

    Thank you for posting here.

    In your ReadBuffer() method, I think which cause the most usage of UPC is the do while loop. We could try to find some way to reduce the loop.

    Two ways which could stop the loop, you do not provide details about both of them. Please provide more information about SerdebHooker and channel.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, June 20, 2018 8:47 AM
    Moderator