Open legaxus opened 6 months ago
If you are a server: Hreg(register, value) If you are a client: writeHreg(address, register, value)
If you are a server: Hreg(register, value) If you are a client: writeHreg(address, register, value)
Thanks for the guidance, just to clarify, server meaning is Master, client meaning is Slave? For Slave/Client, Address = Slave ID?
Thanks in advanced.
Thanks for the guidance, just to clarify, server meaning is Master, client meaning is Slave? For Slave/Client, Address = Slave ID?
Client is Master, Server is Slave Address is SlaveID
Thanks for the clarification. I've tried and got this error, value = 1 NO error, but if value = 0, compilation error. Attached with picture. Thanks.
Thanks for the guidance, just to clarify, server meaning is Master, client meaning is Slave? For Slave/Client, Address = Slave ID?
Client is Master, Server is Slave Address is SlaveID
I've tried with mb.writeHreg(slaveid, 5, 1, NULL) it managed to transmit value of 1 to slave, then I tried with mb.writeHreg(slaveid, 5, 0, NULL), it just hang there without transmitting the 0 to slave. Compilation is success. Is there anything I had missed out? Thanks.
#include <ModbusRTU.h>
#define SERIAL2_RX_PIN 16
#define SERIAL2_TX_PIN 17
// Indeksy rejestrów
#define REG_ID 0
#define REG_PENDING_FLAG 1
#define REG_TARGET_POSITION 2
float targetPosition = 0.00;
unsigned long previousMillis_rs = 0; // Przechowuje czas poprzedniej aktualizacji
const long interval = 1000; // Interwał w milisekundach (1 sekunda)
// Funkcje pomocnicze
void floatToRegisters(float value, uint16_t* regs) {
union {
float f;
uint16_t regs[2];
} data;
data.f = value;
regs[0] = data.regs[0];
regs[1] = data.regs[1];
}
void init_modbus_rtu() {
Serial2.begin(9600, SERIAL_8N1, SERIAL2_RX_PIN, SERIAL2_TX_PIN);
mb.begin(&Serial2);
mb.slave(id);
// Dodawanie rejestrów
mb.addHreg(REG_ID, id); // Rejestr 0: ID
mb.addCoil(REG_PENDING_FLAG, 0);
mb.addHreg(REG_TARGET_POSITION, 12, 1); // Rejestr 1-2: Cel podróży (float)
mb.addHreg(REG_TARGET_POSITION+1, 34, 1); // Rejestr 1-2: Cel podróży (float)
}
void setup() {
Serial.begin(115200);
init_modbus_rtu();
}
void loop() {
uint16_t tempRegs[2];
unsigned long currentMillis = millis();
// Sprawdzanie, czy minęła jedna sekunda
if (currentMillis - previousMillis_rs >= interval) {
previousMillis_rs = currentMillis; // Aktualizacja czasu
targetPosition += 1.1; // Zwiększ wartość o 0.1
// Pakowanie `targetPosition` do rejestrów Modbus
floatToRegisters(targetPosition, tempRegs);
mb.writeHreg(1,REG_TARGET_POSITION, tempRegs[0]);
mb.writeHreg(1,REG_TARGET_POSITION + 1, tempRegs[1]);
//Serial.println(targetPosition); // Wypisanie wartości na Serial Monitor
}
}
mb.addHreg(REG_TARGET_POSITION, 12, 1); <-- What happens if i set 1 to 2? Can i hold float there then?
Handling of long/float over Modbus is described in #158
Appreciated if you could show and guide me how to write single register, thanks.