Avatar of Malek103197
Malek103197
 asked on

MSComm OCX - buffering

I am reading data (ASCII string ) from the serial port that often gets broekn up. What I mean is this -

The sting has a start and end character to deliminate the entire string -
<sasrwaskdmswriuwriwrwer> where there is a fixed number of charcters.

However, the string often arrives in pieces. There may be <sasrwask that arrives, followed soon
after by the rest (i.e. dmswriuwriwrwer>).

I need to assemble the string back together and then bring it into my app. (VB).  I assume I need to keep the parts
in a buffer and then assemble them to make up the proper string.

Can anyone provide me with some hints on how to do this. I have examined the VBTerm sample
but not sure if it can assemble the parts of the string.

Thanks
Visual Basic Classic

Avatar of undefined
Last Comment
CleanupPing

8/22/2022 - Mon
mujahid_ikram

add an api in ur project of sleep

Public Declare Sub Sleep Lib "kernel32" Alias "Sleep" (ByVal dwMilliseconds As Long)

in oncomm of communication control

Private Sub MSComm1_OnComm()
sleep (50)
' here ur code
End Sub

you will get complete data
Malek103197

ASKER
Thanks, I'll try it and get back to you on the results. Might be a few days before I respond.

Cheers
ASKER CERTIFIED SOLUTION
JSMCM

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Malek103197

ASKER
Are you familiar with the VBTerm sample program?

Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Malek103197

ASKER
OK, I've done some tests using the VBTerm sample and here's what is happening -

I've got one instance of the VBTerm sending out text -
MSComm1.Output = ">R84999805315723040202A0308269N10141581E000198<"

On the second instance of VBTerm I've got -

In the OnComm sub there is -

Select Case MSComm1.CommEvent
        ' Event messages.
        Case comEvReceive
            Dim Buffer As Variant
            Buffer = MSComm1.Input
            Debug.Print "Receive - " & StrConv(Buffer, vbUnicode)
            ShowData txtTerm, (StrConv(Buffer, vbUnicode))


Private Static Sub ShowData(Term As Control, Data As String)

The Data variable ends up broken into parts -

">R849998"
"05315723"
"040202A0"
"308269N1"
"0141581E"
"000198<"
">R849998"
"05315723"
"040202A0"
"308269N1"
"0141581E

For testing I am saving the Data to a text file for analysis and I get the above in the text file. Is there a way to avoid re-assemling the string?
Malek103197

ASKER
OK, I think I have figured out half my problem.

In the onComm Sub I now have -

Case comEvReceive
           
            Dim thePos1, thePos2 As Integer
            Buffer = Buffer & MSComm1.Input 'Buffer is declared on top of Form
            thePos1 = InStr(StrConv(Buffer, vbUnicode), ">")
            thePos2 = InStr(StrConv(Buffer, vbUnicode), "<")
            If thePos1 And thePos2 Then
              Debug.Print "Receive - " & StrConv(Buffer, vbUnicode)
              ShowData txtTerm, (StrConv(Buffer, vbUnicode))
              MSComm1.InBufferCount = 0
              Buffer = ""
            End If

So far this seem to work. Sometimes a part of the string is delayed in arriving at the comm port. Therefore, I'll need a timer as a 'Time Out" timer. If the timer exceeds the thresh hold then throw the buffor away.
Malek103197

ASKER
To those who contributed: I sort of figured out my own problem once I was familiar with the VBTerm code, and so far it is working fine.

So what do I do now?I hate to leave questions hanging!
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Malek103197

ASKER
To those who contributed: I sort of figured out my own problem once I was familiar with the VBTerm code, and so far it is working fine.

So what do I do now?I hate to leave questions hanging!
CleanupPing

Malek:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
Experts: Post your closing recommendations!  Who deserves points here?