Can't establish Serial connection after PlatformIO build

I’ve recently installed PlatformIO on my Raspberry PI 3 which I’m using to compile my program onto my Arduino Nano (Atmega328). The build routine runs just fine, but when I tried to access the Serial port via termios on my Raspi it doesn’t recieve data anymore. After a reboot everything just works fine again and when I use the CLI from Arduino itself it works just fine as well (but it’s terribly slow to compile :tired_face: ). So it seems that the platformio run -t upload command doesn’t close the Serial stream after uploading… The strange thing is however, that the platformio monitor just works fine after a build. The code I’m using consists mainly of these parts:

On the Arduino

#include "RSerial.h"

uint32 RSerial::_baudRate = 9600;
bool RSerial::_connected = false;
uint16 RSerial::_msgSize = 255;

RSerial::RSerial()
{

}

RSerial::~RSerial()
{

}

bool RSerial::Connect(uint32 baudRate)
{
	_baudRate = baudRate;
	Serial.begin(baudRate);
	_connected = true;
	return _connected;
}

void RSerial::Write(Reon_Msg message)
{
	if (_connected)
	{
		//DebugPrint("RSerial: " + string((char*)message.msg));
		Serial.write(message.msg, message.size);
	} 
	else
	{
		//DebugPrint("RSerial - not connected");
	}
}

void RSerial::Read(void (*callback) (Reon_Msg))
{
	if (_connected)
	{
		Reon_Msg message;
		byte* buf = new byte[_msgSize];
		DebugPrint("RSerial: read next...");
		while (Serial.available() == 0);
		uint32 size = Serial.readBytes(buf, _msgSize);
		message.size = size;
		message.msg = buf;
		
		callback(message);
	}
}

On the Raspi 3

void Serial::Read(std::function<void(Reon_Msg)> callback)
{
	//----- CHECK FOR ANY RX BYTES -----
	char rxBuffer[MSG_SIZE] = {0};
	//msg.msg = rxBuffer;

    if (_uartFilestream != -1)
    {
        int rxLength = read(_uartFilestream, (void*)rxBuffer, MSG_SIZE);        //Filestream, buffer to store in, number of bytes to read (MSG_SIZE)
        if (rxLength < 0)
        {
            //NOTE: if this occurs settings of serial com is broken --> non blocking
            cout << "no bytes recieved" << endl;
        }
        else if (rxLength == 0)
        {
            //No data waiting
            cout << "no data...waiting" << endl;
        }
        else
        {
            char* newMsgPtr = rxBuffer;
            for (int i=0; i < rxLength; i++)
            {
                //cout << "next: " << (int)rxBuffer[i] << endl;
                if (rxBuffer[i] == 0xFF)
                {
                    _closeBytes++;
                    //cout << "close bytes: " << _closeBytes << endl;
                    if (_closeBytes == 1)
                    {
                        Reon_Msg msg;
                        //cout << "in" << endl;
                        std::memcpy(_currBufferPtr, newMsgPtr, i+1 - (newMsgPtr-rxBuffer));
                        _currBufferPtr += i+1 - (newMsgPtr-rxBuffer);
                        /*for (char* i=&_buffer[0]; i < _currBufferPtr; i++)
                        {
                            cout << (int)*i << endl;
                        }*/
                        msg.size = _currBufferPtr - &_buffer[0] - 1;
                        msg.msg = new char[msg.size];
                        std::memcpy(msg.msg, &_buffer[0], msg.size);
                        callback(msg);
                        _closeBytes = 0;
                        _currBufferPtr = &_buffer[0];
                        //cout << "reset to: " << (int)*_currBufferPtr << endl;
                        //TODO: check not necessary can check below
                        if (i == rxLength - 1)
                        {
                            //cout << "null" << endl;
                            newMsgPtr = nullptr;
                        }
                        else
                        {
                            //cout << "not null" << endl;
                            newMsgPtr = &rxBuffer[i+1];
                        }
        
                    }
                }
            }
            if (_closeBytes < 2)
            {
                if (newMsgPtr != nullptr)
                {
                    std::memcpy(_currBufferPtr, newMsgPtr, (&rxBuffer[rxLength]) - newMsgPtr);
                    _currBufferPtr += (&rxBuffer[rxLength]) - newMsgPtr;
                    /*cout << "in save" << endl;
                    for (char* i=&_buffer[0]; i < _currBufferPtr; i++)
                    {
                        cout << "Save: " << (int)*i << endl;
                    }*/
                }
            }
            //cout << "end: -------------------" << endl;

            //Bytes received

        }
    }
    else
    {
        cerr << "Device " << _uartDevice << " disconnected!" << endl;
    }
    
}

I know it’s pretty messy and error prone but I’m in the early development stage so please don’t judge :slight_smile:

Thank you kindly in advance

Do you mean after rebooting of board or RPi?

A reboot of the Raspberry Pi fixes it but a reset of the arduino does nothing… I also tried to unbind and rebind the usb interface but that doesn’t work either

You can use $ screen command