Fix serial port priting gibberish
This commit is contained in:
parent
259e74a658
commit
2f22c89331
2 changed files with 11 additions and 9 deletions
|
@ -1,30 +1,29 @@
|
|||
#include <SerialCommunicator.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);
|
||||
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();
|
||||
}
|
|
@ -1,10 +1,13 @@
|
|||
#include <Communicator.h>
|
||||
#include <HardwareSerial.h>
|
||||
|
||||
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();
|
||||
|
|
Reference in a new issue