build packet header, (header + payload)

来源:互联网 发布:centos 6.7 修改ip 编辑:程序博客网 时间:2024/04/29 14:52

 public class Header
    {
        public static byte command = 0x03;
        public static byte[] uName = new byte[] { 0x20, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x01, 0x01, 0x00 };
        public static byte[] password = new byte[] { 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x00 };
        public static byte desc;
        public static byte param = 0x00;
        public static byte[] seq = new byte[2];


        // desc = 0x01; // change relay
        // desc = 0x04; // query output

        public static byte[] getChangeRelayHeader(byte[] _seq)
        {
            desc = 0x01; // change relay status

            _seq[0] += Convert.ToByte(1);

            seq[0] = _seq[0];
            seq[1] = _seq[1];

            List<byte> byteList = new List<byte>();
            byteList.Add(command);
            byteList.AddRange(uName);
            byteList.AddRange(password);
            byteList.Add(desc);
            byteList.Add(param);
            byteList.AddRange(seq);

            return byteList.ToArray();
        }

        public static byte[] getQueryRelayHeader(byte[] _seq)
        {
            desc = 0x04; // query relay output status

            _seq[0] += Convert.ToByte(1);

            seq[0] = _seq[0];
            seq[1] = _seq[1];

            List<byte> byteList = new List<byte>();
            byteList.Add(command);
            byteList.AddRange(uName);
            byteList.AddRange(password);
            byteList.Add(desc);
            byteList.Add(param);
            byteList.AddRange(seq);

            return byteList.ToArray();
        }
    }

}

 

// concat header and payload then send
/////////////////////////////////

 byte[] headerBytes = Header.getChangeRelayHeader(seq);

 byte[] payloadByte = Payload.getControlPayload(_channel, onOff);   // channel, status
               
 IEnumerable<byte> bytesSent = headerBytes.Concat(payloadByte);   // concat header and payload
 byte[] bs = bytesSent.ToArray();

socket.Send(bs, 0, 49, SocketFlags.None);

/////////////////////////////////

 


    //byte[] header = {   0x03,       // command

    //                                0x20, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,   //user U 55        len = 23
    //                                0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x01, 0x01, 0x00,        
                               
    //                                0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50,  // passwd P 50       len = 19
    //                                0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x00,
                               
    //                                0x01,            // desc  0x01
    //                                0x00,            // param
    //                                0x00, 0x00      // seq
    //                            };

原创粉丝点击