File: example_project.cpp

package info (click to toggle)
libserial 1.0.0%2Bgit20250903-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,220 kB
  • sloc: cpp: 6,140; makefile: 278; python: 163; sh: 29
file content (65 lines) | stat: -rw-r--r-- 1,974 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/**
 *  @example example_project.cpp
 */

#include <libserial/SerialPort.h>
#include <libserial/SerialStream.h>

#include <iostream>

int main()
{
    using LibSerial::SerialPort ;
    using LibSerial::SerialStream ;

    // You can instantiate a Serial Port or a Serial Stream object, whichever you'd prefer to work with.
    // For this example, we will demonstrate by using both types of objects.
    SerialPort serial_port ;
    SerialStream serial_stream ;

    // Open hardware serial ports using the Open() method.
    serial_port.Open( "/dev/ttyUSB0" ) ;
    serial_stream.Open( "/dev/ttyUSB1" ) ;

    // Set the baud rates.
    using LibSerial::BaudRate ;
    serial_port.SetBaudRate( BaudRate::BAUD_115200 ) ;
    serial_stream.SetBaudRate( BaudRate::BAUD_115200 ) ;

    // Create a few variables with data we can send.
    char write_byte_1 = 'a' ;
    char write_byte_2 = 'b' ;

    char read_byte_1 = 'A' ;
    char read_byte_2 = 'B' ;

    // Read a byte to the serial port using SerialPort Write() methods.
    serial_port.WriteByte(write_byte_1) ;

    // With SerialStream objects you can read/write to the port using iostream operators.
    serial_stream << write_byte_2 ;

    // Specify a timeout value (in milliseconds).
    size_t timeout_milliseconds = 25 ;

    using LibSerial::ReadTimeout ;
    try
    {
        // Read a byte from the serial port using SerialPort Read() methods.
        serial_port.ReadByte(read_byte_1, timeout_milliseconds) ;

        // With SerialStream objects you can read/write to the port using iostream operators.
        serial_stream >> read_byte_2 ;
    }
    catch (const ReadTimeout&)
    {
        std::cerr << "The Read() call has timed out." << std::endl ;
    }

    std::cout << "serial_port read:   " << read_byte_1 << std::endl ;
    std::cout << "serial_stream read: " << read_byte_2 << std::endl ;

    // Close the Serial Port and Serial Stream.
    serial_port.Close() ;
    serial_stream.Close() ;
}