English 中文(简体)
Microprocessor to RS-232 realtime plot using PySerial/Matplotlib?
原标题:

I m new to the world of Python and my programming skills are fairly poor but I m trying to figure a way to use Python to display the output from an EEG circuit (using the OpenEEG circuit http://openeeg.sourceforge.net)

The analogue output is amplified and processed via an ADC (in an ATmega8 microcontroller) and is converted to RS232 by a MAX232.

The RS232 Signal is as follows:

Byte 1: sync value 0xa5
Byte 2: sync value 0x5a
Byte 3: version
Byte 4: Frame number
Byte 5: Channel 1 Low Byte
Byte 6: Channel 1 High Byte
Byte 7: Channel 2 Low Byte
Byte 8: Channel 2 High Byte
...
Bytes 9-16 are for extra electrode channels but data not required since only using the first two
...
Byte 17: Button states (b1-b4)

I ve got some basic PySerial functionality but I need to figure a way to make use of the incoming data by buffering it and plotting the useful values as 2 real-time x-y waveforms (time vs voltage)

Question update:

I m getting the code to print with the obvious few lines of PySerial but it s gibberish. I m trying to strip the data down in to the format of values that can then be plotted. The 17 bytes of data is currently coming in at 256 frames/sec. The (two) channels are made up 10 bits of data each (with 6 zeros to make up the rest of the 2 bytes). They are unsigned giving possible values of 0 to 1023. These correspond to values that should be plotted as positive and negative, so a binary value of 512 corresponds to a plot of zero (micro)volts....

How do I read the incoming stream as 8 bit binary (stripping out the data that I don t need), then combine the two relevant bytes from each channel that I want (possibly removing the surplus 6 zeros if necessary)?

问题回答

To handle the complicated binary data format you could maybe use structured arrays in numpy (see also here for a nice introduction). After defining the structure of the data it should be very easy to read it in. Then you could use numpy s functionality to cook down the data to what you need.

There s a good realtime plotting example here. It s a good example in that it runs with self generated data, so it s easy to test, but it s also obvious where to modify the code for plotting real data, and the code is easy to follow.

The basic idea is to make a plot window and then update that data as it comes in using

set_xdata(np.arange(len(self.data)))
set_ydata(np.array(self.data))

(Though in the current versions of matplotlib you may want to use set_data(xdata, ydata) instead.)

As for parsing the serial port data, it s probably better to ask that as a separate question.





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签