两个串口偶尔会通过代理发送错误的数据? [英] two serial ports and occasionally wrong data sent trhough delegate?

查看:137
本文介绍了两个串口偶尔会通过代理发送错误的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

使用两个物理串行端口Com1,Com2,VB.net出现了一个奇怪的问题.

它们都异步接收数据并运行看起来确实不错的方法,分别为每个文本框和每个文本框使用一个委托.begininvoke(addressof xxx)然后从每个文本框中检索字节.

有时,我会遇到奇怪的事情,应该在一个com端口上输入的一些数据会出现在另一个com端口上.没有错误/异常,除非我亲眼看到它,否则我将完全错过它.

这使我有些头疼,而且我不太明白是什么原因引起的.
我正在使用多线程,并且我想知道RXD数据是否有时选择了错误的线程?

有任何建议吗??????

(提前感谢)

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()
'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
......

推荐答案

我需要复制代码并将其粘贴到编辑器中,很难按原样阅读.

如果要从一个端口获取数据到另一个端口的处理路径,我会寻找一个共享变量.引起我注意的是inputdata.此变量的范围不清楚.

如果没有发生条件 Right(InBuff, 1) = Chr(10) 并且第二个端口已生成事件,那么第一个端口中的inputdata 中的数据会如何处理?

可能有帮助的其他信息是如何设置端口,读取端口时要带入多少字节?缓冲区如何设置?多少字节会生成一个事件?更多信息可能会有所帮助.
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.


这篇关于两个串口偶尔会通过代理发送错误的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆