J
John Wright
I have data coming into a serial port that I need to take action on. I have
created a delegate to read the data from the port and put it into a text
box. Once the data is read, I need to manipulate it. However, when I try
to take any action on the data, I get errors. When I call the function
GetTareandWeight, it gives me an error on the split function because the
data is not done streaming in yet. How can I wait for all the data to get
in from the port before processing it?
John
Code:
Public Delegate Sub myDelegate(ByVal txtBox As TextBox)
Private Sub SerialTare_DataReceived(ByVal sender As Object, ByVal e As
System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialTare.DataReceived
TextBox1.Invoke(New myDelegate(AddressOf UPdateTextBox), New Object()
{TextBox1})
End Sub
Public Sub UPdateTextBox(ByVal UpdateTextBox As TextBox)
Dim bytes As Integer = SerialTare.BytesToRead
Dim buffer(bytes) As Byte
SerialTare.Read(buffer, 0, bytes)
strWeight = System.Text.Encoding.Default.GetString(buffer)
TextBox1.AppendText(strWeight)
GetTareandWeight(strWeight)
End Sub
Private Sub GetTareandWeight(ByVal weight As String)
Dim strweights() As String
Dim newweight As String = Replace(weight, " ", ",")
strweights = Split(weight, ",")
TextBox2.Text &= newweight & vbCrLf
TextBox2.Text &= strweights.Length.ToString & vbCrLf
TextBox2.Text &= "Tare: " & strweights(1).ToString
End Sub
created a delegate to read the data from the port and put it into a text
box. Once the data is read, I need to manipulate it. However, when I try
to take any action on the data, I get errors. When I call the function
GetTareandWeight, it gives me an error on the split function because the
data is not done streaming in yet. How can I wait for all the data to get
in from the port before processing it?
John
Code:
Public Delegate Sub myDelegate(ByVal txtBox As TextBox)
Private Sub SerialTare_DataReceived(ByVal sender As Object, ByVal e As
System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialTare.DataReceived
TextBox1.Invoke(New myDelegate(AddressOf UPdateTextBox), New Object()
{TextBox1})
End Sub
Public Sub UPdateTextBox(ByVal UpdateTextBox As TextBox)
Dim bytes As Integer = SerialTare.BytesToRead
Dim buffer(bytes) As Byte
SerialTare.Read(buffer, 0, bytes)
strWeight = System.Text.Encoding.Default.GetString(buffer)
TextBox1.AppendText(strWeight)
GetTareandWeight(strWeight)
End Sub
Private Sub GetTareandWeight(ByVal weight As String)
Dim strweights() As String
Dim newweight As String = Replace(weight, " ", ",")
strweights = Split(weight, ",")
TextBox2.Text &= newweight & vbCrLf
TextBox2.Text &= strweights.Length.ToString & vbCrLf
TextBox2.Text &= "Tare: " & strweights(1).ToString
End Sub