-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsend_rs232.cpp
More file actions
245 lines (223 loc) · 6.99 KB
/
send_rs232.cpp
File metadata and controls
245 lines (223 loc) · 6.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*************************************
* Send RS232 sequence, wait for reply
*************************************
*
*/
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <string.h>
#include <string>
#include "tty.h"
#include "str_utils.h"
using namespace std;
void usage(const char *s)
{
fprintf(stderr,"Usage:\n\t%s [switches] string tty_device\n\n", s);
fprintf(stderr,"\t-h[elp] - Print help message.\n");
fprintf(stderr,"\t-b[aud] <baud_rate> - Set the baud rate for target connection.\n");
fprintf(stderr,"\t-r NUMBER - Read NUMBER of bytes after write. 0 - means read to EOF\n");
fprintf(stderr,"\t-e CHAR - Read after write. Till character CHAR is accepted.\n");
fprintf(stderr,"\t CHAR may be just a character or \\r \\n for CR or LF\n");
fprintf(stderr,"\t or \\xNN for any hexa code\n");
exit (1);
}
int main(int ac, char *av[])
{
int nparams=0;
int TargetBaud = 0;
int readReq = -1;
bool readChar = false;
uint8_t readTerm = 0;
char *send_str = 0;
char *tty_name = 0;
/* Command line parsing. */
for (int i=1; i<ac; i++)
{
switch (*av[i])
{
case '-':
++av[i];
if (!strcmp(av[i], "b") || !strcmp(av[i], "baud"))
{
if (++i >= ac)
{
fprintf(stderr,"After switch \"%s\" baud rate is expected.\n",av[--i]);
fprintf(stderr,"\nType \"%s -h\" for help.\n", av[0]);
return 1;
}
char *end;
TargetBaud = (int)strtol(av[i], &end, 0);
if (*end)
{
fprintf(stderr,"Invalid baud rate: \"%s\" -- ?\n", end);
fprintf(stderr,"\nType \"%s -h\" for help.\n", av[0]);
return 1;
}
}
else if (!strcmp(av[i], "r") || !strcmp(av[i], "read"))
{
if (++i >= ac)
{
fprintf(stderr,"After switch \"%s\" number of read bytes is expected.\n",av[--i]);
fprintf(stderr,"\nType \"%s -h\" for help.\n", av[0]);
return 1;
}
char *end;
readReq = (int)strtol(av[i], &end, 0);
if (*end)
{
fprintf(stderr,"Invalid number of read bytes: \"%s\" -- ?\n", end);
fprintf(stderr,"\nType \"%s -h\" for help.\n", av[0]);
return 1;
}
}
else if (!strcmp(av[i], "e") || !strcmp(av[i], "end"))
{
if (++i >= ac)
{
fprintf(stderr,"After switch \"%s\" termination character is expected.\n",av[--i]);
fprintf(stderr,"\nType \"%s -h\" for help.\n", av[0]);
return 1;
}
readChar = true;
string t = str::unescape(av[i]);
if (t.length() != 1)
{
fprintf(stderr,"After switch \"%s\" only one termination character is expected.\n",av[--i]);
fprintf(stderr,"\nType \"%s -h\" for help.\n", av[0]);
return 1;
}
readTerm = t[0];
}
else if (!strcmp(av[i], "h") || !strcmp(av[i], "help"))
{
usage(av[0]);
}
else
{
fprintf(stderr,"Invalid switch \"%s\".\n", av[i]);
fprintf(stderr,"Type \"%s -h\" for help.\n", av[0]);
return 1;
}
break;
case '?':
usage(av[0]);
break;
default:
switch (nparams++)
{
case 0:
send_str = av[i];
break;
case 1:
tty_name = av[i];
break;
default:
fprintf(stderr,"Invalid number of parameters.\n");
fprintf(stderr,"Type \"%s -h\" for help.\n", av[0]);
return 1;
}
}
}
if (nparams != 2)
{
fprintf(stderr,"Invalid number of parameters.\n");
fprintf(stderr,"Type \"%s -h\" for help.\n", av[0]);
return 1;
}
// Open tty connection
Tty tty;
int tty_fd = -1;
if ((tty_fd = tty.open(tty_name, TargetBaud)) < 0)
{
fprintf(stderr,"Can't open %s: %s\n", tty_name, strerror(errno));
return 1;
}
// Write
std::string s = str::unescape(send_str);
//// DEBUG ++++
//for (unsigned i = 0; i<s.size(); i++)
// printf("Write[%d] - 0x%02x (%c)\n", i, s[i]&0xff, (s[i] >= ' ' && s[i] <= '~') ? s[i] : '?');
//// DEBUG ----
unsigned rc = write(tty_fd, s.c_str(), s.length());
if (rc != s.length())
{
fprintf(stderr, "%s write error: %s\n", tty_name, strerror(errno));
return 1;
}
// Read till termination character
if (readChar)
{
uint8_t c;
string rc;
while (true)
{
int rd = read(tty_fd, &c, 1);
if (rd < 0)
{
if (errno == EAGAIN)
continue;
fprintf(stderr, "%s read error: %s\n", tty_name, strerror(errno));
return 1;
}
if (rd == 0)
break;
if (c == readTerm)
break;
rc += c;
}
printf("%s\n", rc.c_str());
}
// Read specified number of chars
else if (readReq != -1)
{
#if 0
const int RBUF = 256;
static char rbuf[RBUF+1];
int rcnt = 0;
while (readReq == 0 || rcnt < readReq)
{
int toRead = readReq ? std::min(RBUF, readReq) : RBUF;
int rd = read(tty_fd, rbuf, toRead);
if (rd < 0)
{
if (errno == EAGAIN)
continue;
fprintf(stderr, "%s read error: %s\n", tty_name, strerror(errno));
return 1;
}
if (rd == 0)
break;
rcnt += rd;
rbuf[rd] = 0;
printf("Read %d bytes: \"%s\"\n", rd, rbuf);
}
#else
while (true)
{
uint8_t c;
int rd = read(tty_fd, &c, 1);
if (rd < 0)
{
if (errno == EAGAIN)
continue;
fprintf(stderr, "%s read error: %s\n", tty_name, strerror(errno));
return 1;
}
if (rd == 0)
break;
printf("Read 0x%02x (%c)\n", c&0xff, (c >= ' ' && c <= '~') ? c : '?');
}
#endif
}
return 0;
}