Visual Basic 2008 / 2010 Tutorials
www.visual-basic-tutorials.com

 

Index

How to connect RS232 scale/scanner to vb.net

 
Page 1

The following code is used to connect RS232 scale/scanner to vb.net

The scanner scale used in this tutorial is Magellan 834

Imports System
Imports
System.IO.Ports
Imports
System.Threading
Public
Class Form1

    'scale/scanner declarations
   
Dim WithEvents scale_scannerPort As SerialPort = New System.IO.Ports.SerialPort("COM2", 9600, Parity.Odd, 7, StopBits.One) 'CHANGE PORT# to the port where your scanner is connected

    Dim weightRequested As String = "ERROR"
   
Dim sendDelay As Boolean = True


   
Function sendWeightRequest()
       
If weightRequested = "ERROR" Then
           
Return ("cannot read weight")
       
Else
           
Return weightRequested
           
Try
               
scale_scannerPort.WriteLine("S334" & vbCr)
           
Catch ex As Exception
           
End Try
       
End If
   
End Function

    Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
       
If scale_scannerPort.IsOpen Then
           
scale_scannerPort.Close()
           
scale_scannerPort.Dispose()
       
End If
   
End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       
Control.CheckForIllegalCrossThreadCalls = False
       
scale_scannerPort.ReadTimeout = 500
       
scale_scannerPort.Handshake = Handshake.RequestToSend
       
Try
           
If Not scale_scannerPort.IsOpen Then
               
scale_scannerPort.Open()
           
End If
       
Catch ex As Exception
       
End Try
       
scaleScannerBackgroundWorker.RunWorkerAsync()
   
End Sub

    'catch string returned by the scale/scanner
   
Private Sub REC(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles scale_scannerPort.DataReceived
       
processIncomes(scale_scannerPort.ReadTo(Chr(13)))
   
End Sub
   
'timer is used to read scale status

    Private Sub ReadWeightTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ReadWeightTimer.Tick
       
If sendDelay = True Then
           
If scaleScannerBackgroundWorker.IsBusy = False Then
               
scaleScannerBackgroundWorker.RunWorkerAsync()
           
End If
       
End If
   
End Sub

    'analysing scanner/scale recieved data
   
Public Sub processIncomes(ByVal rec As String)
        'If a barcode is scanned, scanner return S08Axxxxxxxxxxxx WHERE X= barcode digits
       
Try
           
If rec.Contains("S08A") Then
               
upcRecieved.Text = rec.Replace("S08A", "") 'removing S08A
           
End If
       
Catch ex As Exception
       
End Try

        'valid weight response

        'if there is an object on the scale, the scale

        'return S144xxxx where xxxx is the weight multiplied by 100

        Try

            If rec.Contains("S144") Then

                ScaleStatusLabel.Text = rec.Replace("S144", "") / 100 & " LB"

                weightRequested = rec.Replace("S144", "") / 100

            End If

        Catch ex As Exception

        End Try

        'scale is zero

        'when returned value = S143 scale has a zero weight

        Try

            If rec.Contains("S143") Then

                ScaleStatusLabel.Text = "0.00 LB"

                weightRequested = "ERROR"

            End If

        Catch ex As Exception

        End Try

 

        'scale is not stable

        'when returned value = S141 weight is stable (example: when you put your hand on the scale)

        Try

            If rec.Contains("S141") Then

                ScaleStatusLabel.Text = "----"

                weightRequested = "ERROR"

            End If

        Catch ex As Exception

        End Try

        'scale is over capacity

        Try

            If rec.Contains("S142") Then

                ScaleStatusLabel.Text = "----"

                weightRequested = "ERROR"

            End If

        Catch ex As Exception

        End Try

 

        'scale is under zeros

        Try

            If rec.Contains("S145") Then

                ScaleStatusLabel.Text = "----"

                weightRequested = "ERROR"

            End If

        Catch ex As Exception

        End Try

        sendDelay = True

    End Sub

    Private Sub scaleScannerBackgroundWorker_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles scaleScannerBackgroundWorker.DoWork

        Do Until sendDelay = False

            'command S14 + ENTER is sent to the scale to read the weight

            scale_scannerPort.WriteLine("S14" & vbCr)

            sendDelay = False

        Loop

    End Sub

    Private Sub getWeightButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles getWeightButton.Click

        'reading the weight on the scale

        MsgBox(sendWeightRequest)

    End Sub

End Class