Home > Software engineering >  Ask god to help have a look at a serial port receives the data why receive incomplete
Ask god to help have a look at a serial port receives the data why receive incomplete

Time:11-14

The baud rate is 115200, the use of virtual serial port, coming from another serial port to send data can complete to receive, no problem, but with real hardware devices connect up, can only receive a few bytes of data (hardware sending about 120 bytes of data)
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
serial port initialization code is as follows:
'press the keys to open the serial port
Private Sub Command10_Click ()
MPort=PortNo MSComm1.Com
MSComm1. Settings=BTL & amp; ", n, 8, 1 ", "
MSComm1. An InputMode=comInputModeBinary
MSComm1. InBufferCount=0
MSComm1. OutBufferCount=0
MSComm1. RThreshold=1
MSComm1. PortOpen=True
'Shape1. FillColor=& amp; H80000003
Shape1. FillColor=& amp; HC000C0
Com_flag=1
If MSComm1. PortOpen=True Then
Label15. Caption="serial port is open"
End the If
End Sub

a serial port receives the code is as follows:
Private Sub MSComm1_OnComm ()
Hextext. Text=""
Dim InputLen As Integer 'returns the length of the byte array
Dim the result As a String
Dim As Integer I
Select Case mEvent MSComm1.Com
Case comEvReceive
InputLen=MSComm1. InBufferCount
ReDim receivedbyte (InputLen) 'to redefine the array size

Receivedbyte ()=MSComm1. Input

For I=0 To UBound (receivedbyte) - 1 'UBound () returns an array of size

If Len (Hex (receivedbyte (I)))=1 Then
'convert hexadecimal text absence of 0 + space,
Result=result & amp; "0" & amp; Hex (receivedbyte (I)) & amp; "
"The Else
'convert hexadecimal text + space,
Result=result & amp; Hex (receivedbyte (I)) & amp; "
"End the If
Next I

End the Select

'serial return data displayed as hex text
Hextext. Text=result

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --






CodePudding user response:


Actually send data is slow, so I can't again after collect, so you need to wait for a time,
In the OnComm event Case comEvReceive plus add similar code like this
 
Dim T As Single, BytesReceived As Integer
',,,,,,,,,,,
Case comEvReceive
'InputLen=MSComm1 InBufferCount' this line do not
InputLen=0
T=Timer
Do
If InputLen=MSComm1. InBufferSize Then Exit the Do
InputLen=MSComm1. InBufferSize
Loop Until Abs (1000 * (Timer - T)) & gt; 100 'definition timeout 100 ms according to actual condition can be big or small wait time
ReDim receivedbyte (InputLen) 'to redefine the array size
Receivedbyte ()=MSComm1. Input
',,,,,,,,,,,,,,,,

CodePudding user response:

The code comments see somebody else already answered upstairs

CodePudding user response:

refer to the second floor stupid dog flew first response:
actually send data is slow, so I can't after collect at a time, so you need to wait for a time,
In the OnComm event Case comEvReceive plus add similar code like this
 
Dim T As Single, BytesReceived As Integer
',,,,,,,,,,,
Case comEvReceive
'InputLen=MSComm1 InBufferCount' this line do not
InputLen=0
T=Timer
Do
If InputLen=MSComm1. InBufferSize Then Exit the Do
InputLen=MSComm1. InBufferSize
Loop Until Abs (1000 * (Timer - T)) & gt; 100 'definition timeout 100 ms according to actual condition can be big or small wait time
ReDim receivedbyte (InputLen) 'to redefine the array size
Receivedbyte ()=MSComm1. Input
',,,,,,,,,,,,,,,,

Really thank you very much, found the problem, the same code before receipt is very short, so before the code does not have problems, thoroughly understand this time,
  • Related