Ejemplo n.º 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);
        }
Ejemplo n.º 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));
 }
Ejemplo n.º 3
0
 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);
 }
Ejemplo n.º 4
0
 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);
 }
Ejemplo n.º 5
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);
 }
Ejemplo n.º 6
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));
        }
Ejemplo n.º 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], 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);
 }
Ejemplo n.º 8
0
        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));
        }
Ejemplo n.º 9
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));
        }
Ejemplo n.º 10
0
 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);
 }
Ejemplo n.º 11
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));
        }
Ejemplo n.º 12
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));
        }