Как изменить значение хранилища данных Modbus при нажатии на ячейку DataGridView - PullRequest
0 голосов
/ 08 октября 2018

Я пытаюсь реализовать приложение Modbus Ведомый симулятор.
Когда я нажимаю на ячейку DataGridView, данные меняются на единицу для Coils, и это должно быть отражено в Modbus DataStore * 1006.*, где я ссылаюсь NModbus Библиотека.

Мой код для отражения изменений в DataStore и код для создания dataStore при щелчке на соединение приведены ниже.
код, который я написал в методе DataStoreUpdate, изменяет значение в DataStore, но оно локально для этой перикулярной функции.
Когда я запускаю с Master изменения не отражают i, e, когда Master читаетCoils из dataStore.

Как обновить значение dataStore при нажатии на ячейку DataGridView?

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
    if (dataGridView1.CurrentCell.ColumnIndex != 0 && dataGridView1.CurrentCell.RowIndex != -1)
    {
        dataGridView1[dataGridView1.CurrentCell.ColumnIndex, dataGridView1.CurrentCell.RowIndex].Value = 1;

        int index = dataGridView1.CurrentCell.RowIndex;
        //calls the method to update the request.
        DataStoreUpdate(index);
    }
}

//update method for reflecting to dataStore when clicked on the dataGridView cell
private void DataStoreUpdate(int index)
{
    IPAddress address = IPAddress.Parse(textBox1.Text);
    int port = Convert.ToInt32(textBox2.Text);

    TcpListener slaveTcpListener = new TcpListener(address, port);

    slaveTcpListener = new TcpListener(address, port);
    slave.DataStore.CoilDiscretes[index] = true;
    slaveTcpListener.Stop();
}

//Following is the code for creating Modbus TcpSlave and creating a datastore for different registers.
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();

        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);
    }
    catch
    {
        MessageBox.Show("Error in connection");
    }
}
...