Wondered if someone could assist.
I am trying to retrieve the X/Y position of my CNC machine (which has a 2560p at it's heart).
When I request the co-ords (Using M114 which works), I only get the first 64 bytes of data?
The DataCounter stops at 63, therefore I miss the speed and other data reported after the X/Y co-ords are returned.
If I plug the CNC machine directly into my laptop and request M114 in the serial monitor, it returns all the data.
What am I missing?
char TestChar_data[200];
byte Test_data;
int DataCount;
#define GetButton 12
void setup() {
Serial.begin(250000);
Serial1.begin(250000);
pinMode(GetButton,INPUT);
}
void loop() {
if (digitalRead (GetButton) == HIGH){
Test_data=0;
memset(TestChar_data,0,sizeof(TestChar_data)); // Clear data from the array
Serial1.println("M114");
Serial.println("Request data from other Mega...");
do{} while ((GetButton) == HIGH);
}
if (Serial1.available()){ // If the data is available, then decode it
do {
char Test_data=Serial1.read(); // Read the incoming raw data
TestChar_data[DataCount] = Test_data;
DataCount++;
} while (Serial1.available());
Serial.print("DataCount= ");Serial.println(DataCount);
Serial.println(TestChar_data);
DataCount=0;
}
}
Just tried this loop without anything between the request and the retrieval. Same result.
Also should mention I tried dropping the baud to 115200 on the CNC, but it made no difference.
char TestChar_data[200];
byte Test_data;
int DataCount;
void setup() {
Serial.begin(250000);
Serial1.begin(250000);
}
void loop() {
delay(3000);
Test_data=0;
memset(TestChar_data,0,sizeof(TestChar_data)); // Clear data from the array
Serial1.println("M114");
if (Serial1.available()){ // If the data is available, then decode it
do {
char Test_data=Serial1.read(); // Read the incoming raw data
TestChar_data[DataCount] = Test_data;
DataCount++;
} while (Serial1.available());
Serial.print("DataCount= ");Serial.println(DataCount);
Serial.println(TestChar_data);
DataCount=0;
}
}
The serial input buffer is 64 characters long. If it is full and you don't read the data quickly enough, any additional characters arriving are thrown away. It seems odd that your latest code can't keep up though.
In any event, try bringing the baud rate down to something farcical like 9600 and see if it makes a difference.
Just tried 9600. Thought I was being stupid. Same result.
The returned data from the CNC machine is usually short, and it doesn't have a problem with those. You can request a whole host of data using various commands and all that data returns fine - as long as they are not longer than 64 characters.
Seems very odd that it works from the Serial Monitor.
Maybe try this: lose the delay. Use the blink without delay technique to send M114 every ten seconds or so. Then just read from the CNC serial port and print what you get to standard serial.
Now I get garbled data, intermixed with random DataCount numbers. Nothing over 20 characters.
char TestChar_data[250];
byte Test_data;
int DataCount;
void setup() {
Serial.begin(250000);
Serial1.begin(250000);
Serial1.println("M119");
}
void loop() {
if (Serial1.available()){ // If the data is available, then decode it
do {
char Test_data=Serial1.read(); // Read the incoming raw data
TestChar_data[DataCount] = Test_data;
DataCount++;
} while (Serial1.available());
Serial.print("DataCount= ");Serial.println(DataCount);
Serial.println(TestChar_data);
DataCount=0;
}
}
Usually the problem that code like yours gives is that it implicitly assumes that all the data will arrive in one lump. The Arduino is fast compared to serial and so it will often check serial.available to find that there is nothing there.
If that happens, you break out of the loop print and reset data count. Check your output - I'd expect to see all your data but spread over several lines.
You should null terminate the array before you print it too.
You'll also want to implement at timeout in case a receive error causes characters to be dropped. If the timeout occurs, discard the whole buffer and try again.
You could use gfvalvo's code as a starting place. After reading a char into c, add it to your array and null terminate it. Don't reset your count variable.
Use millis to tell you when a second has elapsed and then print your array.