build packet header, (header + payload)

 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
    //                            };

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值