Beispiel #1
0
        public static IModbusCommand Parse(byte[] request, int offset)
        {
            var slave   = request[offset + 0];
            var code    = request[offset + 1];
            var address = ModbusHelper.GetUShort(request, offset + 2);

            switch (code)
            {
            case 1:
                return(Parse01(slave, code, address, request, offset));

            case 2:
                return(Parse02(slave, code, address, request, offset));

            case 3:
                return(Parse03(slave, code, address, request, offset));

            case 4:
                return(Parse04(slave, code, address, request, offset));

            case 5:
                return(Parse05(slave, code, address, request, offset));

            case 6:
                return(Parse06(slave, code, address, request, offset));

            case 15:
                return(Parse15(slave, code, address, request, offset));

            case 16:
                return(Parse16(slave, code, address, request, offset));
            }
            throw Tools.Make("Unsupported function code {0}", code);
        }
Beispiel #2
0
 public object ParseResponse(byte[] response, int offset)
 {
     Assert.Equal(ModbusHelper.GetUShort(response, offset + 0), transactionId, "TransactionId mismatch {0} {1}");
     Assert.Equal(ModbusHelper.GetUShort(response, offset + 2), 0, "Zero mismatch {0} {1}");
     Assert.Equal(ModbusHelper.GetUShort(response, offset + 4), wrapped.ResponseLength, "Length mismatch {0} {1}");
     return(wrapped.ParseResponse(response, offset + 6));
 }
        public void FillRequest(byte[] request, int offset)
        {
            wrapped.FillRequest(request, offset);
            var crc = ModbusHelper.CRC16(request, offset, wrapped.RequestLength);

            request[offset + wrapped.RequestLength + 0] = ModbusHelper.Low(crc);
            request[offset + wrapped.RequestLength + 1] = ModbusHelper.High(crc);
        }
        public void FillResponse(byte[] response, int offset, object value)
        {
            wrapped.FillResponse(response, offset, value);
            var crc = ModbusHelper.CRC16(response, offset, wrapped.ResponseLength);

            response[offset + wrapped.ResponseLength + 0] = ModbusHelper.Low(crc);
            response[offset + wrapped.ResponseLength + 1] = ModbusHelper.High(crc);
        }
 public object ParseResponse(byte[] response, int offset)
 {
     Tools.AssertEqual(response[offset + 0], slave, "Slave mismatch got {0} expected {1}");
     Tools.AssertEqual(response[offset + 1], 16, "Function mismatch got {0} expected {1}");
     Tools.AssertEqual(ModbusHelper.GetUShort(response, offset + 2), address, "Address mismatch got {0} expected {1}");
     Tools.AssertEqual(ModbusHelper.GetUShort(response, offset + 4), values.Length, "Register count mismatch got {0} expected {1}");
     return(null);
 }
        public void FillRequest(byte[] request, int offset)
        {
            FillResponse(request, offset, null);
            var bytes = ModbusHelper.EncodeWords(values);

            request[offset + 6] = (byte)bytes.Length;
            ModbusHelper.Copy(bytes, 0, request, offset + 7, bytes.Length);
        }
Beispiel #7
0
 public object ParseResponse(byte[] response, int offset)
 {
     Assert.Equal(response[offset + 0], slave, "Slave mismatch {0} expected:{1}");
     Assert.Equal(response[offset + 1], 15, "Function mismatch {0} expected:{1}");
     Assert.Equal(ModbusHelper.GetUShort(response, offset + 2), address, "Address mismatch {0} expected:{1}");
     Assert.Equal(ModbusHelper.GetUShort(response, offset + 4), values.Length, "Coil count mismatch {0} expected:{1}");
     return(null);
 }
 public object ParseResponse(byte[] response, int offset)
 {
     Assert.Equal(response[offset + 0], slave, "Slave mismatch got {0} expected {1}");
     Assert.Equal(response[offset + 1], 6, "Function mismatch got {0} expected {1}");
     Assert.Equal(ModbusHelper.GetUShort(response, offset + 2), address, "Address mismatch got {0} expected {1}");
     Assert.Equal(ModbusHelper.GetUShort(response, offset + 4), value, "Value mismatch got {0} expected {1}");
     return(null);
 }
Beispiel #9
0
        private static IModbusCommand Parse05(byte slave, byte code, ushort address, byte[] request, int offset)
        {
            var value = ModbusHelper.DecodeBool(request[offset + 4]);
            var zero  = request[offset + 5];

            Tools.AssertEqual(zero, 0, "Zero mismatch got {0} expected {1}");
            return(new ModbusF05WriteCoil(slave, address, value));
        }
Beispiel #10
0
 public object ParseResponse(byte[] response, int offset)
 {
     Assert.Equal(response[offset + 0], slave, "Slave mismatch {0} expected:{1}");
     Assert.Equal(response[offset + 1], 5, "Function mismatch {0} expected:{1}");
     Assert.Equal(ModbusHelper.GetUShort(response, offset + 2), address, "Address mismatch {0} expected:{1}");
     Assert.Equal(response[offset + 4], ModbusHelper.EncodeBool(value), "Value mismatch {0} expected:{1}");
     Assert.Equal(response[offset + 5], 0, "Pad mismatch {0} expected:{1}");
     return(null);
 }
Beispiel #11
0
 public void FillRequest(byte[] request, int offset)
 {
     request[offset + 0] = slave;
     request[offset + 1] = 5;
     request[offset + 2] = ModbusHelper.High(address);
     request[offset + 3] = ModbusHelper.Low(address);
     request[offset + 4] = ModbusHelper.EncodeBool(value);
     request[offset + 5] = 0;
 }
 public void FillResponse(byte[] response, int offset, object value)
 {
     response[offset + 0] = slave;
     response[offset + 1] = 16;
     response[offset + 2] = ModbusHelper.High(address);
     response[offset + 3] = ModbusHelper.Low(address);
     response[offset + 4] = ModbusHelper.High(values.Length);
     response[offset + 5] = ModbusHelper.Low(values.Length);
 }
        public IModbusWrapper Parse(byte[] request, int offset)
        {
            var wrapped = ModbusParser.Parse(request, offset + 6);

            Assert.Equal(wrapped.RequestLength, ModbusHelper.GetUShort(request, offset + 4), "RequestLength mismatch {0} expected:{1}");
            var transaction = ModbusHelper.GetUShort(request, offset);

            return(new ModbusTCPWrapper(wrapped, transaction));
        }
        public IModbusWrapper Parse(byte[] request, int offset)
        {
            var wrapped = ModbusParser.Parse(request, offset);
            var crc     = ModbusHelper.CRC16(request, offset, wrapped.RequestLength);

            Assert.Equal(crc, ModbusHelper.GetUShortLittleEndian(request, offset + wrapped.RequestLength),
                         "CRC mismatch {0:X4} expected {1:X4}");
            return(new ModbusRTUWrapper(wrapped));
        }
Beispiel #15
0
        private static IModbusCommand Parse15(byte slave, byte code, ushort address, byte[] request, int offset)
        {
            var count  = ModbusHelper.GetUShort(request, offset + 4);
            var values = ModbusHelper.DecodeBools(request, offset + 7, count);
            var bytes  = request[offset + 6];

            Assert.Equal(ModbusHelper.BytesForBools(count), bytes, "Byte count mismatch {0} expected:{1}");
            return(new ModbusF15WriteCoils(slave, address, values));
        }
        public object ParseResponse(byte[] response, int offset)
        {
            var bytes = ModbusHelper.BytesForWords(count);

            Tools.AssertEqual(response[offset + 0], slave, "Slave mismatch got {0} expected {1}");
            Tools.AssertEqual(response[offset + 1], 4, "Function mismatch got {0} expected {1}");
            Tools.AssertEqual(response[offset + 2], bytes, "Bytes mismatch got {0} expected {1}");
            return(ModbusHelper.DecodeWords(response, offset + 3, count));
        }
Beispiel #17
0
 public void FillRequest(byte[] request, int offset)
 {
     request[offset + 0] = slave;
     request[offset + 1] = 1;
     request[offset + 2] = ModbusHelper.High(address);
     request[offset + 3] = ModbusHelper.Low(address);
     request[offset + 4] = ModbusHelper.High(count);
     request[offset + 5] = ModbusHelper.Low(count);
 }
Beispiel #18
0
        public object ParseResponse(byte[] response, int offset)
        {
            var bytes = ModbusHelper.BytesForBools(count);

            Assert.Equal(response[offset + 0], slave, "Slave mismatch {0} expected:{1}");
            Assert.Equal(response[offset + 1], 1, "Function mismatch {0} expected:{1}");
            Assert.Equal(response[offset + 2], bytes, "Bytes mismatch {0} expected:{1}");
            return(ModbusHelper.DecodeBools(response, offset + 3, count));
        }
Beispiel #19
0
        private static IModbusCommand Parse16(byte slave, byte code, ushort address, byte[] request, int offset)
        {
            var count  = ModbusHelper.GetUShort(request, offset + 4);
            var values = ModbusHelper.DecodeWords(request, offset + 7, count);
            var bytes  = request[offset + 6];

            Tools.AssertEqual(ModbusHelper.BytesForWords(count), bytes, "Byte count mismatch got {0} expected {1}");
            return(new ModbusF16WriteRegisters(slave, address, values));
        }
Beispiel #20
0
        public object ParseResponse(byte[] response, int offset)
        {
            var crc1 = ModbusHelper.CRC16(response, offset, wrapped.ResponseLength);
            //crc is little endian page 13 http://modbus.org/docs/Modbus_over_serial_line_V1_02.pdf
            var crc2 = ModbusHelper.GetUShortLittleEndian(response, offset + wrapped.ResponseLength);

            Tools.AssertEqual(crc2, crc1, "CRC mismatch got {0:X4} expected {1:X4}");
            return(wrapped.ParseResponse(response, offset));
        }
Beispiel #21
0
 public void FillResponse(byte[] response, int offset, object value)
 {
     response[offset + 0] = ModbusHelper.High(transactionId);
     response[offset + 1] = ModbusHelper.Low(transactionId);
     response[offset + 2] = 0;
     response[offset + 3] = 0;
     response[offset + 4] = ModbusHelper.High(wrapped.ResponseLength);
     response[offset + 5] = ModbusHelper.Low(wrapped.ResponseLength);
     wrapped.FillResponse(response, offset + 6, value);
 }
Beispiel #22
0
 public void FillRequest(byte[] request, int offset)
 {
     request[offset + 0] = ModbusHelper.High(transactionId);
     request[offset + 1] = ModbusHelper.Low(transactionId);
     request[offset + 2] = 0;
     request[offset + 3] = 0;
     request[offset + 4] = ModbusHelper.High(wrapped.RequestLength);
     request[offset + 5] = ModbusHelper.Low(wrapped.RequestLength);
     wrapped.FillRequest(request, offset + 6);
 }
Beispiel #23
0
        public void FillResponse(byte[] response, int offset, object value)
        {
            var bytes = ModbusHelper.BytesForBools(count);

            response[offset + 0] = slave;
            response[offset + 1] = 1;
            response[offset + 2] = bytes;
            var data = ModbusHelper.EncodeBools((bool[])value);

            ModbusHelper.Copy(data, 0, response, offset + 3, bytes);
        }
Beispiel #24
0
        public void FillResponse(byte[] response, int offset, object value)
        {
            var bytes = ModbusHelper.BytesForWords(count);

            response[offset + 0] = slave;
            response[offset + 1] = 4;
            response[offset + 2] = bytes;
            var data = ModbusHelper.EncodeWords(value as ushort[]);

            ModbusHelper.Copy(data, 0, response, offset + 3, bytes);
        }
Beispiel #25
0
        public byte[] GetException(byte code)
        {
            var exception = new byte[5];

            exception[0] = wrapped.Slave;
            exception[1] = (byte)(wrapped.Code | 0x80);
            exception[2] = code;
            var crc = ModbusHelper.CRC16(exception, 0, 3);

            exception[3] = ModbusHelper.Low(crc);
            exception[4] = ModbusHelper.High(crc);
            return(exception);
        }
 public IModbusWrapper Scan()
 {
     if (buffer.Count >= 6)
     {
         var length = ModbusHelper.GetUShort(buffer[4], buffer[5]);
         if (buffer.Count >= 6 + length)
         {
             var request = buffer.GetRange(0, 6 + length).ToArray();
             buffer.RemoveRange(0, 6 + length);
             return(protocol.Parse(request, 0));
         }
     }
     return(null);
 }
Beispiel #27
0
        public byte[] GetException(byte code)
        {
            var exception = new byte[9];

            exception[0]     = ModbusHelper.High(transactionId);
            exception[1]     = ModbusHelper.Low(transactionId);
            exception[2]     = 0;
            exception[3]     = 0;
            exception[4]     = ModbusHelper.High(3);
            exception[5]     = ModbusHelper.Low(3);
            exception[6 + 0] = wrapped.Slave;
            exception[6 + 1] = (byte)(wrapped.Code | 0x80);
            exception[6 + 2] = code;
            return(exception);
        }
Beispiel #28
0
        public void CheckException(byte[] response, int count)
        {
            if (count < 5)
            {
                Tools.Throw("Partial exception packet got {0} expected >= {1}", count, 5);
            }
            var offset = 0;
            var code   = response[offset + 1];

            if ((code & 0x80) != 0)
            {
                Tools.AssertEqual(response[offset + 0], wrapped.Slave, "Slave mismatch got {0} expected {1}");
                Tools.AssertEqual(code & 0x7F, wrapped.Code, "Code mismatch got {0} expected {1}");
                var crc1 = ModbusHelper.CRC16(response, offset, 3);
                //crc is little endian page 13 http://modbus.org/docs/Modbus_over_serial_line_V1_02.pdf
                var crc2 = ModbusHelper.GetUShortLittleEndian(response, offset + 3);
                Tools.AssertEqual(crc2, crc1, "CRC mismatch got {0:X4} expected {1:X4}");
                throw new ModbusException(response[offset + 2]);
            }
        }
Beispiel #29
0
        private static IModbusCommand Parse04(byte slave, byte code, ushort address, byte[] request, int offset)
        {
            var count = ModbusHelper.GetUShort(request, offset + 4);

            return(new ModbusF04ReadInputRegisters(slave, address, count));
        }
Beispiel #30
0
        private static IModbusCommand Parse06(byte slave, byte code, ushort address, byte[] request, int offset)
        {
            var value = ModbusHelper.GetUShort(request, offset + 4);

            return(new ModbusF06WriteRegister(slave, address, value));
        }