Fix serial port priting gibberish

This commit is contained in:
GHOSCHT 2022-02-04 14:04:13 +01:00
parent 259e74a658
commit 2f22c89331
No known key found for this signature in database
GPG key ID: A35BD466B8871994
2 changed files with 11 additions and 9 deletions

View file

@ -1,30 +1,29 @@
#include <SerialCommunicator.h> #include <SerialCommunicator.h>
#include <HardwareSerial.h> #include <HardwareSerial.h>
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); port.begin(baudRate);
Serial.setTimeout(timeout);
} }
void SerialCommunicator::sendMessage(int *values, int numberOfValues) void SerialCommunicator::sendMessage(int *values, int numberOfValues)
{ {
char message[calculateMessageOutSize(numberOfValues)]; char message[calculateMessageOutSize(numberOfValues)];
parseIDs(values, numberOfValues, message); parseIDs(values, numberOfValues, message);
Serial.println(message); port.println(message);
} }
void SerialCommunicator::sendMessage(char *message) void SerialCommunicator::sendMessage(char *message)
{ {
Serial.println(message); port.println(message);
} }
char *SerialCommunicator::receiveMessage() char *SerialCommunicator::receiveMessage()
{ {
if (Serial.available()) if (port.available())
{ {
memset(getBuffer(), '\0', getBufferSize()); memset(getBuffer(), '\0', getBufferSize());
Serial.readBytesUntil('\n', getBuffer(), getBufferSize()); port.readBytesUntil('\n', getBuffer(), getBufferSize());
} }
return getBuffer(); return getBuffer();
} }

View file

@ -1,10 +1,13 @@
#include <Communicator.h> #include <Communicator.h>
#include <HardwareSerial.h>
class SerialCommunicator : public Communicator class SerialCommunicator : public Communicator
{ {
private: protected:
HardwareSerial &port;
public: 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(int *values, int numberOfValues);
void sendMessage(char *message); void sendMessage(char *message);
char *receiveMessage(); char *receiveMessage();