|
-
Sep 18th, 2018, 02:19 PM
#1
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.
-
Sep 18th, 2018, 03:43 PM
#2
Thread Starter
Addicted Member
Re: MSComm Serial Receive issue
 Originally Posted by passel
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.
 Originally Posted by passel
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!
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
|