【代码片段】两路串口转发的实现代码
在 C# 中,SerialPort 类用于处理串行端口通信。它提供了一种简单的方式来与串行端口进行通信,例如与串行设备(如传感器、微控制器、GPS 接收器等)通信。下面是 SerialPort 类的一些常见用法:
打开串行端口:使用 SerialPort 类可以打开一个串行端口,以便开始进行数据传输。
设置串行端口属性:可以设置串行端口的各种属性,如波特率、数据位、停止位、奇偶校验位等,以确保与设备的通信能够正确进行。
读取数据:可以使用 SerialPort 类从串行端口读取数据,这对于从传感器或其他设备获取信息非常有用。
写入数据:可以使用 SerialPort 类向串行端口写入数据,这对于向设备发送指令或数据非常有用。
监听串行端口事件:SerialPort 类还允许您监视串行端口的各种事件,如数据接收、错误发生等。
当两路串口进行转发示例代码如下更多示例代码片段 搜索关注公号【码字印象】:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO.Ports;
using System.Text;
using System.Windows.Forms;
namespace SerialPortUtil
{
public partial class FormMainFrame : Form
{
private SerialPort mSerialPortFrom = new SerialPort();
private SerialPort mSerialPortTo = new SerialPort();
private readonly object mLock = new object();
private delegate void InvokeCallBack();
private List<string> listStringFrom = new List<string>();
private List<string> listStringTo = new List<string>();
public FormMainFrame()
{
InitializeComponent();
SetStyle(ControlStyles.ResizeRedraw, true);
SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
/*
mSerialPortFrom.BaudRate = 115200;
mSerialPortFrom.Parity = Parity.None;
mSerialPortFrom.StopBits = StopBits.One;
mSerialPortFrom.DataBits = 8
mSerialPortTo.BaudRate = 9600;
mSerialPortTo.Parity = Parity.None;
mSerialPortTo.StopBits = StopBits.One;
mSerialPortTo.DataBits = 7;
serialPortPanelTo.ModifySerialPort(mSerialPortTo); */
buttonStop.Enabled = false;
mSerialPortFrom.DataReceived += ((s, e) =>
{
lock (mLock)
{
try
{
byte[] byteRead = new byte[mSerialPortFrom.BytesToRead];
if (byteRead.Length == 0)
return;
mSerialPortFrom.Read(byteRead, 0, byteRead.Length);
mSerialPortTo.Write(byteRead, 0, byteRead.Length);
string strHex = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
for (int i = 0; byteRead.Length > 0 && i < byteRead.Length; i++)
{
strHex += " " + byteRead[i].ToString("X2");
}
UpdateGridView(listStringTo, dataGridView2, strHex);
}
catch (Exception ex)
{
}
}
});
mSerialPortTo.DataReceived += ((s, e) =>
{
lock (mLock)
{
try
{
byte[] byteRead = new byte[mSerialPortTo.BytesToRead];
if (byteRead.Length == 0)
return;
mSerialPortTo.Read(byteRead, 0, byteRead.Length);
mSerialPortFrom.Write(byteRead, 0, byteRead.Length);
string strHex = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
for (int i = 0; byteRead.Length > 0 && i < byteRead.Length; i++)
{
strHex += " " + byteRead[i].ToString("X2");
}
UpdateGridView(listStringTo, dataGridView2, strHex);
}
catch (Exception ex)
{
}
}
});
}
private void UpdateGridView(List<string> list, DataGridView dataGridView, string strHex)
{
list.Insert(0, strHex);
if (list.Count > 50)
{
list.RemoveAt(50);
}
this.BeginInvoke(new InvokeCallBack(() =>
{
for (int i = 0; i < list.Count; i++)
{
if (dataGridView.Rows.Count <= i)
{
dataGridView.Rows.Add();
}
dataGridView.Rows[i].Cells[0].Value = list[i];
}
dataGridView.AutoResizeColumns();
}));
}
private void Start()
{
Stop();
try
{
serialPortPanelFrom.ModifySerialPort(mSerialPortFrom);
serialPortPanelTo.ModifySerialPort(mSerialPortTo);
mSerialPortTo.Open();
mSerialPortFrom.Open();
buttonStart.Enabled = false;
buttonStop.Enabled = true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
}
}
private void Stop()
{
try
{
mSerialPortTo.Close();
mSerialPortFrom.Close();
buttonStart.Enabled = true;
buttonStop.Enabled = false;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
}
}
private void buttonStart_Click(object sender, EventArgs e)
{
Start();
}
private void buttonStop_Click(object sender, EventArgs e)
{
Stop();
}
}
}