//参数设置
private void button2_Click(object sender, System.EventArgs e)
{
mycom1.PortNum=Convert.ToInt16(t_port.Text); //1,2,3,4
mycom1.BaudRate=Convert.ToInt16(t_rate.Text); //1200,2400,4800,9600
mycom1.ByteSize=Convert.ToByte(t_bytesize.Text,10); //8 bits
mycom1.Parity=Convert.ToByte(t_parity.Text,10); // 0-4=no,odd,even,mark,space
mycom1.StopBits=Convert.ToByte(t_stopbyte.Text,10); // 0,1,2 = 1, 1.5, 2
//iTimeout=3;
if(this.OpenCom())
msg.AppendText("串口初始化成功……\r\n");
else
msg.AppendText("串口初始化失败!\r\n");
}
//程序关闭,结束串口
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
mycom1.Close();
}
private void button5_Click(object sender, System.EventArgs e)
{
if(mycom1.Opened)
{
mycom1.Close();
button5.Text="开启串口";
msg.AppendText("\r\n串口被关闭……");
}
else
{
mycom1.Open();
button5.Text="关闭串口";
msg.AppendText("\r\n串口成功开启……");
}
}
}
}