Пример #1
0
        /// <summary>
        /// 终端心跳
        /// </summary>
        private void SendClientPump()
        {
            HeadPack head = new HeadPack()
            {
                SeqNO = GetNextSeqNum(), MessageId = (ushort)MessageIds.ClientPump, BodyProp = (ushort)0
            };

            head.SetDeviceId(this.txtDeviceId.Text.Trim());
            //ClientPumpPack pack = new ClientPumpPack();
            //byte[] bytesSend = RawFormatter.Instance.Struct2Bytes(pack);
            //BodyPropertyHelper.SetMessageLength(ref head.BodyProp, (ushort)bytesSend.Length);
            BodyPropertyHelper.SetMessageLength(ref head.BodyProp, (ushort)0);

            byte[] headBytes = RawFormatter.Instance.Struct2Bytes(head);
            byte[] fullBytes = headBytes;// headBytes.Concat(bytesSend).ToArray();
            byte   checkByte = PackHelper.CalcCheckByte(fullBytes, 0, fullBytes.Length);

            byte[] bytesSend = (new byte[] { 0x7e }
                                .Concat(PackHelper.EncodeBytes(fullBytes.Concat(new byte[] { checkByte })))
                                .Concat(new byte[] { 0x7e })).ToArray();
            //////////////////////////////////////////////////////////////////////////
            //string e1 = Enum.GetName(typeof(MessageIds), Convert.ToInt32(head.MessageId));
            string e2 = ExtensionMethods.GetDescriptionByName <MessageIds>((MessageIds)Convert.ToInt32(head.MessageId));

            /////////////////////////////////////////////////////////////////////////////
            this.dataGridView1.Rows.Add("↑", head.GetDeviceId(), DateTime.Now, head.SeqNO, "0x" + Convert.ToString(head.MessageId, 16).PadLeft(4, '0') + "(" + e2 + ")", 0, bytesSend.ToHexString());

            this.dataGridView1.FirstDisplayedScrollingRowIndex = this.dataGridView1.Rows.Count - 1;
            //this.dataGridView1.Rows[this.dataGridView1.Rows.Count - 1].Selected = true;
            //this.dataGridView1.CurrentCell = this.dataGridView1[0, this.dataGridView1.Rows.Count - 1];

            SendMessage(bytesSend);;
        }
Пример #2
0
        public static byte[] ToBytes(string deviceId, MessageIds messageId, ushort seqNO, byte[] bodyBytesReturn)
        {
            HeadPack head = new HeadPack()
            {
                BodyProp  = 0,
                MessageId = (ushort)messageId,
                SeqNO     = seqNO
            };

            head.SetDeviceId(deviceId);
            //BodyPropertyHelper.SetMessageLength(ref head.BodyProp,(ushort)bodyBytesReturn.Length);
            BodyPropertyHelper.SetMessageLength(ref head.BodyProp, (ushort)bodyBytesReturn.Length);
            return(RawFormatter.Instance.Struct2Bytes(head).Concat(bodyBytesReturn).ToArray());
        }
Пример #3
0
        /// <summary>
        /// 终端注册
        /// </summary>
        private void SendClientRegist()
        {
            HeadPack head = new HeadPack()
            {
                SeqNO = GetNextSeqNum(), MessageId = (ushort)MessageIds.ClientRegist, BodyProp = (ushort)0
            };

            head.SetDeviceId(this.txtDeviceId.Text.Trim());
            ClientRegistPack pack = new ClientRegistPack()
            {
                Province     = Convert.ToUInt16("32"),
                City         = Convert.ToUInt16("1100"),
                Manufacturer = new byte[5],
                DeviceModel  = new byte[20],
                DeviceId     = new byte[7],
                CarColor     = 2
                               //CarNumber = System.Text.Encoding.GetEncoding("GBK").GetBytes(txtCarNumber.Text.Trim())
            };

            //车牌号
            byte[] carNumber = System.Text.Encoding.GetEncoding("GBK").GetBytes(txtCarNumber.Text.Trim());

            byte[] bytesSend = RawFormatter.Instance.Struct2Bytes(pack);
            //加上车牌
            bytesSend = bytesSend.Concat(carNumber).ToArray();

            BodyPropertyHelper.SetMessageLength(ref head.BodyProp, (ushort)bytesSend.Length);

            byte[] headBytes = RawFormatter.Instance.Struct2Bytes(head);
            byte[] fullBytes = headBytes.Concat(bytesSend).ToArray();
            byte   checkByte = PackHelper.CalcCheckByte(fullBytes, 0, fullBytes.Length);

            bytesSend = (new byte[] { 0x7e }
                         .Concat(PackHelper.EncodeBytes(fullBytes.Concat(new byte[] { checkByte })))
                         .Concat(new byte[] { 0x7e })).ToArray();
            //////////////////////////////////////////////////////////////////////////
            //string e1 = Enum.GetName(typeof(MessageIds), Convert.ToInt32(head.MessageId));
            string e2 = ExtensionMethods.GetDescriptionByName <MessageIds>((MessageIds)Convert.ToInt32(head.MessageId));

            /////////////////////////////////////////////////////////////////////////////
            this.dataGridView1.Rows.Add("↑", head.GetDeviceId(), DateTime.Now, head.SeqNO, "0x" + Convert.ToString(head.MessageId, 16).PadLeft(4, '0') + "(" + e2 + ")", 0, bytesSend.ToHexString());

            this.dataGridView1.FirstDisplayedScrollingRowIndex = this.dataGridView1.Rows.Count - 1;
            //this.dataGridView1.Rows[this.dataGridView1.Rows.Count - 1].Selected = true;
            //this.dataGridView1.CurrentCell = this.dataGridView1[0, this.dataGridView1.Rows.Count - 1];

            SendMessage(bytesSend);
        }