Exemple #1
0
 void CheckCode(byte code)
 {
     if (!"01,02,03,04,05,06,15,16".Contains(code.ToString("00")))
     {
         Thrower.Throw("Unsupported code {0}", code);
     }
 }
Exemple #2
0
 public static void Equal(int a, int b, string format)
 {
     if (a != b)
     {
         Thrower.Throw(format, a, b);
     }
 }
Exemple #3
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));
            }
            Thrower.Throw("Unsupported function code {0}", code);
            return(null);
        }
Exemple #4
0
        private void CheckCode(byte code)
        {
            var valid = "01,02,03,04,05,06,15,16".Contains(code.ToString("00"));

            if (!valid)
            {
                Thrower.Throw("Unsupported code {0}", code);
            }
        }
Exemple #5
0
        public static TcpClient ConnectWithTimeout(string host, int port, int timeout)
        {
            var socket = new TcpClient();
            var result = socket.BeginConnect(host, port, null, null);

            if (!result.AsyncWaitHandle.WaitOne(timeout, true))
            {
                Thrower.Throw("Timeout connecting to {0}:{1}", host, port);
            }
            socket.EndConnect(result);
            return(socket);
        }
        public void CheckException(byte[] response, int count)
        {
            if (count < 9)
            {
                Thrower.Throw("Partial exception packet got {0} expected >= {1}", count, 9);
            }
            var offset = 6;
            var code   = response[offset + 1];

            if ((code & 0x80) != 0)
            {
                Assert.Equal(response[offset + 0], wrapped.Slave, "Slave mismatch got {0} expected {1}");
                Assert.Equal(code & 0x7F, wrapped.Code, "Code mismatch got {0} expected {1}");
                throw new ModbusException(response[offset + 2]);
            }
        }
        public void CheckException(byte[] response, int count)
        {
            if (count < 5)
            {
                Thrower.Throw("Partial exception packet got {0} expected >= {1}", count, 5);
            }
            var offset = 0;
            var code   = response[offset + 1];

            if ((code & 0x80) != 0)
            {
                Assert.Equal(response[offset + 0], wrapped.Slave, "Slave mismatch got {0} expected {1}");
                Assert.Equal(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);
                Assert.Equal(crc2, crc1, "CRC mismatch got {0:X4} expected {1:X4}");
                throw new ModbusException(response[offset + 2]);
            }
        }