Current location - Quotes Website - Team slogan - How to solve the serial communication fault
How to solve the serial communication fault
If there is a certain received byte length, setting Rthreshold equal to the received byte length is beneficial to improve the receiving accuracy. Because the byte lengths returned by your lower computers are not equal, you can change the value of Rthreshold in advance according to the address command to ensure that the received bytes meet the requirements.

Otherwise, how many bytes need to be received to satisfy the next addressing command? Program code processing is difficult.

In fact, the data packet returned by the lower computer can be supplemented with some other bytes in its code to achieve the same length, thus making the received code concise.

After receiving the data with the same length, you can clear the receiving process and wait for the next reception.

Supplement:

If the received byte length can be determined according to the address number of the data returned by the lower computer, the following code can receive unequal length data for reference only:

Private submiscomm1_ oncom ()

Continue to the next step when an error occurs.

Dim BytReceived () is bytes.

Dim strBuff as a string

Dim strData as a string

Mark I as an integer

Mark x as an integer.

Select MSComm 1 case. CommEvent

Case 2

MSComm 1。 InputLen = 0

strBuff = MSComm 1。 invest

BytReceived() = strBuff

For i = 0 to UBound(BytReceived)

If len (hex (Byt received (I)) =1,then

strData = strData & amp0 & amp; Hexadecimal (number of bytes received (I))

other

StrData = strData & amp hexadecimal (number of bytes received (I))

If ... it will be over.

then

Text3 = Text3 + strData

If Left(strData, 2) = "00" and Len(strData) = 8, then

Text 1(0)。 Text = Left(strData,8)

Call data clearing

Then ElseIf Left(strData, 2) = "0 1 "and Len(strData) = 10.

Text 1( 1)。 Text = Left(strData, 10)

Call data clearing

If ... it will be over.

End selection

End joint

Common sub-data clearing ()

MSComm 1。 OutBufferCount = 0' clears the send buffer.

MSComm 1。 InBufferCount = 0

Text3 = " "

End joint

Private Sub-Form _Load ()

MSComm 1.com port = 1' com port.

MSComm 1。 Settings = "9600,n,8, 1 "

MSComm 1。 InputMode = CominputModebinary' adopts binary transmission.

MSComm 1。 InBufferCount = 0' empties the accept buffer.

MSComm 1。 OutBufferCount = 0' Clears the transmission buffer.

MSComm 1。 If the transmission buffer is completely empty, the mscomm event "strhreshold =1"will be generated.

MSComm 1。 Rthreshold = 1' does not generate MSComm events.

MSComm 1。 PortOpen = True

Text3 = "'Open the port.

End joint