TcpClient tcpClient = new TcpClient();
private void btn_OpenPort_Click(object sender, EventArgs e) { try { tcpClient.Connect("192.168.2.35", 502); ModbusIpMaster master = ModbusIpMaster.CreateIp(tcpClient); master.Transport.Retries = 0;
btn_OpenPort.Text = "Close Port"; UpdateRTB(comPort.PortName.ToString() + " opened!"); } catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
// Read private void btn_Read_Click(object sender, EventArgs e) { // Read inputs try { byte slaveID = 1; // PLC address ushort startAddress = 256; // Starting address ushort numOfPoints = 1; // Number of coils to read
bool[] status = ModbusIpMaster.CreateIp(tcpClient).ReadCoils(slaveID, startAddress, numOfPoints); for (int i = 0; i < numOfPoints; i++) { UpdateRTB(startAddress.ToString() + ": " + status[i]); startAddress++; } // Output: <input address>: True/False } catch (Exception ex) { MessageBox.Show(ex.Message, "NModbus Error R1", MessageBoxButtons.OK, MessageBoxIcon.Error); }
}
// Write private void btn_Write_Click(object sender, EventArgs e) { // Write marker M try { byte slaveID = 1; // PLC address ushort startAddress = 9728; // Starting address bool value = false; // value to write ModbusIpMaster.CreateIp(tcpClient).WriteSingleCoil(startAddress, value); UpdateRTB(startAddress.ToString() + ": " + value.ToString()); // Output: <output address>: True/False } catch (Exception ex) { MessageBox.Show(ex.Message, "NModbus Error W1", MessageBoxButtons.OK, MessageBoxIcon.Error); }
}
| | Gavin Hill Director, Highpeak (Pty) Ltd. |
--
You received this message because you are subscribed to a topic in the Google Groups "NModbus" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/nmodbus-discuss/o1F9n7kkRTg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to nmodbus-discuss+unsubscribe@googlegroups.com.
To post to this group, send email to nmodbus-discuss@googlegroups.com.
Visit this group at https://groups.google.com/group/nmodbus-discuss.
For more options, visit https://groups.google.com/d/optout.