Linux application to receive a constant stream of data, parse, and output text
I'm wondering how to go about creating an application a set of commands, maybe contained in a shell script, which:
- rece开发者_Go百科ives a constant stream of binary data (from a weather station attached to a serial port, if you're wondering),
- identifies a pattern (e.g. this byte is
0x9F
, so the next 33 bytes are temperature data), - parses the data (e.g. three bits of the low nibble of byte 2 combine with the high nibble and low nibble of byte 1 to represent temperature in °C),
- and outputs text (e.g.
t0 350
wheret0
is just a sensor id and350
is the temperature in 1/10 °C)
The environment is linux (debian, 2.6.32-5-kirkwood), within which I am only a beginner.
I imagine that there is an idiomatic way of doing this sort of thing in linux. I'm willing to learn the tools necessary if one could point me down the right track. In particular, I'm wondering how to handle bitwise manipulations (unless I use C++
) and a constant input stream.
Ideally, the amount of code I would write would be small compared to using existing linux commands/libraries.
I would prefer not to use Mono, despite the fact that I know C# very well, simply because I have to build mono on the device (which is a SheevaPlug) which takes a long time and is error prone (for me). Also, I haven't figured out cross-compiling for the plug.
(It's a plugin to allow meteohub to log data from a WM918/WX200 weather station, if you're curious.)
Because I always do things the hard way, I would write a 20-line C program to do this task:
- fopen( ) the weather-data input serial device; fopen( ) output device, whatever it is;
- loop forever on fread( ) that weather-data serial device;
- do bitwise/bytewise weather-data manipulation;
- fwrite( ) output device;
- end of loop forever
Yes, you could write a shell script to do some of that, but I think you are going to have to code at least the bit/byte-manipulation part.
-- pete
The core philosophy of any Unix/Linux system is that "everything is a file". So the serial port you've hooked the weather station up to will be represented by a device file in the /dev/
directory. You can read from it using the standard fopen/fread/fclose type functions that most any language supports.
From that point on, it's just a matter of doing whatever processing with the data stream.
精彩评论