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

8 visualizaciones (últimos 30 días)
LeChat
LeChat el 7 de Mzo. de 2023
Comentada: Walter Roberson el 13 de Abr. de 2023
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.

Respuestas (1)

Les Beckham
Les Beckham el 7 de Mzo. de 2023
Editada: Les Beckham el 7 de Mzo. de 2023
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 comentarios
LeChat
LeChat el 13 de Abr. de 2023
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.
Walter Roberson
Walter Roberson el 13 de Abr. de 2023
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.

Iniciar sesión para comentar.

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by