using System; using System.Collections.Generic; using System.Text; //step1. reference nmodbusce.dll, and using the namespaces. using Modbus.Device; //for modbus master using System.Net; //for tcp client using System.Net.Sockets; using System.Threading; namespace input_status { class Program { static void Main(string[] args) { // connect to Modbus TCP Server string ipAddress = "192.168.1.1"; int tcpPort = 502; TcpClient tcpClient = new TcpClient(ipAddress, tcpPort); // create Modbus TCP Master by the tcp client //document->Modbus.Device.Namespace->ModbusIpMaster Class->Create Method ModbusIpMaster master = ModbusIpMaster.CreateIp(tcpClient); // read the input status 0~9 (10001~10010) of the device slave ID 1. //document->Modbus.Device.Namespace->ModbusMaster Class->ReadInput Method byte slaveID = 1; ushort startAddress = 0; ushort numOfPoints = 10; try { bool[] status = master.ReadInputs(slaveID, startAddress, numOfPoints); for (int index = 0; index < status.Length; index++) Console.WriteLine(string.Format("status[{0}] = {1}", index, status[index])); } catch (Exception ex) { Console.WriteLine(ex.Message); } Thread.Sleep(3000); } } }