我有一个麦哲伦扫描仪/秤。它通过rs232连接到我的电脑。我可以通过在超级终端程序中发送命令 S11 + ENTER 来读取秤上的重量,并且重量在超级终端上显示完全没有问题。我的问题是为什么我在使用 vb.net 代码时无法读取重量。
到目前为止,这是我的代码
Imports System
Imports System.IO.Ports
Imports System.Threading
Public Class Form1
Dim WithEvents Myport As SerialPort = New System.IO.Ports.SerialPort("COM2", 9600, Parity.Odd, 7, StopBits.One)
Private Sub btnOpenPort_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpenPort.Click
Try
If Not Myport.IsOpen Then
Myport.Open()
MsgBox("Port Opened")
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub sendCommand_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles sendCommand.Click
Try
Myport.Write("S11" & vbCr)
Thread.Sleep(20)
Myport.ReadTimeout = 500
TextBox1.Text = Myport.ReadExisting
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
更新:当我发送命令 S334(使扫描仪发出哔哔声)时,扫描仪发出哔哔声,这意味着我与扫描仪通信或发送命令没有问题。现在唯一的问题是如何从秤中读取响应。
找到解决方法!!!!!!我必须设置
Myport.Handshake = Handshake.RequestToSend
我还添加了以下事件来捕捉重量和扫描的条形码
Private Sub REC(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles Myport.DataReceived
TextBox2.Text &= Myport.ReadTo(Chr(13))
Try
TextBox2.Text = TextBox2.Text.Replace("S11", "")
If TextBox2.Text.Length = 4 Then
Label1.Text = "Weight: " & TextBox2.Text / 100
End If
Catch ex As Exception
End Try
Try
TextBox2.Text = TextBox2.Text.Replace("S08A", "")
Catch ex As Exception
End Try
End Sub
我的问题解决了!!!