MODBUS RTU communication protocol package developed based on C #

Paid ¥6.99Paid ¥6.99

该内容已接入现有会员/付费系统,解锁后可查看完整资料或下载附件。

查看解锁方式
MODBUS RTU communication protocol package developed based on C #

Create a MODBUS RTU instance based on the passed parameters:

/// <summary>
/// 建立 MODBUS RTU 串口业务
/// String comnum  =     串口号
/// int buad      =      串口的波特率
/// int dataLen   =      数据长度
/// String chklen    =      检验位
/// String stoplen   =      停止位
/// </summary>
public ModbusRTU_Lib(String comnum,int buad,int dataLen,string chklen,string stoplen,int scantime)

 

Add a MODBUS RTU scanning slave:

/// <summary>
/// 清除所有的从站列表
/// </summary>
public void clearAllModbusSalve()

/// <summary>
/// 删除扫描列表中的指定从站
/// </summary>
/// <param name="whichAddr"></param>
public void deletAllModbusSalve(int whichAddr)

Clear the MODBUS RTU multi slave scan list created:

 

/// <summary>
/// 增加 MODBUS 从站扫描列表
/// </summary>
/// <param name="xDeviceMdEnty"></param>
public void addModbusSlaveScanList(DeviceMdEnty xDeviceMdEnty)

Open a communication serial port of MODBUS RTU

/// <summary>
/// 打开串口
/// </summary>
/// <returns></returns>
public Boolean openUartOfModbus()

Close the serial port

/// <summary>
/// 关闭串口
/// </summary>
/// <returns></returns>
public Boolean closeUartOfModbus()

Activate the scheduled scanning service of MODBUS RTU

/// <summary>
/// 启动 MODBUS 扫描业务
/// </summary>
public void startModbusWork()

 

Send MODBUS RTU command

发送数据
*
int slaveAddr = 从站地址
int addrTe = 读写的启始地址
int cmdTe = 功能码
int lenTe =读出数据长度
byte[] msgBuf = 待写入的数据的缓冲期,每个字占 2BYTE
int msgLen  = 写入的数据长度
return :
*   true = 已发送数据,false = 未发送
*
*********************************************************************/
/// <summary>
///
/// </summary>
public Boolean sendModbusMsg(int slaveAddr,int cmdTe, int addrTe, int lenTe, byte[]
msgBuf, int msgLen)

The callback method of MODBUS RTU instance returns all data received by the serial port

Result =  "E00";表示"创建串口失败:"
Result =  "E01";表示"串口号未设置:"
Result =  "E02";表示"停止位参数错误:"
Result =  "E03";表示"校验位参数错误:"
Result =  "F00";表示"校验位参数错误:"
Result =  "Y00";表示"创建串口成功:"
Result =  "M00";表示"接收到串口数据:"

Declare the delegation regarding the event, with the parameter being the data type to be returned and the serial port receiving the data

public delegate void ModbusRTU_EventHandler(string result, byte[] msg) ;

 

The callback method of MODBUS RTU instance, which returns all data received by the serial port and verified by address and checksum

Result =  "AE0";表示"创建串口失败:"
Result =  "CE0";表示"串口号未设置:"
Result =  "DE0";表示"停止位参数错误:"
Result =  "D00";表示"接收到正确解析的串口数据:"
Result =  "D01";表示"功能码 0x01--正确应答数据! :"
Result =  "D02";表示"功能码 0x02--正确应答数据 !"
Result =  "D03";表示"功能码 0x03--正确应答数据 !"
Result =  "D04";表示"功能码 0x04--正确应答数据 !"
Result =  "D05";表示"功能码 0x05--正确应答数据 !"
Result =  "D06";表示"功能码 0x06--正确应答数据 !"
Result =  "D0F";表示"功能码 0x0F--正确应答数据 !"
Result =  "D10";表示"功能码 0x10--正确应答数据 !"

 

, with parameters of the data type to be returned, serial port receiving data and MODBUS verification

public delegate void ModbusRTU_DecodeHandler(string result, byte[] msg) ;

 

establishing one List of MODBUS RTU slave stations;

DLL will automatically scan based on the number of devices; For functions that are not available, specify a length of 0; If the 16 in 16 out digital module is used, the input register and hold register length can be specified as 0

/// <summary>
/// 建立扫描列表
/// </summary>
/// <param name="SlaveAddr">设置当前从站的站号</param>
/// <param name="CoilLen">设置当前从站的输出线圈个数</param>        /// <param name="DiscLen">设置当前从站的输入数字量个数</param>      /// <param name="InputRegLen">设置当前从站的输入寄存器个数</param>  /// <param name="HoldingRegLen">设置当前从站的保持寄存器个数</param>
public DeviceMdEnty(int SlaveAddr,  int CoilLen,  int DiscLen,  int InputRegLen,  int
HoldingRegLen)

 

 

 

 

Put this resource to use in a real project?

Go to the Tool Center for message parsing, CRC verification and device debugging, or submit your requirements for selection and integration advice.

Engineer Membership

Turn this article into actionable debugging resources

After activation, you can use advanced message parsing, resource pack downloads, code examples, engineering cases and priority technical support, suitable for real project delivery.

Unlimited Advanced Tools
Resource & Code Packs
Complete Engineering Case Library
Priority Technical Support

Leave a Reply

Your email address will not be published. Required fields are marked *.