User avatar
howroyd
Posts: 39
Joined: Thu Aug 02, 2012 1:41 pm
Location: Loughborough
Contact: Website

Serial 2 file the back again

Fri Apr 19, 2013 2:14 pm

Anyone know how to save data from the serial (tx,rx) to a file, and then be able to send it back out again at the same speed it was received?

My application is an onboard datalogger for a remote controlled plane, saving the serial stream from the autopilot (an encoded stream), which I can then send out when back on the ground in realtime to my laptop over wifi.

I have tried using ser2net, which only works when there is a TCP connection, so if I lose the link it stops logging the serial stream (which is the exact time I really need it to start recording, lol!

User avatar
[email protected]
Posts: 2020
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website

Re: Serial 2 file the back again

Sat Apr 20, 2013 8:13 am

howroyd wrote:Anyone know how to save data from the serial (tx,rx) to a file, and then be able to send it back out again at the same speed it was received?

My application is an onboard datalogger for a remote controlled plane, saving the serial stream from the autopilot (an encoded stream), which I can then send out when back on the ground in realtime to my laptop over wifi.

I have tried using ser2net, which only works when there is a TCP connection, so if I lose the link it stops logging the serial stream (which is the exact time I really need it to start recording, lol!
It's not a hard programming excercise - however you're going to end up saving something like 3 bytes to a file for every byte recieved over the serial.

(And I'm assuming here that by "speed" you mean the interval between data bytes rather than baud rate)

So read the time (use gettimeofday () for µS accuracy), and store this. This is your epoch or start of time.

Then loop, reading a byte, then read the time, and keep a delta of the time between this byte and the last one (as a simple 16-bit integer for example), and store these 3 bytes in a file (use the buffered IO library - ie. fwrite)

then at playback time, you simply read in 3 bytes at a time, extract the time, wait for the time (delayMicroseconds in wiringPi), then send the byte.

There may be a little bit of jitter, but overall it should be good enough..

The problem may come when the file IO operations decided to write the block to SD card. But you might actually be able to buffer it all in memory - if you know the amount of data you are getting for a flight, add on the overhead of the times, so 3 bytes per sample - with Linux booted and no extra services like X, etc. you might have over 150MB spare - which you can pre-allocate and "lock down" to make sure it's not in swap. (see mlock (2))

-Gordon
--
Gordons projects: https://projects.drogon.net/

User avatar
howroyd
Posts: 39
Joined: Thu Aug 02, 2012 1:41 pm
Location: Loughborough
Contact: Website

Re: Serial 2 file the back again

Sat Apr 20, 2013 8:39 am

Cheers very much. I shall look into this today...after watching the F1 ;-)

Thank you

Return to “General programming discussion”