Exemplo n.º 1
0
        public void ModbusUdpSlave_NotBound()
        {
            UdpClient   client = new UdpClient();
            ModbusSlave slave  = ModbusUdpSlave.CreateUdp(1, client);

            Assert.Throws <InvalidOperationException>(() => slave.Listen());
        }
Exemplo n.º 2
0
        public void ModbusUdpSlave_NotBound()
        {
            UdpClient   client = new UdpClient();
            ModbusSlave slave  = ModbusUdpSlave.CreateUdp(1, client);

            slave.Listen();
        }
Exemplo n.º 3
0
    public void ModbusUdpSlave_NotBound()
    {
        UdpClient   client = new();
        ModbusSlave slave  = ModbusUdpSlave.CreateUdp(1, client);

        Assert.ThrowsAsync <InvalidOperationException>(async() => await slave.ListenAsync());
    }
Exemplo n.º 4
0
 public void ModbusUdpSlave_StartAndStopSlaveBeforeAnyRequests()
 {
     using (UdpClient client = new UdpClient(ModbusMasterFixture.Port))
     {
         ModbusSlave slave = ModbusUdpSlave.CreateUdp(1, client);
         slave.Listen();
     }
 }
Exemplo n.º 5
0
        public static ModbusSlave CreateUdpSlave(byte id, UdpClient adapter)
        {
            ModbusSlave slave = null;

            if (adapter != null)
            {
                slave = ModbusUdpSlave.CreateUdp(id, adapter);
            }
            return(slave);
        }
        public NModbusUdpMasterNModbusUdpSlaveFixture()
        {
            SlaveUdp = new UdpClient(Port);
            Slave    = ModbusUdpSlave.CreateUdp(SlaveUdp);
            StartSlave();

            MasterUdp = new UdpClient();
            MasterUdp.Connect(DefaultModbusIPEndPoint);
            Master = ModbusIpMaster.CreateIp(MasterUdp);
        }
Exemplo n.º 7
0
        private UdpClient CreateAndStartUdpSlave(int port, DataStore dataStore)
        {
            UdpClient   slaveClient = new UdpClient(port);
            ModbusSlave slave       = ModbusUdpSlave.CreateUdp(slaveClient);

            slave.DataStore = dataStore;
            Thread slaveThread = new Thread(slave.Listen);

            slaveThread.Start();

            return(slaveClient);
        }
        public override void Init()
        {
            base.Init();

            SlaveUdp = new UdpClient(Port);
            Slave    = ModbusUdpSlave.CreateUdp(SlaveUdp);
            StartSlave();

            MasterUdp = new UdpClient();
            MasterUdp.Connect(DefaultModbusIPEndPoint);
            Master = ModbusIpMaster.CreateIp(MasterUdp);
        }
Exemplo n.º 9
0
        /// <summary>
        ///     Simple Modbus UDP slave example.
        /// </summary>
        public void StartModbusUdpSlave()
        {
            using (UdpClient client = new UdpClient(502))

            {
                ModbusUdpSlave slave = ModbusUdpSlave.CreateUdp(client);

                slave.DataStore = DataStoreFactory.CreateDefaultDataStore();

                slave.ListenAsync().GetAwaiter().GetResult();
            }
        }
Exemplo n.º 10
0
        /// <summary>
        /// Simple Modbus UDP slave example.
        /// </summary>
        public static void StartModbusUdpSlave()
        {
            using (UdpClient client = new UdpClient(502))
            {
                ModbusUdpSlave slave = ModbusUdpSlave.CreateUdp(client);
                slave.DataStore = DataStoreFactory.CreateDefaultDataStore();

                slave.Listen();

                // prevent the main thread from exiting
                Thread.Sleep(Timeout.Infinite);
            }
        }
Exemplo n.º 11
0
    public static void Udp()
    {
        UdpClient?slaveClient = new(502);

        using ModbusUdpSlave? slave = ModbusUdpSlave.CreateUdp(slaveClient);
        StartSlave(slave);

        UdpClient? masterClient = new();
        IPEndPoint endPoint     = new(new IPAddress(new byte[] { 127, 0, 0, 1 }), 502);

        masterClient.Connect(endPoint);

        using ModbusIpMaster? master = ModbusIpMaster.CreateIp(masterClient);
        ReadRegisters(master);
    }
Exemplo n.º 12
0
        public static void Udp()
        {
            var slaveClient = new UdpClient(502);

            using (var slave = ModbusUdpSlave.CreateUdp(slaveClient))
            {
                StartSlave(slave);

                var        masterClient = new UdpClient();
                IPEndPoint endPoint     = new IPEndPoint(new IPAddress(new byte[] { 127, 0, 0, 1 }), 502);
                masterClient.Connect(endPoint);

                using (var master = ModbusIpMaster.CreateIp(masterClient))
                    ReadRegisters(master);
            }
        }
Exemplo n.º 13
0
 //模拟器初始化
 public override void Simulator(int times, bool IsSystem)
 {
     SimulatorClose();
     _simularorClient     = new UdpClient(new IPEndPoint(IPAddress.Parse(Udp_PARA.SimulatorTCP_IP), int.Parse(Udp_PARA.SimulatorTCP_Port)));
     SimulatorUpdateCycle = times;
     for (int i = 0; i < this.IODevices.Count; i++)
     {
         try
         {
             ModbusSlave DeviceSlave = ModbusUdpSlave.CreateUdp(byte.Parse(this.IODevices[i].IO_DEVICE_ADDRESS), _simularorClient);
             slaves.Add(DeviceSlave);
         }
         catch (Exception emx)
         {
             this.SimulatorAppendLog("创建设备模拟器失败 " + this.IODevices[i].IO_DEVICE_NAME + " " + emx.Message);
         }
     }
 }
Exemplo n.º 14
0
    public void ModbusUdpSlave_EnsureTheSlaveShutsDownCleanly()
    {
        UdpClient client = new(ModbusMasterFixture.Port);

        using ModbusUdpSlave? slave = ModbusUdpSlave.CreateUdp(1, client);
        AutoResetEvent?handle = new(false);

        Thread?backgroundThread = new(async(state) =>
        {
            handle.Set();
            await slave.ListenAsync();
        });

        backgroundThread.IsBackground = true;
        backgroundThread.Start();

        handle.WaitOne();
        Thread.Sleep(100);
    }
Exemplo n.º 15
0
        public void ModbusUdpSlave_EnsureTheSlaveShutsDownCleanly()
        {
            UdpClient client = new UdpClient(ModbusMasterFixture.Port);

            using (var slave = ModbusUdpSlave.CreateUdp(1, client))
            {
                var handle = new AutoResetEvent(false);

                var backgroundThread = new Thread(state =>
                {
                    handle.Set();
                    slave.Listen();
                });

                backgroundThread.IsBackground = true;
                backgroundThread.Start();

                handle.WaitOne();
                Thread.Sleep(100);
            }
        }
Exemplo n.º 16
0
        private void ButtonOpenTCPUDP_Click(object sender, EventArgs e)
        {
            int         tcpPort = int.Parse(tbPort.Text);
            IPAddress   ipAddr  = new IPAddress(0);
            IPHostEntry IPHE    = null;

            try
            {
                if (IPAddress.TryParse(tbIP.Text, out ipAddr))
                {
                    if (cbCommMode.SelectedIndex == 1)                     //TCP
                    {
                        ListenerTCP = new TcpListener(ipAddr, tcpPort);
                    }
                    else if (cbCommMode.SelectedIndex == 2)                     //UDP
                    {
                        ClientUDP = new UdpClient(new IPEndPoint(ipAddr, tcpPort));
                    }
                }
                else
                {
                    IPHE = Dns.GetHostEntry(tbIP.Text);

                    if (cbCommMode.SelectedIndex == 1)                     //TCP
                    {
                        ListenerTCP = new TcpListener(IPHE.AddressList[1], tcpPort);
                    }
                    else if (cbCommMode.SelectedIndex == 2)                     //UDP
                    {
                        ClientUDP = new UdpClient(new IPEndPoint(IPHE.AddressList[1], tcpPort));
                    }
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Invalid IP address or hostname!");
                return;
            }

            if (cbCommMode.SelectedIndex == 1)             //TCP
            {
                slave = ModbusTcpSlave.CreateTcp(unitID, ListenerTCP);
            }
            else if (cbCommMode.SelectedIndex == 2)             //UDP
            {
                slave = ModbusUdpSlave.CreateUdp(unitID, ClientUDP);
            }

            slave.ModbusSlaveRequestReceived += MasterRequests;
            slave.DataStore = dataStore;

            btnOpenTCPUDP.Enabled   = false;
            btnOpenTCPUDP.BackColor = Color.Gainsboro;

            btnCloseTCPUDP.Enabled   = true;
            btnCloseTCPUDP.BackColor = Color.LightSteelBlue;

            tbIP.Enabled       = false;
            tbPort.Enabled     = false;
            cbCommMode.Enabled = false;
            cbRowCount.Enabled = false;

            bckgndThread = new Thread(BckgndThreadTask)
            {
                IsBackground = true
            };
            bckgndThread.Start();
        }