Beispiel #1
0
        public void TcpOverSocketTest()
        {
            var model   = new ModbusModel();
            var scanner = new ModbusTCPScanner();

            using (var server = new TcpServerModel(model, scanner))
                using (var master = ModbusMaster.TCP(IPAddress.Loopback.ToString(), server.Port))
                {
                    //race condition avoided by access order
                    H.SharedSlaveTest(model, master);
                }
        }
Beispiel #2
0
        public void TcpExceptionOverSocketTest()
        {
            var model   = new ModbusModel();
            var scanner = new ModbusTCPScanner();

            using (var server = new TcpServerModel(model, scanner))
                using (var master = ModbusMaster.TCP(IPAddress.Loopback.ToString(), server.Port))
                {
                    var ex = Assert.Throws <ModbusException>(() => H.SharedExceptionTest(master));
                    Assert.AreEqual("Modbus exception 2", ex.Message);
                    Assert.AreEqual(2, ex.Code);
                }
        }
Beispiel #3
0
        public void TcpOverIsolatedTest()
        {
            var model   = new ModbusModel();
            var scanner = new ModbusTCPScanner();

            using (var server = new SerialModel(SlaveCOM, model, scanner))
                using (var stream = new ModbusIsolatedStream(new SerialSettings(MasterCOM), 400))
                {
                    var master = new ModbusMaster(stream, new ModbusTCPProtocol());
                    //race condition avoided by access order
                    H.SharedSlaveTest(model, master);
                }
        }
Beispiel #4
0
        public void TcpExceptionOverIsolatedTest()
        {
            var model   = new ModbusModel();
            var scanner = new ModbusTCPScanner();

            using (var server = new SerialModel(SlaveCOM, model, scanner))
                using (var stream = new ModbusIsolatedStream(new SerialSettings(MasterCOM), 400))
                {
                    var master = new ModbusMaster(stream, new ModbusTCPProtocol());
                    var ex     = Assert.Throws <ModbusException>(() => H.SharedExceptionTest(master));
                    Assert.AreEqual("Modbus exception 2", ex.Message);
                    Assert.AreEqual(2, ex.Code);
                }
        }
Beispiel #5
0
        public void TcpRetryAfterTimeoutTest()
        {
            var model   = new ModbusModel();
            var scanner = new ModbusTCPScanner();

            using (var server = new TcpServerModel(model, scanner))
                using (var master = ModbusMaster.TCP(IPAddress.Loopback.ToString(), server.Port, 50))
                {
                    server.delay = 100;
                    Assert.Throws(typeof(Exception), () => master.WriteCoil(0, 0, false));
                    //client side delay needed to eat all server side delay otherwise
                    //the added socket stream discard on write will still throw tid mismatch
                    System.Threading.Thread.Sleep(100);
                    server.delay = 0;
                    master.WriteCoil(0, 0, false);
                }
        }
Beispiel #6
0
        public static void Main(string[] args)
        {
            AppDomain.CurrentDomain.UnhandledException += UnhandledExceptionTrapper;

            var cmdline = string.Join(" ", args);

            switch (cmdline)
            {
            case "--list":
                var names = SerialPort.GetPortNames();
                foreach (var name in names)
                {
                    Console.WriteLine(name);
                }
                Console.Out.Flush();
                return;
            }

            var serial = new SerialPort();

            Config.Parse(serial, cmdline);
            serial.Open();

            var thread = new Thread(() => { CheckLoop(serial); })
            {
                IsBackground = true
            };

            thread.Start();

            var stream      = new ModbusSerialStream(serial, 800);
            var tcpScanner  = new ModbusTCPScanner();
            var rtuProtocol = new ModbusRTUProtocol();

            var line = Console.ReadLine();

            while (line != null)
            {
                var bytes = Convert.FromBase64String(line);
                tcpScanner.Append(bytes, 0, bytes.Length);
                var tcpRequestCommand = tcpScanner.Scan();
                while (tcpRequestCommand != null)
                {
                    var rtuRequestCommand = rtuProtocol.Wrap(tcpRequestCommand.Wrapped);
                    var rtuRequestBytes   = new byte[rtuRequestCommand.RequestLength];
                    var rtuResponseBytes  = new byte[rtuRequestCommand.ResponseLength];
                    rtuRequestCommand.FillRequest(rtuRequestBytes, 0);
                    stream.Write(rtuRequestBytes);
                    stream.Read(rtuResponseBytes);
                    var rtuResponse      = rtuRequestCommand.ParseResponse(rtuResponseBytes, 0);
                    var tcpResponseBytes = new byte[tcpRequestCommand.ResponseLength];
                    tcpRequestCommand.FillResponse(tcpResponseBytes, 0, rtuResponse);
                    Console.WriteLine(Convert.ToBase64String(tcpResponseBytes, 0, tcpResponseBytes.Length));
                    Console.Out.Flush();
                    tcpRequestCommand = tcpScanner.Scan();
                }
                line = Console.ReadLine();
            }

            throw new Exception("Stdin closed unexpectedly");
        }