Loosing data during serial connection.

2 vues (au cours des 30 derniers jours)
Vic Bay
Vic Bay le 25 Nov 2016
Commenté : Vic Bay le 25 Nov 2016
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;

Réponses (1)

Walter Roberson
Walter Roberson le 25 Nov 2016
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 commentaires
Vic Bay
Vic Bay le 25 Nov 2016
I use USB-UART converter. But I've tried to set BR 9600. Has no effect
Vic Bay
Vic Bay le 25 Nov 2016
My Bad. You were right! Decreasing baud rate to 9600 value helped! Thank you!

Connectez-vous pour commenter.

Catégories

En savoir plus sur Data Type Conversion dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by