Serial.read() unreliable on Artemis

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;
	}
}

How did you create the loop back on Serial port?

I have taken my Artemis ATP and there is a Serial1 port on the pins. I used the Artemis/Apollo3 V2.2.1 library and I have done 2 different tests:

TEST1
I connected RX1 and TX1 together. The UART code for UART0 and UART1 is the same.

I have changed your sketch a little (see Feb19A attached) to do the speed test on Serial1 and print the result on Serial.
I have tested with different baud-rates : 115200, 230400, 460800, 576000 and 921600.
Only at 921600 (the max) the first ‘12’ are always lost: 034567890123456789012345678901234567890123456789, 1000.
Up to 576000 I do not have an issue.

TEST2
I created a Python program on my Ubuntu to open /dev/ttyUSB0, set different speeds and send nearly the same string as yours. (the code is attached). I have changed your sketch so that in loop() it only calls for read_serial(). The on Serial received packet is printed to Serial1 (see sketch Feb19B). On Serial1 I have connected an external data analyser. Between Python and the Serial I have tested with different baud-rates : 115200, 230400, 460800, 576000 and 921600. Only the 921600 speed I see the same issue as with TEST1, the first ‘12’ are missing. Up to 576000 I do not have an issue.

Just for the fun I have then tried the Sparkfun V1.2.3 library of Artemis. This library is MUCH smaller and faster than V2.2.1 but lacks BLE. The 921600 baud rate works without problems.

Rename the attached file to .zip to extract
porttest.txt (2.8 KB)