使用wpf做的一款通过串口与下位机接收、发送数据进行通讯
public void SynReceiveData(object serialPortobj)
{
SerialPort serialPort = (SerialPort)serialPortobj;
// 初始化串口状态
serialPort.PortName = SlectComName;
serialPort.BaudRate = System.Convert.ToInt32(SlectBaudRate);
serialPort.DataBits = System.Convert.ToInt32(SlectDatabit);
serialPort.Parity = (Parity)System.Convert.ToInt32(SlectParity);
serialPort.StopBits = (StopBits)System.Convert.ToInt32(SlectStopBit);
serialPort.WriteTimeout = 3000;
serialPort.ReadTimeout = 3000;
serialPort.ReceivedBytesThreshold = 1; //初始化串口
serialPort.DataReceived = new SerialDataReceivedEventHandler(com_DataReceived);
try
{
serialPort.Open();
}
catch (Exception ee)
{
MessageBox.Show("此端口已被占用");
BtnName = "打开";
return;
}
}
评论