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); } }
/// <summary> /// Test a modbus RTU slave /// </summary> static void Test_ModbusRTUSlave() { byte unit_id = 1; // MODBUS address of the slave // Created datastore for unit ID 1. // This "datastore" will be a MODBUS slave device with address 1. Datastore ds = new Datastore(unit_id); // Crete instance of modbus serial RTU (replace COMx with a valid serial port - ex. COM5). ModbusSlaveSerial ms = new ModbusSlaveSerial(new Datastore[] { ds }, ModbusSerialType.RTU, "COM1", 9600, 8, Parity.Even, StopBits.One, Handshake.None); // Start listening. ms.StartListening(); // Generate a random number (used in the test). Random rnd = new Random(); // Print and write some registers... while (true) { // Print some nice formatting to the console. Console.WriteLine("---------------------- READING ----------------------"); // Create MODBUS holding register 1. ushort holdingReg1 = ms.ModbusDatabase.Single(x => x.UnitID == unit_id).HoldingRegisters[0]; // Print the output to the console. Console.WriteLine("Holding register n.1 : " + holdingReg1.ToString("D5")); // Create MODBUS holding register 60. ushort holdingReg60 = ms.ModbusDatabase.Single(x => x.UnitID == unit_id).HoldingRegisters[59]; // Print the output to the console. Console.WriteLine("Holding register n.60 : " + holdingReg60.ToString("D5")); // Create MODBUS coil 32. bool coil32 = ms.ModbusDatabase.Single(x => x.UnitID == unit_id).Coils[31]; // Print the output to the console. Console.WriteLine("Coil register n.32 : " + coil32.ToString()); // Print some nice formatting to the console. Console.WriteLine("---------------------- WRITING ----------------------"); // Do something (what?) to holding register 2. ms.ModbusDatabase.Single(x => x.UnitID == unit_id).HoldingRegisters[1] = (ushort)rnd.Next(ushort.MinValue, ushort.MaxValue); // Do something (what?) to holding register 2. ushort holdingReg2 = ms.ModbusDatabase.Single(x => x.UnitID == unit_id).HoldingRegisters[1]; // Print the output to the console. Console.WriteLine("Holding register n.2 : " + holdingReg2.ToString("D5")); // Do something (what?) to coil 15. ms.ModbusDatabase.Single(x => x.UnitID == unit_id).Coils[15] = Convert.ToBoolean(rnd.Next(0, 1)); // Do something (what?) to coil 15. bool coil15 = ms.ModbusDatabase.Single(x => x.UnitID == unit_id).Coils[15]; // Print the output to the console. Console.WriteLine("Coil register n.16 : " + coil15.ToString()); // Repeat every 2 seconds. Thread.Sleep(2000); } }