I have created a custom windows software in vb.net, VS 2005. I am reading data from a Pennsylvania scale (model 7300) via my development computer's RS232 port. I am using the SerialPort object and Handling the DataReceived event. I first send the scale a command and then immediately use SerialPort.ReadExisting to get the weight from the scale. Each time I click the GetWeight button I get different results AND it is garbage, like ????, then ?/-=, then ?-=?, etc - usually different every time. Please advise me on how to get the weight out of the characters returned. Here is the code.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' open the serial port
' I have dragged and dropped the SerialPort obj to the form
If mySerialPort.IsOpen Then
mySerialPort.Close()
End If
Try
With mySerialPort
.PortName = myPortName
.BaudRate = 2400
.Parity = 0
.DataBits = 8
.StopBits = IO.Ports.StopBits.One
.Handshake = IO.Ports.Handshake.None
.DtrEnable = True
.RtsEnable = True
End With
mySerialPort.Open()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Private Sub cmdGetWeight_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdGetWeight.Click
' this command makes the scale immediately return the weight
Control.CheckForIllegalCro
ssThreadCa
lls = False
mySerialPort.Write("SGW" & vbCrLf)
End Sub
Private Sub myDataReceived(ByVal sender As Object, ByVal e As IO.Ports.SerialDataReceive
dEventArgs
) Handles mySerialPort.DataReceived
' this fires right after the Write(sgw) thing. the app gets here fine
Dim myScaleOutput As String = ""
myScaleOutput = mySerialPort.ReadExisting
' characters are written to the textbox
boxMultiLine.Text = myScaleOutput
End Sub
Start Free Trial