发送和接收缩放/扫描仪命令

时间:2013-12-21 19:35:11

标签: vb.net port hyperterminal

我有麦哲伦扫描仪/秤。它通过rs232连接到我的电脑。我可以通过在Hyperterminal程序中发送命令S11 + ENTER来读取秤上的重量,并且在Hyperterminal上显示的重量完全没有问题。 我的问题是为什么我在使用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

我的问题解决了!!!

1 个答案:

答案 0 :(得分:0)

这是另一个阅读扫描条形码或读取比例重量的教程 How to connect RS232 scale/scanner to vb.net