Speeding up serial (windows)

#1

Hi,

Im working on a small lidar scanner, i believe OF cant cope with the rate data is coming in. Im sending about 260 measurement per second in an 11 byte packet. I believe that is 260 * 11 * 8 = 22880 bps. This is with the IMU unhooked which will throw another 12 bytes in the mix. OF is not keeping up with the incoming data, at this point im doing nothing else than receiving it and counting the packets.

Im on windows 10 OF 10.1

How do i speed this up? Will a seperate thread help out or is this the way serial is implemented for windows? Are there faster addons for serial communication on windows?

Greets Tom

#2

can you post the serial data handling code? are you programming the lidar code too?

#3

What part of OF are you using to read the serial data?

#4
void ofApp::update(){
	//10 bytes expected + newline
	if (lSerial.available()> 10) {

		unsigned char rBuf[11];
		lSerial.readBytes(rBuf, 11);

		if (rBuf[10] != '\n') {
			lSerial.flush();
		}
		else {
			//unsigned char xBuf[4] = { rBuf[0],rBuf[1],rBuf[2],rBuf[3] };
			//float xRot = *(float *)&xBuf;
			//std::cout << xRot << std::endl;
			pointCount++;
		}

	}
}

Not that much to it right now :expressionless:

#5

First, I’m assuming your serial speed is set to an adequate speed > 22880 bps. The next thing to check – If your ofApp framerate is running @ 60 fps (usually the max for a standard windowed app) you are only reading 11 bytes per frame, so 60 * 11 = 660 bytes per second.

I suspect that you need to empty your serial buffer each frame using a while-loop.

void ofApp::update(){
	//10 bytes expected + newline
	while (lSerial.available()> 10) {

		unsigned char rBuf[11];
		lSerial.readBytes(rBuf, 11);

		if (rBuf[10] != '\n') {
			lSerial.flush();
		}
		else {
			//unsigned char xBuf[4] = { rBuf[0],rBuf[1],rBuf[2],rBuf[3] };
			//float xRot = *(float *)&xBuf;
			//std::cout << xRot << std::endl;
			pointCount++;
		}

	}
}

There are other strategies involving threads and such using https://github.com/bakercp/ofxSerial, but the while loop should help dramatically.

1 Like
#6

As an aside, you may want to use a slightly more sophisticated packet buffering solution because there isn’t a guarantee that the 11th byte in the serial buffer will be aligned on the \n character which could be causing you to skip packets unnecessarily.

You could roll your own or build from this example:

There is a corresponding Arduino library https://github.com/bakercp/PacketSerial/ that is mentioned in the ReadMe that makes packet communications pretty fast / efficient w/ openFrameworks. I’ve used it for very high bandwidth applications. Anyway, good luck!

If you don’t want to use an addon, here is some code that does what I’m talking about:

3 Likes
#7

Thanks, for the advice. The while-solution makes sense :smiley: and works, obviously :slight_smile:.
It is still working in the crude ‘newline in the 11th byte’ method, i think i write something based on your snippet.

2 Likes
#8

That’s very cool! What kind of sensor are you using?

#9

Lidar lite v3 hp, datasheet claims 1kHz sample rate. I think that’s insanely fast but not doable on an Arduino if it has to move stepper motors and serial communications.

1 Like