Commit 842a0c23554265a96c32c76871dbdbe91fb28c3a

Authored by Stéphane Raimbault
1 parent fa207985

Accept IP or device in arg of unit test progs

tests/unit-test-client.c
... ... @@ -77,6 +77,7 @@ int main(int argc, char *argv[])
77 77 int use_backend;
78 78 int success = FALSE;
79 79 int old_slave;
  80 + char *ip_or_device;
80 81  
81 82 if (argc > 1) {
82 83 if (strcmp(argv[1], "tcp") == 0) {
... ... @@ -86,8 +87,9 @@ int main(int argc, char *argv[])
86 87 } else if (strcmp(argv[1], "rtu") == 0) {
87 88 use_backend = RTU;
88 89 } else {
89   - printf("Usage:\n %s [tcp|tcppi|rtu] - Modbus client for unit testing\n\n",
90   - argv[0]);
  90 + printf("Modbus client for unit testing\n");
  91 + printf("Usage:\n %s [tcp|tcppi|rtu]\n", argv[0]);
  92 + printf("Eg. tcp 127.0.0.1 or rtu /dev/ttyUSB1\n\n");
91 93 exit(1);
92 94 }
93 95 } else {
... ... @@ -95,17 +97,36 @@ int main(int argc, char *argv[])
95 97 use_backend = TCP;
96 98 }
97 99  
  100 + if (argc > 2) {
  101 + ip_or_device = argv[2];
  102 + } else {
  103 + switch (use_backend) {
  104 + case TCP:
  105 + ip_or_device = "127.0.0.1";
  106 + break;
  107 + case TCP_PI:
  108 + ip_or_device = "::1";
  109 + break;
  110 + case RTU:
  111 + ip_or_device = "/dev/ttyUSB1";
  112 + break;
  113 + default:
  114 + break;
  115 + }
  116 + }
  117 +
98 118 if (use_backend == TCP) {
99   - ctx = modbus_new_tcp("127.0.0.1", 1502);
  119 + ctx = modbus_new_tcp(ip_or_device, 1502);
100 120 } else if (use_backend == TCP_PI) {
101   - ctx = modbus_new_tcp_pi("::1", "1502");
  121 + ctx = modbus_new_tcp_pi(ip_or_device, "1502");
102 122 } else {
103   - ctx = modbus_new_rtu("/dev/ttyUSB1", 115200, 'N', 8, 1);
  123 + ctx = modbus_new_rtu(ip_or_device, 115200, 'N', 8, 1);
104 124 }
105 125 if (ctx == NULL) {
106 126 fprintf(stderr, "Unable to allocate libmodbus context\n");
107 127 return -1;
108 128 }
  129 +
109 130 modbus_set_debug(ctx, TRUE);
110 131 modbus_set_error_recovery(
111 132 ctx, MODBUS_ERROR_RECOVERY_LINK | MODBUS_ERROR_RECOVERY_PROTOCOL);
... ...
tests/unit-test-server.c
... ... @@ -42,6 +42,7 @@ int main(int argc, char *argv[])
42 42 int use_backend;
43 43 uint8_t *query;
44 44 int header_length;
  45 + char *ip_or_device;
45 46  
46 47 if (argc > 1) {
47 48 if (strcmp(argv[1], "tcp") == 0) {
... ... @@ -51,8 +52,9 @@ int main(int argc, char *argv[])
51 52 } else if (strcmp(argv[1], "rtu") == 0) {
52 53 use_backend = RTU;
53 54 } else {
54   - printf("Usage:\n %s [tcp|tcppi|rtu] - Modbus server for unit testing\n\n",
55   - argv[0]);
  55 + printf("Modbus server for unit testing.\n");
  56 + printf("Usage:\n %s [tcp|tcppi|rtu] [<ip or device>]\n", argv[0]);
  57 + printf("Eg. tcp 127.0.0.1 or rtu /dev/ttyUSB0\n\n");
56 58 return -1;
57 59 }
58 60 } else {
... ... @@ -60,17 +62,36 @@ int main(int argc, char *argv[])
60 62 use_backend = TCP;
61 63 }
62 64  
  65 + if (argc > 2) {
  66 + ip_or_device = argv[2];
  67 + } else {
  68 + switch (use_backend) {
  69 + case TCP:
  70 + ip_or_device = "127.0.0.1";
  71 + break;
  72 + case TCP_PI:
  73 + ip_or_device = "::1";
  74 + break;
  75 + case RTU:
  76 + ip_or_device = "/dev/ttyUSB0";
  77 + break;
  78 + default:
  79 + break;
  80 + }
  81 + }
  82 +
63 83 if (use_backend == TCP) {
64   - ctx = modbus_new_tcp("127.0.0.1", 1502);
  84 + ctx = modbus_new_tcp(ip_or_device, 1502);
65 85 query = malloc(MODBUS_TCP_MAX_ADU_LENGTH);
66 86 } else if (use_backend == TCP_PI) {
67   - ctx = modbus_new_tcp_pi("::0", "1502");
  87 + ctx = modbus_new_tcp_pi(ip_or_device, "1502");
68 88 query = malloc(MODBUS_TCP_MAX_ADU_LENGTH);
69 89 } else {
70   - ctx = modbus_new_rtu("/dev/ttyUSB0", 115200, 'N', 8, 1);
  90 + ctx = modbus_new_rtu(ip_or_device, 115200, 'N', 8, 1);
71 91 modbus_set_slave(ctx, SERVER_ID);
72 92 query = malloc(MODBUS_RTU_MAX_ADU_LENGTH);
73 93 }
  94 +
74 95 header_length = modbus_get_header_length(ctx);
75 96  
76 97 modbus_set_debug(ctx, TRUE);
... ...