Skip to main content
added 412 characters in body
Source Link
chrisl
  • 16.6k
  • 2
  • 18
  • 27

#include <SimpleModbusMaster.h> #define baudrate 115200 #define timeout 2000 #define polling 500 #define retry_count 5 #define Txenablepins 2 #define Led1 12 #define totalregs 9 enum{ PACKET1, PACKET2, TOTAL_NO_OF_PACKETS }; Packet packets[TOTAL_NO_OF_PACKETS]; packetPointer packet1 = &packets[PACKET1]; packetPointer packet2 = &packets[PACKET2];

unsigned int regs[9]; unsigned int write_regs[1];

void setup() {

#include <SimpleModbusMaster.h> #define baudrate 115200 #define timeout 2000 #define polling 500 #define retry_count 5 #define Txenablepins 2 #define Led1 12 #define totalregs 9 enum{ PACKET1, PACKET2, TOTAL_NO_OF_PACKETS }; Packet packets[TOTAL_NO_OF_PACKETS]; packetPointer packet1 = &packets[PACKET1]; packetPointer packet2 = &packets[PACKET2]; unsigned int regs[9]; unsigned int write_regs[1]; void setup() { packet1->id = 1;  packet1->function = PRESET_MULTIPLE_REGISTERS;  packet1->address = 0;  packet1->no_of_registers = 1;  packet1->register_array = write_regs;    packet2->id = 1;  packet2->function = READ_HOLDING_REGISTERS;  packet2->address = 0;  packet2->no_of_registers = 1;  packet2->register_array = regs;   modbus_configure(baudrate, timeout, polling, retry_count, Txenablepins, packets, TOTAL_NO_OF_PACKETS); pinMode(Led1, OUTPUT); } void loop() { unsigned int connection_status1 = modbus_update(&packets[PACKET1]); unsigned int connection_status2 = modbus_update(&packets[PACKET2]); if(connection_status1==1 and connection_status2==1){ digitalWrite(Led1, HIGH); } else{ digitalWrite(Led1, LOW);} checkResponse(); } 

modbus_configure(baudrate, timeout, polling, retry_count, Txenablepins, packets, TOTAL_NO_OF_PACKETS); pinMode(Led1, OUTPUT);

}

void loop() {

unsigned int connection_status1 = modbus_update(&packets[PACKET1]); unsigned int connection_status2 = modbus_update(&packets[PACKET2]); if(connection_status1==1 and connection_status2==1){ digitalWrite(Led1, HIGH); } else{ digitalWrite(Led1, LOW);} checkResponse();

}``` This is my arduino slave code

  #include <SimpleModbusSlave.h> #define ledPin 11 enum { ADC0, ADC1, ADC2, ADC3, ADC4, ADC5, LED_STATE, BUTTON_STATE, TOTAL_ERRORS, TOTAL_REGS_SIZE }; unsigned int holdingRegs[TOTAL_REGS_SIZE]; void setup() { // put your setup code here, to run once: modbus_configure(115200, 1, 2, TOTAL_REGS_SIZE, 0); pinMode(ledPin, OUTPUT); Serial.flush(); } void loop() { // put your main code here, to run repeatedly: holdingRegs[TOTAL_ERRORS] = modbus_update(holdingRegs); int val = analogRead(ADC0); holdingRegs[0] = val; delayMicroseconds(50); if(holdingRegs[0] == 1){ digitalWrite(ledPin, HIGH); } else{ digitalWrite(ledPin, LOW); } }``` [Using this library for modbus master and slave](https://github.com/angeloc/simplemodbusng) 

Using this library for modbus master and slave

#include <SimpleModbusMaster.h> #define baudrate 115200 #define timeout 2000 #define polling 500 #define retry_count 5 #define Txenablepins 2 #define Led1 12 #define totalregs 9 enum{ PACKET1, PACKET2, TOTAL_NO_OF_PACKETS }; Packet packets[TOTAL_NO_OF_PACKETS]; packetPointer packet1 = &packets[PACKET1]; packetPointer packet2 = &packets[PACKET2];

unsigned int regs[9]; unsigned int write_regs[1];

void setup() {

packet1->id = 1; packet1->function = PRESET_MULTIPLE_REGISTERS; packet1->address = 0; packet1->no_of_registers = 1; packet1->register_array = write_regs; packet2->id = 1; packet2->function = READ_HOLDING_REGISTERS; packet2->address = 0; packet2->no_of_registers = 1; packet2->register_array = regs; 

modbus_configure(baudrate, timeout, polling, retry_count, Txenablepins, packets, TOTAL_NO_OF_PACKETS); pinMode(Led1, OUTPUT);

}

void loop() {

unsigned int connection_status1 = modbus_update(&packets[PACKET1]); unsigned int connection_status2 = modbus_update(&packets[PACKET2]); if(connection_status1==1 and connection_status2==1){ digitalWrite(Led1, HIGH); } else{ digitalWrite(Led1, LOW);} checkResponse();

}```

  #include <SimpleModbusSlave.h> #define ledPin 11 enum { ADC0, ADC1, ADC2, ADC3, ADC4, ADC5, LED_STATE, BUTTON_STATE, TOTAL_ERRORS, TOTAL_REGS_SIZE }; unsigned int holdingRegs[TOTAL_REGS_SIZE]; void setup() { // put your setup code here, to run once: modbus_configure(115200, 1, 2, TOTAL_REGS_SIZE, 0); pinMode(ledPin, OUTPUT); Serial.flush(); } void loop() { // put your main code here, to run repeatedly: holdingRegs[TOTAL_ERRORS] = modbus_update(holdingRegs); int val = analogRead(ADC0); holdingRegs[0] = val; delayMicroseconds(50); if(holdingRegs[0] == 1){ digitalWrite(ledPin, HIGH); } else{ digitalWrite(ledPin, LOW); } }``` [Using this library for modbus master and slave](https://github.com/angeloc/simplemodbusng) 
#include <SimpleModbusMaster.h> #define baudrate 115200 #define timeout 2000 #define polling 500 #define retry_count 5 #define Txenablepins 2 #define Led1 12 #define totalregs 9 enum{ PACKET1, PACKET2, TOTAL_NO_OF_PACKETS }; Packet packets[TOTAL_NO_OF_PACKETS]; packetPointer packet1 = &packets[PACKET1]; packetPointer packet2 = &packets[PACKET2]; unsigned int regs[9]; unsigned int write_regs[1]; void setup() { packet1->id = 1;  packet1->function = PRESET_MULTIPLE_REGISTERS;  packet1->address = 0;  packet1->no_of_registers = 1;  packet1->register_array = write_regs;    packet2->id = 1;  packet2->function = READ_HOLDING_REGISTERS;  packet2->address = 0;  packet2->no_of_registers = 1;  packet2->register_array = regs;   modbus_configure(baudrate, timeout, polling, retry_count, Txenablepins, packets, TOTAL_NO_OF_PACKETS); pinMode(Led1, OUTPUT); } void loop() { unsigned int connection_status1 = modbus_update(&packets[PACKET1]); unsigned int connection_status2 = modbus_update(&packets[PACKET2]); if(connection_status1==1 and connection_status2==1){ digitalWrite(Led1, HIGH); } else{ digitalWrite(Led1, LOW);} checkResponse(); } 

This is my arduino slave code

#include <SimpleModbusSlave.h> #define ledPin 11 enum { ADC0, ADC1, ADC2, ADC3, ADC4, ADC5, LED_STATE, BUTTON_STATE, TOTAL_ERRORS, TOTAL_REGS_SIZE }; unsigned int holdingRegs[TOTAL_REGS_SIZE]; void setup() { // put your setup code here, to run once: modbus_configure(115200, 1, 2, TOTAL_REGS_SIZE, 0); pinMode(ledPin, OUTPUT); Serial.flush(); } void loop() { // put your main code here, to run repeatedly: holdingRegs[TOTAL_ERRORS] = modbus_update(holdingRegs); int val = analogRead(ADC0); holdingRegs[0] = val; delayMicroseconds(50); if(holdingRegs[0] == 1){ digitalWrite(ledPin, HIGH); } else{ digitalWrite(ledPin, LOW); } } 

Using this library for modbus master and slave

Source Link

Trying to use an Arduino Uno to control two slaves with one master but its not working

I am trying to use following code but its not working. Need a help in this regard.

#include <SimpleModbusMaster.h> #define baudrate 115200 #define timeout 2000 #define polling 500 #define retry_count 5 #define Txenablepins 2 #define Led1 12 #define totalregs 9 enum{ PACKET1, PACKET2, TOTAL_NO_OF_PACKETS }; Packet packets[TOTAL_NO_OF_PACKETS]; packetPointer packet1 = &packets[PACKET1]; packetPointer packet2 = &packets[PACKET2];

unsigned int regs[9]; unsigned int write_regs[1];

void setup() {

packet1->id = 1; packet1->function = PRESET_MULTIPLE_REGISTERS; packet1->address = 0; packet1->no_of_registers = 1; packet1->register_array = write_regs; packet2->id = 1; packet2->function = READ_HOLDING_REGISTERS; packet2->address = 0; packet2->no_of_registers = 1; packet2->register_array = regs; 

modbus_configure(baudrate, timeout, polling, retry_count, Txenablepins, packets, TOTAL_NO_OF_PACKETS); pinMode(Led1, OUTPUT);

}

void loop() {

unsigned int connection_status1 = modbus_update(&packets[PACKET1]); unsigned int connection_status2 = modbus_update(&packets[PACKET2]); if(connection_status1==1 and connection_status2==1){ digitalWrite(Led1, HIGH); } else{ digitalWrite(Led1, LOW);} checkResponse();

}```

 #include <SimpleModbusSlave.h> #define ledPin 11 enum { ADC0, ADC1, ADC2, ADC3, ADC4, ADC5, LED_STATE, BUTTON_STATE, TOTAL_ERRORS, TOTAL_REGS_SIZE }; unsigned int holdingRegs[TOTAL_REGS_SIZE]; void setup() { // put your setup code here, to run once: modbus_configure(115200, 1, 2, TOTAL_REGS_SIZE, 0); pinMode(ledPin, OUTPUT); Serial.flush(); } void loop() { // put your main code here, to run repeatedly: holdingRegs[TOTAL_ERRORS] = modbus_update(holdingRegs); int val = analogRead(ADC0); holdingRegs[0] = val; delayMicroseconds(50); if(holdingRegs[0] == 1){ digitalWrite(ledPin, HIGH); } else{ digitalWrite(ledPin, LOW); } }``` [Using this library for modbus master and slave](https://github.com/angeloc/simplemodbusng)