From 2f22c893310e814cbf7027867d14008bf0b15509 Mon Sep 17 00:00:00 2001 From: GHOSCHT <31184695+GHOSCHT@users.noreply.github.com> Date: Fri, 4 Feb 2022 14:04:13 +0100 Subject: [PATCH] Fix serial port priting gibberish --- .../lib/Communicator/SerialCommunicator.cpp | 13 ++++++------- .../Firmware/lib/Communicator/SerialCommunicator.h | 7 +++++-- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/Devices/Control/Firmware/lib/Communicator/SerialCommunicator.cpp b/Devices/Control/Firmware/lib/Communicator/SerialCommunicator.cpp index 5faf53a..16ff238 100644 --- a/Devices/Control/Firmware/lib/Communicator/SerialCommunicator.cpp +++ b/Devices/Control/Firmware/lib/Communicator/SerialCommunicator.cpp @@ -1,30 +1,29 @@ #include #include -SerialCommunicator::SerialCommunicator(int baudRate, int timeout, __SIZE_TYPE__ bufferSize) : Communicator(bufferSize) +SerialCommunicator::SerialCommunicator(HardwareSerial &p_out, int baudRate, int timeout, __SIZE_TYPE__ bufferSize) : Communicator(bufferSize), port(p_out) { - Serial.begin(baudRate); - Serial.setTimeout(timeout); + port.begin(baudRate); } void SerialCommunicator::sendMessage(int *values, int numberOfValues) { char message[calculateMessageOutSize(numberOfValues)]; parseIDs(values, numberOfValues, message); - Serial.println(message); + port.println(message); } void SerialCommunicator::sendMessage(char *message) { - Serial.println(message); + port.println(message); } char *SerialCommunicator::receiveMessage() { - if (Serial.available()) + if (port.available()) { memset(getBuffer(), '\0', getBufferSize()); - Serial.readBytesUntil('\n', getBuffer(), getBufferSize()); + port.readBytesUntil('\n', getBuffer(), getBufferSize()); } return getBuffer(); } \ No newline at end of file diff --git a/Devices/Control/Firmware/lib/Communicator/SerialCommunicator.h b/Devices/Control/Firmware/lib/Communicator/SerialCommunicator.h index d92abef..656cc07 100644 --- a/Devices/Control/Firmware/lib/Communicator/SerialCommunicator.h +++ b/Devices/Control/Firmware/lib/Communicator/SerialCommunicator.h @@ -1,10 +1,13 @@ #include +#include class SerialCommunicator : public Communicator { -private: +protected: + HardwareSerial &port; + public: - SerialCommunicator(int baudRate, int timeout, __SIZE_TYPE__ bufferSize); + SerialCommunicator(HardwareSerial &port, int baudRate, int timeout, __SIZE_TYPE__ bufferSize); void sendMessage(int *values, int numberOfValues); void sendMessage(char *message); char *receiveMessage();