Hi All
First time I've posted but have done lots of reading on here as it's an amazing resource.
I am busy writing a program that is going to read data from 2 serial devices. One being a scale and the other being a "load testing" machine. Currently im working on the code to read data from the scale.
I was using the code on the following website: http://www.devx.com/dotnet/Article/31001/0/page/2 which to an extent works to what i would like to do. But im also constantly needing to check my string to see if i get a "Stable" weight.
With the code on the site it seems to "freeze" whenever i put in some checking as per :
Private Function stopread()
Try
lblMessage.Text = cbbCOMPorts.Text & " Closed"
serialport.Close()
btnconnect.Enabled = True
btndisconnect.Enabled = False
'Timer1.Enabled = False
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End Function
'-------------------------------------------
Private Sub DataReceived( _
ByVal sender As Object, _
ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) _
Handles serialport.DataReceived
txtDataRecieved.Invoke(New _
myDelegate(AddressOf updateTextBox), _
New Object() {})
End Sub
'------------------------------------------------------
' Delegate and subroutine to update the Textbox control
'------------------------------------------------------
Public Delegate Sub myDelegate()
Public Sub updateTextBox()
readdata = serialport.ReadByte
With txtDataRecieved
.AppendText(readdata.ToString.PadLeft(3, "0"c).PadRight(3, " "c) & " ")
.ScrollToCaret()
'If readdata = "003" Then
' .AppendText(vbCrLf)
'End If
End With
With txtBinaryData
.AppendText(Chr(readdata))
End With
If readdata = "042" Then
goodread = 1
End If
If readdata = "003" And goodread = 1 Then
stopread()
End If
End Sub
I then tried using a timer:
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim temp As Byte = serialport.ReadByte
Label1.Text += 1
With txtDataRecieved
.AppendText(temp.ToString.PadLeft(3, "0"c).PadRight(3, " "c) & " ")
If temp = "003" Then
.AppendText(vbCrLf)
End If
End With
txtBinaryData.AppendText(CByte(temp))
If temp = "042" Then
goodread = 1
End If
If temp = "003" And goodread = 1 Then
stopread()
End If
End Sub
But this seemed to slow as i can have a stable weight come up on the scale and the system is still writing the data to the screen.
What i would like is to have the program read the bytes from the scale, when it gets the 042 (*) it knows its a stable weight, and then when it read the 003 (end line) it stops reading the data. Then that last set of data from the 042 is collected and the weight is collected.
The data i get is in the following format: 002 042 043 032 032 048 046 054 054 032 107 103 032 032 032 003 which roughly converts to "*+ 0.66 kg "