1.界面初始化
private void FrmModbusRtu_Load(object sender, EventArgs e)
{
this.lst_Info.Columns[1].Width = this.lst_Info.ClientSize.Width -
this.lst_Info.Columns[0].Width;
//串口名
string[] PortName = SerialPort.GetPortNames();
this.cmb_Port.DataSource = PortName;
this.cmb_Port.SelectedIndex = 0;
//波特率
this.cmb_Paud.DataSource = new string[] {"19200","38400","115200"};
this.cmb_Paud.SelectedIndex = 0;
//检验位
this.cmb_Parity.DataSource = Enum.GetNames(typeof(Parity));
this.cmb_Parity.SelectedIndex = 0;
//停止位
this.cmb_StopBits.DataSource = Enum.GetNames(typeof(StopBits));
this.cmb_StopBits.SelectedIndex = 1;
//数据格式
this.cmb_DataFormat.DataSource = Enum.GetNames(typeof(dataFormeat));
this.cmb_DataFormat.SelectedIndex = 0;
//存储区域
this.cmb_StoreArea.DataSource = Enum.GetNames(typeof(StoreArea));
this.cmb_StoreArea.SelectedIndex = 0;
this.cmb_VarType.DataSource = Enum.GetNames(typeof(VarType));
}
2.串口参数设置
if (IsConnected)
{
AddLog(1, "串口已经连接,请勿重复连接");
return;
}
else
{
MyCom.PortName = this.cmb_Port.Text.Trim();
MyCom.BaudRate = int.Parse(this.cmb_Paud.Text.Trim());
MyCom.Parity = (Parity)(Enum.Parse(typeof(Parity),
this.cmb_Parity.Text.Trim(), false));
MyCom.StopBits = (StopBits)(Enum.Parse(typeof(StopBits),
this.cmb_StopBits.Text.Trim(), false));
modBusConfigHelper.OpenCom(MyCom);
AddLog(0, "串口连接成功!");
IsConnected = true;
}
3.串口关闭
if(IsConnected)
{
mod