MCPcopy Create free account
hub / github.com/crayzeewulf/libserial / main

Function main

examples/serial_stream_read_write.cpp:17–160  ·  view source on GitHub ↗

* @brief This example demonstrates multiple methods to read and write * serial stream data utilizing std::iostream functionality. */

Source from the content-addressed store, hash-verified

15 * serial stream data utilizing std::iostream functionality.
16 */
17int main()
18{
19 using namespace LibSerial ;
20 // Instantiate two SerialStream objects.
21 SerialStream serial_stream_1 ;
22 SerialStream serial_stream_2 ;
23
24 // Open the Serial Ports at the desired hardware devices.
25 serial_stream_1.Open("/dev/ttyUSB0") ;
26 serial_stream_2.Open("/dev/ttyUSB1") ;
27
28 // Verify that the serial ports opened.
29 if (!serial_stream_1.IsOpen() ||
30 !serial_stream_2.IsOpen())
31 {
32 std::cerr << "The serial ports did not open correctly." << std::endl ;
33 return EXIT_FAILURE ;
34 }
35
36 // Set the baud rates.
37 serial_stream_1.SetBaudRate(BaudRate::BAUD_115200) ;
38 serial_stream_2.SetBaudRate(BaudRate::BAUD_115200) ;
39
40 // Set the number of data bits.
41 serial_stream_1.SetCharacterSize(CharacterSize::CHAR_SIZE_8) ;
42 serial_stream_2.SetCharacterSize(CharacterSize::CHAR_SIZE_8) ;
43
44 // Turn off hardware flow control.
45 serial_stream_1.SetFlowControl(FlowControl::FLOW_CONTROL_NONE) ;
46 serial_stream_2.SetFlowControl(FlowControl::FLOW_CONTROL_NONE) ;
47
48 // Disable parity.
49 serial_stream_1.SetParity(Parity::PARITY_NONE) ;
50 serial_stream_2.SetParity(Parity::PARITY_NONE) ;
51
52 // Set the number of stop bits.
53 serial_stream_1.SetStopBits(StopBits::STOP_BITS_1) ;
54 serial_stream_2.SetStopBits(StopBits::STOP_BITS_1) ;
55
56 // Variables to store outgoing and incoming data.
57 std::string write_string_1 = "\"Do what you can, with what you have, where you are.\" - Theodore Roosevelt" ;
58 std::string write_string_2 = "\"Simplicity is prerequisite for reliability.\" - Edsger W. Dijkstra" ;
59
60 std::string read_string_1 = "" ;
61 std::string read_string_2 = "" ;
62
63 // Print to the terminal what will take place next.
64 std::cout << "\nUsing write() and read() for a specified number of "
65 << "bytes of data:" << std::endl ;
66
67 // Write a specified number of bytes of data.
68 serial_stream_1.write(write_string_1.c_str(), write_string_1.size()) ;
69 serial_stream_2.write(write_string_2.c_str(), write_string_2.size()) ;
70
71 // Wait until the data has actually been transmitted.
72 serial_stream_1.DrainWriteBuffer() ;
73 serial_stream_2.DrainWriteBuffer() ;
74

Callers

nothing calls this directly

Calls 9

OpenMethod · 0.45
IsOpenMethod · 0.45
SetBaudRateMethod · 0.45
SetCharacterSizeMethod · 0.45
SetFlowControlMethod · 0.45
SetParityMethod · 0.45
SetStopBitsMethod · 0.45
DrainWriteBufferMethod · 0.45
CloseMethod · 0.45

Tested by

no test coverage detected