Ejemplo n.º 1
0
        private byte[] GetResponseData(byte[] data, byte cmd)
        {
            if (data == null || data.Length < 3)
            {
                return(null);
            }
            var na = GetNaPacket();

            if (na[0] != data[0] || na[1] != data[1] || na[2] != data[2] || na[3] != data[3] || cmd != data[4])
            {
                return(null);
            }

            var crc   = new Crc16Modbus();
            var crc16 = crc.Calculate(data, 0, data.Length - 2);

            if (crc16.CrcData.Length != 2)
            {
                return(null);
            }
            if (crc16.CrcData[0] != data[data.Length - 2] || crc16.CrcData[1] != data[data.Length - 1])
            {
                return(null);
            }

            return(data.Take(data.Length - 2).Skip(5).ToArray());
        }
Ejemplo n.º 2
0
        private byte[] CreatePacket(byte cmd, byte[] data = null)
        {
            var result = new List <byte>();

            result.AddRange(GetNaPacket());
            result.Add(cmd);
            if (data != null)
            {
                result.AddRange(data);
            }
            var crc = new Crc16Modbus();

            result.AddRange(crc.Calculate(result.ToArray(), 0, result.Count).CrcData);

            return(result.ToArray());
        }
Ejemplo n.º 3
0
        private byte[] MakePackage(byte cmd1, byte cmd2, byte index, byte[] data = null)
        {
            var result = new List <byte>();

            result.AddRange(GetNaPacket());
            result.Add(cmd1);
            if (cmd2 != 0xff)
            {
                result.Add(cmd2);
            }
            if (index != 0xff)
            {
                result.Add(index);
            }
            if (data != null)
            {
                result.AddRange(data);
            }
            var crc = new Crc16Modbus();

            result.AddRange(crc.Calculate(result.ToArray(), 0, result.Count).CrcData);

            return(result.ToArray());
        }