none
Question working with serial data received.

    Question

  • I followed an online demo that works well, but I am wanting to automate and loop the write command so I can gather 12 samples from an instrument. The problem I am having now is how to determine when the read is done and how to tell the write to capture another sample?   In the end i'd like 12 samples in the richtextbox, and if I get "ER00" I would like to recollect another sample. Any help for the noob, would be appreciated

    read routine:

        Private Sub SerialPort1_DataReceived(sender As Object, e As SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
            receivedtext(SerialPort1.ReadExisting())
        End Sub
    
        Private Sub receivedtext(ByVal [text] As String)   'display results
            Dim tempArray() As String
    
            If Me.OutputTB.InvokeRequired Then
                Dim X As New SetTextCallBack(AddressOf receivedtext)
                Me.Invoke(X, New Object() {(text)})
            Else
                Me.OutputTB.Text &= text
            End If
        End Sub

    Sunday, March 26, 2017 3:11 AM

All replies

  • In the end i'd like 12 samples in the richtextbox, and if I get "ER00" I would like to recollect another sample. Any help for the noob, would be appreciated

    With serial data there is no such thing as 'done', and you won't use a loop.  What you are looking for is a character sequence in the data stream that indicates that the last character in a message has been received.

    You need to identify what constitutes a 'sample':  it will typically be done by a start character (or sequence) and an end character (or sequence) or it might be a start and length. Then, whenever the text in the text box changes, you should scan that text and see if you can find that start and end.  When you do, remove it from the text, and process it as one message.  From the description you have provided, that might mean that you need to issue a new request for data.

    You might like to maintain a string separate from the text box text that you use for this processing, so that you can remove garbage characters (likely with a serial connection) and discard each message as it is identified and processed, so you don't process it again.  You could maintain this string and the associated processing either in the data received event handler, the text box changed event, or in a timer tick event. The advantage of using a timer tick event is that you can create a 'watchdog' counter. It gets incremented at each tick, then reset when you detect a completed message. If it reaches a certain value that means communication has stopped, or the data has become garbled and you can no longer get a complete message from it.

    Sunday, March 26, 2017 3:47 AM
  • This is helpful, I have really two conditions that apply. one is ER00  which is an error condition that I find when I'm writing while the serial is reading. Requiring the need for a additional sample to get to my magic number of 12. The valid condition starts with OK00 which returns the actual string of OK00,CfPH 0.982 and I have another sub that basically grabs the right 4 places. I kind of have a handle on parsing, not the collection effort.

    Pardon  my ignorance, but I can't seem to monitor the richtextbox without running into errors or truncating the results. Any suggestions on the correct approach?

    Sunday, March 26, 2017 5:00 PM
  • This is helpful, I have really two conditions that apply. one is ER00  which is an error condition that I find when I'm writing while the serial is reading. Requiring the need for a additional sample to get to my magic number of 12. The valid condition starts with OK00 which returns the actual string of OK00,CfPH 0.982 and I have another sub that basically grabs the right 4 places. I kind of have a handle on parsing, not the collection effort.

    Pardon  my ignorance, but I can't seem to monitor the richtextbox without running into errors or truncating the results. Any suggestions on the correct approach?

    What is the structure of the message?  What are you using to identify the start and end of the message?

    For assistance with the parsing you will need to show the code you are using and the error that you are getting.

    Sunday, March 26, 2017 9:11 PM