Go Down

Topic: (updated)Help on Serial communication between Arduino and PC!! (Read 1 time) previous topic - next topic

Nick Gammon

Good point. I didn't focus on the PC code. Still, he says it works on one device but not the other.

skyjumper


Good point. I didn't focus on the PC code. Still, he says it works on one device but not the other.


I think he meant to say the code on the Mega worked, but that on the PC does not. If he referenced a different Arduino device that is sending data then I missed it in his post. I think his issue is that the more data there is, the greater the odds that his receive code is going to empty the buffer before it all done being transmitted by the Mega.

Whatever the ultimate issue is, I would suggest that he (1) compute some minimal checksum to send with the data, (2) invent some kind of flag to mark the end of the data stream/packet of data and (3) compute the checksum of the received data and make sure the transmitted checksum matches. Exactly how to best do this will depend on the nature of the data he is sending.

Of course Nick knows all this, I am posting for the benefit of the original poster and the general audience.



Nick Gammon

I was about ready to suggest he got rid of this line:

Code: [Select]
    Serial.flush();


But I can't quite prove to my satisfaction that it is the cause of it.

skyjumper


I was about ready to suggest he got rid of this line:

Code: [Select]
    Serial.flush();


But I can't quite prove to my satisfaction that it is the cause of it.


I think he should eliminate that and the delay(100). Especially if he is using 1.0 which has interrupt driven serial transmission. All those lines do is slow down the sending. But even if he leaves them in, I think the receive code needs to be robust enough to cope with things like unexpected delays, bit errors that happen enroute and so on. That means he needs at least a minimal protocol.

He didn't show us enough of the receive code to to eliminate more ordinary programming errors though.

You're right that if he is on a pre-1.0 environment, that will clear the send buffer, meaning perhaps some data gets killed before it is sent. If he is on a 1.0 environment, all it will do is wait until the buffer is empty. No point to that or the delay();



dxw00d

Quote
on a pre-1.0 environment, that will clear the send buffer


Wrong buffer...

From http://arduino.cc/en/Serial/Flush:
Quote
Waits for the transmission of outgoing serial data to complete. (Prior to Arduino 1.0, this instead removed any buffered incoming serial data.)



Go Up