MATLAB 无法从串行 COM 端口读取数据
MATLAB cannot read data from serial COM port
我正在使用 TSI flowmeter 4040,它使用 RS232 串行 COM 端口(38400 波特率,8 个数据位,1 个停止位,none 极性,none 流控制) .流量计数据格式为ASCII。
根据数据表,在 PuTTY 中,我需要键入命令 DAFxx1000 以从流量计获取 1000 个数据读数。现在,我尝试在 MATLAB 中以同样的方式进行操作。连接成功。但是,我无法读取传感器数据(见图)。请问有人知道为什么吗?
谢谢!
问题已解决。 TSI 4040流量计有点特别。它returns:
确定(换行)
data1,data2,data3(换行)
所以我需要使用下面的 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
我正在使用 TSI flowmeter 4040,它使用 RS232 串行 COM 端口(38400 波特率,8 个数据位,1 个停止位,none 极性,none 流控制) .流量计数据格式为ASCII。
根据数据表,在 PuTTY 中,我需要键入命令 DAFxx1000 以从流量计获取 1000 个数据读数。现在,我尝试在 MATLAB 中以同样的方式进行操作。连接成功。但是,我无法读取传感器数据(见图)。请问有人知道为什么吗?
谢谢!
问题已解决。 TSI 4040流量计有点特别。它returns: 确定(换行) data1,data2,data3(换行) 所以我需要使用下面的 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