LibSerial 1.0.0
LibSerial provides a convenient, object oriented approach to accessing serial ports on POSIX systems.
Loading...
Searching...
No Matches
serial_port_write.cpp
1
5#include <libserial/SerialPort.h>
6
7#include <cstdlib>
8#include <fstream>
9#include <iostream>
10
11constexpr const char* const SERIAL_PORT_2 = "/dev/ttyUSB1" ;
12
19int main(int argc, char** argv)
20{
21 using namespace LibSerial ;
22 // Determine if an appropriate number of arguments has been provided.
23 if (argc < 2)
24 {
25 // Error message to the user.
26 std::cerr << "Usage: " << argv[0] << " <filename>" << std::endl ;
27
28 // Exit the program if no input file argument has been given.
29 return 1 ;
30 }
31
32 // Open the input file for reading.
33 std::ifstream input_file(argv[1]) ;
34
35 // Determine if the input file argument is valid to read data from.
36 if (!input_file.good())
37 {
38 std::cerr << "Error: Could not open file "
39 << argv[1] << " for reading." << std::endl ;
40 return 1 ;
41 }
42
43 // Instantiate a SerialPort object.
44 SerialPort serial_port ;
45
46 try
47 {
48 // Open the Serial Port at the desired hardware port.
49 serial_port.Open(SERIAL_PORT_2) ;
50 }
51 catch (const OpenFailed&)
52 {
53 std::cerr << "The serial port did not open correctly." << std::endl ;
54 return EXIT_FAILURE ;
55 }
56
57 // Set the baud rate of the serial port.
58 serial_port.SetBaudRate(BaudRate::BAUD_115200) ;
59
60 // Set the number of data bits.
61 serial_port.SetCharacterSize(CharacterSize::CHAR_SIZE_8) ;
62
63 // Turn off hardware flow control.
64 serial_port.SetFlowControl(FlowControl::FLOW_CONTROL_NONE) ;
65
66 // Disable parity.
67 serial_port.SetParity(Parity::PARITY_NONE) ;
68
69 // Set the number of stop bits.
70 serial_port.SetStopBits(StopBits::STOP_BITS_1) ;
71
72 // Read characters from the input file and write them to the serial port.
73 std::cout << "Writing input file contents to the serial port." << std::endl ;
74
75 while (input_file)
76 {
77 // Create a variable to store data from the input file and write to the
78 // serial port.
79 char data_byte ;
80
81 // Read data from the input file.
82 input_file.read(&data_byte, 1) ;
83
84 // Write the data to the serial port.
85 serial_port.WriteByte(data_byte) ;
86
87 // Wait until the data has actually been transmitted.
88 serial_port.DrainWriteBuffer() ;
89
90 // Print to the terminal what is being written to the serial port.
91 std::cout << data_byte ;
92 }
93
94 // Successful program completion.
95 std::cout << "The example program successfully completed!" << std::endl ;
96 return EXIT_SUCCESS ;
97}
Exception error thrown when the serial port could not be opened.
SerialPort allows an object oriented approach to serial port communication. A serial port object can ...
Definition SerialPort.h:56
void SetBaudRate(const BaudRate &baudRate)
Sets the baud rate for the serial port to the specified value.
void SetParity(const Parity &parityType)
Sets the parity type for the serial port.
void WriteByte(char charbuffer)
Writes a single byte to the serial port.
void SetFlowControl(const FlowControl &flowControlType)
Sets flow control for the serial port.
void SetCharacterSize(const CharacterSize &characterSize)
Sets the character size for the serial port.
void Open(const std::string &fileName, const std::ios_base::openmode &openMode=std::ios_base::in|std::ios_base::out, bool exclusive=true)
Opens the serial port associated with the specified file name and the specified mode.
void DrainWriteBuffer()
Waits until the write buffer is drained and then returns.
void SetStopBits(const StopBits &stopBits)
Sets the number of stop bits to be used with the serial port.