예제 #1
0
        private void btn_set_mode_Click(object sender, EventArgs e)
        {
            int current_mode = int.Parse(txt_mode.Text.Trim());

            byte[] msg = SerialMessage.pack_msg(new byte[] { 0x52, (byte)current_mode });
            serial1.Write(msg, 0, msg.Length);
        }
예제 #2
0
 // 串口采样功能启动
 private void serial1_send_0x69()
 {
     if (!serial1.IsOpen)
     {
         return;
     }
     byte[] msg = SerialMessage.pack_msg(new byte[] { 0x69 });
     serial1.Write(msg, 0, msg.Length);
 }
예제 #3
0
        private void serial1_controller_set_int(byte index, int value)
        {
            List <byte> li = new List <byte>();

            li.Add(0x57);
            li.Add(index);
            li.AddRange(BitConverter.GetBytes(value));
            byte[] msg = SerialMessage.pack_msg(li.ToArray());
            serial1.Write(msg, 0, msg.Length);
        }
예제 #4
0
 // 发送心跳数据
 private void serial1_send_0x50()
 {
     if (!serial1.IsOpen)
     {
         // TODO 如果串口处于关闭状态
         return;
     }
     byte[] msg = SerialMessage.pack_msg(new byte[] { 0x50 });
     serial1.Write(msg, 0, msg.Length);
 }
        // 处理一段接收到的消息
        // 返回通过验证的消息
        // 如果处理失败则返回null
        public static byte[] analyze_msg(byte[] data)
        {
            // 非null验证
            if (data == null)
            {
                return(null);
            }
            // 消息长度不小于5
            if (data.Length < 5)
            {
                return(null);
            }
            // 长度验证
            if (data[0] != (byte)data.Length)
            {
                return(null);
            }
            // CHECKSUM
            int sum = 0;

            foreach (byte b in data)
            {
                sum += b;
            }
            sum = sum & 0xFF;
            if (sum != 0)
            {
                return(null);
            }
            // CHECK CRC16
            data[1] = 0;
            ushort r_crc = (ushort)((data[data.Length - 2] << 8) | (data[data.Length - 1]));

            data[data.Length - 2] = 0;
            data[data.Length - 1] = 0;
            ushort c_crc = SerialMessage.calculate_crc16(data);

            if (r_crc != c_crc)
            {
                return(null);
            }
            // 通过验证 删掉消息中的验证码部分
            LinkedList <byte> li = new LinkedList <byte>(data);

            li.RemoveFirst();
            li.RemoveFirst();
            li.RemoveLast();
            li.RemoveLast();
            return(li.ToArray());
        }
예제 #6
0
        private void serial1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            // 如果还有没有读取的缓冲区字符
            while (serial1.BytesToRead > 0)
            {
                byte b = (byte)serial1.ReadByte();
                // DEBUG
                // Debug.Print("{0} ", serial1.ReadByte());

                // 如果没有处于接收状态
                if (!serial_cmd_is_receive)
                {
                    if (b == SerialMessage.MSG_STTC)
                    {
                        serial_cmd_is_receive = true;
                        serial_cmd_is_escape  = false;
                    }
                    continue;
                }
                // (接收状态中) 如果处于转义中
                if (serial_cmd_is_escape)
                {
                    // 直接将数据放入List
                    serial_cmd.AddLast(b);
                    serial_cmd_is_escape = false;
                    continue;
                }
                // (接收状态中,无转义)如果接收到结束字符或者转义字符
                if (b == SerialMessage.MSG_ENDC)
                {
                    // 处理数据
                    InterfaceHandle interfaceUpdateHandle = new InterfaceHandle(serial_execute_command);
                    this.Invoke(interfaceUpdateHandle, new object[] { SerialMessage.analyze_msg(serial_cmd.ToArray()) });
                    // SerialMessage.analyze_msg(serial_cmd.ToArray());
                    // 清空
                    serial_cmd.Clear();
                    serial_cmd_is_receive = false;
                    serial_cmd_is_escape  = false;
                    continue;
                }
                if (b == SerialMessage.MSG_ESCC)
                {
                    serial_cmd_is_escape = true;
                    continue;
                }
                // (接收状态 无转义 不是特殊字符)
                serial_cmd.AddLast(b);
            }
        }
예제 #7
0
        // 发送时间同步数据
        private void serial1_send_0x78()
        {
            byte[] msg = new byte[5];
            msg[0] = 0x78;
            DateTime dt    = DateTime.Now;
            long     ticks = dt.AddYears(-1969).Ticks;

            ticks  /= 10000000;//转换到Unix秒时间戳
            msg[1]  = (byte)(ticks & 0xFF);
            ticks >>= 8;
            msg[2]  = (byte)(ticks & 0xFF);
            ticks >>= 8;
            msg[3]  = (byte)(ticks & 0xFF);
            ticks >>= 8;
            msg[4]  = (byte)(ticks & 0xFF);
            // 打包并发送
            byte[] pack = SerialMessage.pack_msg(msg);
            serial1.Write(pack, 0, pack.Length);
        }
예제 #8
0
 private void serial1_controller_require_int(byte index)
 {
     byte[] msg = SerialMessage.pack_msg(new byte[] { 0x56, index });
     serial1.Write(msg, 0, msg.Length);
 }