Results 1 to 5 of 5

Thread: Why MSComm.InBufferCount comparison affectS MSComm.Input content?

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2011
    Posts
    3

    Talking 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 = ` 00000000066090083628147 000000416902588
    If iCheckNumber = 75 ; InputBytes = ` 00000000066090080911987 000000416903588734702681597949121544

    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

  2. #2
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    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.

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2011
    Posts
    3

    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.

  4. #4
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    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.

  5. #5

    Thread Starter
    New Member
    Join Date
    Nov 2011
    Posts
    3

    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
  •  



Click Here to Expand Forum to Full Width