一、安装Modbus包
二、创建modbushelper类
1、打开串口
public bool IfCOMOpend; //用于实例内的COM口的状态
public SerialPort OpenedCOM;//用于手动输入的COM转成SERIAL PORT
/// <summary>
/// 打开串口
/// </summary>
/// <param name="COMname">串口名称</param>
/// <param name="baudRate">波特率</param>/// <param name="parity">奇偶校验 (无,奇校验,偶校验)</param>
/// <param name="stopBits">停止位(1,2)</param>/// <param name="dataBits">数据位</param>
public bool OpenCOMPort(string COMname,string baudRate,string parity,string stopBits,string dataBits)
{
try
{Parity Comparity;
StopBits Comstopbits;
switch (parity)
{
case "无":
Comparity = Parity.None;
break;case "奇校验":
Comparity = Parity.Odd;
break;case "偶校验":
Comparity = Parity.Even;
break;
}文章来源:https://www.toymoban.com/news/detail-835377.htmlswitch (stopBits)
{
case "1":
Comstopbits = StopBits.One;
break;文章来源地址https://www.toymoban.com/news/detail-835377.html
到了这里,关于C#串口 Modbus通讯工具类的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!