-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserial.c
More file actions
313 lines (241 loc) · 6.81 KB
/
serial.c
File metadata and controls
313 lines (241 loc) · 6.81 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
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <termios.h>
#include <string.h>
#include <math.h>
#include <sys/time.h>
typedef struct {
char commandStr[16]; // size might not be enough?
float distance; //
} HPGLCommand;
int openSerial(const char *port) {
int fd = open(port, O_RDWR | O_NOCTTY);
// get the current options
struct termios toptions;
tcgetattr(fd, &toptions);
// set speed
cfsetispeed(&toptions, B115200);
cfsetospeed(&toptions, B115200);
/* 8 bits, no parity, no stop bits */
toptions.c_cflag &= ~PARENB;
toptions.c_cflag &= ~CSTOPB;
toptions.c_cflag &= ~CSIZE;
toptions.c_cflag |= CS8;
/* no hardware flow control */
toptions.c_cflag &= ~CRTSCTS;
/* enable receiver, ignore status lines */
toptions.c_cflag |= CREAD | CLOCAL;
/* disable input/output flow control, disable restart chars */
toptions.c_iflag &= ~(IXON | IXOFF | IXANY);
/* disable canonical input, disable echo,
disable visually erase chars,
disable terminal-generated signals */
toptions.c_iflag &= ~(ICANON | ECHO | ECHOE | ISIG);
/* disable output processing */
toptions.c_oflag &= ~OPOST;
/* wait for 24 characters to come in before read returns */
toptions.c_cc[VMIN] = 0;
/* no minimum time to wait before read returns */
toptions.c_cc[VTIME] = 0;
/* commit the options */
tcsetattr(fd, TCSANOW, &toptions);
return fd;
}
char answerBuffer[1024];
// read serial until ;
void readAnswer(int fd, char *buffer) {
// we just wait for an input
char c[1];
c[0] = 0;
char *ptr = buffer;
while(c[0] != ';') {
int count = read(fd, c, 1);
if(count > 0) {
*ptr = c[0];
ptr++;
//printf("%c\n", c);
} else {
usleep(1000);
}
}
*ptr = 0;
}
void sendCommand(char *cmd, int ttyFile) {
write(ttyFile, cmd, strlen(cmd));
// read answer
readAnswer(ttyFile, answerBuffer);
char answer[256];
read(ttyFile, answer, 256);
//printf("%s\n", answerBuffer);
}
int commandCount = 0; // total number of command
float totalDistance = 0.0f; // total distance of the pen
HPGLCommand *commandBuffer = NULL;
// return 1 if number read, 0 otherwise
int parseInt(int *v, char **ptr) {
int negative = 0;
// first remove space
while(isspace(**ptr)) {
(*ptr)++;
}
// < 0?
if(**ptr == '-') {
negative = 1;
(*ptr)++;
}
if(!isdigit(**ptr)) {
// no number here!
return 0;
}
// parse the number
*v = 0;
while(isdigit(**ptr) && **ptr != 0) {
*v = *v * 10 + (**ptr - '0');
(*ptr)++;
}
if(negative) {
*v = -*v;
}
return 1;
}
// process HPGL file
void processHPGLFile(const char *hpglFilename) {
FILE *hpglFile = fopen(hpglFilename, "r");
if(hpglFile == NULL) {
fprintf(stderr, "Can't open file %s!!\n", hpglFilename);
return;
}
// read data from the file and send command
char buffer[256];
char *bufferPtr = buffer;
char c;
int commaCount = 0; // number of comma read, used to split the PU and PD command
int currentX = 0;
int currentY = 0;
int commandBufferSize = 256;
commandBuffer = (HPGLCommand *)malloc(sizeof(HPGLCommand) * commandBufferSize);
while((c = fgetc(hpglFile)) != EOF) {
// accept only alpha, num and ;
if(!(isalpha(c) || isdigit(c) || c == ';' || c == ','))
continue;
*bufferPtr = c;
bufferPtr++;
if(c == ',') {
commaCount++;
}
if((c == ';') || (c == ',' && commaCount == 2)) {
// make sure we end with ';'
bufferPtr--; *bufferPtr = ';'; bufferPtr++;
*bufferPtr = 0; // end the string
// increase buffer size if needed
if(commandCount >= commandBufferSize) {
commandBufferSize *= 2;
commandBuffer = (HPGLCommand *)realloc(commandBuffer, sizeof(HPGLCommand) * commandBufferSize);
}
// handle the command
float dist = 0;
if((strncmp(buffer, "PD", 2) == 0) || (strncmp(buffer, "PU", 2) == 0) || (strncmp(buffer, "PA", 2) == 0)) {
char *ptr = &buffer[2];
int x, y;
if(parseInt(&x, &ptr)
&& ++ptr
&& *ptr != 0
&& parseInt(&y, &ptr)) {
float _x = x - currentX;
float _y = y - currentY;
dist = sqrtf(_x * _x + _y * _y);
totalDistance += dist;
//printf("%d, %d %f\n", x, y, dist);
currentX = x;
currentY = y;
}
}
strcpy(commandBuffer[commandCount].commandStr, buffer);
commandBuffer[commandCount].distance = dist;
commandCount++;
// reset buffer
if(c == ',') {
// we keep the command for the next one
bufferPtr = &buffer[2];
} else {
// we finished the current command
bufferPtr = buffer;
}
// reset commaCount
commaCount = 0;
}
}
fclose(hpglFile);
printf("Command count : %d\n", commandCount);
printf("Total pen distance : %.0f\n", totalDistance);
}
void timeToStr(double time, char *str) {
int h = (int)time / 3600;
int m = ((int)time % 3600) / 60;
int s = ((int)time % 3600) % 60;
sprintf(str, "%dh%dm%ds", h, m, s);
}
void sendCommandToSerial(int ttyFile) {
struct timeval begin, end;
double timeSpent = 0.0;
double nextEvalTime = 20.0;
double speed = -1; // pen speed
float currentDistance = 0;
int i;
// start timer
gettimeofday(&begin, NULL);
for(i = 0; i < commandCount; i++) {
char timeStr[256];
if(speed < 0.0) {
sprintf(timeStr, "unknown");
} else {
float remainingDistance = totalDistance - currentDistance;
float remainingTime = remainingDistance * speed;
timeToStr(remainingTime, timeStr);
}
char runTime[256];
timeToStr(timeSpent, runTime);
// print status
printf("%.0f%% ETA %s Running Time %s (cmd %d/%d) (dist %d/%d) %s... \r",
currentDistance / totalDistance * 100.0f,
timeStr,
runTime,
i, commandCount,
(int)currentDistance, (int)totalDistance,
commandBuffer[i].commandStr);
// send command
sendCommand(commandBuffer[i].commandStr, ttyFile);
// update distance
currentDistance += commandBuffer[i].distance;
// update timer
gettimeofday(&end, NULL);
timeSpent = (double) (end.tv_usec - begin.tv_usec) / 1000000 + (double) (end.tv_sec - begin.tv_sec);
// update speed
if(timeSpent > nextEvalTime) {
speed = timeSpent / currentDistance;
nextEvalTime += 60.0; // next update
}
}
printf("\n");
}
int main(int argc, char *argv[]) {
if(argc != 3) {
printf("Usage %s ttyport hpglfile\n\n", argv[0]);
exit(0);
}
// process file
processHPGLFile(argv[2]);
int fd = openSerial(argv[1]);
/* Wait for the Arduino to reset */
usleep(1000*1000);
/* Flush anything already in the serial buffer */
tcflush(fd, TCIFLUSH);
printf("Waiting Arduino...\n");
readAnswer(fd, answerBuffer);
printf("Arduino : %s\n", answerBuffer);
// send commands
sendCommandToSerial(fd);
return 0;
}