reading serial COM port on the fly (weighting scale -> WinPC)
조회 수: 9 (최근 30일)
이전 댓글 표시
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
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);
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
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.
댓글 수: 0
답변 (1개)
Les Beckham
2023년 3월 7일
편집: Les Beckham
2023년 3월 7일
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
Walter Roberson
2023년 4월 13일
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.
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!