Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

visual basic 6 MSComm receive data problem

Status
Not open for further replies.

fabianuat

Computer
Jan 4, 2007
1
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

 
Replies continue below

Recommended for you

Fabian, do not declare the receive buffer as variant. And change the inputmode property to comInputModeText.

Code:
Private Sub Form_Load()

MSComm1.CommPort = 1
MSComm1.Settings = "38400,O,8,1"
MSComm1.InputMode = 0 'comInputModeText
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

End Sub


Private Sub MSComm1_OnComm()

Dim strBuffer As String
Dim i As Integer
Dim sTemp As String
Dim strINPUT As String

Select Case MSComm1.CommEvent
  Case comEvReceive
    strBuffer = MSComm1.Input

    For i = 1 To len(Buffer)
      sTemp = Right("0" & Hex(Asc(Mid(strBuffer, i, 1))), 2)
      strINPUT = strINPUT & sTemp & " "
      Text1.Text = strINPUT
    Next
End Select

End Sub
 
Status
Not open for further replies.
Back
Top