Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.15 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <math.h>
  5. #include <ctype.h>
  6.  
  7. #include <unistd.h>
  8. #include <argp.h>
  9.  
  10. #include <gtk/gtk.h>
  11.  
  12. #include <modbus/modbus.h>
  13. #include <modbus/modbus-rtu.h>
  14. //GLOBALS
  15. extern const char *__progname;
  16. int debug = 0;
  17. char *ttyDevice = "/dev/ttyUSB2";
  18. char *modbusSlaveAddress = "1";
  19. modbus_t *modbusDeviceContext;
  20.  
  21. enum DPSRegisters {
  22.     dpsUSET  = 0x0000,
  23.     dpsISET  = 0x0001,
  24.     dpsUOUT  = 0x0002,
  25.     dpsIOUT  = 0x0003,
  26.     dpsPOWER = 0x0004,
  27.     dpsUIN   = 0x0005,
  28.     dpsKLOCK = 0x0006,
  29.     dpsPROT  = 0x0007,
  30.     dpsCVCC  = 0x0008,
  31.     dpsONOFF = 0x0009,
  32.     dpsB_LED = 0x000A,
  33.     dpsMODEL = 0x000B,
  34.     dpsVERS  = 0x000C,
  35. };
  36.  
  37. uint16_t modbusRead(uint16_t reg)
  38. {
  39.     uint16_t register_val[2];
  40.     if (modbus_read_registers(modbusDeviceContext, reg, 1, register_val) != 1) { //(context, base, count, result)
  41.         return 0;
  42.     } else {
  43.         return register_val[0];
  44.     }
  45. }
  46. uint16_t modbusWrite(uint16_t reg, uint16_t val)
  47. {
  48.     return modbus_write_register(modbusDeviceContext, reg, val); //(context, base, count, result)
  49. }
  50.  
  51. int main (int argc, char **argv)
  52. {
  53.  
  54.  
  55.     int c;
  56.     opterr = 0;
  57.  
  58.     while ((c = getopt (argc, argv, "p:a:d")) != -1)
  59.         switch (c) {
  60.         case 'a':
  61.             //slave address
  62.             modbusSlaveAddress = optarg;
  63.             break;
  64.         case 'd':
  65.             debug = 1;
  66.             break;
  67.         case 'p':
  68.             ttyDevice = optarg;
  69.             break;
  70.         case '?':
  71.             if (optopt == 'p' || optopt == 'a')
  72.                 fprintf (stderr, "Option -%c requires an argument.\n", optopt);
  73.             else if (isprint (optopt))
  74.                 fprintf (stderr, "Unknown option `-%c'.\n", optopt);
  75.             else
  76.                 fprintf (stderr,
  77.                          "Unknown option character `\\x%x'.\n",
  78.                          optopt);
  79.             return 1;
  80.         }
  81.     // end while
  82.  
  83.     if (debug) {
  84.         printf ("%s starting...\n"
  85.                 "----\n"
  86.                 " Port: %s\n"
  87.                 "SAddr: %s\n\n"
  88.                 , __progname, ttyDevice, modbusSlaveAddress);
  89.     }
  90.  
  91.     gtk_init (&argc, &argv);
  92.  
  93.     modbusDeviceContext = modbus_new_rtu(ttyDevice, 9600, 'N', 8, 1);
  94.     if (modbusDeviceContext == NULL) {
  95.         fprintf(stderr, "Unable to create the libmodbus context\n");
  96.         return -1;
  97.     }
  98.  
  99.     if (debug) modbus_set_debug(modbusDeviceContext, TRUE);
  100.  
  101.     if (modbus_connect(modbusDeviceContext) == -1) {
  102.         fprintf(stderr, "Connection failed: %s\n", modbus_strerror(errno));
  103.         modbus_free(modbusDeviceContext);
  104.         return -1;
  105.     }
  106.  
  107.     modbus_set_slave(modbusDeviceContext, atoi(modbusSlaveAddress));
  108.    
  109.     int mod = modbusRead(dpsMODEL);
  110.     int ver = modbusRead(dpsVERS);
  111.     if (mod == 3005 && ver==13) {
  112.         printf("DPS-%d v%d", mod,ver);
  113.     } else {
  114.         printf("DPS module not detected on %s",ttyDevice);
  115.     }
  116.  
  117.     modbus_close(modbusDeviceContext);
  118.     modbus_free(modbusDeviceContext);
  119.  
  120.  
  121. //int index;
  122. // for (index = optind; index < argc; index++)
  123. //   printf ("Non-option argument %s\n", argv[index]);
  124.     return 0;
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement