MATLAB Answers

How to store multiple serial objects

9 views (last 30 days)
Hello everybody,
I would like to load eight different COM ports as serial objects and send strings to these serial objects simultaneously. These COM ports represent micromanipulators which I can tell to move to certain xyz coordinates.
Right now I initiate them using these lines:
m1=serial('COM10');
m2=serial('COM11');
m3=serial('COM12');
m4=serial('COM13');
m5=serial('COM14');
m6=serial('COM15');
m7=serial('COM16');
m8=serial('COM17');
fopen(m1);
fopen(m2);
fopen(m3);
fopen(m4);
fopen(m5);
fopen(m6);
fopen(m7);
fopen(m8);
m1.terminator='CR';
m2.terminator='CR';
m3.terminator='CR';
m4.terminator='CR';
m5.terminator='CR';
m6.terminator='CR';
m7.terminator='CR';
m8.terminator='CR';
Afterwards I would like to adress all (or only some which I select through a checkbox GUI) serial objects with these lines:
fprintf(m3,'OUT')
pause(5)
fprintf(m3,'ABSZ -15000')
pause(2)
fprintf(m3,'RELZ 70000')
pause(2)
fprintf(m3,'IN')
fclose(m3)
I could write 8 if loops and leave everything as is, but I have a feeling that there should be a better and shorter solution? Is there a way to store the serial objects in some kind of array or anything like that so I can adress the serial objects with a for loop? Apparantly I can not store them in an array, because then this happens:
>> s{1}=serial('COM10')
Cell contents assignment to a non-cell array object.
I would appreciate your help.

  0 Comments

Sign in to comment.

Accepted Answer

Andrew Newell
Andrew Newell on 26 Apr 2017
You certainly can store them in an array, e.g.:
n = 8;
s = cell(8,1);
for ii=1:8
s{ii} = serial(sprintf('COM1%d',ii-1),'terminator','CR');
end
I'm guessing it didn't work for you because s was already defined as some other class of object.

  0 Comments

Sign in to comment.

More Answers (0)

Sign in to answer this question.