Solved: Flutter serial-USB comm won't work after USB disconnection or power on/off. It works after sketch upload or opening serial monitor

I have an Arduino MEGA for data logging (pulse sensor at ADC) and sends it to a PC (windowds 10) via USB (which also powers Arduino) where a flutter program receives data (one direction communication) using a custom communication protocol (5 bytes header, 10 bytes data, 2 bytes footer). After sketch upload, it works OK. The Tx LED takes 2 seconds to start blinking, and the LED L is on immediatly. When connectng IDE serial monitor, it works OK. If it's working OK, after pressing reset button, it continues working OK.

uint8_t    DataPacket[17];
void setup()
{
  Serial.begin(115200);
  Serial.println("Intilaziting  ");
}
void loop()
{
  delay(400);
  DataPacket[0] = 0x0A;  // sync0
  DataPacket[1] = 0xFA;
  DataPacket[2] = 10;
  DataPacket[3] = 0;
  DataPacket[4] = 0x02;
  DataPacket[5] = 6;
  DataPacket[6] = 7;
  DataPacket[7] = 8;
  DataPacket[8] = 9;
  DataPacket[9] = 3;
  DataPacket[10] = 4;
  DataPacket[11] = 2;
  DataPacket[12] = 3;
  DataPacket[13] = 4;
  DataPacket[14] = 5;
  DataPacket[15] = 0x00;
  DataPacket[16] = 0x0b;
  for (int i = 0; i < 17; i++) Serial.write(DataPacket[i]); // transmit the data over USB
}

I have this problem (even with Arduino board with nothing connected to it and with the simplified sketch attached): if I power it off and then on (disconnecting USB and reconnecting), the flutter program receives an erroneous packet, it receives a two-byte or one byte packet: [1, 0], [1,1], [1], [0] for example.
Also, after powering it on: 1) the Tx LED takes 11 seconds to start blinking, 2) the LED L is not turning on. After pressing reset button, it continues working in this way.

The way to go back to the correct packet transmission is by starting the IDE serial monitor (and stopping it and reconnecting to flutter) or by re-uploading the sketch.
I've tried powering using Vin and got the same result.

Any ideas?
Maybe flow control? DTR / RTS control ? A capacitor on reset? I've tried a 22uF C since it's the only I got now, but no luck. Also, I've tried a pull-up and pull-down resistor on reset.
I've read in another post: "In the protocol setup in the terminal emulator and the vb program I changed the handshake from "None" to "RequestToSend" and that fixed it. Apparently the arduino was waiting for a request to send the data.", but I didn't find how to do it in the flutter program.

thankssss!! :upside_down_face:

Try waiting a bit after the Serial.begin() and attempting to print something over the serial.

1 Like

Thanks for repplying!

I've introduced a <wait(13000);> before and another after the <serial.begin();>. And <wait(800);> in the <loop()>

Same response...

That would indicate the problem lies at the other end of the connection.

1 Like

the problem lies at the other end of the connection.

I'm not sure about that, because the Arduino shows two different behaviours, even not connected to USB: 1) after uploading sketch (delays 2 sec to start program*) and 2) after powering off-on (delays 11 seconds to start program*) even when USB disconnected from the PC (powered by Vin)...
*here I blink an external led (not pin13 led) or see the Tx start blinking

At the other end, the PC program always does the same, it's only sniffing the serial port, regardless of the Arduino behaviour (after uploading or after power reset)....

Beside this, even if the problem lies at the other end (the PC program), I can save it by replicating what the IDE serial plotter does (if posssible), though I'm not sure. What's happening when opening the IDE serial plotter? The IDE sends bytes? There's a reset? what's the difference between this reset and power on/off reset?why The LED at pin13 blinks?

Thank you Paul!

try

    while (!Serial);

while (!Serial);

I've tried this, and other combinations without luck

void setup()
{
  delay(13000);
  while (!Serial);
  Serial.begin(115200);//115200
  delay(13000);
  while (!Serial);
  Serial.println("Intilaziting  ");
  while (!Serial);
}

The issue is summerized here:
After uploading sketch or connecting IDE's serial monitor, the Tx LED is ON, and the communicationwith the PC (using my flutter program) is OK (it can be stopped and resumed indeed)

But after disconnecting USB (keeping power via Vin or just resetting power), the Tx LED turns off, and after reconnecting USB, the Tx LED blinks (at the rate in my sketch, defined by <delay(400);> in <loop()> ), and the communicationwith the PC (using my flutter program) is erroneous (it receives two or one byte packet: [1, 0], [1,1], [1], [0] instead of the 17 bytes packet).

I think this info on Tx LED can give a hint!

Thank you all!

If your flutter program uses a custom protocol with 17 bytes packets, how can it be that it received packets of 1 and 2 bytes?
This clearly indicates that this program is written incorrectly. If the protocol describes packets of 5 bytes header and 2 bytes footer - any packets shorter than 7 bytes should be discarded.

Judging by the error description, your PC program lacks read synchronization - that is, the program does not wait for a legal packet header, but reads everything in a row. When you restart the Arduino, the PC program reads the packet from its beginning and everything works. After turning it on and off, the synchronization is lost and the program reads the packet from a random position, so the data is received with an error.
This again indicates that the PC program is written incorrectly

1 Like

Hi b707!

The situation is not as you describe.

After unplugging USB, the flutter program cannot synch because it never reads the header. It only receives erroneous data [1], [1,0], etc.

If resetting the Arduino, the situation is the same.

The synch is only possible after uploading sketch or connecting serial monitor from IDE and at the same UART speed than that of the serial in the arduino, i.e. not just connecting the serial monitor.

I believe it could be the serial2USB converter in Arduino. Can I drive it by Hardware or Software?

Thanks!

Doesn't this indicate a bug in the program? it seems to be obvious to me.

I don't understand this. Did you try to connect your flutter program and Arduino Serial Monitor to the same USB port at the same time?

1 Like

When flutter inherits USB-serial config from serial monitor, the communitcation works fine.

But when I restart the USB (disconnecting and reconnecting), flutter USB-config has to be set. So I set flutter and Arduino the same serial config: baudrate (9600 or 115200), parity (no), stopBits (1), bits (8) and DTR, etc, and also tried other values).

Here flutter reads erroneus data, so it can't synch. If Arduino sends data from 0 to 64, flutter reads correctly from 0 to 31 (0x1F), and then it splits 32 into [1, 30], 33 into [2, 30], and so on... it seems it reads 5 bits, and splits one byte into two...

I had to look up flutter:

SOLVED!
flutter serial port needs to be configured! This is the code that worked:

   var portConfig = SerialPortConfig()
                            ..baudRate = 115200
                            ..bits = 8
                            ..stopBits = 1;
                          _serialPort.config = portConfig;

this code didn't work (for the future generations! hehe):

                      _serialPort.config.baudRate = 115200;
                      _serialPort.config.parity = 0; 
                      _serialPort.config.bits = 8;
                      _serialPort.config.stopBits = 1;

My last question is why Arduino takes 11 sec to start sending data...

Thank you all!

There is always a reset in order to synchronize the two devices. The alternative is a master/slave pairing with the master continuing to ask the slave for a begin message, then continue with the synched messaging.