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.
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.
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?
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).
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
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?
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...
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.