#define SLAVE_ID 1
#define START_ADDRESS 0
#define NUMBER_REGISTERS 14
#define DEBUG 1
int main(int argc, char *argv[])
{
const char *CONFIG_FILE = "modbuslog.cfg";
config_t cfg;
//config_setting_t *setting;
const char *modbus_device_address;
int modbus_baud_rate;
int modbus_data_bits;
const char *modbus_parity;
int modbus_stop_bits;
config_init(&cfg);
if(!config_read_file(&cfg, CONFIG_FILE))
{
fprintf(stderr, "%s:%d - %s\n", config_error_file(&cfg),
config_error_line(&cfg), config_error_text(&cfg));
config_destroy(&cfg);
fprintf(stderr, "Unable to find CONFIG_FILE.\n");
return -1;
}
modbus_t *ctx;
uint16_t tab_reg[64];
int rc;
int i;
if(!(config_lookup_string(&cfg, "modbus.device", &modbus_device_address)
&& config_lookup_int(&cfg, "modbus.baud", &modbus_baud_rate)
&& config_lookup_int(&cfg, "modbus.data_bits", &modbus_data_bits)
&& config_lookup_string(&cfg, "modbus.parity", &modbus_parity)
&& config_lookup_int(&cfg, "modbus.stop_bits", &modbus_stop_bits))
){
fprintf(stderr, "Check modbus configuration in configuration file.\n");
return -1;
}
if(DEBUG){
printf("%s %d %c %d %d\n",modbus_device_address, modbus_baud_rate, modbus_parity[0], modbus_data_bits, modbus_stop_bits);
}
ctx = modbus_new_rtu(modbus_device_address, modbus_baud_rate, modbus_parity[0], modbus_data_bits, modbus_stop_bits);
modbus_set_slave(ctx, SLAVE_ID);
modbus_set_debug(ctx, DEBUG);
if (ctx == NULL) {
fprintf(stderr, "Unable to create the libmodbus context\n");
return -1;
}
if (modbus_connect(ctx) == -1) {
fprintf(stderr, "Connection failed: %s\n", modbus_strerror(errno));
modbus_free(ctx);
return -1;
}
rc = modbus_read_input_registers(ctx, START_ADDRESS, NUMBER_REGISTERS, tab_reg);
if (rc == -1) {
fprintf(stderr, "ERROR: %s\n", modbus_strerror(errno));
return -1;
}
for (i=0; i < rc; i=i+2) {
printf("reg[%d]=%d\n", i, tab_reg[i]+tab_reg[i+1]);
}
modbus_close(ctx);
modbus_free(ctx);
return 0;
}
5c5ea4a9d24cf55a0a7be345f1449a22d91&showmsg=1&follow=1'>Expand)Author | Files | Lines |
2011-12-07 | update scan files for de-* | Christoph Pfister | 16 | -113/+90 |
2011-09-08 | cleanups | Christoph Pfister | 1 | -1/+1 |
2011-09-08 | update scan files for fi-* | Christoph Pfister | 51 | -10/+61 |
2011-09-08 | update scan file for es-Donostia | Christoph Pfister | 1 | -11/+10 |
2011-09-08 | update scan file for Thor-1.0W | Christoph Pfister | 1 | -1/+30 |
2011-09-08 | update scan file for hr-All | Christoph Pfister | 1 | -11/+11 |
2011-09-08 | update scan file for de-Berlin | Christoph Pfister | 1 | -1/+1 |
2011-07-16 | update scan file for de-Berlin | Christoph Pfister | 1 | -0/+1 |
2011-07-16 | update scan file for de-Berlin | Christoph Pfister | 1 | -5/+4 |
2011-07-16 | update scan files for sk-* | Christoph Pfister | 26 | -42/+143 |
2011-07-16 | remove outdated scan files fr-* | Christoph Pfister | 18 | -272/+0 |
2011-07-16 | update scan file us-Cable-Standard-center-frequencies-QAM256 | Christoph Pfister | 1 | -0/+36 |
2011-06-26 | update scan file for au-Sydney_North_Shore | Christoph Pfister | 1 | -1/+1 |