#include #include #include #include /* This example code shows a quick and dirty way to get an arduino to talk to a modbus master device with a device ID of 1 at 9600 baud. */ //Setup the brewtrollers register bank //All of the data accumulated will be stored here modbusDevice regBank; //Create the modbus slave protocol handler modbusSlave slave; #define LED1 2 #define LED2 3 void setup() { //Assign the modbus device ID. regBank.setId(1); /* modbus registers follow the following format 00001-09999 Digital Outputs, A master device can read and write to these registers 10001-19999 Digital Inputs, A master device can only read the values from these registers 30001-39999 Analog Inputs, A master device can only read the values from these registers 40001-49999 Analog Outputs, A master device can read and write to these registers Analog values are 16 bit unsigned words stored with a range of 0-32767 Digital values are stored as bytes, a zero value is OFF and any nonzer value is ON It is best to configure registers of like type into contiguous blocks. this allows for more efficient register lookup and and reduces the number of messages required by the master to retrieve the data */ //Add Digital Output registers 00001-00016 to the register bank regBank.add(1); regBank.add(2); /* Assign the modbus device object to the protocol handler This is where the protocol handler will look to read and write register data. Currently, a modbus slave protocol handler may only have one device assigned to it. */ slave._device = ®Bank; // Initialize the serial port for coms at 9600 baud slave.setBaud(9600); pinMode(LED1, OUTPUT); pinMode(LED2, OUTPUT); } void loop() { while(1){ int DO1 = regBank.get(1); if (DO1 <= 0 && digitalRead(LED1) == HIGH) digitalWrite(LED1, LOW); if (DO1 >= 1 && digitalRead(LED1) == LOW) digitalWrite(LED1, HIGH); int DO2 = regBank.get(2); if (DO2 <= 0 && digitalRead(LED2) == HIGH) digitalWrite(LED2, LOW); if (DO2 >= 1 && digitalRead(LED2) == LOW) digitalWrite(LED2, HIGH); slave.run(); } }