The need to plot (and filter) digital data from a sensor or front end circuit is ubiquitous in embedded systems. It’s always good to plot/filter/process the data in the same environment, so it’s always good to plot data in Matlab (if you have access to it). This post will talk about how to write a simple .m file to read data from the serial port of a PC and plot it onto a figure. We will do this in “real-time”, kind of like how an oscilloscope does.
I assume that you already have a microcontroller or something else that will sample the analog signal you want to plot, convert the HEX values into ASCII and send it over a UART/RS232 interface to your PC. The problem this post addresses is capturing this data from Matlab and plotting it continuously.
The capture part is really straight forward, you just have to create a serial port object, open it and perform
fread() on it, like shown below:
1 2 3 4 5 6 7 8 9 10
Once you get the data into matlab using
fread(), the next problem is plotting this data using
plot(). By default,
plot() clears old data in the figure and re-draws the figure at every call. To avoid this and to have
plot() append data you need to set
Now you have a graph that is updated in real time, with each sample. This might be good enough if your sampling rate is slow (a few 100mS or so); but for a faster sampling rate (mine was 4mS) it is too slow and soon my PC started catching fire. More importantly, the real strength of Matlab/Octave lies in vector operations – and it really does well in handling large chunks of data.
So to improve the performance, we modify the design to `fread()’ 1 second worth of samples and plot all of that data in one shot, every second. Further, to create an oscilloscope effect, one screen will display X seconds of data; after that the screen will be refreshed and another X seconds are displayed 1s at a time. This goes on for Y number of times.
Now ideally, you want Y to be infinite, and the capture should only stop when you press a button or something. So we add a callback function
myCallback for the
WindowButtonDownFcn event to cause the capture to stop when you click on the plot figure with your mouse.
While debugging my code I encountered a few problems:
- If for some reason, your script breaks and you are not able to close the serial port, the next time when you try to run the script you will get a port open error. One way to solve this is the execute the commands
at the begining of the script.
- If matlab does not receive data on the serial port, the object times out and you get a warning. You could catch this by something like:
1 2 3 4 5 6 7 8 9 10 11
- It is also faster to have calculate the X and Y scales statically instead of using
The code for the plotter is here. Each input data sample is assumed to be in the form +xxxx- where xxxx are any hex character in ASCII. For example the data samples of 0x123F and 0xD278 would be sent as +123F-+D278- .. and so on.