简单的串口通讯
1 .选择windows窗体里面的SerialPort事件
2. 配置好串口,确保能检测到串口,打开串口
private SerialPort EscaleSP = null;
Escale.EType = this.cmbEscale1_Type.Text;
Escale.PortName = this.cmbEscale1_PortNum.Text;
Escale.BaudRate = int.Parse(this.cmbEscale1_BaudRate.Text);
Escale.DataBit = int.Parse(this.cmbEscale1_DataBits.Text);
Escale.StopBit = cmbEscale1_StopBits.Text;
Escale.Parity = cmbEscale1_Parity.Text;
Escale.Open() ; //打开串口
Escale.Close(); //关闭串口
3.加一个计时器监控串口的状态
// 使用串口的 IsOpen 属性(bool类型)
private void timer1_Tick(object sender, EventArgs e)
{
if(EscaSP.IsOpen){} //检测串口的状态
}
串口的serialPort1_DataReceived事件接收数据
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) { }