]>
Commit | Line | Data |
---|---|---|
7587b16f WW |
1 | #include <string> |
2 | #include <iostream> | |
cac9ae42 | 3 | #include <stdio.h> |
f7cee5e1 JH |
4 | #include <time.h> |
5 | ||
6 | #ifdef __MACH__ | |
7 | #include <mach/clock.h> | |
8 | #include <mach/mach.h> | |
9 | #endif | |
7587b16f | 10 | |
241daf30 WW |
11 | #include <boost/thread.hpp> |
12 | ||
93b8dfe2 | 13 | #include "serial/serial.h" |
7587b16f | 14 | |
f7cee5e1 JH |
15 | using std::string; |
16 | using std::cout; | |
17 | using std::endl; | |
18 | ||
241daf30 | 19 | int run(int argc, char **argv) |
7587b16f | 20 | { |
5324caf2 JH |
21 | if(argc < 3) { |
22 | std::cerr << "Usage: test_serial <serial port address> <baudrate>" << std::endl; | |
b78a44e9 WW |
23 | return 0; |
24 | } | |
25 | std::string port(argv[1]); | |
5324caf2 JH |
26 | unsigned long baud = 0; |
27 | ||
28 | sscanf(argv[2], "%lu", &baud); | |
29 | ||
241daf30 | 30 | // port, baudrate, timeout in milliseconds |
f7cee5e1 | 31 | serial::Serial serial(port, baud, 10000); |
7587b16f | 32 | |
4eb3e51a WW |
33 | std::cout << "Is the serial port open?"; |
34 | if(serial.isOpen()) | |
35 | std::cout << " Yes." << std::endl; | |
36 | else | |
37 | std::cout << " No." << std::endl; | |
38 | ||
f4430b48 | 39 | int count = 0; |
fc4364f7 | 40 | while (count >= 0) { |
f7cee5e1 JH |
41 | struct timespec start, end; |
42 | double diff; | |
43 | ||
44 | #ifdef __MACH__ // OS X does not have clock_gettime, use clock_get_time | |
45 | clock_serv_t cclock; | |
46 | mach_timespec_t mts; | |
47 | host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock); | |
48 | clock_get_time(cclock, &mts); | |
49 | mach_port_deallocate(mach_task_self(), cclock); | |
50 | start.tv_sec = mts.tv_sec; | |
51 | start.tv_nsec = mts.tv_nsec; | |
52 | ||
53 | #else | |
54 | clock_gettime(CLOCK_REALTIME, &start); | |
55 | #endif | |
56 | ||
57 | size_t bytes_wrote = serial.write("Testing.\n"); | |
58 | ||
59 | #ifdef __MACH__ // OS X does not have clock_gettime, use clock_get_time | |
60 | host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock); | |
61 | clock_get_time(cclock, &mts); | |
62 | mach_port_deallocate(mach_task_self(), cclock); | |
63 | end.tv_sec = mts.tv_sec; | |
64 | end.tv_nsec = mts.tv_nsec; | |
65 | ||
66 | #else | |
67 | clock_gettime(CLOCK_REALTIME, &end); | |
68 | #endif | |
69 | ||
70 | end.tv_sec -= start.tv_sec; | |
71 | end.tv_nsec -= start.tv_nsec; | |
72 | printf("write: %05lu.%09lu\n", end.tv_sec, end.tv_nsec); | |
73 | ||
74 | #ifdef __MACH__ // OS X does not have clock_gettime, use clock_get_time | |
75 | host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock); | |
76 | clock_get_time(cclock, &mts); | |
77 | mach_port_deallocate(mach_task_self(), cclock); | |
78 | start.tv_sec = mts.tv_sec; | |
79 | start.tv_nsec = mts.tv_nsec; | |
80 | ||
81 | #else | |
82 | clock_gettime(CLOCK_REALTIME, &start); | |
83 | #endif | |
84 | ||
85 | std::string result = serial.readline(); | |
86 | #ifdef __MACH__ // OS X does not have clock_gettime, use clock_get_time | |
87 | host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock); | |
88 | clock_get_time(cclock, &mts); | |
89 | mach_port_deallocate(mach_task_self(), cclock); | |
90 | end.tv_sec = mts.tv_sec; | |
91 | end.tv_nsec = mts.tv_nsec; | |
92 | ||
93 | #else | |
94 | clock_gettime(CLOCK_REALTIME, &end); | |
95 | #endif | |
96 | ||
97 | ||
98 | end.tv_sec -= start.tv_sec; | |
99 | end.tv_nsec -= start.tv_nsec; | |
100 | printf("read: %05lu.%09lu\n", end.tv_sec, end.tv_nsec); | |
101 | ||
102 | if (result == string("Testing.\n")) { | |
103 | } | |
104 | else { | |
241daf30 WW |
105 | std::cout << ">" << count << ">" << bytes_wrote << ">"; |
106 | std::cout << result.length() << "<" << result << std::endl; | |
f7cee5e1 JH |
107 | cout << "No" << endl; |
108 | } | |
f4430b48 | 109 | |
f7cee5e1 JH |
110 | count += 1; |
111 | boost::this_thread::sleep(boost::posix_time::milliseconds(100)); | |
7587b16f WW |
112 | } |
113 | ||
114 | return 0; | |
0f75942f | 115 | } |
241daf30 WW |
116 | |
117 | int main(int argc, char **argv) { | |
d8874120 | 118 | // try { |
241daf30 | 119 | return run(argc, argv); |
d8874120 JH |
120 | // } catch (std::exception &e) { |
121 | // std::cerr << "Unhandled Exception: " << e.what() << std::endl; | |
122 | // } | |
241daf30 | 123 | } |