Modbus TCP从站Null异常,对象引用未设置为对象的实例

时间:2018-10-03 05:53:38

标签: c# modbus-tcp

我正在开发Windows窗体,以用作Modbus tcp从站Simuator。我试图接收来自modbus主站的多个线圈请求。我能够正确接收该请求,但是当我尝试执行写操作时, 引发"object reference is not set to an instance of an object"异常。这是说slave为null,但是我已经创建了slave实例并对其进行了初始化。我正在使用NModbus库。这是我的代码

        private void Button1_Click(object sender, EventArgs e)
        {

              try
              {

                byte slaveAddress = Convert.ToByte(textBox3.Text);

                IPAddress address = IPAddress.Parse(textBox1.Text);
                int port = Convert.ToInt32(textBox2.Text);

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


                ModbusSlave slave = ModbusTcpSlave.CreateTcp(slaveAddress, slaveTcpListener);


                slave.Listen();
                slave.DataStore = DataStoreFactory.CreateDefaultDataStore();
                DataStore dataStore = slave.DataStore;


                slave.ModbusSlaveRequestReceived += new EventHandler<ModbusSlaveRequestEventArgs>(Modbus_Request_Event);

                slave.DataStore.DataStoreWrittenTo += new EventHandler<DataStoreEventArgs>(DataStoreWrittenTo);



                // prevent the main thread from exiting
                listBox1.Items.Clear();

                Thread.Sleep(Timeout.Infinite);
                //slaveTcpListener.Stop();


              }
              catch
              {
                MessageBox.Show("Error in connection");
              }
        }


 private void DataStoreWrittenTo(object sender, DataStoreEventArgs e)
        {

            switch (e.ModbusDataType)
            {

                case ModbusDataType.HoldingRegister:


                    for (int i = 1; i < e.Data.B.Count; i++)
                    {

               //Line A         slave.DataStore.HoldingRegisters[e.StartAddress + i + 1] = e.Data.B[i];
                    }

                    break;
                case ModbusDataType.Coil:

                    for (int i = 1; i < e.Data.A.Count; i++)
                    {
 //Line B                       slave.DataStore.CoilDiscretes[e.StartAddress + i + 1] = e.Data.A[i];
                    }
                    break;
                    //case Modbus.


            }


        }   

如果我尝试写入多个保持寄存器,则在A行出现异常;如果我尝试写入多个线圈,则在B行出现异常。 谁能帮我解决这个问题?

1 个答案:

答案 0 :(得分:1)

您似乎没有正确实例化<script src="https://code.jquery.com/jquery-1.12.4.js"></script> <script src="https://cdn.datatables.net/1.10.13/js/jquery.dataTables.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore.js"></script> <link href="https://cdn.datatables.net/1.10.13/css/jquery.dataTables.min.css" type="text/css" rel="stylesheet" /> <table id="table1"> <thead> <tr> <th>Fruit</th> <th>sumCondition</th> <th># Eaten</th> <th># Remaining</th> </tr> </thead> <tfoot> <tr> <th></th> <th align="center">Count</th> <th align="left"></th> <th align="left"></th> </tr> </tfoot> <tbody> <tr> <td>Apples</td> <td>Use</td> <td>6</td> <td>5</td> </tr> <tr> <td>Oranges</td> <td>Card</td> <td>6</td> <td>5</td> </tr> <tr> <td>Oranges</td> <td>Other</td> <td>6</td> <td>5</td> </tr> <tr> <td>Oranges</td> <td>Rahul</td> <td>2</td> <td>9</td> </tr> <tr> <td>Mango</td> <td>Use</td> <td>6</td> <td>5</td> </tr> <tr> <td>Bananas</td> <td>Rahul</td> <td>2</td> <td>9</td> </tr> </tbody> </table>。您正在创建范围为slave的局部变量slave

Button1_Click

您正在访问ModbusSlave slave = ModbusTcpSlave.CreateTcp(slaveAddress, slaveTcpListener); 中的slave,它应该具有类范围。

您应将上述实例更改为:

DataStoreWrittenTo