-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArduinoTest.c
More file actions
125 lines (102 loc) · 2.97 KB
/
ArduinoTest.c
File metadata and controls
125 lines (102 loc) · 2.97 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
// NOTE: You must change the 'const char * device' line below.
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <termios.h>
#include <string.h>
int serialport_read_until(int fd, char* buf, char until, int buf_max, int timeout)
{
char b[1]; // read expects an array, so we give it a 1-byte array
int i=0;
do {
int n = read(fd, b, 1); // read a char at a time
if( n==-1) return -1; // couldn't read
if( n==0 ) {
usleep( 1 * 1000 ); // wait 1 msec try again
timeout--;
if( timeout==0 ) { buf[i]=0; return -2; }
continue;
}
// printf("serialport_read_until: i=%d, n=%d b='%c'\n",i,n,b[0]); // debug
buf[i] = b[0];
i++;
} while( b[0] != until && i < buf_max && timeout>0 );
buf[i] = 0; // null terminate the string
return 0;
}
int main(int argc, char **argv)
{
printf("Argc is %d.\n", argc);
// Open the Arduino's virtual COM port.
const char * device = "/dev/ttyUSB0";
int fd = open(device, O_RDWR | O_NONBLOCK );
if (fd == -1)
{
perror(device);
return 1;
} else {
printf("FD opened: %d.\n", fd);
}
struct termios options;
if ( tcgetattr(fd, &options) < 0 ) {
printf("Error reading termios option.\n");
close(fd);
return 1;
}
cfsetispeed(&options, B9600);
cfsetospeed(&options, B9600);
// 8N1
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
// no flow control
options.c_cflag &= ~CRTSCTS;
//options.c_cflag &= ~HUPCL; // disable hang-up-on-close to avoid reset
options.c_cflag |= CREAD | CLOCAL; // turn on READ & ignore ctrl lines
options.c_iflag &= ~(IXON | IXOFF | IXANY); // turn off s/w flow ctrl
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // make raw
options.c_oflag &= ~OPOST; // make raw
// see: http://unixwiz.net/techtips/termios-vmin-vtime.html
options.c_cc[VMIN] = 0;
options.c_cc[VTIME] = 0;
//options.c_cc[VTIME] = 20;
tcsetattr(fd, TCSANOW, &options);
if( tcsetattr(fd, TCSAFLUSH, &options) < 0) {
perror("init_serialport: Couldn't set term attributes");
return -1;
}
printf("Options set.\n");
usleep(1500000);
char versionString[100];
const char *verCmd = "A";
int n, l;
if ( argc > 1 ) {
l = strlen(argv[1]);
n = write(fd, argv[1], l);
} else {
l = strlen(verCmd);
n = write(fd, verCmd, l);
}
if ( n != l )
{
perror("error writing");
return -1;
}
printf("Wrote command: %s (%d bytes)", (argc > 1 ) ? argv[1] : verCmd, n);
usleep(500000);
printf("Reading...\n");
n = serialport_read_until(fd, versionString, '\n', 100, 2000);
printf("serialport_read_until returned %d.\n", n);
if ( n == 0 ) {
printf("Read: %s.\n", versionString);
}
/* int br = -1;
br = read(fd, versionString, 1);
printf("Read %d bytes.\n", br);
if ( br > 0 ) {
printf("Read: %s.\n", versionString);
} */
close(fd);
return 0;
}