/// <summary> /// 设置工作模式 /// </summary> /// <param name="sp">串口辅助类对象</param> /// <param name="wm">工作模式</param> public static void SetWorkMode(this SerialPortUtil sp, WorkMode wm) { byte[] msg, crc16; msg = SerialPortUtil.HexToByte("FE 10 03 EB 00 01 02 00 00"); msg[8] = (byte)(int)wm; crc16 = SerialPortUtil.CRC16(msg, CRCMode.MODBUS); Array.Reverse(crc16); sp.WriteData(msg.Concat(crc16).ToArray()); }
/// <summary> /// 控制继电器 /// </summary> /// <param name="sp">串口辅助类对象</param> /// <param name="channel">通道(从1开始)</param> /// <param name="closing">闭合为true,断开为false</param> public static void ControlRelay(this SerialPortUtil sp, int channel, bool closing) { byte[] msg, crc16; msg = SerialPortUtil.HexToByte("FE 05 00 00 00 00"); msg[3] = (byte)(channel - 1); msg[4] = (byte)(closing ? 0xFF : 0x00); crc16 = SerialPortUtil.CRC16(msg, CRCMode.MODBUS); Array.Reverse(crc16); sp.WriteData(msg.Concat(crc16).ToArray()); }
private static byte[] CheckInputData(int start, int num) { byte[] msg, crc16; msg = SerialPortUtil.HexToByte("FE 02 00 00 00 00"); msg[3] = (byte)(start - 1); msg[5] = (byte)num; crc16 = SerialPortUtil.CRC16(msg, CRCMode.MODBUS); Array.Reverse(crc16); return(msg.Concat(crc16).ToArray()); }
/// <summary> /// 控制继电器 /// </summary> /// <param name="sp">串口辅助类对象</param> /// <param name="channel">通道(从1开始)</param> /// <param name="closing">闪闭为true,闪开为false</param> /// <param name="time">时间,单位100ms</param> public static void ControlRelay(this SerialPortUtil sp, int channel, bool closing, int time) { byte[] msg, crc16; msg = SerialPortUtil.HexToByte("FE 10 00 00 00 02 04 00 00 00 00"); msg[3] = (byte)(channel * 5 - 2); msg[8] = (byte)(closing ? 0x04 : 0x02); msg[10] = (byte)time; crc16 = SerialPortUtil.CRC16(msg, CRCMode.MODBUS); Array.Reverse(crc16); sp.WriteData(msg.Concat(crc16).ToArray()); }