You are on page 1of 3

Imports System.

Text
Imports System
Imports System.IO.Ports
Imports System.IO
Imports Microsoft.VisualBasic
Module Module1
Public ComportSelect As String
Public test1 As String
Public filename As String = "standard"
Public labelString As String
Function getserialdata()
' GET DATA FROM COM PORT
ComportSelect = My.Forms.LABELSELECT.ComportSelect
Dim returnStr As String
Dim a As Int16 = 0
Dim b As Int16
returnStr = "1"
Do
Try
REM Dim buffer As New StringBuilder()
Using comPort As SerialPort =
My.Computer.Ports.OpenSerialPort(ComportSelect)
' SEND "P" COMMAND TO SCALES TO START TRANSMITTING
Dim outgoing As String
outgoing = "P"
comPort.WriteLine(outgoing)
' READ DATA
comPort.ReadTimeout = 2000
Dim line As String = comPort.ReadLine()
returnStr = (line)
comPort.Close()
returnStr = returnStr.Substring(1)
End Using
' REMOVE ALL UNWANTED CHARACTERS
If returnStr.Length > 2 Then
a = 0
Dim TestString As String = returnStr
Dim TestArray() As String = Split(TestString)
' TestArray holds {"chr2)", "", "", "", "0.0", "lb", "gr", ""}
Dim LastNonEmpty As Integer = -1
For i As Integer = 0 To TestArray.Length - 1
If TestArray(i) <> "" Then
LastNonEmpty += 1
TestArray(LastNonEmpty) = TestArray(i)
End If
Next
If TestArray(0) = "-" Then
TestArray(1) = TestArray(0) + TestArray(1)
b = 1
returnStr = TestArray(1) + " " + TestArray(2)
Else : b = 0
returnStr = TestArray(0) + " " + TestArray(1)
End If
ReDim Preserve TestArray(LastNonEmpty)
' TestArray now holds {"10.9", "Lb", "Gr"
REM Test weight toloerence
Dim weightD As Decimal
'Dim weightS As String = "1"
Dim hightol As Decimal
Dim lowtol As Decimal
' TestArray now holds {"10.9", "Lb", "Gr"
weightD = Convert.ToDecimal(TestArray(b))
hightol =
Convert.ToDecimal(My.Forms.LABELSELECT.maxweight.Text)
lowtol = Convert.ToDecimal(My.Forms.LABELSELECT.minweight.Text)
REM test for max weight
If weightD < hightol And weightD > lowtol Then Exit Do
If weightD > hightol Then
My.Forms.COMPORT.Comla.Text = "WEIGHT IS TO HIGH"
My.Forms.COMPORT.comlb.Text = "PRESS OK TO RE-MEASURE"
My.Forms.COMPORT.ShowDialog()
returnStr = "No Comport"
a = 1
End If
REM test for min weight
If weightD < lowtol Then
My.Forms.COMPORT.Comla.Text = "WEIGHT IS TO LOW"
My.Forms.COMPORT.comlb.Text = "PRESS OK TO RE-MEASURE"
My.Forms.COMPORT.ShowDialog()
returnStr = "No Comport"
a = 1
End If
Else
My.Forms.COMPORT.Comla.Text = "Comport Has no Connection, Check
Connection"
My.Forms.COMPORT.comlb.Text = "PRESS CANCEL"
My.Forms.COMPORT.ShowDialog()
returnStr = "No Comport"
a = 0
End If

Catch e As System.ArgumentException
If e Is Nothing Then
Else
My.Forms.COMPORT.Comla.Text = "No Comport selected"
My.Forms.COMPORT.comlb.Text = "ArgumentException, PRESS CANCEL"
My.Forms.COMPORT.ShowDialog()
returnStr = "No Comport"
a = 0
End If
Catch e As System.UnauthorizedAccessException
If e Is Nothing Then
Else
My.Forms.COMPORT.Comla.Text = "Comport Has no Connection,
Select another port"
My.Forms.COMPORT.comlb.Text = "or UnauthorizedAccessException,
press cancel"
My.Forms.COMPORT.ShowDialog()
returnStr = "No Comport"
a = 0
End If
Catch e As TimeoutException
If e Is Nothing Then
a = 0
Else
My.Forms.COMPORT.Comla.Text = "Scale in motion press ok to
retry"
My.Forms.COMPORT.comlb.Text = "or TimeoutException, press
cancel"
My.Forms.COMPORT.ShowDialog()
a = 1
returnStr = "No Comport"
End If
REM Console.WriteLine(e)

End Try
If My.Forms.COMPORT.DialogResult =
System.Windows.Forms.DialogResult.Cancel Then Exit Do
If a = 0 Then Exit Do
Loop
Return returnStr
End Function
End Module

https://www.codeproject.com/Questions/66460/Take-input-of-weight-from-electronic-
weight-machin

You might also like