I couldn't find what I was looking for on this forum so I thought I would just ask.
I am trying to make a G-code interpreter for a CNC-machine in objective-C. My Arduino is just there pass along the commands.
My objective-C app interpretates the G-code and sends a signal to the serial port for every step there has to be made. It runs pretty good for the first couple of hundred steps or so, but after that I guess some of the memory gets full because signals stop showing up in my terminal and the machine stops making steps. So my question would be: What memory bank gets clutched and how do I clear them?
My objective-C app interpretates the G-code and sends a signal to the serial port for every step there has to be made.
It doesn't send a "signal". It sends a byte.
if(incomingByte == 97){
Way more obvious to use:
if(incomingByte == 'a')
{
What memory bank gets clutched and how do I clear them?
If the sender is sending data faster than the Arduino can read it (likely as it has to move a stepper for most of the bytes received), then the serial buffer will get full, and bytes will be silently discarded. There is nothing you can do to "clear" that on the Arduino end.
You have to have the Arduino tell the sender when it is ready for more data, and have the sender shut up for a while, until the Arduino says "OK, send me more data".
OK. The next step is to do nothing with the received byte except echo it back to the sending program. The goal is to narrow down where the issue is. Comment out the if statements and blocks.
This echo's the code and the same thing happens here some were around step 200 the communication just stops.
I won't be able to read this in my app since I used the popen() way of communicating with arduino: Arduino Playground - Cocoa
I could rewrite my program to read back from the Arduino, but wouldn't that result in basically the same data?
Update: I counted the steps it can make, and it is 144 every time.
Cascading if statements are difficult to read and, depending upon its depth, can be inefficient. If you have 30 such if's and it's the last one that is true, you perform 29 unwanted tests. Replace with a switch:
switch (incomingByte) {
case 'a':
MotorX->step(1, FORWARD, SINGLE);
break;
case 'b':
MotorX->step(1, BACKWARD, SINGLE);
break;
// The rest of your code, followed by:
default:
Serial.print("Shouldn't be here: incomingByte = ");
Serial.println(incomingByte, HEX);
break;
}
A switch generates a jump table, so it bypasses the unnecessary if test. I think they are easier to read, too.