通过 COM 从秤中请求重量值
Request Weight Value from Scale through COM
我一直在尝试使用 COM 串行端口从两个不同的秤中检索重量值。
天平是 CAS ER Plus 和 UWE AM-15K。
我已经设法创建了一个小程序来启动 PC 和 Scales 之间的通信,但我 运行 遇到了一个问题,我一直找不到解决方案。
我可以发送 ENQ 命令并从两个秤成功接收到 ACK,但是当我发送 DC1 命令检索重量时,我总是从消息中得到各种变化
"Hex number must be smaller than 7fffffffffffffff"
和
"Hex Value Must be Maximum Length of 32 characters."
我配置 COM 端口的代码如下:
Try
comport.PortName = "COM1"
comport.BaudRate = 9600
comport.Parity = Parity.None
comport.DataBits = 8
comport.StopBits = StopBits.One
comport.Handshake = Handshake.XOnXOff
If DTRCheck.Checked Then
comport.DtrEnable = True
End If
If RTSCheck.Checked Then
comport.RtsEnable = True
End If
'Dim encoding As System.Text.Encoding = System.Text.Encoding.GetEncoding("IBM860")
Dim encoding As System.Text.Encoding = System.Text.Encoding.ASCII
comport.Encoding = encoding
comport.Open()
erroCOM = False
Catch ex As Exception
erroCOM = True
End Try
我发送命令的代码:
If comport.IsOpen Then
TxtValor.Clear()
Try
If EnqComBtn.Checked Then 'Verify if the scale is "listening"
ListBox1.Items.Add("PC: ENQ - >")
comport.Write(New Byte() {&H5}, 0, 1)
Thread.Sleep(20)
ListBox1.Items.Add("PC: DC1 - >")
comport.Write(New Byte() {&H11}, 0, 1)
End If
If DC1ComBtn.Checked Then 'Request weight value
ListBox1.Items.Add("PC: DC1 - >")
comport.Write(New Byte() {&H11}, 0, 1)
Thread.Sleep(20)
End If
Catch ex As Exception
End Try
End If
发送命令后,我让程序从 COM 串行端口读取通过 DataReceived 事件接收到的值,并将十六进制值转换为十进制值。
编辑:
这是我用来读取接收到的字节并将其从字节转换为十六进制的代码。我在 Whosebug 上的另一个 post 上找到了这段代码,并编辑了空格部分。
Private Sub comport_DataReceived(sender As Object, e As SerialDataReceivedEventArgs) 句柄 comport.DataReceived
Dim RXByte As Byte
Do
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 Until (comport.BytesToRead = 0)
'----- End of communication protocol handling -------------------------------------------------------------
Me.Invoke(New MethodInvoker(AddressOf Display)) ' Start "Display" on the UI thread
Loop Until (comport.BytesToRead = 0) ' Don't return if more bytes have become available in the meantime
End Sub
有没有人以前发现过这个问题,如果有,你找到解决方案了吗?
谢谢!
我找到了解决问题的方法!
问题是我正在逐字节读取 BytesToRead,我必须执行 ReadExisting。
前一个代码:
Dim RXByte As Byte
'Dim RXBytes() As Byte
Dim bytString As String = ""
Do
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 Until (comport.BytesToRead = 0)
'----- End of communication protocol handling -------------------------------------------------------------
Me.Invoke(New MethodInvoker(AddressOf Display)) ' Start "Display" on the UI thread
Loop Until (comport.BytesToRead = 0) ' Don't return if more bytes have become available in the meantime
解决方案:
RXValue = ""
Try
RXValue = comport.ReadExisting()
If RXValue <> "" Then
Me.Invoke(New MethodInvoker(AddressOf Display)) ' Start "Display" on the UI thread
Else
End If
Catch ex As Exception
End Try
希望对遇到同样问题的人有所帮助!
我一直在尝试使用 COM 串行端口从两个不同的秤中检索重量值。
天平是 CAS ER Plus 和 UWE AM-15K。
我已经设法创建了一个小程序来启动 PC 和 Scales 之间的通信,但我 运行 遇到了一个问题,我一直找不到解决方案。
我可以发送 ENQ 命令并从两个秤成功接收到 ACK,但是当我发送 DC1 命令检索重量时,我总是从消息中得到各种变化
"Hex number must be smaller than 7fffffffffffffff"
和
"Hex Value Must be Maximum Length of 32 characters."
我配置 COM 端口的代码如下:
Try
comport.PortName = "COM1"
comport.BaudRate = 9600
comport.Parity = Parity.None
comport.DataBits = 8
comport.StopBits = StopBits.One
comport.Handshake = Handshake.XOnXOff
If DTRCheck.Checked Then
comport.DtrEnable = True
End If
If RTSCheck.Checked Then
comport.RtsEnable = True
End If
'Dim encoding As System.Text.Encoding = System.Text.Encoding.GetEncoding("IBM860")
Dim encoding As System.Text.Encoding = System.Text.Encoding.ASCII
comport.Encoding = encoding
comport.Open()
erroCOM = False
Catch ex As Exception
erroCOM = True
End Try
我发送命令的代码:
If comport.IsOpen Then
TxtValor.Clear()
Try
If EnqComBtn.Checked Then 'Verify if the scale is "listening"
ListBox1.Items.Add("PC: ENQ - >")
comport.Write(New Byte() {&H5}, 0, 1)
Thread.Sleep(20)
ListBox1.Items.Add("PC: DC1 - >")
comport.Write(New Byte() {&H11}, 0, 1)
End If
If DC1ComBtn.Checked Then 'Request weight value
ListBox1.Items.Add("PC: DC1 - >")
comport.Write(New Byte() {&H11}, 0, 1)
Thread.Sleep(20)
End If
Catch ex As Exception
End Try
End If
发送命令后,我让程序从 COM 串行端口读取通过 DataReceived 事件接收到的值,并将十六进制值转换为十进制值。
编辑:
这是我用来读取接收到的字节并将其从字节转换为十六进制的代码。我在 Whosebug 上的另一个 post 上找到了这段代码,并编辑了空格部分。
Private Sub comport_DataReceived(sender As Object, e As SerialDataReceivedEventArgs) 句柄 comport.DataReceived
Dim RXByte As Byte
Do
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 Until (comport.BytesToRead = 0)
'----- End of communication protocol handling -------------------------------------------------------------
Me.Invoke(New MethodInvoker(AddressOf Display)) ' Start "Display" on the UI thread
Loop Until (comport.BytesToRead = 0) ' Don't return if more bytes have become available in the meantime
End Sub
有没有人以前发现过这个问题,如果有,你找到解决方案了吗? 谢谢!
我找到了解决问题的方法!
问题是我正在逐字节读取 BytesToRead,我必须执行 ReadExisting。
前一个代码:
Dim RXByte As Byte
'Dim RXBytes() As Byte
Dim bytString As String = ""
Do
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 Until (comport.BytesToRead = 0)
'----- End of communication protocol handling -------------------------------------------------------------
Me.Invoke(New MethodInvoker(AddressOf Display)) ' Start "Display" on the UI thread
Loop Until (comport.BytesToRead = 0) ' Don't return if more bytes have become available in the meantime
解决方案:
RXValue = ""
Try
RXValue = comport.ReadExisting()
If RXValue <> "" Then
Me.Invoke(New MethodInvoker(AddressOf Display)) ' Start "Display" on the UI thread
Else
End If
Catch ex As Exception
End Try
希望对遇到同样问题的人有所帮助!