reading serial COM port on the fly (weighting scale -> WinPC)

Hi,
I am trying to read on the fly output data from a serial COM port on a weighting scale (mine is a Sartorius Entris), on my PC (Matlab 2021b for Win10).
On the weighting scale my setup is:
Protocol: SBI
Baudrate: 9600
Parity: Odd
Hanshake: none
DataBits: 7 bits
In Device Manager I also checked that COM6 is active, and that I have the save Bits per sec., Data bits, Parity as before, and that Stop bits=1 and Flow control = None. The weighting scale is connected either with a broad serial to USB-A adapter, or a USB-C to USB-A adapter, depending of the version of weighting scale I am trying, I have an old one and a newer one with USB-C.
In Matlab, I do (it is from an older code in which I use serial() instead of the newer serialport() function but if you have a better version of this I am interested):
if (~isempty(instrfind))
fclose(p);
delete(instrfind)
disp('instrfind empty!')
end
Warning: instrfind will be removed in a future release. There is no simple replacement for this.
close all;
clear all;
% %% set SERIAL PORT settings - Mac:
% p=serial('/dev/tty.usbserial-A101BF73','BaudRate',9600);
%% set SERIAL PORT settings - PC (Windows):
p=serial('COM6','BaudRate',9600);
Warning: serial will be removed in a future release. Use serialport instead.
If you are using serial with icdevice, continue using serial in this MATLAB release.
set(p,'Parity','odd','FlowControl','none'); % for SARTORIUS weighting-scale (factory settings)
%
%% use the output data to plot a live-updated plot
time = [];
masse = [];
tic %time origin
lastingtime=100;%sec
%
figure(1)
%
while(toc <= lastingtime) % measurements during 100 sec.
%first measurement
s=fopen(p);%fgets(p);
t=toc
mass2 = sscanf(s,'N %s %f g');
signe = mass2(1);% get measurement sign
mass_abs = mass2(2);% get absolute value of curent measurement
if signe == 43 % positive
mass1 = mass_abs;
elseif signe == 48 % nul
mass1 = 0;
elseif signe == 45 % negative
mass1 = - mass_abs;
end
% save data
mass = [mass;mass1];
time = [time ;t];
% Plot data
hold on
plot(t,mass1,'-+r')
title('Flowing mass')
drawnow
end
Error using serial/fopen
Too many output arguments.
But at the line:
s=fopen(p);
Matlab gives me the following error:
Error using serial/fopen
Too many output arguments.
Error in myCode (line 54)
s=fopen(p);%fgets(p);
Would anyone know why I get this error?
Thank you for your help and ideas!
PS: I am leaving the rest of the code in case it is useful for the global understanding of what I am trying to do, and/or for other users.

Answers (1)

The serial object overload of fopen doesn't have any output arguments, so change that line of code to the following.
fopen(p)
Also, you should move that line outside of the loop.
I think you also want to use fscanf instead of sscanf to read the data.
mass2 = sscanf(p, 'N %s %f g'); %<<< read from p (the serial object)

6 Comments

In the newer serialport() class there is no fopen step by the way. There is also no fscanf for it: you have to fetch a line and sscanf the line.
Thank you both for your answer. (sorry for the delay of my response, I got carried away by job applications).
I tried the following, taking your proposals into account.
bitrate=9600;%=baudrate (bits/sec)
p=serialport('COM6',bitrate);
set(p,'Parity','none','FlowControl','none'); % for SARTORIUS weightning scale (factory settings)
time = [];
masse = [];
% flowrate =[];
tic % time origin
figure(1)
while(toc <= 1000) % measurements during 1000 sec.
t=toc;
pause(0.5) %try, to slow down data transfert from weightning scale to PC
%
masse2 = sscanf(readline(p),'%s %s %f g\n');
masse2(1)=[];
signe = masse2(1);% get sign
masse_abs = masse2(2);% get absolute value
if signe == 43 % positive
masse1 = masse_abs;
elseif signe == 48 % zero
masse1 = 0;
elseif signe == 45 % negative
masse1 = - masse_abs;
end
%
disp([num2str(t),' s ## ', num2str(masse1),' g'])
%
% save data
masse = [masse;masse1];
time = [time ;t];
%
hold on
plot(t,masse1,'-o','MarkerFaceColor','r','MarkerEdgeColor','k')
title('Flowing mass')
drawnow
end
But the mass value displayed on the scale's screen is not the same as compared to that received by the PC (somehow the PC data seems late, delayed). This time delay increases with time, in an uncontrolled fashion.
I tried the code on two different PCs, I changed the baudrate, the parity (with the Windows settings and the scales settings edited accordingly).
Would you have any idea why there is this delay in my measure?
Thank you
Unfortunately, I don't have any ideas on the time delay problem except, perhaps, to move the plotting outside of the loop. Collect and display the data in the loop and make the plot after all the data is collected.
masse2 = sscanf(readline(p),'%s %s %f g\n');
When you use sscanf() with the first entry being %s or %c, then you will get back a vector of character in which there is no way to tell where the entries end, and all numeric values such as read in by the %f will be converted by way of char() -- as in
char(42.3)
ans = '*'
double(ans)
ans = 42
.. which loses all fractions and discards negative values.
If you have multiple entries on a line I recommend you textscan() instead,
masse2_cell = textscan(readline(p),'%s %s %f');
Thank you for both you answers. Actually, only plotting once every n (n=2 sec for instance) improved the reactivity of the script during the experiment. Thank you.
I also tried Walter's solution in order to do things the best way, using textscan, but I am not so familiar with cells I guess and could not get it to work. I tried:
mass1=str2num(cell2mat(masse2_cell{2}));
But the obtained value remains at zero. I will try again next week but if you think there is a syntax issue, please do not hesitate to tell me so, somehow I feel there must be a more direct way to do str2num(cell2mat(mycell{2})).
Thank you very much for your support. Stay well.
If you were to use a %f format instead of a %s format then
masse2_cell = textscan(readline(p),'%f %s %f');
mass1 = masse2_cell{1};
and you could find out whether it was negative or positive by using sign instead of checking the first non-blank character of the representation.

Sign in to comment.

Products

Release

R2021b

Asked:

on 7 Mar 2023

Commented:

on 13 Apr 2023

Community Treasure Hunt

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

Start Hunting!