示例#1
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);
        }