首页 > 解决方案 > MATLAB 无法从串行 COM 端口读取数据

问题描述

我正在使用TSI 流量计 4040,它使用 RS232 串行 COM 端口(38400 波特率,8 个数据位,1 个停止位,无极性,无流量控制)。流量计数据格式为 ASCII。

根据数据表,在 PuTTY 中,我需要键入命令DAFxx1000从流量计获取 1000 个数据读数。现在,我尝试在 MATLAB 中做同样的事情。连接成功。但是,我无法读取传感器数据(见图)。请问有人知道为什么吗?

谢谢!

MATLAB 截图

标签: matlabserial-port

解决方案


问题解决了。TSI 4040 流量计有点特别。它返回: OK (change line) data1,data2,data3 (change line) 所以我需要使用以下 MATLAB 命令:

% Show all available COM serial ports on the PC
serialportlist

% Set COM serial port, baud rate
%{ 
Default setting: 
Baud rate: 38400
Data bits: 8 bits
Stop bit: 1 bit
Parity: None
Flow control: None
Timeout: 10 seconds
%}
s = serialport("COM1",38400);


% Set terminator for ASCII string communication
% CR = Carriage Return
% LF = Line Feed
configureTerminator(s,"CR/LF");


% Display the terminator 
s.Terminator


%% We MUST manually stop the while loop when it's finished
%{
Steps: 
1. Send command to TSI flowmeter 
2. Read and save data to the string array: OK (change line)
3. Read and save data to the string array: flowmeter return data
%}
i = 1;
while 1
    writeline(s,"DAFTP0100");

    OK(i) = readline(s);
    data(i) = readline(s);
    i = i + 1;
end

推荐阅读