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:
- Stellarium broadcasts #:GR# command
- Arduino reads the command and responds
- 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.
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;
}