Loosing data during serial connection.

조회 수: 3 (최근 30일)
Vic Bay
Vic Bay 2016년 11월 25일
댓글: Vic Bay 2016년 11월 25일
Dear All! I have a device sending data to my PC via COM port. Firstly device sends 1000 strings with data, then another info (plus 3 strings) and after all EOT symbol. I receive and process all this data on my PC using MATLAB. But MATLAB receives only 608 strings, at that the last string includes a EOT symbol. When I use a terminal programm to receive data from my device it receives all the data. Please help me to resolve this problem. And one more question: I receive data while EOT symbol is received. Is there any elegant way to catch it? Thank you!
try
sp = serial(currentPort, 'baudRate', 115200);
msg = strcat(currentPort,' opened successfully!');
fopen(sp);
set(handles.state, 'ForegroundColor', [0, 1, 0]);
set(handles.state, 'String', msg);
catch ME
set(handles.state, 'ForegroundColor', [1, 0, 0]);
set(handles.state, 'String', 'Error');
fclose(sp);
msg = strcat(currentPort,' CLOSED!');
set(handles.state, 'ForegroundColor', [0, 0, 0]);
set(handles.state, 'String', msg);
delete(sp);
clear sp;
end;
%----------DATA aquisition&Processing----------
%----------------------------------------------
try
fprintf(sp, 'r');
set(handles.state, 'String', 'Aquiring data...');
i = 1;
while(sp.BytesAvailable)
receivedChar = fscanf(sp);
msg = strrep(receivedChar, ';', ' ');
msg = strrep(msg, ':', ' ');
if(str2num(msg))
numOfData = numOfData + 1;
end;
if(contains(msg, 'key'))
numOfKeys = numOfKeys + 1;
end;
dataCells{i,1} = msg;
i = i + 1;
disp(dataCells);
end;
catch ME
set(handles.state, 'ForegroundColor', [1, 0, 0]);
set(handles.state, 'String', 'Error');
fclose(sp);
msg = strcat(currentPort,' CLOSED!');
set(handles.state, 'ForegroundColor', [0, 0, 0]);
set(handles.state, 'String', msg);
delete(sp);
clear sp;
end;
keys = zeros(numOfKeys, 1);
time = zeros(numOfData,1);
dataODS10L = zeros(numOfData,1);
dataODS96 = zeros(numOfData, 1);
k = 1;
l = 1;
try
set(handles.state, 'String', 'Data processing...');
for i = 1 : size(dataCells)
if((~isempty(dataCells{i,1})))
if(contains(dataCells{i,1}, 'key'))
[keyToken, keyRemain] = strtok(dataCells{i, 1}, ' ')
keys(k) = str2double(keyRemain);
k = k + 1;
end;
[data, status] = str2num(dataCells{i,1});
if(status)
time(l) = data(1);
dataODS10L(l) = data(2);
dataODS96(l) = data(3);
l = l + 1;
end;
end;
end;
catch ME
set(handles.state, 'ForegroundColor', [1, 0, 0]);
set(handles.state, 'String', 'Error');
fclose(sp);
msg = strcat(currentPort,' CLOSED!');
set(handles.state, 'ForegroundColor', [0, 0, 0]);
set(handles.state, 'String', msg);
delete(sp);
clear sp;
end;
fclose(sp);
delete(sp);
clear sp;

답변 (1개)

Walter Roberson
Walter Roberson 2016년 11월 25일
When you use rates beyond 9600 with actual rs232 then you will typically lose data unless you turn on hardware flow control.
This consideration is not relevant for serial emulated over USB.
  댓글 수: 2
Vic Bay
Vic Bay 2016년 11월 25일
I use USB-UART converter. But I've tried to set BR 9600. Has no effect
Vic Bay
Vic Bay 2016년 11월 25일
My Bad. You were right! Decreasing baud rate to 9600 value helped! Thank you!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by