-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
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 upstairsCodePudding user response: