代码
public class ModbusHelp { #region 功能码 /// <summary> /// 读线圈寄存器 /// </summary> public const byte MB_READ_COILS = 0x01; /// <summary> /// 读离散输入寄存器 /// </summary> public const byte MB_READ_DISCRETE = 0x02; /// <summary> /// 读保持寄存器 /// </summary> public const byte MB_READ_HOLD_REG = 0x03; /// <summary> /// 读输入寄存器 /// </summary> public const byte MB_READ_INPUT_REG = 0x04; /// <summary> /// 写单个线圈 /// </summary> public const byte MB_WRITE_SINGLE_COIL = 0x05; /// <summary> /// 写单寄存器 /// </summary> public const byte MB_WRITE_SINGLE_REG = 0x06; /// <summary> /// 写多线圈 /// </summary> public const byte MB_WRITE_MULTIPLE_COILS = 0x0f; /// <summary> /// 写多寄存器 /// </summary> public const byte MB_WRITE_MULTIPLE_REGS = 0x10; #endregion #region 协议 /// <summary> /// 事物处理标识符 /// </summary> public byte[] MB_Thing = new byte[] { 0x00, 0x0B }; /// <summary> /// 协议标识符 /// </summary> public byte[] MB_Agreement = new byte[] { 0x00, 0x00 }; /// <summary> /// 单元标识符 /// </summary> public byte MB_Cell = 0x04; #endregion /// <summary> /// 计算数据长度 /// </summary> /// <param name="byteData"></param> /// <returns></returns> public byte[] GetDataLength(byte[] byteData) { int leng = byteData.Length; return IntToByte(leng); } /// <summary> /// int类型转字节 /// </summary> /// <param name="data"></param> /// <returns></returns> public byte[] IntToByte(int data) { byte[] bytes = BitConverter.GetBytes(Convert.ToInt16(data)); return ReplenishByte(bytes); } /// <summary> /// 补充字节 /// </summary> /// <returns></returns> public byte[] ReplenishByte(byte[] byteData) { if (byteData.Length == 2 && byteData[1] == 0) { byteData = new byte[] { 0x00, byteData.FirstOrDefault() }; } else { byteData = new byte[] { byteData[1], byteData[0] }; } return byteData; } //public byte[] IntToByte(int data) //{ // var hexValue = data.ToString("x"); // if (hexValue.Length <= 2) // { // return new byte[] { 0x00, Convert.ToByte(hexValue, 16) }; // } // else // { // if (hexValue.Length % 2 != 0)//不是两位的补0 // { // hexValue = "0" + hexValue; // } // char[] hexCharacters = hexValue.ToCharArray(); // byte[] byteArray = new byte[Convert.ToInt32(Math.Ceiling((double)hexCharacters.Length / 2))]; // for (int i = 0; i < byteArray.Length; i++) // { // string hexVal = hexValue.Substring(i * 2, 2); // byteArray[i] = Convert.ToByte(hexVal, 16); // } // if (byteArray.Length <= 1) // { // return new byte[] { 0x00, byteArray.FirstOrDefault() }; // } // return byteArray; // } //} /// <summary> /// 读线圈寄存器 /// </summary> /// <param name="state">读取位置</param> /// <param name="places">读取多少位</param> /// <returns></returns> public List<byte> ReadModbusSingle(int state, int places) { List<byte> SendCommand = new List<byte>(); //事物处理标识符 SendCommand.AddRange(MB_Thing); //协议标识符 SendCommand.AddRange(MB_Agreement); //长度 SendCommand.AddRange(new byte[] { 0x00, 0x06 }); //单元标识符 SendCommand.Add(MB_Cell); //功能码 SendCommand.Add(MB_READ_HOLD_REG); //起始地址 SendCommand.AddRange(IntToByte(state)); //结束地址-长度 SendCommand.AddRange(IntToByte(places)); return SendCommand; } /// <summary> /// 写单寄存器 /// </summary> /// <param name="state">读取位置</param> /// <param name="places">读取多少位</param> /// <returns></returns> public List<byte> WriteModbusSingle(int index, int value) { List<byte> SendCommand = new List<byte>(); //事物处理标识符 SendCommand.AddRange(MB_Thing); //协议标识符 SendCommand.AddRange(MB_Agreement); //长度 SendCommand.AddRange(new byte[] { 0x00, 0x06 }); //单元标识符 SendCommand.Add(MB_Cell); //功能码 SendCommand.Add(MB_WRITE_SINGLE_REG); //起始地址 SendCommand.AddRange(IntToByte(index)); //结束地址-长度 SendCommand.AddRange(IntToByte(value)); return SendCommand; } /// <summary> /// 写入多个寄存器 /// </summary> /// <param name="index">位置</param> /// <param name="values">值</param> /// <returns></returns> public List<byte> WriteModbusList(int index, List<int> values) { //写入的值 List<byte> valueByte = new List<byte>(); foreach (var it in values) { valueByte.AddRange(IntToByte(it)); } byte[] valueLeng = BitConverter.GetBytes(Convert.ToInt16(valueByte.Count())); valueByte.Insert(0, valueLeng.FirstOrDefault()); List<byte> SendCommand = new List<byte>(); //事物处理标识符 SendCommand.AddRange(MB_Thing); //协议标识符 SendCommand.AddRange(MB_Agreement); //长度 SendCommand.AddRange(IntToByte(valueByte.Count() + 6)); //单元标识符 1 SendCommand.Add(MB_Cell); //功能码 1 SendCommand.Add(MB_WRITE_MULTIPLE_REGS); //写入位置 2 SendCommand.AddRange(IntToByte(index)); //写入寄存器数量 SendCommand.AddRange(IntToByte(values.Count())); //写入的值 SendCommand.AddRange(valueByte); return SendCommand; } }
标签:协议,MB,List,AddRange,modubs,byte,public,SendCommand From: https://www.cnblogs.com/shuaimeng/p/18333068