private void btnReadDiscreteInputs_Click(object sender, EventArgs e) { try { if (!modbusClient.Connected) { button3_Click(null, null); } bool[] serverResponse = modbusClient.ReadDiscreteInputs(int.Parse(txtStartingAddressInput.Text) - 1, int.Parse(txtNumberOfValuesInput.Text)); lsbAnswerFromServer.Items.Clear(); for (int i = 0; i < serverResponse.Length; i++) { lsbAnswerFromServer.Items.Add(serverResponse[i]); } } catch (Exception exc) { MessageBox.Show(exc.Message, "Exception Reading values from Server", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
public void GetValues(ConnectionProperties connectionProperties) { modbusClient = connectionProperties.modbusClient; if (!modbusClient.Connected) { modbusClient.IPAddress = connectionProperties.ModbusTCPAddress; modbusClient.Port = connectionProperties.Port; modbusClient.Connect(); } foreach (FunctionProperties functionProperty in connectionProperties.FunctionPropertiesList) { switch (functionProperty.FunctionCode) { case FunctionCode.ReadCoils: functionProperty.values = modbusClient.ReadCoils(functionProperty.StartingAdress, functionProperty.Quantity); break; case FunctionCode.ReadDiscreteInputs: functionProperty.values = modbusClient.ReadDiscreteInputs(functionProperty.StartingAdress, functionProperty.Quantity); break; case FunctionCode.ReadHoldingRegisters: functionProperty.values = modbusClient.ReadHoldingRegisters(functionProperty.StartingAdress, functionProperty.Quantity); break; case FunctionCode.ReadInputRegisters: functionProperty.values = modbusClient.ReadInputRegisters(functionProperty.StartingAdress, functionProperty.Quantity); break; default: break; } } if (valuesChanged != null) { valuesChanged(this); } }