使用两个串口进行通讯 [英] Using Two Serial port for communication

查看:151
本文介绍了使用两个串口进行通讯的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我在程序中使用了两个串行端口.所需要的是,我需要从一个端口接收数据并在另一个端口上发送,反之亦然.我为此使用下面的代码.此代码从一个端口接收数据,但不能从另一个端口接收数据.我试图通过使用超级终端来模拟此代码,并且它在两个com端口上均正常工作.正如我在Vb6中也写过代码的地方,它给出的问题与从一个端口接收数据时提供中断但没有给出从另一端口接收数据的中断相同.我知道另一种接收数据的方法是使用计时器,并通过命令以readline()的形式读取数据,但是如何使两个端口都产生接收数据的中断.

请帮助找出问题,代码在Vb.net中,如下所示.

谢谢
Niraj Shah.

私有 Sub SerialPort1_DataReceived( ByVal 发送者 收件人 System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) 句柄 SerialPort1.收到数据

Hi,
 
I am using two serial port in  my programm.  What is want is, i need to receive data from one port and send on another port and vice a versa. I have use below code for this.  This code receive data from one port but can't receive data from another port. I have tried to simulate this code by using hyper terminal and it works properly with both com port. Where as i have written code in Vb6 too and its is giving same problem as giving interupt while recieved data from one port but not giving interupt which receiving from another port. I know the another way to receive data is by using timer and read data by command as readline(), but what to do to make both the port gives interupt which receive data.

Please help to find out the porblem, the code is in Vb.net and it is as under.

Thank you
Niraj Shah.

Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived

暗淡 RDta As 字符串 = "

Dim RDta As String = ""

尝试

Try

如果 Btn_Rof.Text = "远程打开" 然后

If Btn_Rof.Text = "Remote On" Then

Threading.Thread.Sleep(2000)

Threading.Thread.Sleep(2000)

RDta = SerialPort1.ReadExisting()

RDta = SerialPort1.ReadExisting()

结束 如果

End If

<字体大小= 2>

捕获 ex As 例外

Catch ex As Exception

MsgBox( " SerialPort1_DataReceived --- Data =" & RDta& " & ex.Message.ToString())

MsgBox("SerialPort1_DataReceived ---Data = " & RDta & " " & ex.Message.ToString())

结束 尝试

End Try

结束 Sub

End Sub

Sub SendDataBack( ByVal RDta 字符串 )

Sub SendDataBack(ByVal RDta As String)

尝试

Try

SerialPort2.WriteLine(RDta.ToString())

SerialPort2.WriteLine(RDta.ToString())

Threading.Thread.Sleep(500)

Threading.Thread.Sleep(500)

捕获 ex As 例外

Catch ex As Exception

MsgBox( " SendDataBack --- Data =" & RDta& " & ex.Message.ToString())

MsgBox("SendDataBack---Data = " & RDta & " " & ex.Message.ToString())

结束 尝试

End Try



私人 Sub SerialPort2_DataReceived( ByVal 发送方 System.Object, ByVal e As System.IO .Ports.SerialDataReceivedEventArgs) 句柄 SerialPort2.DataReceived

Private Sub SerialPort2_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort2.DataReceived

暗淡 RDta As 字符串 = "

Dim RDta As String = ""

尝试

Try

Threading.Thread.Sleep(2000)

Threading.Thread.Sleep(2000)

RDta = SerialPort2.ReadExisting()

RDta = SerialPort2.ReadExisting()

SendData(RDta)

SendData(RDta)

捕获 ex 例外

Catch ex As Exception

MsgBox( " SerialPort2_DataReceived & RDta& "" & ex.Message.ToString())

MsgBox("SerialPort2_DataReceived " & RDta & " " & ex.Message.ToString())

结束 尝试

End Try

结束 Sub

End Sub

Sub SendData( ByVal RDta 字符串 )

Sub SendData(ByVal RDta As String)

尝试

Try

暗淡 i 整数

Dim i As Integer

暗淡 Sdata(20) 字符串

Dim Sdata(20) As String

SerialPort1.WriteLine(RDta)

SerialPort1.WriteLine(RDta)

Threading.Thread.Sleep(500)

Threading.Thread.Sleep(500)

捕获 ex As 例外

Catch ex As Exception

MsgBox( " SendData" & RDta& ; "" & ex.Message.ToString())

MsgBox("SendData " & RDta & " " & ex.Message.ToString())

结束 尝试

End Try

结束 Sub

End Sub

推荐答案

您是否设置了ReceivedBytesThreshold?除非缓冲区中接收到的字节大于您设置的值,否则DataReceived事件将不会触发

您可以像这样使用它,当超过一个字节到达时将触发DataReceived事件

Did you set the ReceivedBytesThreshold? the DataReceived Event won't fire unless bytes recieved in the buffer is larger than the value you set

You may use it like this which will fire DataReceived Event when more than one byte arrive

SerialPort2.ReceivedBytesThreshold = 1


这篇关于使用两个串口进行通讯的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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