serial_example.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /***
  2. * This example expects the serial port has a loopback on it.
  3. *
  4. * Alternatively, you could use an Arduino:
  5. *
  6. * <pre>
  7. * void setup() {
  8. * Serial.begin(<insert your baudrate here>);
  9. * }
  10. *
  11. * void loop() {
  12. * if (Serial.available()) {
  13. * Serial.write(Serial.read());
  14. * }
  15. * }
  16. * </pre>
  17. */
  18. #include <string>
  19. #include <iostream>
  20. #include <cstdio>
  21. // OS Specific sleep
  22. #ifdef _WIN32
  23. #include <windows.h>
  24. #else
  25. #include <unistd.h>
  26. #endif
  27. #include "serial/serial.h"
  28. using std::string;
  29. using std::exception;
  30. using std::cout;
  31. using std::cerr;
  32. using std::endl;
  33. using std::vector;
  34. void my_sleep(unsigned long milliseconds) {
  35. #ifdef _WIN32
  36. Sleep(milliseconds); // 100 ms
  37. #else
  38. usleep(milliseconds*1000); // 100 ms
  39. #endif
  40. }
  41. void enumerate_ports()
  42. {
  43. vector<serial::PortInfo> devices_found = serial::list_ports();
  44. vector<serial::PortInfo>::iterator iter = devices_found.begin();
  45. while( iter != devices_found.end() )
  46. {
  47. serial::PortInfo device = *iter++;
  48. printf( "(%s, %s, %s)\n", device.port.c_str(), device.description.c_str(),
  49. device.hardware_id.c_str() );
  50. }
  51. }
  52. void print_usage()
  53. {
  54. cerr << "Usage: test_serial {-e|<serial port address>} ";
  55. cerr << "<baudrate> [test string]" << endl;
  56. }
  57. int run(int argc, char **argv)
  58. {
  59. if(argc < 2) {
  60. print_usage();
  61. return 0;
  62. }
  63. // Argument 1 is the serial port or enumerate flag
  64. string port(argv[1]);
  65. if( port == "-e" ) {
  66. enumerate_ports();
  67. return 0;
  68. }
  69. else if( argc < 3 ) {
  70. print_usage();
  71. return 1;
  72. }
  73. // Argument 2 is the baudrate
  74. unsigned long baud = 0;
  75. #if defined(WIN32) && !defined(__MINGW32__)
  76. sscanf_s(argv[2], "%lu", &baud);
  77. #else
  78. sscanf(argv[2], "%lu", &baud);
  79. #endif
  80. // port, baudrate, timeout in milliseconds
  81. serial::Serial my_serial(port, baud, serial::Timeout::simpleTimeout(1000));
  82. cout << "Is the serial port open?";
  83. if(my_serial.isOpen())
  84. cout << " Yes." << endl;
  85. else
  86. cout << " No." << endl;
  87. // Get the Test string
  88. int count = 0;
  89. string test_string;
  90. if (argc == 4) {
  91. test_string = argv[3];
  92. } else {
  93. test_string = "Testing.";
  94. }
  95. // Test the timeout, there should be 1 second between prints
  96. cout << "Timeout == 1000ms, asking for 1 more byte than written." << endl;
  97. while (count < 10) {
  98. size_t bytes_wrote = my_serial.write(test_string);
  99. string result = my_serial.read(test_string.length()+1);
  100. cout << "Iteration: " << count << ", Bytes written: ";
  101. cout << bytes_wrote << ", Bytes read: ";
  102. cout << result.length() << ", String read: " << result << endl;
  103. count += 1;
  104. }
  105. // Test the timeout at 250ms
  106. my_serial.setTimeout(serial::Timeout::max(), 250, 0, 250, 0);
  107. count = 0;
  108. cout << "Timeout == 250ms, asking for 1 more byte than written." << endl;
  109. while (count < 10) {
  110. size_t bytes_wrote = my_serial.write(test_string);
  111. string result = my_serial.read(test_string.length()+1);
  112. cout << "Iteration: " << count << ", Bytes written: ";
  113. cout << bytes_wrote << ", Bytes read: ";
  114. cout << result.length() << ", String read: " << result << endl;
  115. count += 1;
  116. }
  117. // Test the timeout at 250ms, but asking exactly for what was written
  118. count = 0;
  119. cout << "Timeout == 250ms, asking for exactly what was written." << endl;
  120. while (count < 10) {
  121. size_t bytes_wrote = my_serial.write(test_string);
  122. string result = my_serial.read(test_string.length());
  123. cout << "Iteration: " << count << ", Bytes written: ";
  124. cout << bytes_wrote << ", Bytes read: ";
  125. cout << result.length() << ", String read: " << result << endl;
  126. count += 1;
  127. }
  128. // Test the timeout at 250ms, but asking for 1 less than what was written
  129. count = 0;
  130. cout << "Timeout == 250ms, asking for 1 less than was written." << endl;
  131. while (count < 10) {
  132. size_t bytes_wrote = my_serial.write(test_string);
  133. string result = my_serial.read(test_string.length()-1);
  134. cout << "Iteration: " << count << ", Bytes written: ";
  135. cout << bytes_wrote << ", Bytes read: ";
  136. cout << result.length() << ", String read: " << result << endl;
  137. count += 1;
  138. }
  139. return 0;
  140. }
  141. int main(int argc, char **argv) {
  142. try {
  143. return run(argc, argv);
  144. } catch (exception &e) {
  145. cerr << "Unhandled Exception: " << e.what() << endl;
  146. }
  147. }