Hello,
I sent this post because I am currently trying (and failing) to communicate with a Digital F-T sensor via a C++ code. This sensor is connected to the computer via a RS485 card and the communication is supposed to follow the modbus protocol. I used a C++ library called libmodbus to connect to the device. The documentation of this library is available here :
https://libmodbus.org/docs/v3.1.6/
I wrote the following code to communicate with the sensor:
// CONNECTION INITIALIZATION
const char* sp_name = "COM3";
int baud_rate = 1250000;
int data_size = 8;
int stop_size = 1;
char parity = 'E';
int slave_adress = 10;
int err, length;
// Create ModBus context
modbus_t* modbus_context = modbus_new_rtu(sp_name, baud_rate, parity, data_size, stop_size);
// Set ModBus controlled device
err = modbus_set_slave(modbus_context,slave_adress);
if (err == -1)
{
fprintf(err_file, "Invalid slave ID\n");
modbus_free(modbus_context);
fflush(err_file);
return FALSE;
}
// Connect to device
if (modbus_connect(modbus_context) == -1)
{
fprintf(err_file, "Connection failed: %s\n", modbus_strerror(errno));
modbus_free(modbus_context);
fflush(err_file);
return FALSE;
}
// READ CALIBRATION REGISTER
int calib_register_addr = 0x00e3;
const int nb_bits = 8 * sizeof(Calibration_Struct);
const int nb_reg = 1;
uint16_t reg_destination;
uint8_t bit_destination[nb_bits * sizeof(uint8_t)];
int err = 0;
// Read holding register containing the information
err = modbus_read_input_registers(modbus_context, calib_register_addr, nb_reg, ®_destination);
if (err == -1)
{
fprintf(err_file, "Error during reading of registers : %s\n", modbus_strerror(errno));
//return FALSE;
}
for (int i(0); i < nb_reg; i++)
{
fprintf(out_file, "Register : reg[%d] = %d\n", i, reg_destination);
}
err = modbus_read_bits(modbus_context, reg_destination, nb_bits, bit_destination);
if (err == -1)
{
fprintf(err_file, "Error during reading bits : %s\n", modbus_strerror(errno));
//return FALSE;
}
calib_FT_sensor = (Calibration_Struct*)bit_destination;
fprintf(out_file, "Calibration ID : ");
for (int i(0); i < 8; i++)
{
fprintf(out_file, "%i", calib_FT_sensor->CalibSerialNumber[i]);
}
fprintf(out_file, "\n");
I followed the documentation available on the constructor website to write this code (section 8 for programmation):
https://www.ati-ia.com/app_content/documents/9620-05-Digital%20FT.pdf
The problem I encounter is that I fail to read the holding register containing the calibration (or any other register I tried to read) with both functions:
modbus_read_registers
modbus_read_input_registers
Nevertheless, the error string I get after reading the registers says : "No error", therefore I have no idea of what is going wrong. Could you expain to me what I did wrong or transfer me a detailled example (as I am a beginner in this field) of communication with a sensor through modbus protocol in C++?
Thank you !