123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- // Copyright (c) 2020-2023 Cesanta Software Limited
- // All rights reserved
- #include <syslog.h>
- #include <modbus.h>
- #include <unistd.h>
- #include <errno.h>
- #include <stdio.h>
- #include <time.h>
- #include <sys/time.h>
- #include <string.h>
- #include <ctype.h>
- #include <stdbool.h>
- #include <regex.h>
- static int s_sig_num;
- static void signal_handler(int sig_num)
- {
- signal(sig_num,signal_handler);
- s_sig_num = sig_num;
- }
- static void mylog(char ch,void* param)
- {
- static char buf[256];
- static size_t len;
- buf[len++] = ch;
- if(ch == '\n' || len >= sizeof(buf))
- {
- syslog(LOG_INFO,"%.*s",(int)len,buf); // Send logs
- len = 0;
- }
- }
- int is_hex_string(const char* str)
- {
- regex_t regex;
- int ret;
- // 编译正则表达式
- ret = regcomp(®ex,"^(0x|0X)[0-9a-fA-F]+$",REG_EXTENDED);
- if(ret != 0)
- {
- printf("Failed to compile regex\n");
- return 0; // 编译失败,返回假
- }
- // 执行匹配
- ret = regexec(®ex,str,0,NULL,0);
- regfree(®ex);
- return (ret == 0); // 匹配成功返回真,否则返回假
- }
- int main(int argc,char* argv[])
- {
- #ifdef CODE_06
- if(argc != 6)
- {
- printf("Usage: %s <device> <baud> <slave> <address> <value> \n",argv[0]);
- printf("Example: %s /dev/ttyUSB0 9600 1 0 0\n",argv[0]);
- return -1;
- }
- #else // CODE_03 : read only
- if(argc != 5)
- {
- printf("Usage: %s <device> <baud> <slave> <address>\n",argv[0]);
- printf("Example: %s /dev/ttyUSB0 9600 1 0\n",argv[0]);
- return -1;
- }
- #endif
- int baud = atoi(argv[2]);
- int slave = atoi(argv[3]);
- int address;
- if(is_hex_string(argv[4]))
- {
- sscanf(argv[4],"%x",&address);
- }
- else
- {
- address = atoi(argv[4]);
- }
- #ifdef CODE_06
- int value;
- if(is_hex_string(argv[5]))
- {
- sscanf(argv[5],"%x",&value);
- }
- else
- {
- value = atoi(argv[5]);
- }
- #endif
- char buf[200] = { 0 };
- sprintf(buf,"%s",argv[1]);
- modbus_t* ctx = modbus_new_rtu(buf,baud,'N',8,1);
- if(ctx == NULL)
- {
- printf("Unable to create the libmodbus context");
- return -1;
- }
- if(modbus_connect(ctx) == -1)
- {
- printf("Connection failed: %s",modbus_strerror(errno));
- modbus_free(ctx);
- return -1;
- }
- struct timeval t;
- t.tv_sec = 0;
- t.tv_usec = 500000; // 500ms
- modbus_set_response_timeout(ctx,0,500000);
- modbus_set_slave(ctx,slave);
- uint16_t tab_reg[32];
- int rc;
- #ifdef CODE_06
- rc = modbus_write_register(ctx,address,value);
- if(rc != 1)
- {
- printf("Write register failed: %s\n",modbus_strerror(errno));
- }
- else
- {
- #endif
- rc = modbus_read_registers(ctx,address,1,tab_reg);
- if(rc != 1)
- {
- printf("Read register failed: %s\n",modbus_strerror(errno));
- }
- else
- {
- printf("Read register: %d\n",tab_reg[0]);
- }
- #ifdef CODE_06
- }
- #endif
- // Clean up
- modbus_close(ctx);
- modbus_free(ctx);
- return 0;
- }
|