因为前段时间需要用到串口接收数据,且使用的串口号不确定,需采用多线程的方式来实现接收。
创建线程:
Thread thread = null;
for (int i = 0; i < PortNum; i++)//根据选择的串口号数量创建对应数量的线程
{
thread = new Thread(Text_Run);
thread.Name = Convert.ToString(i);
thread.IsBackground = true;//将线程转为后台线程
thread.Start(comport[i]);//传入串口号
}
线程运行函数:
public static void Run(string serialName )
{
SerialPort sp1 = new SerialPort();
sp1.PortName = serialName;//设置串口名
sp1.BaudRate = 57600; //波特率
sp1.DataBits = 8; //数据位
sp1.StopBits = StopBits.One;//