[Modbus]
The Modbus86 Master enables the 86Duino to emulate a Modbus Master and send packets to the Slave node on the channel.
Example
Example of Modbus Master with RS485 enabled and using Modbus RTU as the communication method.
#include <Modbus86.h> ModbusMaster bus; ModbusMasterNode node1; ModbusMasterNode node2; int led = 0; uint32_t value = 0; void setup() { Serial485.begin(115200); /* Modbus RTU Mode via RS485. */ bus.begin(MODBUS_RTU, Serial485); /* Slave node 1 with ID 11. */ node1.attach(11, bus); /* Slave node 2 with ID 12. */ node2.attach(12, bus); } void loop() { uint16_t reg[2]; /* Write 1 coil to address 0 of the slave with ID 11. */ node1.writeSingleCoil(0, led); /* Write 2 word to holding registers address 16 of the slave with ID 11. */ reg[0] = value & 0xFFFF; reg[1] = (value >> 16) & 0xFFFF; node1.setTransmitBuffer(0, reg[0]); node1.setTransmitBuffer(1, reg[1]); node1.writeMultipleRegisters(16, 2); /* Read 2 word from holding registers address 16 of the slave with ID 11. */ node1.readHoldingRegisters(16, 2); Serial.print("From Node 1 Holding Register: "); value = node1.getResponseBuffer(0) | (node1.getResponseBuffer(1) << 16); Serial.println(value); /* Read 1 word from input registers address 2 of the slave with ID 11. */ node1.readInputRegisters(2, 1); Serial.print(" Input Register: "); Serial.print(node1.getResponseBuffer(0)); Serial.println(); /* Write 1 coil to address 0 of the slave with ID 12. */ node2.writeSingleCoil(0, led); /* Write 2 word to holding registers address 16 of the slave with ID 12. */ reg[0] = value & 0xFFFF; reg[1] = (value >> 16) & 0xFFFF; node2.setTransmitBuffer(0, reg[0]); node2.setTransmitBuffer(1, reg[1]); node2.writeMultipleRegisters(16, 2); /* Read 2 word from holding registers address 16 of the slave with ID 12. */ node2.readHoldingRegisters(16, 2); Serial.print("From Node 2 Holding Register: "); value = node2.getResponseBuffer(0) | (node2.getResponseBuffer(1) << 16); Serial.println(value); /* Read 1 word from input registers address 2 of the slave with ID 12. */ node2.readInputRegisters(2, 1); Serial.print(" Input Register: "); Serial.print(node2.getResponseBuffer(0)); Serial.println(); led = !led; value++; delay(1000); }
Example of a Modbus Master using Ethernet and Modbus TCP as the communication method.
#include <Ethernet.h> #include <Modbus86.h> ModbusMaster bus; ModbusMasterNode node; byte mac[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; IPAddress localIp(192,168,1,101); IPAddress serverIp(192,168,1,102); int led = 0; uint32_t value = 0; void setup() { Ethernet.begin(mac, localIp); /* Modbus TCP Mode via Ethernet. */ bus.begin(MODBUS_TCP, serverIp); /* Slave node initialize. */ node.attach(11, bus); } void loop() { uint16_t reg[2]; /* Write 1 coil to address 0 of the slave with ID 11. */ node.writeSingleCoil(0, led); /* Write 2 word to holding registers address 16 of the slave with ID 11. */ reg[0] = value & 0xFFFF; reg[1] = (value >> 16) & 0xFFFF; node.setTransmitBuffer(0, reg[0]); node.setTransmitBuffer(1, reg[1]); node.writeMultipleRegisters(16, 2); /* Read 2 word from holding registers address 16 of the slave with ID 11. */ node.readHoldingRegisters(16, 2); Serial.print("From Node 1 Holding Register: "); value = node.getResponseBuffer(0) | (node.getResponseBuffer(1) << 16); Serial.println(value); /* Read 1 word from input registers address 2 of the slave with ID 11. */ node.readInputRegisters(2, 1); Serial.print(" Input Register: "); Serial.print(node.getResponseBuffer(0)); Serial.println(); led = !led; value++; delay(1000); }
Libraries Reference Home
The text of the 86Duino reference is a modification of the Arduino reference and is licensed under a Creative Commons Attribution-ShareAlike 3.0 License. Code samples in the reference are released into the public domain.