C#WinForm串口调试工具源码
功能介绍:
运行本程序,在“发送数据”文本框中输入要传送的数据,
单击【发送】按钮,将传送的数据发送到所选择 的端口号中
主要代码:
private void btnOpenPort_Click(object sender, EventArgs e)
{
if (!openState)
{
openState = true;//已经打开
btnOpenPort.Text = "关闭串口";
this.pictureBox.Image = global::SerialDemo.Properties.Resources.btnOk2;
strPortName = cmbPort.Text;
strBaudRate = cmbBaudRate.Text;
strDataBits = cmbDataBits.Text;
strStopBits = cmbStopBit.Text;
sp.PortName = strPortName;
sp.BaudRate = int.Parse(strBaudRate);
sp.DataBits = int.Parse(strDataBits);
sp.StopBits = (StopBits)int.Parse(strStopBits);
sp.ReadTimeout = 500;
//打开
sp.Open();
sp.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived);
}
else
{
openState = false;
btnOpenPort.Text = "打开串口";
this.pictureBox.Image = global::SerialDemo.Properties.Resources.can1;
sp.DataReceived -= new SerialDataReceivedEventHandler(sp_DataReceived);
sp.Close();
}
}
1