CC/communication/cpp_linux_serial/main.cpp

33 lines
906 B
C++
Raw Normal View History

2024-01-27 23:52:22 +03:00
#include <chrono>
#include <thread>
#include <CppLinuxSerial/SerialPort.hpp>
using namespace mn::CppLinuxSerial;
#define PORT "/dev/ttyACM0"
#define WAIT_SEC 3
int main(int argc, char const *argv[]) {
SerialPort serialPort(PORT, BaudRate::B_9600, NumDataBits::EIGHT,
Parity::NONE, NumStopBits::ONE);
serialPort.SetTimeout(100);
std::cout << "Opening " PORT "... " << std::flush;
serialPort.Open();
std::cout << "OK" << std::endl;
std::cout << "Waiting Arduino restart... " << std::flush;
std::this_thread::sleep_for(std::chrono::seconds(WAIT_SEC));
std::cout << "OK" << std::endl;
std::cout << "Sending string data... " << std::flush;
std::string txDataString = "d";
serialPort.Write(txDataString);
std::cout << "OK" << std::endl;
std::cout << "Clossing port... " << std::flush;
serialPort.Close();
std::cout << "OK" << std::endl;
return 0;
}