| 8 | #include <iostream> |
| 9 | |
| 10 | int main() |
| 11 | { |
| 12 | using LibSerial::SerialPort ; |
| 13 | using LibSerial::SerialStream ; |
| 14 | |
| 15 | // You can instantiate a Serial Port or a Serial Stream object, whichever you'd prefer to work with. |
| 16 | // For this example, we will demonstrate by using both types of objects. |
| 17 | SerialPort serial_port ; |
| 18 | SerialStream serial_stream ; |
| 19 | |
| 20 | // Open hardware serial ports using the Open() method. |
| 21 | serial_port.Open( "/dev/ttyUSB0" ) ; |
| 22 | serial_stream.Open( "/dev/ttyUSB1" ) ; |
| 23 | |
| 24 | // Set the baud rates. |
| 25 | using LibSerial::BaudRate ; |
| 26 | serial_port.SetBaudRate( BaudRate::BAUD_115200 ) ; |
| 27 | serial_stream.SetBaudRate( BaudRate::BAUD_115200 ) ; |
| 28 | |
| 29 | // Create a few variables with data we can send. |
| 30 | char write_byte_1 = 'a' ; |
| 31 | char write_byte_2 = 'b' ; |
| 32 | |
| 33 | char read_byte_1 = 'A' ; |
| 34 | char read_byte_2 = 'B' ; |
| 35 | |
| 36 | // Read a byte to the serial port using SerialPort Write() methods. |
| 37 | serial_port.WriteByte(write_byte_1) ; |
| 38 | |
| 39 | // With SerialStream objects you can read/write to the port using iostream operators. |
| 40 | serial_stream << write_byte_2 ; |
| 41 | |
| 42 | // Specify a timeout value (in milliseconds). |
| 43 | size_t timeout_milliseconds = 25 ; |
| 44 | |
| 45 | using LibSerial::ReadTimeout ; |
| 46 | try |
| 47 | { |
| 48 | // Read a byte from the serial port using SerialPort Read() methods. |
| 49 | serial_port.ReadByte(read_byte_1, timeout_milliseconds) ; |
| 50 | |
| 51 | // With SerialStream objects you can read/write to the port using iostream operators. |
| 52 | serial_stream >> read_byte_2 ; |
| 53 | } |
| 54 | catch (const ReadTimeout&) |
| 55 | { |
| 56 | std::cerr << "The Read() call has timed out." << std::endl ; |
| 57 | } |
| 58 | |
| 59 | std::cout << "serial_port read: " << read_byte_1 << std::endl ; |
| 60 | std::cout << "serial_stream read: " << read_byte_2 << std::endl ; |
| 61 | |
| 62 | // Close the Serial Port and Serial Stream. |
| 63 | serial_port.Close() ; |
| 64 | serial_stream.Close() ; |
| 65 | } |
nothing calls this directly
no test coverage detected