|
-
Nov 22nd, 2011, 08:17 PM
#1
Thread Starter
New Member
Why MSComm.InBufferCount comparison affectS MSComm.Input content?
Programming Language = Visual Basic 6 (SP6a)
Related To : Having MSComm to accept signal returned from Serial COM Port
My intention is to compare if the MSComm1.InBufferCount is larger than a specified number, then assign the ENTIRE ORIGINAL MSComm1.Input to InputBytes.
My Sample Codes:-
/***
Dim InputBytes() As Byte
Dim iCheckNumber As Interger
Do While MSComm1.PortOpen
If MSComm1.InBufferCount >= iCheckNumber Then 'expect structure length
InputBytes = MSComm1.Input ' In coming data from serial port
Exit Do ' Exit loop
Else
DoEvents
End If
Loop
***/
My Finding:-
If iCheckNumber = 50 ; InputBytes = `00000000066090083628147000000416902588
If iCheckNumber = 75 ; InputBytes = `00000000066090080911987000000416903588734702681597949121544
My Question:-
I would like to know whether my findings is true? Is there any better way to achieve the result I wish.
Thanks
Regards,
Mr.SU
-
Nov 22nd, 2011, 11:54 PM
#2
Re: Why MSComm.InBufferCount comparison affectS MSComm.Input content?
The MSComm internal buffer is being filled up asychronously, in your first example once 50 bytes or more are available to be read you fetch them from the buffer, meanwhile more data is put into the buffer.
Normally, I would expect the sender to terminate the transmission with some sort of framing character / sequence, so you know when all the data has been received, or include the message length as part of the message so you know how many bytes to expect.
-
Nov 23rd, 2011, 11:55 PM
#3
Thread Starter
New Member
Re: Why MSComm.InBufferCount comparison affectS MSComm.Input content?
Basically I need to detect 2 scenarios
{{{
If MSComm1.InBufferCount >= 75 Then
msgbox "EDC : FULL RECEIVED SUCCESS"
InputBytes = MSComm1.Input
Exit Do ' Exit loop
ElseIf MSComm1.InBufferCount >= 50 And _
MSComm1.InBufferCount < 75 Then
msgbox "EDC : DECLINE"
InputBytes = MSComm1.Input
Exit Do ' Exit loop
Else
DoEvents
End If
}}}
I noticed even though MSComm1.InBufferCount is 100, still it matched msgbox "EDC : DECLINE"
FYI, the sender does not terminate the transmission with any framing character / sequence.
After read the buffer, then perform an algorithm to segregate the meaningful content from left-to-right. If got balance characters, then ignore them.
-
Nov 24th, 2011, 02:09 AM
#4
Re: Why MSComm.InBufferCount comparison affectS MSComm.Input content?
The problem you have is that you don't know when the sender has finished sending and all the data has been received. As I said before, the buffer is being filled asynchronously so it is quite possible that when the line
Code:
If MSComm1.InBufferCount >= 75 Then
is executed, there are 50 bytes in the buffer, so your code will branch to
Code:
ElseIf MSComm1.InBufferCount >= 50 And _
MSComm1.InBufferCount < 75 Then
by which time there may be 60 bytes in the buffer, so the "Decline" MsgBox will be displayed and by the time you get to
Code:
InputBytes = MSComm1.Input
there may be 100 bytes in the buffer.
Without any sort of handshaking going on you may just have to use a time delay and hope that by the time you read the information, it's all there. That can be a bit hit-and-miss.
Do you have any control over the sending software?
Is the device using hardware handshaking perhaps?
Last edited by Doogle; Nov 24th, 2011 at 03:50 AM.
-
Nov 28th, 2011, 09:41 PM
#5
Thread Starter
New Member
Re: Why MSComm.InBufferCount comparison affectS MSComm.Input content?
I add in "delay (0.5)" , now is okay. Thank You!
Working Codes:-
/***
Dim InputBytes() As Byte
Dim iCheckNumber As Interger
Do While MSComm1.PortOpen
If MSComm1.InBufferCount >= iCheckNumber Then 'expect structure length
delay (0.5) '''##add in this line##
InputBytes = MSComm1.Input ' In coming data from serial port
Exit Do ' Exit loop
Else
DoEvents
End If
Loop
***/
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
|