Results 1 to 10 of 10

Thread: [RESOLVED] MSComm Serial Receive issue

  1. #1

    Thread Starter
    Addicted Member beic's Avatar
    Join Date
    Jun 2012
    Posts
    150

    Resolved [RESOLVED] MSComm Serial Receive issue

    Hi there,

    I trying to receive/collect data and parse from my microcontroller, but for some reason I got data received with random breaks and I can't parse them while receiving.

    Code:
    Private Sub comRS232_OnComm()
     
     Dim vrBuffer As String
     
     Dim lgFwV As Long
     Dim lgFwDate As Long
     Dim lgFwTime As Long
     
     comRS232.InputLen = 0
     
     vrBuffer = ""
     
     vrBuffer = comRS232.Input
     
     lgFwV = InStr(vrBuffer, "FWVER=")
     
     lgFwDate = InStr(vrBuffer, "DATE=")
     lgFwTime = InStr(vrBuffer, "TIME=")
     
     If lgFwV > 0 Then
      lblFwVer.Caption = Mid$(vrBuffer, lgFwV + 6, 3)
     End If
     
     If lgFwDate > 0 Then
      lblFwDate.Caption = Mid$(vrBuffer, lgFwDate + 5, 11)
     End If
     
     If lgFwTime > 0 Then
      lblFwTime.Caption = Mid$(vrBuffer, lgFwTime + 5, 8)
     End If
     
     Debug.Print vrBuffer
     
     txtSerial.Text = txtSerial.Text + vrBuffer
     txtSerial.SelStart = Len(txtSerial.Text)
     
     Exit Sub
    End Sub
    I got these differend Debug output receiving from two idetical microcontroller board, but with different serial chip:

    Board 1:
    Code:
    RDW Rem
    ote Cont
    rol
    
    
    FWV
    ER=1.2
    
    DATE
    =Sep
     18 
    2018
    
    T
    IME=
    13:3
    4:23
    Board 2:
    Code:
    RDW Remote Control
    FWVER=1.2
    D
    ATE=Sep 18 2018
    TIME=13:34:23
    The board sketch is very simple one:
    Code:
    void setup()
    {
      Serial.begin(9600);
      while (!Serial)
      {
        delay(10);
      }
    
      delay(500);
    
      Serial.print(F("RDW Remote Control"));
      Serial.println(F(""));
      Serial.println(F("FWVER=1.2"));
      Serial.println(F("DATE=Sep 18 2018"));
      Serial.println(F("TIME=13:34:23"));
      Serial.println(F(""));
    }
    They are using all the same serial connection settings:
    Code:
    9600, 8, N, 1
    I already posted my issue on the official board forum, but they say it's not board relevant and it's must be in my VB6 code.

    Can someone tell me what I'm doing wrong?

    Thank you!

  2. #2
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,418

    Re: MSComm Serial Receive issue

    If your setup is always the same, why not just replace all linebreaks, blanks (and whatnot) with vbnullstring, and then search your fields you're looking for?
    btw: What do you have for RThreshold?
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  3. #3
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: MSComm Serial Receive issue

    Serial Data is a stream, and the serial port has no idea how long your data is so it just sends you an event when it has received some data. You can control how often it sends the event based on the RThreshold value.
    Most likely, since it looks like you sent two (end of line) values in a row at the end of your data, you could just collect the Input into a string each time you get the event until you see the double end of line sequence.
    You could then parse the data and clear the string, ready to accumulate the next stream of characters into the string for your next message.

  4. #4

    Thread Starter
    Addicted Member beic's Avatar
    Join Date
    Jun 2012
    Posts
    150

    Re: MSComm Serial Receive issue

    Quote Originally Posted by Zvoni View Post
    If your setup is always the same, why not just replace all linebreaks, blanks (and whatnot) with vbnullstring, and then search your fields you're looking for?
    btw: What do you have for RThreshold?
    Okay, can you please show me some in-line example?

    And sorry I forgot to post my full serial settings:

    Code:
    With frmMAIN.comRS232
      .CommPort = frmMAIN.txtPort.Text
      .Settings = frmMAIN.txtBaud.Text & "," & frmMAIN.txtParity.Text & "," & frmMAIN.txtData.Text & "," & frmMAIN.txtStop.Text
      .InputLen = 0
      .InputMode = comInputModeText
      .InBufferSize = 1024
      .OutBufferSize = 512
      .EOFEnable = False
      .Handshaking = comNone
      .RThreshold = 1
      .SThreshold = 1
      
      If blConnectOnStartup = True Then
       .PortOpen = True
      End If
     End With
    Thank you for your support!

  5. #5

    Thread Starter
    Addicted Member beic's Avatar
    Join Date
    Jun 2012
    Posts
    150

    Re: MSComm Serial Receive issue

    Quote Originally Posted by passel View Post
    Most likely, since it looks like you sent two (end of line) values in a row at the end of your data, you could just collect the Input into a string each time you get the event until you see the double end of line sequence.
    Code:
    println
    means that it will print/send out with new line.

    I tried already like this:

    Code:
    If comRS232.CommEvent = comEvReceive Then
     vrBuffer = vrBuffer & comRS232.Input
    End If
    and I got in Debug.Print vrBuffer result:
    Code:
    RDW R
    emot
    e Co
    ntro
    l
    F
    WVER=1.
    2
    D
    ATE=
    Sep 
    18 2
    018
    
    
    TIM
    E=13
    :34:
    23
    But in my txtSerial.Text output it's showing the correct data?!

    Name:  serial_output.png
Views: 295
Size:  3.1 KB
    Last edited by beic; Sep 18th, 2018 at 12:48 PM. Reason: typo

  6. #6
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: MSComm Serial Receive issue

    So, the debug print shows you're receiving some number of characters each event, so the full message comes across in 18 or more pieces.
    You accumulate these pieces in the textbox, so see the fully assembled message. You need to accumulate these pieces in your string until you have the full message to parse, or you could accumulate lines, and parse as you go, but you may as well just accumulate the full message as your code is basically making the assumption you have the full message when you parse.

    You do a println with the last data line followed by a println with an empty string, so there should be two line terminations in a row at the end of your data.
    I don't have a serial setup to test, but given your original code, something along the lines of :
    Code:
    Private Sub comRS232_OnComm()
     
     Static vrBuffer As String
     
     Dim lgFwV As Long
     Dim lgFwDate As Long
     Dim lgFwTime As Long
     
     comRS232.InputLen = 0
     
    'rem  vrBuffer = ""
      Dim EndOfMsgFlag As String
      EndOfMsgFlag = vbLf & vbLf  'or possibly vbCrLf & vbCrLf depending on what your line terminations are.
      vrBuffer = vrBuffer & comRS232.Input   'Add whatever characters have been received this event to the string
    
      If InStr(vrBuffer, EndOfMsgFlag) <> 0 Then  'vrBuffer has all the data, including the two line terminations at the end
     
        lgFwV = InStr(vrBuffer, "FWVER=")
     
        lgFwDate = InStr(vrBuffer, "DATE=")
        lgFwTime = InStr(vrBuffer, "TIME=")
     
        If lgFwV > 0 Then
          lblFwVer.Caption = Mid$(vrBuffer, lgFwV + 6, 3)
        End If
     
        If lgFwDate > 0 Then
          lblFwDate.Caption = Mid$(vrBuffer, lgFwDate + 5, 11)
        End If
     
        If lgFwTime > 0 Then
          lblFwTime.Caption = Mid$(vrBuffer, lgFwTime + 5, 8)
        End If
     
        Debug.Print vrBuffer
     
        txtSerial.Text = txtSerial.Text + vrBuffer
        txtSerial.SelStart = Len(txtSerial.Text)
    
       vrBuffer = ""   'we've parsed the complete message so empty the string to accumulate the next message  
      End If    
    End Sub
    Note that vrBuffer is declared to be static, so that it will hold its previous contents from one event to the next. If you "Dim" it, it goes away at the end of the sub and is created anew every time the sub is called.
    Last edited by passel; Sep 18th, 2018 at 02:27 PM.

  7. #7

    Thread Starter
    Addicted Member beic's Avatar
    Join Date
    Jun 2012
    Posts
    150

    Re: MSComm Serial Receive issue

    Quote Originally Posted by passel View Post
    You do a println with the last data line followed by a println with an empty string, so there should be two line terminations in a row at the end of your data.
    Your implementation just worked great, I got working it with vbCrLf & vbCrLf, did try to rework everithing (both source) just for vbCrLf, but that was not working for some reason, don't know why.

    Quote Originally Posted by passel View Post
    Note that vrBuffer is declared to be static, so that it will hold its previous contents from one event to the next. If you "Dim" it, it goes away at the end of the sub and is created anew every time the sub is called.
    It's nice that you pointed me out for that!

    Also noticed that, if there is a MsgBox shown while receiving data, after pressing OK button on it, the Buffer will output like this:
    Code:
    T
    EMP=25.40
    HUMI=58.50
    Is it because of blocking?

    Thank you for your great support!

  8. #8
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: MSComm Serial Receive issue

    I'm not sure how often your data is sent, or how long the msgbox is displayed, but there could be a buffer overrun I suppose if the msgbox is up and the data is continuing to come in. I would have thought the I/O would be on another thread, so would buffer data even with the msgbox up. You would think there would be enough buffer (I'm thinking 1024 bytes), in the serial control, but I haven't played with VB6 serial in a long time, and not too much serial data connections even when I did.
    I could see that depending on when you started, the first shot could be a partial message, but after you've found the first double newline instance, the reading would be synced up after that.
    Anyway, I guess I'm done.

  9. #9

    Thread Starter
    Addicted Member beic's Avatar
    Join Date
    Jun 2012
    Posts
    150

    Re: MSComm Serial Receive issue

    Quote Originally Posted by passel View Post
    I'm not sure how often your data is sent
    It is sent every 2 seconds.

    Quote Originally Posted by passel View Post
    but there could be a buffer overrun I suppose if the msgbox is up and the data is continuing to come in.
    Yes, thats right, it's buffer overrun, because the msgbox is up.
    Now I replaced the msgbox with beep for now, so we solved that too.

    Quote Originally Posted by passel View Post
    Anyway, I guess I'm done.
    Yes, and thank you again, you really helped me alot! +1
    Last edited by beic; Sep 18th, 2018 at 04:39 PM. Reason: typo

  10. #10
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: [RESOLVED] MSComm Serial Receive issue

    It is never a bad idea to wrap your transmissions in start and/or end characters. I almost always do this so when receiving I can be 100% sure I am parsing the right bit of data even when it is variable length. Is a good habit to get into and can avoid a lot of headaches.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width