串口通讯很慢 [英] Serial port communication very slow
本文介绍了串口通讯很慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Public Shared Function ReceiveData() As String
Dim RXCnt As Integer = 0
Dim RXByte As Byte = 0
Dim reply As String = String.Empty
Dim STX As Byte() = {&H2, &HD}
Dim hexwrite As String = String.Empty
Dim ack As Double = 0
Dim errorCode As String = Nothing
Dim strText As String = Nothing
Dim RXArray As Char() = New Char(2047) {}
Dim errorDescription = String.Empty
Dim LookUpTable As String = "0123456789ABCDEF"
Do
'----- Start of communication protocol handling ---------------------------------------------
RXCnt = 0
Do
RXByte = COMPort.ReadByte
RXArray(RXCnt) = LookUpTable(RXByte >> 4) ' Convert each byte to two hexadecimal characters
RXCnt = RXCnt + 1
RXArray(RXCnt) = LookUpTable(RXByte And 15)
RXCnt = RXCnt + 1
Loop While Not ((COMPort.BytesToRead = 0))
'----- End of communication protocol handling -------------------------------------------------------------
recievesend = False
reply = RXArray(0).ToString & RXArray(1).ToString
If reply = "06" Then
If readwrite = True Then
Try
COMPort.Write(STX, 0, STX.Length)
Catch ex As TimeoutException
errorCodeText = String.Concat(errorCodeText, ex.Message)
Catch ex As InvalidOperationException
errorCodeText = String.Concat(errorCodeText, ex.Message)
Catch ex As UnauthorizedAccessException
errorCodeText = String.Concat(errorCodeText, ex.Message)
End Try
recievesend = True
hexwrite = "020D"
ElseIf readwrite = False Then
If ack < 1 Then
ack = ack + 1
Try
COMPort.Write(writebytes, 0, writebytes.Length)
Catch ex As TimeoutException
errorCodeText = String.Concat(errorCodeText, ex.Message)
Catch ex As InvalidOperationException
errorCodeText = String.Concat(errorCodeText, ex.Message)
Catch ex As UnauthorizedAccessException
errorCodeText = String.Concat(errorCodeText, ex.Message)
End Try
recievesend = True
hexstring = StringToHex(writedata)
Else
ack = 0
End If
End If
ElseIf reply = "15" Then
errorCode = RXArray(3).ToString()
End If
Thread.Sleep(300) 'sleep between incoming bytes
Loop While Not ((COMPort.BytesToRead = 0))
strText = New String(RXArray, 0, RXCnt) & vbCrLf
Return DecodeHexString(strText)
End Function
我尝试过:
我用来从端口读取数据的代码非常慢,请帮助我,如果有任何新选项可以尝试加固读数。
What I have tried:
This code i am using to read data from the port is very slow, please help me if there are any new options to try to fasten the reading.
推荐答案
可能没有:串行端口以固定速度运行(称为波特率),它控制在连接的设备之间传输字符的最大速度它。如果串口设置为9600波特,则每秒最多可传输960个字符(大约),不能再传输。
Probably, there aren't : serial ports run at a fixed speed (known as a Baud Rate) which controls the maximum speed at which characters can be transfered between devices connected to it. If you serial port is set to 9600 baud, then you can transfer up to 960 characters per second (approximately) and no more.
引用:
我用来从端口读取数据的代码非常慢
This code i am using to read data from the port is very slow
按设计,串口通信速度非常慢。
实际串口,最大速度是115200波特,这意味着如果有一个连续的流量,则11520个标准杆秒。
你的波特实际速度是多少,每秒多少个字符。你收到了吗?
By design, Serial port communications are very slow.
On real serial port, maximum speed is 115200 bauds, which means 11520 chars par second if there is a continuous flow.
What is your actual speed in baud and how many char per seconds do you receive ?
这篇关于串口通讯很慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文