Finally cracked it !! ![]()
In no particular order:
-
Like Paul said, seems as though Serial and Serial1 are not really independent at all, as some of the literature suggests.
My attempts at debugging by sending back information from Serial.print to the Serial Monitor were part of the problem!
Deleting all my Serial.print "debugging" calls, apart from at the end of the main loop, and allowing Serial1 to get on with reading the data without interruption made a big difference. -
Putting a "delay(1)" before every "Serial1.read()" statement also seems to be essential.
Not sure why this is the case - maybe someone can explain?
(It does not seem to be necessary before "if (Serial1.available())" statements). -
The two "while(Serial1.available() <0) {}" loops in the "READ" section of my code to check for data availability seem to be redundant - at least it works OK without them for now...
-
Dumb error: The declaration "byte d = 0" should be inside the start of the "void loop()" instead of before "void setup()".
(Otherwise, the main loop exits with d = 0x7E (having detected the end flag) . It then enters the main loop again with d still = 0x7E; so thinks its immediately found a start flag, and falls through the loop second time round. The net result is that the length of the collected data will alternate between 1 and 320 bytes collected. D'Oh!).
Re-setting d = 0 at the start of the main loop solves this problem.
On the hardware side of things:
-
Using a different USB cable sorted out the problem with the 'SERIAL_8E1 was not declared in this scope' error, and other bizarre errors on trying to upload the code.
-
Making sure I had good connections to both the Serial1Rx and Earth pin headers on the Mega helped - flying leads into the pin headers are great for prototyping, but probably not the most reliable for data transfer...
Anyway, I'm just relieved to have solved my problem & hope the above helps anyone with similar woes.
Thanks again for putting me on the right track Paul!
Cheers
Dave