Vb.net 向天平/扫描仪发送和接收命令
我有一个麦哲伦扫描仪/秤。它通过rs232连接到我的电脑。我可以通过在超级终端程序中发送S11+ENTER命令读取磅秤上的重量,重量在超级终端上显示,没有任何问题。 我的问题是,为什么我在使用vb.net代码时无法读取重量 这是到目前为止我的代码Vb.net 向天平/扫描仪发送和接收命令,vb.net,port,hyperterminal,Vb.net,Port,Hyperterminal,我有一个麦哲伦扫描仪/秤。它通过rs232连接到我的电脑。我可以通过在超级终端程序中发送S11+ENTER命令读取磅秤上的重量,重量在超级终端上显示,没有任何问题。 我的问题是,为什么我在使用vb.net代码时无法读取重量 这是到目前为止我的代码 Imports System Imports System.IO.Ports Imports System.Threading Public Class Form1 Dim WithEvents Myport As SerialPort =
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
我的问题解决了 这里还有另一个阅读扫描条形码或读取秤重的教程
您能否将解决方案作为答案发布,而不是将其合并到问题中。