Hi everyone,
I have a strange problem with VB.net using two physical serial ports Com1,Com2.
They both receive data async and run what seems to be really well, using a delegate for each and an individual textbox.begininvoke(addressof xxx ) then the bytes retrieved from each.
Occasionally, I get an odd thing occur, some of the data that should have come in on one com port appears on the other com port. No error/exception is raised and unless I saw it with my own eyes, I would have missed it completely.
This is causing me a few headaches and I cannot quite see what could be causing the problem.
I am using mutli threading and I am wondering if the RXD data is picking the wrong thread some times?
Any suggestions please???????
(Thanks in advance)
Public Delegate Sub mydelegate()
Public Delegate Sub Phonedelegate()
Private Sub datareceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles EPort.DataReceived
Try
RXD.BeginInvoke(New mydelegate(AddressOf updateTextBox), New Object() {})
Catch ex As Exception
End Try
End Sub
Private Sub PHONEdatareceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles MPort.DataReceived
Try
RXD.BeginInvoke(New Phonedelegate(AddressOf updatePhoneBox), New Object() {})
Catch ex As Exception
Beep()
End Try
End Sub
Public Sub updateTextBox()
Dim InBuff As String
InBuff = Me.EPort.ReadExisting
TextBox14.Text = InBuff
If Strings.Right(InBuff, 1) = Chr(10) Then
inputdata = inputdata & InBuff
Dim Pmydatenow As Date = Now
Dim Pmydate As String()
Pmydate = Split(Pmydatenow.ToString("s"), "T")
Dim PmyDay As String = Strings.Mid(Pmydate(0), 9, 2)
Dim Pmymonth As String = Strings.Mid(Pmydate(0), 6, 2)
Dim Pmyyear As String = Strings.Mid(Pmydate(0), 3, 2)
Dim Pmyhr As String = Strings.Mid(Pmydate(1), 1, 2)
dat = PmyDay & Pmymonth & Pmyyear
Dim TimeNow As String = " " & Trim(Pmydate(1)) & " " & PmyDay & "/" & Pmymonth & "/" & Pmyyear
datenow = TimeNow
Dim x As Integer
OQue = Split(inputdata, Chr(10))
Dim Quelen As Integer = OQue.Length - 1
Try
For x = 0 To Quelen
OQue(x).Replace(Chr(10), "")
OQue(x).Replace(Chr(13), "")
If Len(OQue(x)) > 0 Then
ORxdQueue.Enqueue(OdQue(x) & TimeNow)
If Button12.Text = "Debug Enabled" Then
If TextBox10.TextLength & 32000 Then
TextBox10.Text = Strings.Right(TextBox10.Text, 30000)
End If
TextBox10.AppendText(vbCrLf & "Add to Queue " & OQue(x) & TimeNow & vbCrLf)
TextBox10.AppendText(vbCrLf &"Queue length =" & ORxdQueue.Count & vbCrLf)
TextBox10.ScrollToCaret()
TextBox10.Update()
End If
Dim JB As Integer = 0
While ORxdQueue.Count > 0
If Processingit = False Then
PD = True : displayit2(ORxdQueue.Dequeue)
End If
Thread.Sleep(50)
End While
End If
Next
Catch ex As Exception
End Try
inputdata = ""
Else
inputdata = inputdata & InBuff
End If
End Sub
Public Sub updatePhoneBox()
Dim InBuff As String
InBuff = Me.MPort.ReadExisting
......