- Включено: Raspberry Pi 4B
- ОС: Ubuntu 22.04.4 Jammy
- Версия libmodbus: 3.1.10
< li>RS485 Pi 2.0 USB UART - Устройство Modbus подключено к: порту /dev/ttyUSB0.
- Идентификатор ведомого устройства: 33
- Регистры: 30001, 30002, 30003, 30004.
- В связи с наличием ведомого устройства: для чтения данных необходим опрос.
- Целью кода является установление соединения с конкретным устройством Modbus и чтение данных из определенных регистров. Ниже приведен точный пример подключения, взятый из Windows с опросом приложения.

- Выход кода — это отладочный вывод, полученный после установления соединения и чтения определенных регистров.
- Однако вместо ожидаемых данных были получены неожиданные результаты (см. раздел вывода ниже).

Код: Выделить всё
#include
#include
#include
#include
int main() {
// The ID of the device being queried
const int REMOTE_ID = 33;
modbus_t *ctx;
uint16_t tab_reg[4];
// Creating a Modbus connection
ctx = modbus_new_rtu("/dev/ttyUSB0", 9600, 'O', 8, 1);
modbus_set_slave(ctx, 33); // Setting the Slave ID
modbus_rtu_set_serial_mode(ctx, MODBUS_RTU_RS485);
modbus_rtu_set_rts(ctx, MODBUS_RTU_RTS_UP);
modbus_rtu_set_rts_delay(ctx, 3);
modbus_set_debug(ctx, 1); // Enabling debugging
if (ctx == NULL) {
fprintf(stderr, "Unable to create the libmodbus context\n");
return -1;
}
// Establishing the connection
if (modbus_connect(ctx) == -1) {
fprintf(stderr, "Connection failed: %s\n", modbus_strerror(errno));
modbus_free(ctx);
return -1;
}
// Reading the registers
int rc = modbus_read_registers(ctx, 0, 3, tab_reg);
if (rc == -1) {
fprintf(stderr, "%s\n", modbus_strerror(errno));
std::cout
Подробнее здесь: [url]https://stackoverflow.com/questions/78428110/incorrect-data-retrieval-from-desired-registers-despite-correct-slave-id[/url]