LibSerial 1.0.0
LibSerial provides a convenient, object oriented approach to accessing serial ports on POSIX systems.
Loading...
Searching...
No Matches
serial_stream_write.cpp
1
5#include <libserial/SerialStream.h>
6
7#include <cstdlib>
8#include <fstream>
9#include <iostream>
10
11constexpr const char* const SERIAL_PORT_2 = "/dev/ttyUSB1" ;
12
13
20int main(int argc, char** argv)
21{
22 using namespace LibSerial ;
23 // Determine if an appropriate number of arguments has been provided.
24 if (argc < 2)
25 {
26 // Error message to the user.
27 std::cerr << "Usage: " << argv[0] << " <filename>" << std::endl ;
28
29 // Exit the program if no input file argument has been given.
30 return 1 ;
31 }
32
33 // Open the input file for reading.
34 std::ifstream input_file(argv[1]) ;
35
36 // Determine if the input file argument is valid to read data from.
37 if (!input_file.good())
38 {
39 std::cerr << "Error: Could not open file "
40 << argv[1] << " for reading." << std::endl ;
41 return 1 ;
42 }
43
44 // Instantiate a SerialStream object.
45 SerialStream serial_stream ;
46
47 try
48 {
49 // Open the Serial Port at the desired hardware port.
50 serial_stream.Open(SERIAL_PORT_2) ;
51 }
52 catch (const OpenFailed&)
53 {
54 std::cerr << "The serial port did not open correctly." << std::endl ;
55 return EXIT_FAILURE ;
56 }
57
58 // Set the baud rate of the serial port.
59 serial_stream.SetBaudRate(BaudRate::BAUD_115200) ;
60
61 // Set the number of data bits.
62 serial_stream.SetCharacterSize(CharacterSize::CHAR_SIZE_8) ;
63
64 // Turn off hardware flow control.
65 serial_stream.SetFlowControl(FlowControl::FLOW_CONTROL_NONE) ;
66
67 // Disable parity.
68 serial_stream.SetParity(Parity::PARITY_NONE) ;
69
70 // Set the number of stop bits.
71 serial_stream.SetStopBits(StopBits::STOP_BITS_1) ;
72
73 // Read characters from the input file and write them to the serial port.
74 std::cout << "Writing input file contents to the serial port." << std::endl ;
75
76 // Create a variable to store data from the input file and write to the serial port.
77 char data_byte = 0 ;
78
79 while (input_file)
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_stream.write(&data_byte, 1) ;
86
87 // Wait until the data has actually been transmitted.
88 serial_stream.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.
SerialStream is a stream class for accessing serial ports on POSIX operating systems....
void DrainWriteBuffer()
Waits until the write buffer is drained and then returns.
void SetCharacterSize(const CharacterSize &characterSize)
Sets the character size for the serial port.
void SetBaudRate(const BaudRate &baudRate)
Sets the baud rate for the serial port to the specified value.
void SetFlowControl(const FlowControl &flowControlType)
Sets flow control for the serial port.
void SetStopBits(const StopBits &stopBits)
Sets the number of stop bits to be used with 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 SetParity(const Parity &parityType)
Sets the parity type for the serial port.