Click here to Skip to main content
15,889,096 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
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)

VB
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()
'date format 2009-03-14T17:00:02
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
......
Posted
Updated 23-May-11 0:54am
v2
Comments
Dave Kreskowiak 23-May-11 7:48am    
FROM OP:

I'll add another observation...
I have this code running on three machines, 2 are P4 ~1.5ghz. one is a Core2Duo. the C2D has not played up in the last two hrs that I have been monitoring the problem.
(I put a reporting watch statement in as one ports data received should never be more than a few bytes so I can see if it is longer and raise an error).

This problem only happens a few times an hour with around 10kb of data through the ports.

1 solution

I needed to copy the code and paste it into my editor, it is difficult to read as posted.

If you are getting data from one port into the processing path of the other port I would look for a shared variable. The one that catches my eye is inputdata. The scope of this variable is not clear.

What happens to the data in inputdata from the first port if the conditional Right(InBuff, 1) = Chr(10) hasn’t occurred and the second port has generated an event?

Addotional information that may help is how are the ports set up, how many bytes are you bringing in when you read the port? How are the buffers set up? How many bytes generate an event? More information may be helpful.
 
Share this answer
 
Comments
wallyg34 23-May-11 17:35pm    
Hi, I think you might just have it. The inputdata variable is a public shared variable, but for this site I have cut the full name of the variable out. However, stupidly, I used the same variable on both coms ports. So, you might just be right!!!

I will try changing this to another name and let you know.


Thank you for your response, it is greatly appreciated.
Rob.
wallyg34 24-May-11 4:32am    
I have left it running all day and found it works fine now.

Thanks very much for your simple but obviously overlooked suggestion!

I was jumping off too early thinking it was thread related.
In creating the second com port routine, I had basically copied it.
Problem was, I was using the same variable to build the input data from the com ports for both ports.

I am surprised that it has not been an issue up to now, It has been working fine for about 6 months. I guess due to timing with the other routines in my code, since I have changed a lot of the other processes, maybe it's just highlighted this problem now.

Anyway, thank you again.

Rob Gater.
S Houghtelin 24-May-11 7:38am    
You are very welcome, I am very glad that worked for you.

Sometimes the simple solutions aren't so easy to spot, I fall victim to that quite often. I was recently working on a project where there were intermittent drops in the data stream, so before I went and stormed off and told the other programmers, I checked my own code. I found some errors, and fixed them. Did not fix the intermittent issue but I can present the issue without fear of the issue being distracted by my own mistakes.

Peer reviews aren’t fun as most programmers/designers are masters of the obvious, but so aren’t we all. :)

Also, welcome to Code Project! I have found this site to contain very useful information. Most of the folks here are helpful and accommodating.

[Edit] And thanks for the upvote, I appreciate it!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900