C#怎么使用COM端口?

最好有实例项目介绍
2024-11-15 06:31:18
推荐回答(1个)
回答1:

用SerialPort

SerialPort _Serial = new SerialPort
{
PortName = "COM1",
BaudRate = 9600,
DataBits = 8,
Parity = Parity.None,
StopBits = StopBits.One,
WriteTimeout = SerialPort.InfiniteTimeout,
ReadTimeout = SerialPort.InfiniteTimeout,
Handshake = Handshake.None,
ReadBufferSize = 1024
};

_Serial.DataReceived += new SerialDataReceivedEventHandle(Serial_DataReceived);

_Serial.Open();


private void Serial_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
//在这里写接收到数据时的处理
...
}