-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_usb.c
249 lines (200 loc) · 7.19 KB
/
test_usb.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
// ===========================================================================
// App for testing return of laser scanner data via USB.
// ===========================================================================
/*
Copyright 2017 Darren Faulke <[email protected]>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// ===========================================================================
#include <unistd.h> // UNIX standard function definitions.
#include <stdio.h> // Standard Input/Output definitions.
#include <stdlib.h>
#include <string.h> // String function definitions.
#include <stdint.h> // Standard type definitions.
#include <fcntl.h> // File control definitions.
#include <stdbool.h> // Boolean definitions.
#include <termios.h> // POSIX terminal control definitions.
#include "urg.h"
// Commands ------------------------------------------------------------------
// ===========================================================================
// Clears serial port.
// ===========================================================================
void serial_flush(serial_t *serial)
{
tcdrain(serial->fd);
tcflush(serial->fd, TCIOFLUSH);
}
// ===========================================================================
// Sets serial baud rate.
// ===========================================================================
int serial_set_baud(serial_t *serial, uint16_t baud)
{
uint16_t baud_val;
switch (baud)
{
case 4800:
baud_val = B4800;
break;
case 9600:
baud_val = B9600;
break;
case 19200:
baud_val = B19200;
break;
case 38400:
baud_val = B38400;
break;
case 57600:
baud_val = B57600;
break;
case 115200:
baud_val = B115200;
break;
default:
return -1;
}
cfsetospeed(&serial->settings, baud_val);
cfsetispeed(&serial->settings, baud_val);
tcsetattr(serial->fd, TCSADRAIN, &serial->settings);
serial_flush(serial->fd);
return (0);
}
// ===========================================================================
// Initialises serial port.
// ===========================================================================
int serial_open(serial_t *serial, const char *device, uint16_t baud)
{
int flags = 0;
int ret = 0;
serial->fd = open(device, O_RDWR | O_NOCTTY | O_NONBLOCK);
if (serial->fd < 0)
{
perror("USB open");
return (-1);
}
flags = fcntl(serial->fd, F_GETFL, 0);
fcntl(serial->fd, F_SETFL, flags & ~O_NONBLOCK);
// Get current port options.
tcgetattr(serial->fd, &serial->settings);
// Set port options (lifted from the urg library source code).
serial->settings.c_iflag = 0;
serial->settings.c_oflag = 0;
serial->settings.c_cflag &= ~(CSIZE | PARENB | CSTOPB);
serial->settings.c_cflag |= CS8 | CREAD | CLOCAL;
serial->settings.c_lflag &= ~(ICANON | ECHO | ISIG | IEXTEN);
serial->settings.c_cc[VMIN] = 0;
serial->settings.c_cc[VTIME] = 0;
ret = serial_set_baud(serial, baud);
return ret;
}
// ===========================================================================
// Closes port.
// ===========================================================================
int serial_close(serial_t *serial)
{
return close(serial->fd);
}
// ===========================================================================
// Opens instance of URG sensor.
// ===========================================================================
int sensor_open(sensor_t *sensor, const char *device, uint16_t baud)
{
sensor->active = false;
return serial_open(&sensor->serial, device, baud);
}
// ===========================================================================
// Writes command to port.
// ===========================================================================
int serial_write(serial_t *serial, const char *cmd, uint8_t len)
{
if (DEBUG) PRINT_CMD(cmd);
err = write(serial->fd, cmd, len);
if (err < 0)
{
printf("Error writing command.\n");
perror("Write to port");
return (err);
}
usleep( 100000 ); // Definitely needs this!
return err;
}
// ===========================================================================
// Waits until data is available on serial port.
// ===========================================================================
int serial_wait(sensor_serial_t *serial, int timeout)
{
fd_set rfds;
struct timeval tv;
FD_ZERO(&rfds);
FD_SET(serial->fd, &rfds);
tv.tv_sec = timeout / 1000;
tv.tv_usec = (timeout % 1000) * 1000;
if (select(serial->fd + 1, &rfds, NULL, NULL,
(timeout < 0) ? NULL : &tv) <= 0) return (0);
return 1;
}
// ===========================================================================
// Returns data block from sensor.
// ===========================================================================
int serial_read(serial_t *serial, char *data)
{
char c;
int i;
i = 0;
while (read(fd, &c, 1) > 0 && (c != STRING_LF))
{
data[i++] = c;
}
// data[i+1] = STRING_LF;
return (i-1);
}
// ===========================================================================
// Main routine.
// ===========================================================================
int main(void)
{
sensor_t sensor;
int fd;
int err;
char id[16] = "Jaguar";
char *data;
const char *device = "/dev/ttyACM0";
long baud = 115200;
err = sensor_open(&sensor.serial, device, baud);
printf("Open sensor error = %d\n", err);
err = serial_write(&sensor, "VV", 3);
printf("Send command error = %d\n", err);
err = serial_read(&sensor.serial, data);
printf("Data = %s\n", data);
err = serial_write(&sensor, "BM", 3);
printf("Send command error = %d\n", err);
err = serial_read(&sensor.serial, data);
printf("Data = %s\n", data);
err = serial_write(&sensor, "BM", 3);
printf("Send command error = %d\n", err);
err = serial_read(&sensor.serial, data);
printf("Data = %s\n", data);
err = serial_write(&sensor, "QT", 3);
printf("Send command error = %d\n", err);
err = serial_read(&sensor.serial, data);
printf("Data = %s\n", data);
err = serial_write(&sensor, "QT", 3);
printf("Send command error = %d\n", err);
err = serial_read(&sensor.serial, data);
printf("Data = %s\n", data);
err = serial_write(&sensor, "VV", 3);
printf("Send command error = %d\n", err);
err = serial_read(&sensor.serial, data);
printf("Data = %s\n", data);
close(fd);
return (0);
}