vb.netporthyperterminal

Sending and receiving command to scale/scanner


I have a Magellan scanner/scale. It is connected to my pc through rs232. I can read the weight on the scale by sending the command S11 + ENTER in Hyperterminal program and the weight shows on Hyperterminal with no problems at all. My question is why can't I read the weight when I use vb.net code.

Here is my code so far

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

Update: When I send a command S334 (make scanner beeps) the scanner beeps which means I don't have a problem communicating with the scanner or sending the command. The only problem is now how to read the response from the scale.

Solution Found!!!!! I had to set Myport.Handshake = Handshake.RequestToSend I also added the following event to catch the weight and the scanned barcodes

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

My problem is Solved!!!


Solution

  • Here is also another tutorial to read a scanned barcode, or reading the the scale weight How to connect RS232 scale/scanner to vb.net