Serial Communication and real time cabpable stepper controll

I’m using a Arduino Due to driving steppers by a TB6560 stepper driver. The velocity of steppers should be controlled by commands over the Serial port. Per Serial communication, I send commands to do a reference drive (case 1) or drive with a certain velocity (case 2). Due to its necessary to receive dates every time it’s not possible to use delays, so I generate the square wave signal with running throw a loop and change the Output only if necessary.

currentMic = micros();
if (currentMic - prevMic >= velocity) {
   if (state_puls == HIGH) {
       state_puls = LOW;
   }
   else {
       state_puls = HIGH;
   }
       prevMic = currentMic;
}
digitalWrite(PUL1, state_puls);

This code works perfectly, but if I use communication (Serial.begin(115200); Serial.setTimeout(1);), it stops the process and it’s only possible to generate a maximum frequency of 98Hz, but I must reach a maximum of 8kHz (with delayMicroseconds() it’s possible).

currentMic = micros();

    while (Serial.available()) {
      while (!Serial.available()) {}
      read_serial = Serial.readString();
    }

    if (read_serial.startsWith(string_drive_reference)) {
      case_state = 1;
    }
    else if (read_serial.startsWith(string_drive_velocity)) {
      case_state = 2;
      DIR = read_serial.substring(2, 3).toInt();
      faktor = read_serial.substring(3, 4).toInt();
      value = read_serial.substring(4).toInt();
    }
    if (read_serial == "") {
      return;
    }
    read_serial = "";

    switch (case_state) {
      case 1:
        reference_drive1();
        break;
      case 2:
        velocity = value * pow(10, faktor);

        if (currentMic - prevMic >= velocity) {
          if (state_puls == HIGH) {
            state_puls = LOW;
          }
          else {
            state_puls = HIGH;

          }
          prevMic = currentMic;
        }
        digitalWrite(PUL1, state_puls);
        break;
    }

My question is, is there a possibility to speed up the communication or generate a square wave signal where I can change the frequency continuously by using the Serial Communication?

stepper.ino (4.36 KB)

while (!Serial.available()) {}

That will stop execution of everything else while it waits for serial input. Not going to make a responsive program.

read_serial = Serial.readString();

Then once there is something is available, wait till a ‘\n’ is received (signifying the end of the message) or a timeout occur. That may take a while, too.

If you want a responsive program you need to get rid of the blocking code (above). And to prevent hard to diagnose problems, due to memory contamination, lose the String class and use c-strings.

The methods in the serial input basics tutorial show how to read serial data into a null terminated character array (c-string) in a non blocking fashion,

Also. make sure that each message to the Arduino does not exceed 64 bytes or it will overflow the Serial Input Buffer.

I have a system for controlling 3 stepper motors on a small CNC lathe. The Arduino sends a message to the PC when it is ready for the next data. Then it collects the message that has already arrived and executes Move D. When Move D is complete the message for Move E should be waiting in the Serial Input Buffer and the whole process repeats.

...R