Ejemplo n.º 1
0
        public static void GetBytesObject(byte[] buf, object anything, ref uint index, uint nSize)
        {
            try
            {
                #region HEAD
                if (anything is HEAD)
                {
                    HEAD Hd = anything as HEAD;
                    Hd.STX = TcpUtil.ConvertByte(buf, ref index);
                    // Hd.Version = TcpUtil.ConvertByte(buf, ref index);
                    // Hd.DeviceID = TcpUtil.ConvertUShort(buf, ref index);
                    Hd.opcode = TcpUtil.ConvertByte(buf, ref index);
                    // Hd.Reserved = TcpUtil.ConvertByte(buf, ref index);
                    Hd.Length = TcpUtil.ConvertUShort(buf, ref index);
                }
                #endregion

                #region TAIL
                if (anything is TAIL)
                {
                    TAIL Tl = anything as TAIL;
                    //  Tl.Reserved = TcpUtil.GetBytesToBytes(buf, ref index, 2);
                    Tl.Checksum = TcpUtil.ConvertByte(buf, ref index);
                    Tl.ETX      = TcpUtil.ConvertByte(buf, ref index);
                }
                #endregion

                //#region ACK
                //if (anything is BIS_OP_66)
                //{
                //    BIS_OP_66 op66 = anything as BIS_OP_66;
                //    //op66.SendDate = TcpUtil.ConvertUInt(buf, ref index);
                //    //op66.SendTime = TcpUtil.ConvertUInt(buf, ref index);
                //    op66.OccurDate = TcpUtil.ConvertUInt(buf, ref index);
                //    op66.OccurTime = TcpUtil.ConvertUInt(buf, ref index);
                //    op66.bus_speed = TcpUtil.ConvertByte(buf, ref index);
                //    op66.Pos = TcpUtil.ConvertUInt(buf, ref index);
                //    op66.heading = TcpUtil.ConvertByte(buf, ref index);

                //    op66.bus_speed = TcpUtil.ConvertByte(buf, ref index);
                //    op66.device_stat = TcpUtil.ConvertUInt(buf, ref index);

                //    op66.bnode_id = TcpUtil.ConvertUInt(buf, ref index);
                //    op66.bm_seqno = TcpUtil.ConvertByte(buf, ref index);
                //    op66.dptc_seqno = TcpUtil.ConvertByte(buf, ref index);
                //    op66.travel_time = TcpUtil.ConvertByte(buf, ref index);
                //    op66.cdma_grade = TcpUtil.ConvertInt(buf, ref index);

                //    op66.Reserved = TcpUtil.GetBytesToBytes(buf, ref index, 2);
                //}
                //#endregion
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// TCP 통신을 보낸다.
        /// </summary>
        public bool Send(byte OpCode, byte[] cSendBuf, uint nSize)
        {
            //if (IsDispose || !tcpClient.Connected) return false;

            try
            {
                List <byte> sendmsg = new List <byte>();
                HEAD        head    = new HEAD();

                head.STX    = 0x02;
                head.bid_no = 0x66;
                // head.DeviceID = ushort.Parse(m_DeviceID);
                head.opcode = OpCode;
                head.Length = (ushort)nSize;

                sendmsg.AddRange(TcpUtil.ObjectToByte(head));

                if (cSendBuf != null)
                {
                    sendmsg.AddRange(cSendBuf);
                }

                TAIL tail = new TAIL();
                //tail.Reserved = new byte[2];
                //Array.Clear(tail.Reserved, 0, 2);
                tail.ETX = 0x03;

                byte bCheckSum = 0x00;
                for (int i = 0; i < sendmsg.Count; i++)
                {
                    bCheckSum ^= sendmsg[i];
                }
                tail.Checksum = bCheckSum;
                sendmsg.AddRange(TcpUtil.ObjectToByte(tail));

                stream.Write(sendmsg.ToArray(), 0, sendmsg.Count);
                MakeLog(string.Format("[SendData] [Send0x{0:x2}] {1}", OpCode, TcpUtil.ToHexString(sendmsg.ToArray())), 0);
                //MakeLog(string.Format("[SendData] - {0}", sendmsg.Count), 1);
            }
            catch (Exception ex)
            {
                TcpUtil.ActionException(ex);
            }
            return(true);
        }