Arduino - Stellarium communication

Hi All,
I am trying to motorize my telescope using stepper motors controlled by Arduino, with input data concerning telescope's desired position coming from Stellarium (PC software).
I am trying to use LX200 protocol to communicate between Arduino and Stellarium.

I have come up with the code below, which is supposed to work like this:

  1. Stellarium broadcasts #:GR# command
  2. Arduino reads the command and responds
  3. Other commands follow

Regarding point 1. I tried to monitor serial port communication using Serial Port Monitor 8.0 and checked Stellarium logs - #:GR# command is transmitted.
Regarding point 2. My code does not work and it does not respond to the command (I have hardcoded the answer for simplicity).

When I use this code in Arduino's IDE Serial Monitor I do receive the expected response to the #:GR# command - which confuses me even more. Also, printing back any of the commands (random strings) I input in the Serial Monitor I see they are exactly as I expect them to be.
SerialMonitor

Could anyone help me with this problem? Right now I have no idea where the problem might be.

Best regards,
kornetka


char incoming[25] = "";

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
}

void loop() {
  // Answer (send data) when you receive question (command):
  if (readline(Serial.read(), incoming, 25) > 0) {
    // Serial.print(">");
    // Serial.print(incoming);
    // Serial.println("<");

    // Get RA command
    if (strcmp(incoming, "#:GR#") == 0) Serial.print("02:31:51#");

    memset(incoming, 0, sizeof(incoming));
  }
}

// Function from Majenko technologies 'https://majenko.co.uk/blog/reading-serial-arduino'
int readline(int readch, char *buffer, int len) {
  static int pos = 0;
  int rpos;

  if (readch > 0) {
    switch (readch) {
      case '\r': // Ignore CR
        break;
      case '\n': // Return on new-line
        rpos = pos;
        pos = 0;  // Reset position index ready for next time
        return rpos;
      default:
        if (pos < len - 1) {
          buffer[pos++] = readch;
          buffer[pos] = 0;
        }
    }
  }
  return 0;
}

This may be useful: Arduino + Stellarium with Meade lx200 Protocol - #5 by system

You rarely have to clear an entire text buffer if it only contains C strings. Instead of using memset() to clear the buffer, you only need to write one null terminator to the first position. That represents an empty string.

Also that job belongs better semantically with readline(), than with loop()-if then call readline() the way you do it.

My Arduino Software Solutions has a number of example codes for reading from Serial with their pros and cons.

Thank you all for replies.
I've managed to get it going by adopting the solution from link provided by blh64. I've modified it to use char strings instead of Strings and introduced small delay (25ms I think) in the loop. My interpretation is that without the delay, the loop completed before the next character was received (this is what I saw happening in serial monitor, series of one characters. Still don't know why the code from my original post doesn't work, but fortunately I have another solution.
Regards

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.