calling shared lib functions - serial port

4 views (last 30 days)
Big picture: I'm interested in controling a device through MatLab which has a shared library of functions; to open a serial port, get data, and close the port. The data of interest is voltages with time.
I have loaded the library into Matlab successfully, and could use some advice on how to call each function properly in order to work with the incoming data later on in the code. I have attached my current non working code. When I call a c function, do I need to define the variables which are passed into it as defined in c?
I realize anyone attempting helpful hints won't understand the specific function properties, although I could describe variable formats later if needed.
%%load the Ser1ch.dll into Matlab and call header files
loadlibrary ser1ch ser1ch.h addheader SrHelp.h alias A2D
%view functions imported
libfunctionsview A2D
%confirm load was successful
libisloaded A2D
%set up serial port
%s1=serial('COM1');
port=1
%%FUNCTION Ser1chOpen --> c function to open port for A/D board
calllib(A2D, 'Ser1chOpen', port, 'NULL');
%%read data
%%Call Function Ser1chGetData --> c function to get data from board as
%%double pression-32integer
counts=[];
calllib(A2D, 'Ser1chGetData', counts, 'NULL', 'NULL');
%convert counts to voltage
%%Convert count data from A/D Board to Voltages
slope=2.96E-7; %count per volt
volt=slope *count;
%%Close port
calllib(A2D, 'Ser1chClose', port, 'NULL');

Accepted Answer

Chirag Gupta
Chirag Gupta on 13 Apr 2011
Hi David,
What you are doing seems to be the correct approach. What would really be useful is to check the header file that you have and see the function signatures. Loadlibrary commands are susceptible to datatypes being passed. For example:
callib(A2D, 'Ser1chOpen',port,NULL)
function could be expecting an unsigned int as the input for the port. But you are passing a double (since MATLAB defaults to a double). You would see the types it expects with libfunctionsview as well.
Also what is the exact error and which line of code?
  2 Comments
David
David on 13 Apr 2011
Hi Chirag, thanks for the motivation.
Your correct, there are unsigned int as the input for the port; as follows:
Ser1chOpen( int SerialPortNumber, int *Error )
Could you suggest how to pass a unsigned int in place for the port number?
Chirag Gupta
Chirag Gupta on 13 Apr 2011
In matlab, use:
c = uint32(1)
whos c
You will see that c is a unsigned integer

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!