MATLAB Answers

Function to aquire serial data - without creating & connecting every time it is called....

1 view (last 30 days)
Pavel
Pavel on 19 Apr 2012
Hi @ all!
I am working on a programm that reads, logs, and displays some Data. The data can be acquired from a file, from a function (when I am debugging I simply create some random Data) and from serial ports. Since I want to keep it simple I wanted to “outsource” the data acquisition, i.e. write some functions that, when being invoked, return the Data. The functions for reading out Data from a file and creating random data already work fine, but I am stuck with the serial readout function. My first draft was:
function [Data] = serialRead()
s=serial(COM1’…etc.);
fopen(s)
fprintf(s,Give Me Data);
Data=fgets(s);
fclose(s);
end
The problem is that I read the Data over and over until the program is terminated, and I don’t want to create a new serial object& connect and disconnect it to the device(sensor) for each Data request (i.e. function call) since this really slows things down.
So I thought of checking if the serial object has been created before, when the function is called, but as soon as a function ‘is done’ the variables are being cleared, so
if ~exist(s) ..create and connect to device….else just send and receive data
doesn’t work either. I also thought about creating and connecting the serial object in the main program and passing it to the function, but this is very unsexy and Matlab won’t let me pass a serial object as input argument to a function. So does anybody have a creative idea?
Thanks for reading & kind regards,
Pavel

  0 Comments

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!