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

Function main

examples/serial_stream_read.cpp:15–64  ·  view source on GitHub ↗

* @brief This example demonstrates configuring a serial stream and * reading serial stream data. */

Source from the content-addressed store, hash-verified

13 * reading serial stream data.
14 */
15int main()
16{
17 using namespace LibSerial ;
18 // Instantiate a SerialStream object.
19 SerialStream serial_stream ;
20
21 // Open the Serial Port at the desired hardware port.
22 serial_stream.Open("/dev/ttyUSB0") ;
23
24 // Set the baud rate of the serial port.
25 serial_stream.SetBaudRate(BaudRate::BAUD_115200) ;
26
27 // Set the number of data bits.
28 serial_stream.SetCharacterSize(CharacterSize::CHAR_SIZE_8) ;
29
30 // Turn off hardware flow control.
31 serial_stream.SetFlowControl(FlowControl::FLOW_CONTROL_NONE) ;
32
33 // Disable parity.
34 serial_stream.SetParity(Parity::PARITY_NONE) ;
35
36 // Set the number of stop bits.
37 serial_stream.SetStopBits(StopBits::STOP_BITS_1) ;
38
39 // Wait for data to be available at the serial port.
40 while(serial_stream.rdbuf()->in_avail() == 0)
41 {
42 usleep(1000) ;
43 }
44
45 // Keep reading data from serial port and print it to the screen.
46 while(serial_stream.IsDataAvailable())
47 {
48 // Variable to store data coming from the serial port.
49 char data_byte ;
50
51 // Read a single byte of data from the serial port.
52 serial_stream.get(data_byte) ;
53
54 // Show the user what is being read from the serial port.
55 std::cout << data_byte ;
56
57 // Wait a brief period for more data to arrive.
58 usleep(1000) ;
59 }
60
61 // Successful program completion.
62 std::cout << "Done." << std::endl ;
63 return EXIT_SUCCESS ;
64}

Callers

nothing calls this directly

Calls 7

OpenMethod · 0.45
SetBaudRateMethod · 0.45
SetCharacterSizeMethod · 0.45
SetFlowControlMethod · 0.45
SetParityMethod · 0.45
SetStopBitsMethod · 0.45
IsDataAvailableMethod · 0.45

Tested by

no test coverage detected