MATLAB Scripting

 

NOTE: This page describes MATLAB scripting for an obsolete product. The concepts are valid for present products such as the PALC line. The Fast Code arguments may be updated for use with the PALC line. The Fast Codes begin with the '#' character.

 

Matlab M-files

SOSI Model 2 has been designed to operate with MATLAB. SOSI communicates in ASCII text directly with the MATLAB serial() function on MS-Windows® or Linux®. A zip file collection of tested m-files is located on the Downloads page. This package contains M-files of basic commands to operate and interact with SISO-M2. It also contains related M-file functions used to create more specialized SOSI-M2 functions. For example, the function serial9600() opens the serial port and returns a serial port object with its communication parameters set correctly. This is used as one of the input arguments to cmd().

Stand-Alone Functions Description
runcont(port,nsample) start continous data acquisiton
trigger(port) do a single data acquisition
setchan(port,bit,mode) set channel mode analog I or digital I/O
output(port,bit,state) set digital out high or low
showtime(port) show time settings
showchan(port) show channel settings
setsamplefast(port,msec) set sample rate fast mode
setsampleslow(port,min,sec) set sample rate slow mode
settrigger(port,mode) set trigger mode internal time, external event
setformat(port,tmode) set x-axis mode time, count, or none
setsleep(timeout) set sleep timeout seconds
Subfunctions Description
serial9600(port) Create and open serial port object
serialread(spo) Read serial port buffer until empty
setchansub(port,bit,mode) set channel mode analog I or digital I/O
cmd(spo,command,delay) Send command, delay, read reply
cmdb(spo,command,delay) Same as cmd() but do not print
setsamplefastsub(spo,min,sec) Set slow sample rate
setsampleslowsub(spo,msec) Set fast sample rate
runcontsub(spo,count) read continously, save data to matrix
wakeupsub(spo) Wake up and return when reply received.



The above codes are structured the following way:

try (catches errors and closes serial port)
{
create serial port object, open serial port
{
| one or more sub-function(s) consisting of:
| A. send command
main body | B. delay*
| C. read reply or data, test for validity
| repeat from A or B if necessary
}
close port, return
}
catch error, print error message, close port, return


*After a command is sent, a delay is required before reading a serial communication reply from SOSI-M2 in order to allow the host serial buffer to receive the reply string. A 0.05 second delay is enough time to allow the string plus terminator to arrive. Commands in which SOSI first writes to non-volitile EEPROM memory before sending a reply require a delay of 0.5 seconds before a read is performed. These commands are:.


'set sample ...'
'set trigger ...'
'set output format ...'
'exit' (from channel menu)
('#sesa ...')
('#set ...')
('#seo ...')
('e')



The main body of stand-alone functions contain the following steps:

  1. Function to wake up SOSI or confirm SOSI awake
  2. Function to configure the sample rate, sleep timeout, x-axis, or channel mode if nessessary.
  3. Function to get data or set a digital bit

MATLAB Command Line

One may test run sub-functions from the command line before implementing a longer function. For example,

First, disable sleep mode (using the stand-alone function here):

port='COM2'
setsleep(port,0)

Open the serial port:

spo=serial9600(port)

Display the present channel configuration:

fprintf(spo,'#shc')
serialread(spo)

Change the output format to print the 'x' axis as sample count:

fprintf(spo,'#seoi')
serialread(spo)

We are finished for now:

fclose(spo

If an M-file contained these commands, enclose the above code in a try-catch block. In the case of an error, the script would otherwise return before the ending fclose(). Placing fclose() in the catch will close the serial port.

 

Script Errors

If a serial port M-file initially runs but then MATLAB gives the error message:

Port: (port) is not available. Available ports: (other port)

Use INSTRFIND to determine if other instrument objects are connected to the requested device.

The serial port was not closed by a previous M-file before the last M-file tried to re-open it. The catch-try code is intended to prevent this. Restart MATLAB to free the port.

Free serial port monitors are available which may be used to monitor opening and closing of the serial port and thereby aid code debugging.

 

MATLAB Serial Bugs

The M-file scripts have been tested working with MATLAB R12 (under Linux) and R14 (under Windows). Certain versions of MATLAB have serial communication issues which may require these steps:

R12 (MS-Windows)

"MATLAB relies on the javax.comm.properties file to inform it what serial ports are available. There have been some reported cases where the serial ports are seen only if the javax.comm.properties file is moved from it's current location:

matlabroot\java\jarext\commapi\win32

to:

matlabroot\sys\java\jre\win32\jre\lib

Note, this dependency on the javax.comm.properties file will be removed in the next release of MATLAB.

Source

 

R13 (MS-Windows)

A patch for the serial communication problems with MATLAB 6.5 (R13)

Source

 
Joomla Templates by Joomlashack