Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MSComm receive data

Status
Not open for further replies.

fabianuat

Programmer
Jan 4, 2007
4
0
0
US
Hello

Im using the MSComm to send and receive binary data to a video server, the sending part works fine, but when a receive more than 8 byets, the input variable only keeps the bytes over the eighth byte. ...Please some can some one tell me where im wrong??? these are the settings and the OnComm part

MSComm1.CommPort = 1
MSComm1.Settings = "38400,O,8,1"
MSComm1.InputMode = 1
MSComm1.InputLen = 0
MSComm1.RThreshold = 1
MSComm1.SThreshold = 1
MSComm1.DTREnable = True
MSComm1.RTSEnable = True
MSComm1.NullDiscard = False
If MSComm1.PortOpen = False Then
MSComm1.PortOpen = True
End If



Private Sub MSComm1_OnComm()
Dim buffer As Variant
Dim Arr() As Byte
Dim i As Integer
Dim iTemp As Integer
Dim sTemp As String
Dim strINPUT As String

Select Case MSComm1.CommEvent
Case comEvReceive
Do While MSComm1.InBufferCount > 0
buffer = MSComm1.Input
Arr = buffer

For i = LBound(Arr) To UBound(Arr)
iTemp = Asc(Chr$(Arr(i)))
sTemp = Hex$(iTemp)

If Len(sTemp) = 1 Then 'For display in a text box
strINPUT = strINPUT & "0" & sTemp & " "
Else
strINPUT = strINPUT & sTemp & " "
End If

Text1.Text = strINPUT

Next
Loop

 
It appears that you are replacing the contents of "buffer" on each pass through the loop rather than concatentaing new data to earlier data. Try something like
Code:
[b]Private[/b] [b]Sub[/b] MSComm1_OnComm()
[b]Dim[/b] buffer                      [b]As[/b] [b]Variant[/b]
[b]Dim[/b] Arr()                       [b]As[/b] [b]Byte[/b]
[b]Dim[/b] i                           [b]As[/b] [b]Integer[/b]
[b]Dim[/b] iTemp                       [b]As[/b] [b]Integer[/b]
[b]Dim[/b] sTemp                       [b]As[/b] [b]String[/b]
[b]Dim[/b] strINPUT                    [b]As[/b] [b]String[/b]

[b]Select[/b] [b]Case[/b] MSComm1.CommEvent
    [b]Case[/b] comEvReceive
        [b]Do[/b] [b]While[/b] MSComm1.InBufferCount > 0
            buffer = buffer & MSComm1.[b]Input[/b]
        [b]Loop[/b]
[b]End[/b] [b]Select[/b]

Arr = buffer
[b]For[/b] i = [b]LBound[/b](Arr) [b]To[/b] [b]UBound[/b](Arr)
    iTemp = Asc(Chr$(Arr(i)))
    sTemp = Hex$(iTemp)

    [b]If[/b] [b]Len[/b](sTemp) = 1 [b]Then[/b]
        strINPUT = strINPUT & [navy]"0"[/navy] & sTemp & [navy]" "[/navy]
    [b]Else[/b]
        strINPUT = strINPUT & sTemp & [navy]" "[/navy]
    [b]End[/b] [b]If[/b]

    Text1.Text = strINPUT
[b]Next[/b]
[b]End[/b] [b]Sub[/b]
 
The number of characters read depends on the speed of your computer, the faster, the less characters per read so you have to accumulate them over a number of cycles into a buffer.

Be sure to make this buffer="" when you have collected the full string otherwise it gets bigger and bigger with each read and overflows.

You have to send some sort of end of line or file character such as EOF or chr(13) or chr(10) to signify when to clear it.

You can have trouble if some lengthy other activity occurs and the data has no time gaps between each string. You then have to reduce the buffer length by the length of each line of data rather than clearing it completely otherwise you will miss part of the next string.
After End Select above I use something like:
L=Instr(buffer,chr(13)) 'finds position of an End of File Character you must include in transmission (or other character of your choice not normally used like chr(255))
If L>0 then
Arr=left(Buffer,L)'found end if string
Text1.Text=right(Arr)'one line of text + chr(13) if needed
Buffer=Right(Buffer,L+1)'trim off the part of the buffer you just found in case there is a second string
End If

If you have other carriage return(13) and line feed(10) you might have to vary the +1 to +2 to clip it off the next string read.

One way to check this is to temporarily remark out the trimming statement then resume with it it back in after transmitting a few lines repeated.
 
Hi thanks for your answers, i'll try with that....and thanks for your time..
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top