main.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // Copyright (c) 2020-2023 Cesanta Software Limited
  2. // All rights reserved
  3. #include <syslog.h>
  4. #include <modbus.h>
  5. #include <unistd.h>
  6. #include <errno.h>
  7. #include <stdio.h>
  8. #include <time.h>
  9. #include <sys/time.h>
  10. #include <string.h>
  11. #include <ctype.h>
  12. #include <stdbool.h>
  13. #include <regex.h>
  14. static int s_sig_num;
  15. static void signal_handler(int sig_num)
  16. {
  17. signal(sig_num,signal_handler);
  18. s_sig_num = sig_num;
  19. }
  20. static void mylog(char ch,void* param)
  21. {
  22. static char buf[256];
  23. static size_t len;
  24. buf[len++] = ch;
  25. if(ch == '\n' || len >= sizeof(buf))
  26. {
  27. syslog(LOG_INFO,"%.*s",(int)len,buf); // Send logs
  28. len = 0;
  29. }
  30. }
  31. int is_hex_string(const char* str)
  32. {
  33. regex_t regex;
  34. int ret;
  35. // 编译正则表达式
  36. ret = regcomp(&regex,"^(0x|0X)[0-9a-fA-F]+$",REG_EXTENDED);
  37. if(ret != 0)
  38. {
  39. printf("Failed to compile regex\n");
  40. return 0; // 编译失败,返回假
  41. }
  42. // 执行匹配
  43. ret = regexec(&regex,str,0,NULL,0);
  44. regfree(&regex);
  45. return (ret == 0); // 匹配成功返回真,否则返回假
  46. }
  47. int main(int argc,char* argv[])
  48. {
  49. #ifdef CODE_06
  50. if(argc != 6)
  51. {
  52. printf("Usage: %s <device> <baud> <slave> <address> <value> \n",argv[0]);
  53. printf("Example: %s /dev/ttyUSB0 9600 1 0 0\n",argv[0]);
  54. return -1;
  55. }
  56. #else // CODE_03 : read only
  57. if(argc != 5)
  58. {
  59. printf("Usage: %s <device> <baud> <slave> <address>\n",argv[0]);
  60. printf("Example: %s /dev/ttyUSB0 9600 1 0\n",argv[0]);
  61. return -1;
  62. }
  63. #endif
  64. int baud = atoi(argv[2]);
  65. int slave = atoi(argv[3]);
  66. int address;
  67. if(is_hex_string(argv[4]))
  68. {
  69. sscanf(argv[4],"%x",&address);
  70. }
  71. else
  72. {
  73. address = atoi(argv[4]);
  74. }
  75. #ifdef CODE_06
  76. int value;
  77. if(is_hex_string(argv[5]))
  78. {
  79. sscanf(argv[5],"%x",&value);
  80. }
  81. else
  82. {
  83. value = atoi(argv[5]);
  84. }
  85. #endif
  86. char buf[200] = { 0 };
  87. sprintf(buf,"%s",argv[1]);
  88. modbus_t* ctx = modbus_new_rtu(buf,baud,'N',8,1);
  89. if(ctx == NULL)
  90. {
  91. printf("Unable to create the libmodbus context");
  92. return -1;
  93. }
  94. if(modbus_connect(ctx) == -1)
  95. {
  96. printf("Connection failed: %s",modbus_strerror(errno));
  97. modbus_free(ctx);
  98. return -1;
  99. }
  100. struct timeval t;
  101. t.tv_sec = 0;
  102. t.tv_usec = 500000; // 500ms
  103. modbus_set_response_timeout(ctx,0,500000);
  104. modbus_set_slave(ctx,slave);
  105. uint16_t tab_reg[32];
  106. int rc;
  107. #ifdef CODE_06
  108. rc = modbus_write_register(ctx,address,value);
  109. if(rc != 1)
  110. {
  111. printf("Write register failed: %s\n",modbus_strerror(errno));
  112. }
  113. else
  114. {
  115. #endif
  116. rc = modbus_read_registers(ctx,address,1,tab_reg);
  117. if(rc != 1)
  118. {
  119. printf("Read register failed: %s\n",modbus_strerror(errno));
  120. }
  121. else
  122. {
  123. printf("Read register: %d\n",tab_reg[0]);
  124. }
  125. #ifdef CODE_06
  126. }
  127. #endif
  128. // Clean up
  129. modbus_close(ctx);
  130. modbus_free(ctx);
  131. return 0;
  132. }