Missing Serial Port Data

10 views (last 30 days)
Rodney
Rodney on 20 Mar 2014
I am currently working on a GUI-based application that records serial data from an embedded system that is transmitted at 50Hz. The problem is that MATLAB appears incapable of recording this data reliably, and drops samples. The format of the transmitted data is as follows:
4918 22279 29342 3161 0 24953 29814 5319 1 0
4919 22279 29348 2997 1 24953 29838 5037 0 0
4920 22279 29357 2682 0 24953 29853 4544 0 0
Each row is terminated by a CR/LF. I have written a test function to try and figure out what is going on - the code for this is below.
function stack_stream(time)
column_count = 10;
serial_object = create_serial_object;
fopen(serial_object);
date_vector = clock;
file_name = datestr(date_vector,30);
file_name = [file_name '.txt'];
file_identifier = fopen(file_name,'w');
tic;
while (toc < time)
if (serial_object.UserData.is_new_data == true)
raw_chunk = serial_object.UserData.data;
serial_object.UserData.is_new_data = false;
data_chunk = sscanf(raw_chunk,'%d');
data_chunk_length = length(data_chunk);
if (mod(data_chunk_length,column_count) == 0)
data_column_count = data_chunk_length/column_count;
data = reshape(data_chunk,column_count,data_column_count);
fprintf(file_identifier,...
'%6d %6d %6d %6d %6d %6d %6d %6d %6d %6d\r\n',data);
end
end
end
fclose(file_identifier);
fclose(serial_object);
delete(serial_object);
end
function serial_object_callback(object,event)
new_data = fscanf(object,'%c',object.BytesAvailable);
if (object.UserData.is_new_data == false)
object.UserData.data = new_data;
object.UserData.is_new_data = true;
else
object.UserData.data = [object.UserData.data new_data];
end
end
function serial_object = create_serial_object()
serial_object = serial('COM2');
serial_object.BaudRate = 57600;
serial_object.DataBits = 8;
serial_object.FlowControl = 'none';
serial_object.StopBits = 1;
serial_object.Terminator = 'CR/LF';
serial_object.InputBufferSize = 2^18;
serial_object.BytesAvailableFcnMode = 'terminator';
serial_object.BytesAvailableFcn = {@serial_object_callback};
serial_object.UserData.data = [];
serial_object.UserData.is_new_data = false;
serial_object.UserData.response = [];
serial_object.UserData.is_new_response = false;
end
In essence, this function writes `time` seconds worth of data to file, using a callback to transfer data between the serial object buffer and a user buffer. Using `stack_stream` I find that I miss around 10 rows of data for every 3000 received - ie about 10 a minute.
Anecdotal evidence would lead me to believe that MATLAB is capable of handling serial data in the kHz range, so I am at somewhat of a loss to determine why I cannot get it to work properly at a paltry 50Hz. Is there anything I can do to improve performance without having to resort to the real-time target for windows or the like?
  1 Comment
Varsha Parthasarathy
Varsha Parthasarathy on 22 Aug 2020
Hey Rodney! Were you able to fix your problem? I am having similar issues where I always miss about 10% of my samples during serial communications.
Thanks

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 20 Mar 2014
Never use 57600 baud without hardware flow control.
  1 Comment
Rodney
Rodney on 20 Mar 2014
Given that I can use Hyperterminal to record the data stream, and not miss a single line of data in several hours of acquisition, the problem is not the lack of hardware flow control. Its MATLAB.

Sign in to comment.

Tags

Products

Community Treasure Hunt

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

Start Hunting!