I'm trying to send a text-file from Terminal.exe to the Arduino, one line at a time. This is because I decode the line into X- and Y-moves and want those to finish before tackling the next line of commands.
When I receive a CR (carriage-return) I send an XOFF then process the line, act on it, and send an XON. Which appears to work for small files, < 180 bytes or so.
Does the Serial.read buffer clear out 'old stuff' as it is read? I thought I understood that the buffer is 126 bytes (or thereabouts) and none of my lines of text are anywhere near that long - all are less than 20 characters.
I send an XON, expecting this to start sending the next chunk from the text file, so Serial.available() becomes true. Then read each character into a string, expecting that character to be removed from the buffer, and the next character, and the next, until I get a CR then send XOFF. So Serial.available should become false? And the data in the buffer should all transfer to my string, whilst emptying the buffer?
void loop()
{
myString = " ";
if(byteCounter > 124)
{
Serial.println("Buffer Full");
Serial.write(XOFF);
byteCounter =0;
Serial.flush();
delay(2500);
Serial.write(XON);
}
if (Serial.available() > 0 && carryOn == true && goSub ==false)
{
int strLength =0;
incomingByte = Serial.read();
// try to build One Line of 'commands' e.g. G1 or M99
while( incomingByte != CR && incomingByte != LF && incomingByte != EOF)
{
if (Serial.available() > 0)
{
incomingByte = Serial.read();
myString += char(incomingByte);
byteCounter ++;
strLength = myString.length();
}
}
if ( (incomingByte == CR || incomingByte ==LF || incomingByte == EOF) && (strLength !=0))
{
Serial.write(XOFF);
decodeSuccess = decodeCommandString(myString);
// Special for Arcs
if(decodeSuccess == true && doingRadius == true) // we've decoded one line, now action it
{
carryOn = false;
if(quarter == 1 || quarter ==3)
carryOn = DriveRadiusQ1_3( Radius, quarter);
else if(quarter == 2 || quarter ==4)
carryOn = DriveRadiusQ2_4( Radius, quarter);
done();
}
// Normal X, Y moves
else if(decodeSuccess == true ) // we've decoded one line, now action it
{
carryOn = false;
carryOn = interpolateDriveMotors((NewXpos - OldXpos), (NewYpos - OldYpos));
done();
}
else
{
Serial.println("DecodeSuccess is FALSE");
carryOn = false;
}
decodeSuccess = false;
}
}
}
// end the loop for now
//-----------------------------------------------------------------------
void done()
{
delay(1000);
myString = " ";
if(carryOn == true)
{
Serial.write(XON);
}
byteCounter =0;
}
but I cannot persuade it to read to the end of any file longer than about 180 bytes. What baffles me is that this suggests it is 'storing' (buffering?) more than one-line of commands. This file works fine, right to the end:
(W)
G90
G21
G92 X0 Y0
G51 S0.67
G1 X2
G1 Y-10
G4
F45
G1 X0 Y0
X-3 Y-10
X-5 Y0
X-8 Y-10
G4
G1 X0
G1 Y0
G92 X0 Y0
M30
No one line of which is longer than 12 bytes + CR. (The total file length is 165 bytes)
but this one does not:
(letter W)
G90
G21
G92 X0 Y0
G51 S0.67
G1 X2
G1 Y-10
G4
G4
G4
F45
G1 X0 Y0
X-3 Y-10
X-5 Y0
X-8 Y-10
G4
G4
G1 X0
G1 Y0
G92 X0 Y0
M30
This is the same code but 'padded' with a few extra characters, total length of 185 bytes.
Reading the "Serial.read() - Arduino Reference" offers no help - I've been there.
Someone on this forum stated that he can send/receive files of many thousands of bytes, so I'm baffled that I can't seem to get past this small (180 bytes) limit.
Does the buffer 'fill' from both directions? I.e. Serial.read()obviously fills it, but does Serial.write (or Serial.print) also use the same buffer?
Geoff