#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](https://github.com/angeloc/simplemodbusng)
I am trying to use an Arduino Uno to control two slaves with one master, but I am encountering issues with the code. Although I have attempted to connect the master to one slave, it is not working properly and I am unsure where I am making a mistake. I would greatly appreciate any assistance in resolving this problem. Thank you in advance for your help.
Please grab pen and paper and make schematics.
Thanks for using autoformat and code tags!
What kind of code is "..."? Post the full code.
Without knowing the symptoms of the difficulty you are experiencing, it might be challenging to pinpoint the actual problem. Yet there are a few possible issues that might be a problem:
Invalid slave ID: Ensure that the slave ID in your master code and slave code are identical. In both codes, the slave ID is initially set to 1, although it can be modified.
Wrong register address: Verify that the register address in your slave code matches the register address you are attempting to read from or write to. You are attempting to read/write register address 0 in your master code, however you have defined registers with addresses ranging from 0 to 7 in your slave code.
Delays in your code can cause problems because Modbus communication demands exact timing. Make sure your delays are not excessively long or brief and that your code does not stall for an excessive amount of time.
Make sure your wiring is correct and that all connections are strong to avoid wiring problems.
Debug statements can also be added to your code to aid with problem identification. You may print the values of the registers you are reading or writing as well as the connection status that the modbus update() function has returned, for instance.
Last but not least, confirm that the modbus communication parameters—such as baud rate, timeout, and polling interval—have been correctly configured in your code.
Z
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.