//通用设置命令 //功能码固定为0x10 //int startaddress 起始地址 //int[] data 数据数组 static public byte[] ModbusSetData(byte address, byte function, int startaddress, int[] data) { byte[] outByte = new byte[9 + data.Length * 2]; byte[] temp = new byte[4]; outByte[0] = address; outByte[1] = function; temp = BitConverter.GetBytes(startaddress - 1); outByte[2] = temp[1]; outByte[3] = temp[0]; temp = BitConverter.GetBytes(data.Length); outByte[4] = temp[1]; outByte[5] = temp[0]; outByte[6] = Convert.ToByte(data.Length * 2); for (int i = 0; i < data.Length * 2; i++) { temp = BitConverter.GetBytes(data[i / 2]); if (i % 2 == 0) { outByte[7 + i] = temp[1]; } else { int c = data[i / 2]; outByte[7 + i] = temp[0]; } } return(DataInfo.CRC16(outByte)); }
static public byte[] SetData(byte address, byte function) { byte[] outByte = new byte[9]; outByte[0] = 0x21; outByte[1] = 0x58; outByte[2] = 0x44; outByte[3] = address; outByte[4] = 0xA0; outByte[5] = function; outByte[6] = 0x00; return(DataInfo.CRC16(outByte)); }
//数据处理方法 #region //获取记录总数 功能码0x06 public static byte[] Get_recordcount(byte address) { byte[] outByte = new byte[9]; outByte[0] = 0x21; outByte[1] = 0x58; outByte[2] = 0x44; outByte[3] = address; outByte[4] = 0xb0; outByte[5] = 0x06; outByte[6] = 0x00; return(DataInfo.CRC16(outByte)); }
//读取第N条记录 功能码0x07 public static byte[] Get_recordn(byte address, int n) { byte[] outByte = new byte[10]; outByte[0] = 0x21; outByte[1] = 0x58; outByte[2] = 0x44; outByte[3] = address; outByte[4] = 0xb0; outByte[5] = 0x07; outByte[6] = 0x01; outByte[7] = Convert.ToByte(n % 256); return(DataInfo.CRC16(outByte)); }
//设置时间 public static byte[] Set_time(byte address) { byte[] outByte = new byte[12]; outByte[0] = 0x21; outByte[1] = 0x58; outByte[2] = 0x44; outByte[3] = address; outByte[4] = 0xb0; outByte[5] = 0x01; outByte[6] = 0x03; outByte[7] = DataInfo.bytetoBCD(Convert.ToByte(DateTime.Now.Second)); outByte[8] = DataInfo.bytetoBCD(Convert.ToByte(DateTime.Now.Minute)); outByte[9] = DataInfo.bytetoBCD(Convert.ToByte(DateTime.Now.Hour)); return(DataInfo.CRC16(outByte)); }
static public byte[] SetData(byte address, byte function, byte[] data) { byte[] outByte = new byte[9 + data.Length]; outByte[0] = 0x21; outByte[1] = 0x58; outByte[2] = 0x44; outByte[3] = address; outByte[4] = 0xA0; outByte[5] = function; outByte[6] = Convert.ToByte(data.Length); for (int i = 0; i < data.Length; i++) { outByte[7 + i] = data[i]; } return(DataInfo.CRC16(outByte)); }
//通用生成读取命令函数 //byte address 设备地址 //byte function 功能码 //int startaddress 起始地址 //int endaddress 结束地址 static public byte[] ModbusGetData(byte address, byte function, int startaddress, int endaddress) { int length = endaddress - startaddress + 1; byte[] outByte = new byte[8]; byte[] temp = new byte[4]; outByte[0] = address; outByte[1] = function; temp = BitConverter.GetBytes(startaddress - 1); outByte[2] = temp[1]; outByte[3] = temp[0]; temp = BitConverter.GetBytes(length); outByte[4] = temp[1]; outByte[5] = temp[0]; return(DataInfo.CRC16(outByte)); }
//设置日期 public static byte[] Set_date(byte address) { byte[] outByte = new byte[12]; outByte[0] = 0x21; outByte[1] = 0x58; outByte[2] = 0x44; outByte[3] = address; outByte[4] = 0xb0; outByte[5] = 0x02; outByte[6] = 0x03; outByte[7] = DataInfo.bytetoBCD(Convert.ToByte(DateTime.Now.Day)); outByte[8] = DataInfo.bytetoBCD(Convert.ToByte(DateTime.Now.Month)); outByte[9] = DataInfo.bytetoBCD(Convert.ToByte(DateTime.Now.Year % 100)); return(DataInfo.CRC16(outByte)); }
//补水泵启动 public static byte[] Set_addpumprun(byte address) { byte[] buffer = { address, 0x0F, 0x00, 0x06, 0x00, 0x01, 0x01, 0x01, 0x00, 0x00 }; return(DataInfo.CRC16(buffer)); }
//循环泵急停 public static byte[] Set_cycpumpstop(byte address) { byte[] buffer = { address, 0x0F, 0x00, 0x05, 0x00, 0x01, 0x01, 0x01, 0x00, 0x00 }; return(DataInfo.CRC16(buffer)); }
static public byte[] xd100GetData(byte address, byte function, byte menu) { byte[] outByte = { 0x21, 0x58, 0x44, address, 0xA0, function, 0x01, menu, 0x00, 0x00 }; return(DataInfo.CRC16(outByte)); }