Ejemplo n.º 1
0
 /// <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());
 }
Ejemplo n.º 2
0
 /// <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());
 }
Ejemplo n.º 3
0
        /// <summary>
        /// 查询DI通道状态
        /// </summary>
        /// <param name="sp">串口辅助类对象</param>
        /// <param name="start">开始通道(从1开始)</param>
        /// <param name="num">查询通道数量(应不大于8)</param>
        /// <returns>通道闭合状态数组</returns>
        public static int[] CheckInput(this SerialPortUtil sp, int start, int num)
        {
            var status  = new int[num];
            var message = new byte[6];

            sp.SendCommand(CheckInputData(start, num), ref message, 100);
            int[] res = DataParse(message);
            Array.Copy(res, 0, status, 0, num);
            return(status);
        }
Ejemplo n.º 4
0
 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());
 }
Ejemplo n.º 5
0
 /// <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());
 }
Ejemplo n.º 6
0
 /// <summary>
 /// 控制所有继电器
 /// </summary>
 /// <param name="sp">串口辅助类对象</param>
 /// <param name="closing">闭合为true,断开为false</param>
 public static void ControlAllRelays(this SerialPortUtil sp, bool closing) =>
 sp.WriteData(closing ? SerialPortUtil.HexToByte("FE 0F 00 00 00 04 01 FF 31 D2") : SerialPortUtil.HexToByte("FE 0F 00 00 00 04 01 00 71 92"));
Ejemplo n.º 7
0
 /// <summary>
 /// 使用定时器查询DI通道状态,但不返回结果,需要使用DataReceived事件获取结果
 /// </summary>
 /// <param name="sp">串口辅助类对象</param>
 /// <param name="start">开始通道(从1开始)</param>
 /// <param name="num">查询通道数量(应不大于8)</param>
 /// <param name="timer">Timer控件对象</param>
 public static void CheckInput(this SerialPortUtil sp, int start, int num, Timer timer) =>
 timer.Tick += (sender, e) => { sp.WriteData(CheckInputData(start, num)); };