Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

How to write single register #348

Open
legaxus opened this issue May 3, 2024 · 7 comments
Open

How to write single register #348

legaxus opened this issue May 3, 2024 · 7 comments

Comments

@legaxus
Copy link

legaxus commented May 3, 2024

Appreciated if you could show and guide me how to write single register, thanks.

@Oaluiser
Copy link

Oaluiser commented May 3, 2024

If you are a server: Hreg(register, value)
If you are a client: writeHreg(address, register, value)

@legaxus
Copy link
Author

legaxus commented May 3, 2024

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.

@emelianov
Copy link
Owner

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

@legaxus
Copy link
Author

legaxus commented May 6, 2024

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.
writeHreg_Error1

@legaxus
Copy link
Author

legaxus commented May 7, 2024

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.

@MarcinanProfesjonalista
Copy link

MarcinanProfesjonalista commented Sep 23, 2024

#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?

@emelianov
Copy link
Owner

Handling of long/float over Modbus is described in #158

# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

4 participants