[Solved] Clear serial memory?

Hello,

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.

#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"

int incomingByte = 0;

Adafruit_MotorShield AFMS0 = Adafruit_MotorShield(0x61);
Adafruit_MotorShield AFMS1 = Adafruit_MotorShield(0x60);

Adafruit_StepperMotor *MotorX = AFMS1.getStepper(200, 2);
Adafruit_StepperMotor *MotorY = AFMS1.getStepper(200, 1);
Adafruit_StepperMotor *MotorZ = AFMS0.getStepper(200, 1);


void setup() {
  Serial.begin(57600);
  Serial.println("Let's do this!");

  AFMS0.begin();
  AFMS1.begin();
  
  MotorX->setSpeed(180);
  MotorY->setSpeed(180);
  MotorZ->setSpeed(180);// 180 rpm   
}

void loop() {
  if (Serial.available() > 0) {
    // read the incoming byte:
    incomingByte = Serial.read();
    Serial.println(incomingByte);
    if(incomingByte == 97){
      MotorX->step(1, FORWARD, SINGLE);
    }
    else if(incomingByte == 98){
      MotorX->step(1, BACKWARD, SINGLE);
    }
    else if(incomingByte == 99){
      MotorY->step(1, FORWARD, SINGLE);
    }
    else if(incomingByte == 100){
      MotorY->step(1, BACKWARD, SINGLE);
    }
    else if(incomingByte == 101){
      MotorZ->step(1, FORWARD, SINGLE);
    }
    else if(incomingByte == 102){
      MotorZ->step(1, BACKWARD, SINGLE);
    }
  }
}

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?

Thanks,

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".

Thanks for your quick reply,

I tried timing the data sends to 1 byte every second but still some were around the 200 bytes/steps every things stops.

What happens if, instead of reading serial data, you choose a random number in the range 97 to 102, and use that to choose what to do?

In other words, is your problem a hardware problem (overheating?) or a software problem?

If I make a void loop that would move every stepper 1 step can keep turning for hours,

I also changed my void loop the way you said,

void loop() {
  
  incomingByte = random(97, 102);

    Serial.println(incomingByte);
    if(incomingByte == 'a'){
      MotorX->step(1, FORWARD, SINGLE);
    }
    else if(incomingByte == 'b'){
      MotorX->step(1, BACKWARD, SINGLE);
    }
    else if(incomingByte == 99){
      MotorY->step(1, FORWARD, SINGLE);
    }
    else if(incomingByte == 100){
      MotorY->step(1, BACKWARD, SINGLE);
    }
    else if(incomingByte == 101){
      MotorZ->step(1, FORWARD, SINGLE);
    }
    else if(incomingByte == 102){
      MotorZ->step(1, BACKWARD, SINGLE);
    }
  
  delay(16);
}

And this one is running for a few minutes with out a problem as well.

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.

And get rid of the delay()! That is NOT helping.

The delay() was just there to make sure it wasn't generating faster than it would make steps. It worked without it but it would only do 1 in 16 steps.

I made a new void loop():

void loop() {
  if (Serial.available() > 0) {

    incomingByte = Serial.read();
  
    Serial.print((char)incomingByte);
  }
  
}

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.

I won't be able to read this in my app since I used the popen() way of communicating with arduino

Then why are you writing anything to the serial port? Perhaps the issue is on the Mac end when you never read from the pipe.

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.

As it TURNS (get it? :P) out to be I made a small mistake in my objective-C codes, works fine now. Thanks for your help any way Paul!

econjack that sounds a whole lot cleaner I will definitely give that a try