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

Function main

examples/serial_stream_write.cpp:17–86  ·  view source on GitHub ↗

* @brief This example reads the contents of a file and writes the entire * file to the serial port one character at a time. To use this * example, simply utilize TestFile.txt or another file of your * choosing as a command line argument. */

Source from the content-addressed store, hash-verified

15 * choosing as a command line argument.
16 */
17int main(int argc, char** argv)
18{
19 using namespace LibSerial ;
20 // Determine if an appropriate number of arguments has been provided.
21 if (argc < 2)
22 {
23 // Error message to the user.
24 std::cerr << "Usage: " << argv[0] << " <filename>" << std::endl ;
25
26 // Exit the program if no input file argument has been given.
27 return 1 ;
28 }
29
30 // Open the input file for reading.
31 std::ifstream input_file(argv[1]) ;
32
33 // Determine if the input file argument is valid to read data from.
34 if (!input_file.good())
35 {
36 std::cerr << "Error: Could not open file "
37 << argv[1] << " for reading." << std::endl ;
38 return 1 ;
39 }
40
41 // Instantiate a SerialStream object.
42 SerialStream serial_stream ;
43
44 // Open the Serial Port at the desired hardware port.
45 serial_stream.Open("/dev/ttyUSB1") ;
46
47 // Set the baud rate of the serial port.
48 serial_stream.SetBaudRate(BaudRate::BAUD_115200) ;
49
50 // Set the number of data bits.
51 serial_stream.SetCharacterSize(CharacterSize::CHAR_SIZE_8) ;
52
53 // Turn off hardware flow control.
54 serial_stream.SetFlowControl(FlowControl::FLOW_CONTROL_NONE) ;
55
56 // Disable parity.
57 serial_stream.SetParity(Parity::PARITY_NONE) ;
58
59 // Set the number of stop bits.
60 serial_stream.SetStopBits(StopBits::STOP_BITS_1) ;
61
62 // Read characters from the input file and write them to the serial port.
63 std::cout << "Writing input file contents to the serial port." << std::endl ;
64
65 // Create a variable to store data from the input file and write to the serial port.
66 char data_byte = 0 ;
67
68 while (input_file)
69 {
70 // Read data from the input file.
71 input_file.read(&data_byte, 1) ;
72
73 // Write the data to the serial port.
74 serial_stream.write(&data_byte, 1) ;

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
DrainWriteBufferMethod · 0.45

Tested by

no test coverage detected