-
Notifications
You must be signed in to change notification settings - Fork 1
/
serialtest.cpp
134 lines (108 loc) · 3.25 KB
/
serialtest.cpp
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
/*
Name: serialtest
Author: cLx / RJS <clx.kat@gmail.com>
Description: Send datas to ... whatever...
Copyright: CC http://clx.freeshell.org/
*/
#include <stdio.h>
#include <getopt.h>
#include <string.h>
#include <cstdlib>
#include "serial.h"
#ifndef TRUE
#define TRUE 1
#define FALSE 0
#endif
#ifdef WIN32
#define DEFAULTSERIALPORT "COM1"
#else
#define DEFAULTSERIALPORT "/dev/ttyUSB0"
#include <unistd.h>
void Sleep(unsigned int ms) {
usleep(1000*ms);
}
#endif
struct option long_options[] = {
{"help", 0, NULL, 'h'},
{"serialport", 1, NULL, 's'},
{0, 0, 0, 0}
};
void usage(char *prog){
int i = 0;
fprintf(stderr, "usage: %s\n", prog);
while(long_options[i].name){
printf("-%c, --%-15s\n", long_options[i].val, long_options[i].name);
i++;
}
exit(1);
}
int main(int argc, char *argv[]){
char data[300] = {0};
unsigned int dataout[300] = {0};
int c;
bool err = FALSE, printusage = FALSE;
char *serialport = NULL;
serial port1;
int option_index = 0;
while ((c = getopt_long(argc, argv, "abc:d:012", long_options, &option_index)) != -1){
switch (c){
case 'h':
printusage = TRUE;
break;
case '?': // print usage
printusage = TRUE;
break;
case 's': // serialport
serialport = new char(strlen(optarg)+1);
strcpy(serialport, optarg);
break;
default:
fprintf(stderr, "Sorry, --%s (-%c) is not yet implemented.\n", long_options[option_index].name, c);
break;
}
}
if (!serialport){
serialport = new char(strlen(DEFAULTSERIALPORT)+1);
strcpy(serialport, DEFAULTSERIALPORT);
}
if (err || printusage) { usage(argv[0]); }
if (!err){
if (!port1.isopened()){
printf("Opening [%s]...\n", serialport);
port1.open(serialport, 9600);
}
if (!port1.isopened()){
perror("Got some kind of problems with that %%#@!* serial port");
return 1;
}
printf("Now sending... ^C to stop !\n");
{
unsigned int i=0, j=0, k=0;
char buf[8], minibuf[10];
char sendstr[] = "Hello serial world !";
for(;;){
port1.send(sendstr[i++]);
if (sendstr[i] == 0) {
i=0, k++;
sprintf(minibuf, " %3d", k);
port1.send(minibuf);
port1.send('\n');
}
//printf("%s\n", sendstr);
Sleep(30);
if (++j<50){
if (port1.receive(buf, sizeof(buf))){
printf("%s", buf);
fflush(stdout);
}
}
j++;
if (j>150){j=0;};
}
}
}
// maintenant qu'on a terminé, on range tout bien proprement...
if (port1.isopened()) { port1.close(); }
if (serialport) { delete serialport; serialport = NULL; }
return err;
}