can you sum it up I cant hardly understand what you say.
Do you mean that I can still use the code as long as a delay is present in the sending of data?
Printable View
can you sum it up I cant hardly understand what you say.
Do you mean that I can still use the code as long as a delay is present in the sending of data?
Yes, you could use it if the delay is long enough.
Have you tried it?
I'd say yes. VB is considerably fast and parsing for the separators does take no time compared to the transmission of a character, which takes a fixed time. If your baud rate is below 56600 you will not get any problem with the described code. Even if processing received numbers would suppres one or two OnComm events, the characters are buffered by MSComm and read with the next firing of OnComm().
I don't know what your program is gonna do after having received three numbers. I'm sure you will perform something, before the next set of three numbers is received.
But if your processing should really take long time you'd have to see if you cannot use hardware handshake to make the transmitter wait until the receiver is ready to process more data. This depends on how the RS232 is implemented on your microcontroller. Is it 3-wire-only or is it a full Rs232 with CTS,RTS,DTR,DSR signals?
Without knowing what the other end is doing there is no way to say for sure. But keep in mind that the code is testing the right 2 characters if there is no pause in between the groups of data this will almost certianly fail.
AAfter my experience it is not likely to fail, but if you are in doubt we shall modify the code like that:
This way we only pull the valid line from the RLine buffer, leaving everything else in it.Code:Private Num1%, Num2%, Num3%
Private RLine$
Private Sub MSComm1_OnComm()
RLine = RLine + MSComm1.Input
If Len(RLine>2) Then
dim p%
p = Instr(RLine, vbCrLf)
If p Then ' a complete record with three numbers has been received now
dim a$()
a = Split(Left$(RLine,p-1), "/") 'split the line into the three numbers
If UBound(a) = 2 Then
Num1 = a(0) 'get first number
Num2 = a(1) 'get second number
Num3 = a(2) 'get third number
End If
RLine = Mid$(RLine,p+2) 'reset the receiver variable for the next line
End If
End If
End Sub
Also we make a security check so as only a valid three number line is accepted. This avoids wrong input when switching into a running stream of incoming data. After the first vbCrLf was received the reception will run fully synchronuous.
Now this is a safe way. I have already implemented receivers like this with speeds up to 56600 baud without any breaks or pause between the records.
Yep, That should work fine. The pervious method would fail in many cases where back to back transmissions were recieved.
For example your reads may get something like
12
3/456
/456CR
LF23
in which case the first method would not process the data [last 2 characters are not CRLF at anytime ] and this senario or any one of a thousand others is very likely to occur if the data is not broken up by pauses.
Yeah. Right.
First thing was more or less a hack to get things going, showing the principle sort of thing. :)
Although it is likely to work ok after the first record, because it will always be fast enough to react as soon as the LF from CrLf is received, making the Right$(RLine,2) a vbCrLf.
Only ever the first record could be too short then, as the transmission has already started earlier.
I'am only using CR at the end. Is it still ok, your code use VBCRLF?
So the output of this will be each of three digit number, like:
345 452 128
I encounter error when using the code, this error is in this section " If Len(RLine>2)"
that is a syntax error should be if Len(Rline)>2
but no the code will not work properly you must change it to suit what you are sending. This should be pretty clear.
Yes.
If you are using only Cr, there are no 2 characters at the end, so you have to use
If Len(RLine) > 1 Then...
This statement was for avoiding error if the string is empty. In this last version of the routine you can leave it away anyway (together with the matching End If of course).
It was only to avoid problems with the Right$(RLine,2), which is not used anymore in the recent version.
The INSTR() statement is the one that will need to be changed. Right now is looking for vbCRLF will never be true if sending just a cr.
Right. The Mid$() statement either. So let's resume and finally make a working procedure:
Code:Private Sub MSComm1_OnComm()
Dim p%
RLine = RLine + MSComm1.Input
p = Instr(RLine, vbCr)
If p Then ' a complete record with three numbers has been received now
dim a$()
a = Split(Left$(RLine,p-1), "/") 'split the line into the three numbers
If UBound(a) = 2 Then
Num1 = a(0) 'get first number
Num2 = a(1) 'get second number
Num3 = a(2) 'get third number
End If
RLine = Mid$(RLine,p+1) 'reset the receiver variable for the next line
End If
End Sub
"Private Num1%, Num2%, Num3%
Private RLine$"
How to declare this i get an error in this.
Is it ok if I use CR only? Compared to CRLF which better?
when you say vbCR is it the same as CR?
Is there something missing in the code, like "If Len(RLine) > 1"?
when I try the code there are no output in the textbox, what should be the output? it is something like this:
452 241 342
it should output a 3 digit right?
to output the result shoud I do it like this "text1.text=Num1"?
there are no output when I do it like this.
1: no quotes in definitions; Private does not apply inside sub routines, use Dim instead
2: Yes you can use CR or for that matter you can use any character that is not going to be part of your data. What you use is up to you. The code will have to be modified to suit the character you choose. There should be plenty of info in this thread at this point for you to figure out what you need to do.
Nonono. Don't Dim within the sub. Num1%, Num2%, Num3% and RLine MUST NOT be declared WITHIN the OnComm() event. They were meant to be declared Private within the Form. That is, the declarations
are OUTSIDE the sub, on top of the Form's code. There a Private does not give an error.Code:Private Num1%, Num2%, Num3%
Private RLine$
It was thought that further processing would happen outside the OnComm event, so the Num and RLine variables were made to be known to other subs as well (private to the form).
But to stop confusing, here is a version that would do everything within the OnComm event.
This assumes that you have 3 TextBoxes Text1, Text2 and Text3 to take the numbers. So we don't need the Num variables anymore, as we put the results directly to the textboxes.Code:Private Sub MSComm1_OnComm()
Dim p%
Static RLine$
RLine = RLine + MSComm1.Input
p = Instr(RLine, vbCr)
If p Then ' a complete record with three numbers has been received now
dim a$()
a = Split(Left$(RLine,p-1), "/") 'split the line into the three numbers
If UBound(a) = 2 Then
Text1.Text = a(0) 'get first number
Text2.Text = a(1) 'get second number
Text3.Text = a(2) 'get third number
End If
RLine = Mid$(RLine,p+1) 'reset the receiver variable for the next line
End If
End Sub
If further processing is really required, the values could be taken from the textboxes .Text properties again. This is as good as a private variable.