首页 > Python基础教程 >
-
C#教程之C# 虚拟串口通信
将主端口COM8拆分成 COM1和COM2两个虚拟端口
COM8接收的消息会传递给COM1和COM2
SerialPort spSend;//spSend,spReceive用虚拟串口连接,它们之间可以相互传输数据。spSend发送数据
SerialPort spReceive; //spReceive接受数据
private delegate void MyDelegate(string indata); //委托,处理接收的数据
private MyDelegate showDelegate = null;
showDelegate = new MyDelegate(show);
spReceive = new SerialPort("COM1", 19200, Parity.None, 8, StopBits.One);
spReceive.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler); //虚拟串口监听接收消息监听事件,该事件并不在主线程运行
spReceive.Open();//打开串口,工作完成时务必关闭串口
void show(string indata)
{
//处理COM1接收的消息
}
private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
this.Invoke(showDelegate, new object[] { sp.ReadExisting() });
}
软件:Serial Splitter 4.2
1
|
http://www.pc0359.cn/downinfo/57195.html |