项目地址:GitHub - xiaoxinpro/SerialPortHelper: 基于C#的串口助手类库
using SerialPortHelperLib;
//定义SerialPortHelper类
private SerialPortHelper spb;
//实例化串口助手
spb = new SerialPortHelper();
//设置串口号ComboBox,并写入默认配置
spb.ConfigSerialPort = new ConfigCom(cbPortName).GetConfigComData();
//定义ConfigCom类
private ConfigCom cc;
//修改配置
cc = new ConfigCom(cbSerial);
cc.BaudRate = 14400;
cc.DataBits = 7;
cc.StopBits = StopBits.OnePointFive;
cc.Parity = Parity.Even;
//写入串口配置
spb.ConfigSerialPort = cc.GetConfigComData();
spb.OpenCom(out string strError);
if(strError != "null")
{
//串口开启失败
MessageBox.Show(strError);
}
spb.CloseCom(out string strError);
if (strError != "null")
{
//关闭串口失败
MessageBox.Show(strError);
}
spb.Write(Byte数组);
spb.Write(SerialData.ToHexByteArray(十六进制字符串));
spb.Write(SerialData.ToByteArray(字符串));