private void button1_Click(object sender, EventArgs e) { byte unit_id = 1; // Created datastore for unit ID 1 Datastore ds = new Datastore(unit_id); // Crete instance of modbus serial RTU (replace COMx with a free serial port - ex. COM5) ModbusSlaveSerial ms = new ModbusSlaveSerial(new Datastore[] { ds }, ModbusSerialType.RTU, "COM1", 9600, 8, Parity.Even, StopBits.One, Handshake.None); // Start listen ms.StartListen(); // Print and write some registers... Random rnd = new Random(); while (true) { textBox1.Text = "Holding register n.1 : " + ms.ModbusDB.Single(x => x.UnitID == unit_id).HoldingRegisters[0].ToString("D5") + "Holding register n.60 : " + ms.ModbusDB.Single(x => x.UnitID == unit_id).HoldingRegisters[59].ToString("D5") + "Coil register n.32 : " + ms.ModbusDB.Single(x => x.UnitID == unit_id).Coils[31].ToString(); ms.ModbusDB.Single(x => x.UnitID == unit_id).HoldingRegisters[1] = (ushort)rnd.Next(ushort.MinValue, ushort.MaxValue); textBox2.Text = "Holding register n.2 : " + ms.ModbusDB.Single(x => x.UnitID == unit_id).HoldingRegisters[1].ToString("D5"); ms.ModbusDB.Single(x => x.UnitID == unit_id).Coils[15] = Convert.ToBoolean(rnd.Next(0, 1)); textBox3.Text = "Coil register n.16 : " + ms.ModbusDB.Single(x => x.UnitID == unit_id).Coils[15].ToString(); // Exec the cicle each 2 seconds delayTime(2); } }
/// <summary> /// Test a modbus RTU slave /// </summary> static void Test_ModbusRTUSlave() { byte unit_id = 1; // Created datastore for unit ID 1 Datastore ds = new Datastore(unit_id); // Crete instance of modbus serial RTU (replace COMx with a free serial port - ex. COM5) ModbusSlaveSerial ms = new ModbusSlaveSerial(new Datastore[] { ds }, ModbusSerialType.RTU, "COM1", 9600, 8, Parity.Even, StopBits.One, Handshake.None); // Start listen ms.StartListen(); // Print and write some registers... Random rnd = new Random(); while (true) { Console.Write( "---------------------- READING ----------------------" + Environment.NewLine + "Holding register n.1 : " + ms.ModbusDB.Single(x => x.UnitID == unit_id).HoldingRegisters[0].ToString("D5") + Environment.NewLine + "Holding register n.60 : " + ms.ModbusDB.Single(x => x.UnitID == unit_id).HoldingRegisters[59].ToString("D5") + Environment.NewLine + "Coil register n.32 : " + ms.ModbusDB.Single(x => x.UnitID == unit_id).Coils[31].ToString() + Environment.NewLine + "---------------------- WRITING ----------------------" + Environment.NewLine); ms.ModbusDB.Single(x => x.UnitID == unit_id).HoldingRegisters[1] = (ushort)rnd.Next(ushort.MinValue, ushort.MaxValue); Console.WriteLine( "Holding register n.2 : " + ms.ModbusDB.Single(x => x.UnitID == unit_id).HoldingRegisters[1].ToString("D5")); ms.ModbusDB.Single(x => x.UnitID == unit_id).Coils[15] = Convert.ToBoolean(rnd.Next(0, 1)); Console.WriteLine( "Coil register n.16 : " + ms.ModbusDB.Single(x => x.UnitID == unit_id).Coils[15].ToString()); // Exec the cicle each 2 seconds Thread.Sleep(2000); } }