Visual Basic on Visual Studio 17 - manipulation of string list data RRS feed

  • Question

  • The program is communicating to a hardware device over a serial port.  It sends data requests to the device (several per second) and the device responds with a string of characters delimited by a semicolon. The send and receive tasks run asynchronously and I needed a technique whereby the receiving routine stored the incoming messages into a buffer where a separate thread would pull the oldest message out of the buffer to process it and at the same time remove the message from the buffer. 

    The buffering technique I chose to use is a list of strings.  The receiving routine reads from the serial port and uses the .Add method to add the new message to the list as shown here:

     If SP1.BytesToRead > 1 Then    
                        incoming = SP1.ReadLine
                    Catch ex As Exception
                        MessageBox.Show("Stack Trace: " & vbCrLf & ex.StackTrace)
                    End Try


                    (more code follows)

    Running in debug mode and watching the responseList shows that items are being properly added to the list.

                    Code in the processing thread looks like this:

                If responseList.Count > 0 Then
                    receivedResponse = responseList.First
                    listCount = responseList.Count

    The receivedResponse variable does retrieve the first item in the list correctly.

    Code later in the processing thread deletes the first item in the string list this way:

     If listCount > 0 Then  

    And that code removes the first item correctly.  Now, the problem.  When all threads are running, with commands being sent to the device and responses being received, this error occurs in the receiving routine at the Try-Catch code:

    When that happens, via Watch in debug I can see that my string list has a few items in it, the serialport.bytestoread is > zero and the serialport.newline is set to ";" properly.  So, I'm at a loss as to why the "value" item appears to be null.  Any ideas?

    Friday, January 25, 2019 7:24 PM

All replies