Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <math.h>
- #include <ctype.h>
- #include <unistd.h>
- #include <argp.h>
- #include <gtk/gtk.h>
- #include <modbus/modbus.h>
- #include <modbus/modbus-rtu.h>
- //GLOBALS
- extern const char *__progname;
- int debug = 0;
- char *ttyDevice = "/dev/ttyUSB2";
- char *modbusSlaveAddress = "1";
- modbus_t *modbusDeviceContext;
- enum DPSRegisters {
- dpsUSET = 0x0000,
- dpsISET = 0x0001,
- dpsUOUT = 0x0002,
- dpsIOUT = 0x0003,
- dpsPOWER = 0x0004,
- dpsUIN = 0x0005,
- dpsKLOCK = 0x0006,
- dpsPROT = 0x0007,
- dpsCVCC = 0x0008,
- dpsONOFF = 0x0009,
- dpsB_LED = 0x000A,
- dpsMODEL = 0x000B,
- dpsVERS = 0x000C,
- };
- uint16_t modbusRead(uint16_t reg)
- {
- uint16_t register_val[2];
- if (modbus_read_registers(modbusDeviceContext, reg, 1, register_val) != 1) { //(context, base, count, result)
- return 0;
- } else {
- return register_val[0];
- }
- }
- uint16_t modbusWrite(uint16_t reg, uint16_t val)
- {
- return modbus_write_register(modbusDeviceContext, reg, val); //(context, base, count, result)
- }
- int main (int argc, char **argv)
- {
- int c;
- opterr = 0;
- while ((c = getopt (argc, argv, "p:a:d")) != -1)
- switch (c) {
- case 'a':
- //slave address
- modbusSlaveAddress = optarg;
- break;
- case 'd':
- debug = 1;
- break;
- case 'p':
- ttyDevice = optarg;
- break;
- case '?':
- if (optopt == 'p' || optopt == 'a')
- fprintf (stderr, "Option -%c requires an argument.\n", optopt);
- else if (isprint (optopt))
- fprintf (stderr, "Unknown option `-%c'.\n", optopt);
- else
- fprintf (stderr,
- "Unknown option character `\\x%x'.\n",
- optopt);
- return 1;
- }
- // end while
- if (debug) {
- printf ("%s starting...\n"
- "----\n"
- " Port: %s\n"
- "SAddr: %s\n\n"
- , __progname, ttyDevice, modbusSlaveAddress);
- }
- gtk_init (&argc, &argv);
- modbusDeviceContext = modbus_new_rtu(ttyDevice, 9600, 'N', 8, 1);
- if (modbusDeviceContext == NULL) {
- fprintf(stderr, "Unable to create the libmodbus context\n");
- return -1;
- }
- if (debug) modbus_set_debug(modbusDeviceContext, TRUE);
- if (modbus_connect(modbusDeviceContext) == -1) {
- fprintf(stderr, "Connection failed: %s\n", modbus_strerror(errno));
- modbus_free(modbusDeviceContext);
- return -1;
- }
- modbus_set_slave(modbusDeviceContext, atoi(modbusSlaveAddress));
- int mod = modbusRead(dpsMODEL);
- int ver = modbusRead(dpsVERS);
- if (mod == 3005 && ver==13) {
- printf("DPS-%d v%d", mod,ver);
- } else {
- printf("DPS module not detected on %s",ttyDevice);
- }
- modbus_close(modbusDeviceContext);
- modbus_free(modbusDeviceContext);
- //int index;
- // for (index = optind; index < argc; index++)
- // printf ("Non-option argument %s\n", argv[index]);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement