Archive for November, 2008

Real-time USB in Matlab (1 of 2)

November 8, 2008 3 comments

The TI MSP430 wireless development tool (eZ430-RF2500), pictured on the right, can communicate with a PC using a virtual serial port over USB and also use wireless ZigBee communication to communicate with other low power modules.
The development kit comes with a demo program to show how both the wireless and serial communication can be used. The eZ430-RF2500 Sensor Monitor Demo displays the temperature, voltage, and signal strength (~distance) of all nearby modules.
Today I’m interested in establishing real-time communication between the USB-adapted device and a PC using MATLAB Simulink.
Virtual serial port communication with MATLAB
* Note: When the MSP430 drivers are properly installed, the device is listed in the Device Manager  under Ports (COM & LPT) as MSP430 Application UART (COM11).
The following MATLAB *.m file code is used to read the transmitted serial signals from the MSP430 device:
s = serial(‘COM11’);
set(s,’BaudRate’,300000,’DataBits’,8,’Parity’,’none’,’InputBufferSize’, 128);

s.ByteOrder = ‘bigEndian’;
data = fread(s);
values(:,1) = data;

clear s

This is the output of the MATLAB script, which corresponds to the signals sent by the MSP430 device.
Node:HUB0,Temp: 79.1F,Battery:3.6V,Strength:000%,RE:no  
This method allows real-time communication with the device by simply looping this code to display signals as they are received.
Applying this concept to Simulink
The same results can be achieved by using MATLAB Simulink if the INSTRUMENT CONTROL TOOLBOX is available.
The Simulink model uses only the “Query Instrument” block from the “Instrument Control Toolbox.”
The only trick to using the virtual COM port at COM11 is that the block’s drop down menus only allow serial COM ports COM1 through COM4. The workaround is to define the variable ‘s’ used in the *.m file version of the communication above, which points to COM11. This is done by running two lines of code when the model starts in the following way:
1.  When viewing the Simulink model, open the Model Properties at File > Model Properties.
2.  Look at the “Callbacks” tab.
3.  Add the following text to the InitFcn callback:
s = serial(‘COM11’);
set(s,’BaudRate’,300000,’DataBits’,8,’Parity’,’none’,’InputBufferSize’, 60);
4.  Double-click on the Query Instrument block in the Simulink model.
5.  Choose the “Use interface object from the MATLAB workspace” radio button and type in “s” (no quotes).
This procedure displays each byte received from serial communication with the MSP430 as received in the MATLAB *.m file version. I’d like to go further to condition the received data and identify battery voltages and temperatures to be plotted independently, but that will have to be left for a future project.