Serial Port: ReadByte never returns

I have a thread that run continuously and read data from a serial port. Randomly, sometimes, it never returns from the ReadByte routine. Any suggestions?


Please see attached code.
private void UartReaderThread()
        {
            m_SerialPort.ReadTimeout = 100;
            while (m_RunDataAcquisitionThreads)
            {
                try
                {
                    List<byte> bBuffer = new List<byte>();
                    for (int i = 0; i < 3; i++)
                    {
                        bBuffer.Add((byte)m_SerialPort.ReadByte());
                    }

                    lock (m_BufferQueueLock)
                    {
                        m_BufferQueue.Enqueue(bBuffer);
                    }
                    Thread.Sleep(5);
                }
                catch (TimeoutException)
                {
                }
                catch (IOException)
                {
                }
                catch (Exception Ex)
                {
                    MessageBox.Show(string.Format("{0}: {1}", m_Name, Ex.Message), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

Open in new window

ychuriAsked:
Who is Participating?
 
TomasPCommented:
I have seen two solutions to this problem. One requires a change to read() rather than readbyte()
The second solution requires that rather than use Any CPU when building your exe, you select the Compile to x86. This is especially true if you are running Windows x64. Some thunking logic perhaps
The second solution seemed to work for most people
0
 
ychuriAuthor Commented:
Answer to generic to the specific case that was asked
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.