From b8dadd82d4afff0e5e6b6a8a00df948045d31503 Mon Sep 17 00:00:00 2001 From: Simon Gagne Date: Thu, 16 Mar 2023 10:06:04 -0400 Subject: Commit pour deplacement vers VSStudio --- include/serial/SerialPort.cpp | 131 ++++++++++++++++++++++++++++++++++++++++++ include/serial/SerialPort.hpp | 31 ++++++++++ 2 files changed, 162 insertions(+) create mode 100644 include/serial/SerialPort.cpp create mode 100644 include/serial/SerialPort.hpp (limited to 'include/serial') diff --git a/include/serial/SerialPort.cpp b/include/serial/SerialPort.cpp new file mode 100644 index 0000000..2fff251 --- /dev/null +++ b/include/serial/SerialPort.cpp @@ -0,0 +1,131 @@ +/* +* Author: Manash Kumar Mandal +* Modified Library introduced in Arduino Playground which does not work +* This works perfectly +* LICENSE: MIT +*/ + +#include "SerialPort.hpp" + +SerialPort::SerialPort(const char *portName, int BAUD) +{ + this->connected = false; + + this->handler = CreateFileA(static_cast(portName), + GENERIC_READ | GENERIC_WRITE, + 0, + NULL, + OPEN_EXISTING, + FILE_ATTRIBUTE_NORMAL, + NULL); + if (this->handler == INVALID_HANDLE_VALUE) + { + if (GetLastError() == ERROR_FILE_NOT_FOUND) + { + std::cerr << "ERROR: Handle was not attached.Reason : " << portName << " not available\n"; + } + else + { + std::cerr << "ERROR!!!\n"; + } + } + else + { + DCB dcbSerialParameters = {0}; + + if (!GetCommState(this->handler, &dcbSerialParameters)) + { + std::cerr << "Failed to get current serial parameters\n"; + } + else + { + dcbSerialParameters.BaudRate = BAUD; + dcbSerialParameters.ByteSize = 8; + dcbSerialParameters.StopBits = ONESTOPBIT; + dcbSerialParameters.Parity = NOPARITY; + dcbSerialParameters.fDtrControl = DTR_CONTROL_ENABLE; + + if (!SetCommState(handler, &dcbSerialParameters)) + { + std::cout << "ALERT: could not set serial port parameters\n"; + } + else + { + this->connected = true; + PurgeComm(this->handler, PURGE_RXCLEAR | PURGE_TXCLEAR); + Sleep(ARDUINO_WAIT_TIME); + } + } + } +} + +SerialPort::~SerialPort() +{ + if (this->connected) + { + this->connected = false; + CloseHandle(this->handler); + } +} + +// Reading bytes from serial port to buffer; +// returns read bytes count, or if error occurs, returns 0 +int SerialPort::readSerialPort(const char *buffer, unsigned int buf_size) +{ + DWORD bytesRead{}; + unsigned int toRead = 0; + + ClearCommError(this->handler, &this->errors, &this->status); + + if (this->status.cbInQue > 0) + { + if (this->status.cbInQue > buf_size) + { + toRead = buf_size; + } + else + { + toRead = this->status.cbInQue; + } + } + + memset((void*) buffer, 0, buf_size); + + if (ReadFile(this->handler, (void*) buffer, toRead, &bytesRead, NULL)) + { + return bytesRead; + } + + return 0; +} + +// Sending provided buffer to serial port; +// returns true if succeed, false if not +bool SerialPort::writeSerialPort(const char *buffer, unsigned int buf_size) +{ + DWORD bytesSend; + + if (!WriteFile(this->handler, (void*) buffer, buf_size, &bytesSend, 0)) + { + ClearCommError(this->handler, &this->errors, &this->status); + return false; + } + + return true; +} + +// Checking if serial port is connected +bool SerialPort::isConnected() +{ + if (!ClearCommError(this->handler, &this->errors, &this->status)) + { + this->connected = false; + } + + return this->connected; +} + +void SerialPort::closeSerial() +{ + CloseHandle(this->handler); +} \ No newline at end of file diff --git a/include/serial/SerialPort.hpp b/include/serial/SerialPort.hpp new file mode 100644 index 0000000..a207d0c --- /dev/null +++ b/include/serial/SerialPort.hpp @@ -0,0 +1,31 @@ +/* +* Author: Manash Kumar Mandal +* Modified Library introduced in Arduino Playground which does not work +* This works perfectly +* LICENSE: MIT +*/ + +#pragma once + +#define ARDUINO_WAIT_TIME 2000 +#define MAX_DATA_LENGTH 255 + +#include +#include + +class SerialPort +{ +private: + HANDLE handler; + bool connected; + COMSTAT status; + DWORD errors; +public: + explicit SerialPort(const char *portName, int BAUD); + ~SerialPort(); + + int readSerialPort(const char *buffer, unsigned int buf_size); + bool writeSerialPort(const char *buffer, unsigned int buf_size); + bool isConnected(); + void closeSerial(); +}; \ No newline at end of file -- cgit v1.2.3