コード例 #1
0
ファイル: NmodbusManager.cs プロジェクト: disklosr/NmodSlave
        public void StartListening()
        {
            slaveTcpListener = new TcpListener(addr, port);
            slaveTcpListener.Start();
            slave = ModbusTcpSlave.CreateTcp(1, slaveTcpListener);

            slave.DataStore = DataStoreFactory.CreateDefaultDataStore();
            slave.DataStore.DataStoreWrittenTo += DataStore_DataStoreWrittenTo;

            slave.Listen();
        }
コード例 #2
0
        private void btnStart_Click(object sender, EventArgs e)
        {
            // create and start the TCP slave
            try
            {
                byte      ip1 = Convert.ToByte(ConfigurationManager.AppSettings["ip1"]);
                byte      ip2 = Convert.ToByte(ConfigurationManager.AppSettings["ip2"]);
                byte      ip3 = Convert.ToByte(ConfigurationManager.AppSettings["ip3"]);
                byte      ip4 = Convert.ToByte(ConfigurationManager.AppSettings["ip4"]);
                byte[]    xip = new byte[] { ip1, ip2, ip3, ip4 };
                IPAddress aaa = new IPAddress(xip);
                //IPHostEntry ipEntry = Dns.GetHostEntry(Dns.GetHostName());
                //IPAddress[] addr = ipEntry.AddressList;
                //slaveTcpListener = new TcpListener(aaa, port);
                //slaveTcpListener.Start();

                IPHostEntry ipEntry = Dns.GetHostEntry(Dns.GetHostName());
                IPAddress[] addr    = ipEntry.AddressList;
                slaveTcpListener = new TcpListener(addr[1], port);
                slaveTcpListener.Start();
                slave = Modbus.Device.ModbusTcpSlave.CreateTcp(slaveID, slaveTcpListener);
                slave.ModbusSlaveRequestReceived += new EventHandler <ModbusSlaveRequestEventArgs>(Modbus_Request_Event);
                slave.DataStore = Modbus.Data.DataStoreFactory.CreateDefaultDataStore();
                slave.DataStore.DataStoreWrittenTo += new EventHandler <DataStoreEventArgs>(Modbus_DataStoreWriteTo);
                slave.Listen();
            }
            catch (Exception ee)
            {
                LB_status.Text = ee.Message;
                label2.Text    = ee.Message;
            }

            try
            {
                slave.DataStore.HoldingRegisters[2001] = 0;
                slave.DataStore.HoldingRegisters[2002] = 0;

                slave.DataStore.HoldingRegisters[2003] = 0;
                slave.DataStore.HoldingRegisters[2004] = 0;
                slave.DataStore.HoldingRegisters[2005] = 0;
                slave.DataStore.HoldingRegisters[2006] = 0;
            }
            catch (Exception ee)
            {
                Console.WriteLine("slave.DataStor" + ee.Message);
                throw;
            }
            timerPD.Enabled  = true;
            timer1.Enabled   = true;
            btnStart.Enabled = false;
            btStop.Enabled   = true;
        }
コード例 #3
0
ファイル: TestCases.cs プロジェクト: KirillDZR/nmodbus
		public static void StartSlave(ModbusSlave slave)
		{
			slave.DataStore = DataStoreFactory.CreateTestDataStore();
			var slaveThread = new Thread(slave.Listen);
			slaveThread.Start();
		}
コード例 #4
0
ファイル: Form1.cs プロジェクト: seaker000/ModbusTerm
        private void button4_Click(object sender, EventArgs e)
        {
            button3.Enabled = true;
            button4.Enabled = false;

            byte slaveID = (byte)numericUpDown3.Value;
            int port = (int)numericUpDown2.Value;
            IPAddress address = IPAddress.Parse(comboBox3.Text); //new IPAddress(new byte[] { 10, 36, 5, 217 });

            // create and start the TCP slave
            slaveTcpListener = new TcpListener(address, port);
            slaveTcpListener.Start();

            slaveTcpM = ModbusTcpSlave.CreateTcp(slaveID, slaveTcpListener);
            slaveTcpM.DataStore = DataStoreFactory.CreateDefaultDataStore();

            slaveTcp = new Thread(slaveTcpM.Listen);
            slaveTcp.IsBackground = true;
            slaveTcp.Start();

            // prevent the main thread from exiting
            //Thread.Sleep(Timeout.Infinite);
        }
コード例 #5
0
 public static Message.IModbusMessage SlaveRequestApply(ModbusSlave slave, Message.IModbusMessage request)
 {
     return(slave.ApplyRequest(request));
 }