首页 > 其他分享 >串口通信SerialPort

串口通信SerialPort

时间:2022-09-07 11:48:19浏览次数:76  
标签:Handshake 通信 SerialPort Write 串口 new mySerialPort

1.NuGet 添加SerialPort

//配置SerialPort的名称,端口号,波特率等
SerialPort mySerialPort = new SerialPort("COM2");
mySerialPort.BaudRate = 9600;
mySerialPort.Parity=Parity.None;
mySerialPort.StopBits = StopBits.One;
mySerialPort.DataBits = 8;
mySerialPort.Handshake = Handshake.Non;
//数据接收事件
mySerialPort.DataReceived += new SerialDataReceivedEvenHandler(DataReceive_Method);
//打开串口
mySerialPort.Open();
//发送数据
mySerialPort.Write("Hello World");
mySerialPort.Write(new byte[] { 0x0A, 0xE2, 0xFF }, 0, 3);
//关闭串口
mySerialPort.Close();

private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{            
   Console.WriteLine(mySerialPort.ReadExisting());
}

 

标签:Handshake,通信,SerialPort,Write,串口,new,mySerialPort
From: https://www.cnblogs.com/YSAN718596403/p/16664823.html

相关文章