/
serialtest2.c
291 lines (248 loc) · 7.33 KB
/
serialtest2.c
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
// Code taken from
// https://www.pololu.com/docs/0J73/15.6
// and modified for my needs.
// gcc -m64 serialtest.c -O3 -Wall -oserialtest.exe
// strip serialtest.exe
// Frank B, 10/2021
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <windows.h>
void print_error(const char * context)
{
DWORD error_code = GetLastError();
char buffer[256];
DWORD size = FormatMessageA(
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_MAX_WIDTH_MASK,
NULL, error_code, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
buffer, sizeof(buffer), NULL);
if (size == 0) { buffer[0] = 0; }
fprintf(stderr, "%s: %s\n", context, buffer);
}
// Opens the specified serial port, configures its timeouts, and sets its
// baud rate. Returns a handle on success, or INVALID_HANDLE_VALUE on failure.
HANDLE open_serial_port(const char * device, uint32_t baud_rate)
{
HANDLE port = CreateFileA(device, GENERIC_READ | GENERIC_WRITE, 0, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (port == INVALID_HANDLE_VALUE)
{
print_error(device);
return INVALID_HANDLE_VALUE;
}
// Flush away any bytes previously read or written.
BOOL success = FlushFileBuffers(port);
if (!success)
{
print_error("Failed to flush serial port");
CloseHandle(port);
return INVALID_HANDLE_VALUE;
}
// Configure read and write operations to time out after 100 ms.
COMMTIMEOUTS timeouts = {0};
timeouts.ReadIntervalTimeout = 0;
timeouts.ReadTotalTimeoutConstant = 100;
timeouts.ReadTotalTimeoutMultiplier = 0;
timeouts.WriteTotalTimeoutConstant = 100;
timeouts.WriteTotalTimeoutMultiplier = 0;
success = SetCommTimeouts(port, &timeouts);
if (!success)
{
print_error("Failed to set serial timeouts");
CloseHandle(port);
return INVALID_HANDLE_VALUE;
}
// Set the baud rate and other options.
DCB state = {0};
state.DCBlength = sizeof(DCB);
state.BaudRate = baud_rate;
state.ByteSize = 8;
state.Parity = NOPARITY;
state.StopBits = ONESTOPBIT;
success = SetCommState(port, &state);
if (!success)
{
print_error("Failed to set serial settings");
CloseHandle(port);
return INVALID_HANDLE_VALUE;
}
success = FlushFileBuffers(port);
if (!success)
{
print_error("Failed to flush serial port #2");
CloseHandle(port);
return INVALID_HANDLE_VALUE;
}
return port;
}
// Writes bytes to the serial port, returning 0 on success and -1 on failure.
int write_port(HANDLE port, uint8_t * buffer, size_t size)
{
DWORD written;
BOOL success = WriteFile(port, buffer, size, &written, NULL);
if (!success)
{
print_error("Failed to write to port");
return -1;
}
if (written != size)
{
print_error("Failed to write all bytes to port");
return -1;
}
return 0;
}
// Reads bytes from the serial port.
// Returns after all the desired bytes have been read, or if there is a
// timeout or other error.
// Returns the number of bytes successfully read into the buffer, or -1 if
// there was an error reading.
SSIZE_T read_port(HANDLE port, char * buffer, size_t size)
{
DWORD received;
BOOL success = ReadFile(port, buffer, size, &received, NULL);
if (!success)
{
print_error("Failed to read from port");
exit(1);
return -1;
}
return received;
}
#define DEFAULT_LINE_LEN 38 // Expected default line length
int main(int argc, char **argv)
{
// COM ports higher than COM9 need the \\.\ prefix, which is written as
// "\\\\.\\" in C because we need to escape the backslashes.
char device[16];
long pnum;
long scnt = DEFAULT_LINE_LEN;
int qck = 0;
if ( argc < 2 || strlen(argv[1]) < 4 ||
argv[1][0] != 'C' || argv[1][1] != 'O' || argv[1][2] != 'M' ||
(argv[1][3] < '1' || argv[1][3] > '9')) {
fprintf(stderr, "Usage: %s COMx [SZx] [Q] \n", argv[0]);
return 1;
}
pnum = strtol(&argv[1][3], NULL, 10);
if (pnum > 9) snprintf(device, sizeof device, "\\\\.\\COM%ld", pnum);
else snprintf(device, sizeof device, "COM%ld", pnum);
if ( argc >= 3 && argv[2][0] == 'S' && argv[2][1] == 'Z' ) {
scnt = strtol(&argv[2][2], NULL, 10);
if ( scnt < 1 ) scnt = DEFAULT_LINE_LEN;
}
if ( argc >= 4 && argv[3][0] == 'Q' ) {
qck = 1;
}
printf("Port: %s\n", device);
uint32_t baud_rate = 9600;
HANDLE port = open_serial_port(device, baud_rate);
if (port == INVALID_HANDLE_VALUE) { return 1; }
printf("After start, you'll probably see some garbage. Things will normalize after a few seconds.\n");
printf("Waiting...");
unsigned int lines = 0;
unsigned int pos = 0;
unsigned int lcnt = 0;
SSIZE_T r;
SSIZE_T rmax = 0;
char line[256];
char buf[2000000];
memset(buf, 0, sizeof buf);
memset(line, 0, sizeof line);
do {
r = read_port(port, buf, 1);
//if (r) printf("%c", buf[0]);
} while ( r < 1 || buf[0] != '\n' );
printf("Start.\n");
//uint8_t Hello[] = "Hello World - from serialTest\n"; // this message could tell Teensy how many "SZ" bytes to send
//write_port(port, Hello, sizeof(Hello) );
unsigned int gCnt = 0;
unsigned int zCnt = 1;
unsigned long bcnt = 0;
long unsigned int stats[3] = {0, 0, 0};
if ( qck == 1 ) {
do {
r = read_port(port, buf, sizeof buf);
pos = 0;
while (r-- && pos < sizeof buf) {
char ch = buf[pos++];
if (lcnt < sizeof line) {
line[lcnt++] = ch;
bcnt++; // count bytes parsed
}
else {
line[sizeof(line) - 1] = '\0';
printf("\nLONG SKIP of %u: %s", lcnt, line);
lcnt = 0;
bcnt = 0;
break;
}
if (ch == '\n') {
line[lcnt] = '\0';
if ( lcnt != scnt ) {
printf("\t%s", line);
}
else lines++;
lcnt = 0;
bcnt = 0;
}
}
} while (1);
}
else {
do {
r = read_port(port, buf, sizeof buf);
if ( r > rmax ) rmax = r;
pos = 0;
while (r-- && pos < sizeof buf) {
char ch = buf[pos++];
if (lcnt < sizeof line) {
line[lcnt++] = ch;
bcnt++; // count bytes parsed
}
else {
line[sizeof(line) - 1] = '\0';
printf("\nLONG SKIP of %u: %s", lcnt, line);
lcnt = 0;
bcnt = 0;
break;
}
if (ch == '\n') {
line[lcnt] = '\0';
if (lcnt != scnt ) {
if ( lines == 100096) {
stats[0]++;
printf( "." );
if ( !( gCnt % 50) )
printf("\nLines-Delta: %d. Received: %s", lines, line);
if ( !( gCnt % 200) ) {
printf( "\tstats: 100K=%lu less=%lu repeated %lu rmax=%I64d\n", stats[0], stats[1], stats[2], rmax );
rmax = 0;
}
gCnt++;
}
else {
if ( 0 != zCnt ) {
printf("\n\tX_Lines-Delta: %d ::%lu bytes Received: %s", lines, bcnt, line);
if ( 0 != stats[0] )
stats[1]++;
}
else {
if ( 0 != stats[0] )
stats[2]++;
printf( ".X_%lu", bcnt );
}
zCnt = lines;
}
lines = 0;
}
else lines++;
lcnt = 0;
bcnt = 0;
}
}
} while (1);
}
CloseHandle(port);
return 0;
}