I’m having trouble getting consistent serial reading on the Artemis. On Arduino I can reliably send and receive at up to 500000 baud with no issues. When I try the same code on Artemis I have to drop the baud well below that to receive data (sending from Artemis to PC is fine). I’ve gone as low as 115200, but still run into trouble occasionally.
I’ve used up to 1M baud in the past on Artemis with success, but only from Artemis to PC. I have an application where I need this speed which is why I turned to the Artemis, but I didn’t expect to have issues in the other direction. I’ve confirmed with my current code that 1M baud still works just fine in the send direction, it’s only receiving that has the problem.
Importantly, this same code works just fine on Arduino, so I don’t think it has anything to do with how I’m handling the incoming data unless there’s something fundamentally different about how Artemis buffers the data.
This code isolates the problem. It continuously spits out a long string and times how long to do the sending. Higher bauds result in less time as expected and it works on my system up to 1M. If you send data the code will receive it and return it to you, again timing the send process. The problem is that the returned data doesn’t always match the sent data, and sometimes doesn’t return anything at all. Sometimes I have to send blank lines over and over and keep getting partial results back.
#define SERIAL_BAUD 500000
void setup() {
Serial.begin(SERIAL_BAUD);
Serial.println("Ready...");
}
void loop() {
static unsigned long send_interval = 500; // milliseconds
static unsigned long next_send_time = 0;
static unsigned long receive_interval = 10; // milliseconds
static unsigned long next_receive_time = 0;
static unsigned long now = 0;
// send at some interval
if (millis() >= next_send_time) {
next_send_time += send_interval;
now = micros();
Serial.print("01234567890123456789012345678901234567890123456789"); // some long string
Serial.print(", ");
Serial.println(micros() - now); // print how many microsecods to print the above. higher baud -> less time
}
// check for incoming at some interval
if (millis() >= next_receive_time) {
next_receive_time += receive_interval;
read_serial();
}
}
void read_serial() {
static char inChar;
static char inString[100];
static int i = 0;
static bool end_char_received = false;
static unsigned long now = 0;
while (Serial.available() > 0) { // read the buffer
inChar = Serial.read(); // one character at a time
if (inChar == '\n') { // check for end of line
end_char_received = true;
break;
} else {
if (i >= 100) {
i = 0; // avoid writing to random memory
}
inString[i] = inChar; // otherwise append to string
i++;
}
}
if (end_char_received) {
inString[i] = '\0'; // finalize the string
now = micros();
Serial.print(inString); // print it back
Serial.print(", ");
Serial.println(micros() - now);
end_char_received = false;
i = 0;
}
}