Skip to content

Instantly share code, notes, and snippets.

@hyounggyu
Last active November 3, 2016 01:38
Show Gist options
  • Save hyounggyu/a82e215bd1ecd6ad97ccce27277525b7 to your computer and use it in GitHub Desktop.
Save hyounggyu/a82e215bd1ecd6ad97ccce27277525b7 to your computer and use it in GitHub Desktop.
AirConditioner.c
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <modbus.h>
const int START_REG = 36;
int main(int argc, char *argv[])
{
modbus_t *ctx = NULL;
uint16_t tab_reg[64];
int rc;
int i;
uint8_t dest;
ctx = modbus_new_rtu("/dev/ttyUSB0", 9600, 'N', 8, 1);
if (ctx == NULL) {
fprintf(stderr, "Unable to create the libmodbus context\n");
return -1;
}
modbus_set_slave(ctx, 1);
if (modbus_connect(ctx) == -1) {
fprintf(stderr, "Connection failed: %s\n", modbus_strerror(errno));
modbus_free(ctx);
return -1;
}
rc = modbus_read_registers(ctx, START_REG, 2, tab_reg);
if (rc == -1) {
fprintf(stderr, "%s\n", modbus_strerror(errno));
return -1;
}
for (i=0; i < rc; i++) {
printf("reg[%d]=%d (0x%X)\n", i+START_REG, tab_reg[i], tab_reg[i]);
}
modbus_close(ctx);
modbus_free(ctx);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment