C#使用Modbus协议读写汇川PLC的M区寄存器

[删除(380066935@qq.com或微信通知)]

更好的阅读体验请查看原文:https://mp.weixin.qq.com/s/OolJxwJq9GMxChZGkWG1ZA

C#使用Modbus-TCP协议读取汇川PLC,Modbus读写是按照MW地址来处理的【寄存器单位是字WORD,占用两个字节,类似于C#中的ushort(UInt16)】,实际测试发现字符串是按照字节颠倒的本文封装函数为基础数据类型读写,字节流读写、长字符串读写。

主要封装函数有:public int WriteValue<T>(int startAddress, T value, out string msg) where T : struct

public int WriteValue(int startAddress, byte[] buffer, out string msg)

public int WriteString(int startAddress, int length, string barcode, out string msg)

public int WriteLongString(int startAddress, string longString, out string msg)

public int ReadValue<T>(int startAddress, out T value, out string msg) where T : struct

public int ReadValue(int startAddress, int length, out byte[] values, out string msg)

public int ReadLongString(int startAddress, int length, out string longString, out string msg)