-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split the hardware and software flow control settings. Added an example.
- Loading branch information
Showing
5 changed files
with
111 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,45 @@ | ||
#include <chrono> | ||
#include <thread> | ||
|
||
#include "CppLinuxSerial/SerialPort.hpp" | ||
|
||
using namespace std::chrono_literals; | ||
using namespace mn::CppLinuxSerial; | ||
|
||
int main() { | ||
SerialPort serialPort("/dev/ttyUSB0", BaudRate::B_9600, NumDataBits::EIGHT, Parity::NONE, NumStopBits::ONE, FlowControl::NONE); | ||
serialPort.SetTimeout(-1); // Block when reading until any data is received | ||
// This example relies on a serial device which echos serial data at 9600 baud, 8n1. | ||
std::cout << "FlowControll.cpp::main() called." << std::endl; | ||
SerialPort serialPort("/dev/ttyACM0", BaudRate::B_9600, NumDataBits::EIGHT, Parity::NONE, NumStopBits::ONE, HardwareFlowControl::ON, SoftwareFlowControl::OFF); | ||
serialPort.SetTimeout(1000); // Block when reading for 1000ms | ||
|
||
serialPort.Open(); | ||
serialPort.Write("Hello"); | ||
std::string readData; | ||
serialPort.Read(readData); | ||
std::cout << "readData: " << readData << std::endl; | ||
|
||
std::this_thread::sleep_for(100ms); | ||
|
||
std::thread t1([&]() { | ||
// Do Something | ||
for (int x = 0; x < 10; x++) { | ||
// std::this_thread::sleep_for(100ms); | ||
std::cout << "Reading" << std::endl; | ||
std::string readData; | ||
serialPort.Read(readData); | ||
std::cout << "readData: " << readData << std::endl; | ||
} | ||
}); | ||
|
||
std::thread t2([&]() { | ||
// Do Something | ||
std::this_thread::sleep_for(100ms); | ||
for (int x = 0; x < 10; x++) { | ||
std::this_thread::sleep_for(100ms); | ||
std::cout << "Writing \"Hello\"" << std::endl; | ||
serialPort.Write("Hello"); | ||
} | ||
}); | ||
|
||
t1.join(); | ||
t2.join(); | ||
|
||
serialPort.Close(); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
/// \file SerialPort.hpp | ||
/// \author Geoffrey Hunter <[email protected]> (www.mbedded.ninja) | ||
/// \created 2014-01-07 | ||
/// \last-modified 2019-05-30 | ||
/// \last-modified 2022-11-12 | ||
/// \brief The main serial port class. | ||
/// \details | ||
/// See README.rst in repo root dir for more info. | ||
|
@@ -81,11 +81,16 @@ namespace mn { | |
TWO, | ||
}; | ||
|
||
/// \brief All the possible options for setting the flow control. | ||
enum class FlowControl { | ||
NONE, | ||
HARDWARE, | ||
SOFTWARE, | ||
/// \brief All the possible options for setting the hardware flow control. | ||
enum class HardwareFlowControl { | ||
OFF, | ||
ON, | ||
}; | ||
|
||
/// \brief All the possible options for setting the software flow control. | ||
enum class SoftwareFlowControl { | ||
OFF, | ||
ON, | ||
}; | ||
|
||
/// \brief Represents the state of the serial port. | ||
|
@@ -108,7 +113,8 @@ namespace mn { | |
SerialPort(const std::string &device, BaudRate baudRate, NumDataBits numDataBits, Parity parity, NumStopBits numStopBits); | ||
|
||
/// \brief Constructor that sets up serial port and allows the user to specify all the common parameters and flow control. | ||
SerialPort(const std::string &device, BaudRate baudRate, NumDataBits numDataBits, Parity parity, NumStopBits numStopBits, FlowControl flow); | ||
SerialPort(const std::string &device, BaudRate baudRate, NumDataBits numDataBits, Parity parity, | ||
NumStopBits numStopBits, HardwareFlowControl hardwareFlowControl, SoftwareFlowControl softwareFlowControl); | ||
|
||
/// \brief Constructor that sets up serial port with the basic parameters, and a custom baud rate. | ||
SerialPort(const std::string &device, speed_t baudRate); | ||
|
@@ -226,8 +232,11 @@ namespace mn { | |
/// \brief The num. of stop bits. Defaults to 1 (most common). | ||
NumStopBits numStopBits_ = NumStopBits::ONE; | ||
|
||
/// \brief The flow control of the system. Defaults to None (most common). | ||
FlowControl flowControl_ = FlowControl::NONE; | ||
/// \brief The hardware flow control setting. Defaults to OFF (most common). | ||
HardwareFlowControl hardwareFlowControl_ = HardwareFlowControl::OFF; | ||
|
||
/// \brief The software flow control setting. Defaults to OFF (most common). | ||
SoftwareFlowControl softwareFlowControl_ = SoftwareFlowControl::OFF; | ||
|
||
/// \brief The file descriptor for the open file. This gets written to when Open() is called. | ||
int fileDesc_; | ||
|
@@ -243,7 +252,6 @@ namespace mn { | |
static constexpr int32_t defaultTimeout_ms_ = -1; | ||
static constexpr unsigned char defaultReadBufferSize_B_ = 255; | ||
|
||
|
||
}; | ||
|
||
} // namespace CppLinuxSerial | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters