-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample1.cpp
More file actions
26 lines (19 loc) · 857 Bytes
/
example1.cpp
File metadata and controls
26 lines (19 loc) · 857 Bytes
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
#include <CppLinuxSerial/SerialPort.hpp>
using namespace mn::CppLinuxSerial;
int main() {
// Create serial port object and open serial port at 57600 buad, 8 data bits, no parity bit, and one stop bit (8n1)
SerialPort serialPort("/dev/ttyUSB0", BaudRate::B_9600, NumDataBits::EIGHT, Parity::NONE, NumStopBits::ONE);
// Use SerialPort serialPort("/dev/ttyACM0", 13000); instead if you want to provide a custom baud rate
serialPort.SetTimeout(-1); // Block when reading until any data is received
serialPort.Open();
// Write some ASCII data
// serialPort.Write("Hello");
// Read some data back (will block until at least 1 byte is received due to the SetTimeout(-1) call above)
std::string readData;
while(true){
serialPort.Read(readData);
std::cout<< readData<<std::endl;
}
// Close the serial port
serialPort.Close();
}