In using the SerialTransfer.h / pySerialTransfer libraries to send commands between my laptop and an Arduino Mega, I am sending multiple sequential strings between the devices. However, I'm having trouble "clearing" the receiving buffer on the Arduino side - it never shrinks, despite a few different attempts to clear the string (memset; a for loop).
I'm sending a string to the Arduino of the format , where there are anywhere from 1 to 5 asterisks. Then the Arduino is simply sending the exact same string back, without any processing (as of yet). And in the python terminal, I'm printing it all out; this is what I get:
SENT (17 bytes): <rand_len_str=**>
RECD (17 bytes): <rand_len_str=**>
SENT (18 bytes): <rand_len_str=***>
RECD (18 bytes): <rand_len_str=***>
SENT (19 bytes): <rand_len_str=****>
RECD (19 bytes): <rand_len_str=****>
SENT (17 bytes): <rand_len_str=**>
RECD (19 bytes): <rand_len_str=**>*>
SENT (18 bytes): <rand_len_str=***>
RECD (19 bytes): <rand_len_str=***>>
SENT (18 bytes): <rand_len_str=***>
RECD (19 bytes): <rand_len_str=***>>
As long as the string is growing, all is well - but see the error in the 4th pair, with the extra *> characters, etc.
The following is my Arduino code; I've tried to reset the receiving string using memset, but this seems not to be working.
#include "SerialTransfer.h"
SerialTransfer myTransfer;
const int CMD_LEN = 200;
char buff[CMD_LEN];
char received_str[CMD_LEN];
void setup()
{
Serial.begin(115200);
myTransfer.begin(Serial);
}
void loop()
{
if(myTransfer.available())
{
//////////////////////////////////////////////
// handle call from Python
memset(received_str, '\0', CMD_LEN*sizeof(char));
myTransfer.rxObj(received_str, CMD_LEN);
sprintf(buff, "%s", received_str);
//////////////////////////////////////////////
//////////////////////////////////////////////
// send response
myTransfer.txObj(buff, strlen(buff));
myTransfer.sendData(strlen(buff));
//////////////////////////////////////////////
}
else if(myTransfer.status < 0)
{
Serial.print("ERROR: ");
if(myTransfer.status == -1)
Serial.println(F("CRC_ERROR"));
else if(myTransfer.status == -2)
Serial.println(F("PAYLOAD_ERROR"));
else if(myTransfer.status == -3)
Serial.println(F("STOP_BYTE_ERROR"));
}
}
I didn't see anything in SerialTransfer.h about clearing the rxObj; I can always just use the first terminating > character to parse the command - but I imagine there is a cleaner / better solution in clearing the buffer, or at least, I'd like to understand what's going on! Thanks in advance.