1、定义串口对象private SerialPort _Port;
2、初始化串口对象包括实例化串口和打开串口。public SerialPort(string portName,int baudRate,Parity parity,int dataBits,StopBits stopBits)portName 要使用的端口(例如 COM1)。baudRate 波特率。parity Parity 值之一。dataBits 数据位值。stopBits StopBits 值之一。
3、发送数据下面介绍字节数据发送和字符串数据发送。
4、关闭串口_Port.Close();
5、查看效果使用VSPD模拟一组串口,测试程序使用COM1口,串口接收程序使用COM2口,两个是一组串口可以相互通信。接收数据到的数据以16进制显示如下:以字节发送的数据:11 22 33 44以字符串发送的数据:31 31 32 32 33 33 34 34