|
-
Sep 18th, 2018, 02:19 PM
#6
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.
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|