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

Function main

examples/main_page_example.cpp:10–58  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

8#include <iostream>
9
10int main()
11{
12 using LibSerial::SerialPort ;
13 using LibSerial::SerialStream ;
14
15 // Instantiate a Serial Port and a Serial Stream object.
16 SerialPort serial_port ;
17 SerialStream serial_stream ;
18
19 // Open the hardware serial ports.
20 serial_port.Open( "/dev/ttyUSB0" ) ;
21 serial_stream.Open( "/dev/ttyUSB1" ) ;
22
23 // Set the baud rates.
24 using LibSerial::BaudRate ;
25 serial_port.SetBaudRate( BaudRate::BAUD_115200 ) ;
26 serial_stream.SetBaudRate( BaudRate::BAUD_115200 ) ;
27
28 char write_byte_1 = 'a' ;
29 char write_byte_2 = 'b' ;
30
31 char read_byte_1 = 'A' ;
32 char read_byte_2 = 'B' ;
33
34 // Write a character.
35 serial_port.WriteByte(write_byte_1) ;
36 serial_stream << write_byte_2 ;
37
38 size_t timeout_milliseconds = 5 ;
39
40 using LibSerial::ReadTimeout ;
41 try
42 {
43 // Read a character.
44 serial_port.ReadByte(read_byte_1, timeout_milliseconds) ;
45 serial_stream >> read_byte_2 ;
46 }
47 catch (const ReadTimeout&)
48 {
49 std::cerr << "The Read() call has timed out." << std::endl ;
50 }
51
52 std::cout << "serial_port read: " << read_byte_1 << std::endl ;
53 std::cout << "serial_stream read: " << read_byte_2 << std::endl ;
54
55 // Close the Serial Port and Serial Stream.
56 serial_port.Close() ;
57 serial_stream.Close() ;
58}

Callers

nothing calls this directly

Calls 5

WriteByteMethod · 0.80
ReadByteMethod · 0.80
OpenMethod · 0.45
SetBaudRateMethod · 0.45
CloseMethod · 0.45

Tested by

no test coverage detected