Read data from serial device

1 次查看(过去 30 天)
Jim O'Doherty
Jim O'Doherty 2013-8-12
Hi,
I'm trying to read data from a device which constantly spits out data (every second I think). I've found some very good tips in other posts but I'm still a bit stuck. I have the code:
function test_pushbutton_Callback(hObject, eventdata, handles)
s = serial('COM1','BaudRate',19200);
set(s, 'FlowControl', 'hardware');
s.BytesAvailableFcnCount = 28;
s.BytesAvailableFcnMode = 'byte';
s.BytesAvailableFcn = @instrcallback;
fopen(s)
fscanf(s)
fclose(s)
delete(s)
clear s
which produces the output:
BytesAvailable event occurred at 16:36:33 for the object: Serial-COM1.
BytesAvailable event occurred at 16:36:34 for the object: Serial-COM1.
BytesAvailable event occurred at 16:36:35 for the object: Serial-COM1.
BytesAvailable event occurred at 16:36:36 for the object: Serial-COM1.
BytesAvailable event occurred at 16:36:37 for the object: Serial-COM1.
BytesAvailable event occurred at 16:36:38 for the object: Serial-COM1.
BytesAvailable event occurred at 16:36:39 for the object: Serial-COM1.
BytesAvailable event occurred at 16:36:40 for the object: Serial-COM1.
BytesAvailable event occurred at 16:36:41 for the object: Serial-COM1.
BytesAvailable event occurred at 16:36:42 for the object: Serial-COM1.
Warning: Unsuccessful read: A timeout occurred before the
Terminator was reached.
000017 000002 000000 000000
ÿ000017 000002 000000 000000
ÿ000017 000002 000000 000000
ÿ000017 000002 000000 000000
ÿ000017 000002 000000 000000
ÿ000017 000002 000000 000000
ÿ000017 000002 000000 000000
ÿ000017 000002 000000 000000
ÿ000017 000002 000000 000000
ÿ000017 000002 000000 000000
ÿ
Would anyone have any idea how I would format the stream so that I get only each row for each second? Basically I just want to write the stream to an excel file. Each row is always the same length. I think the "ÿ" might be a terminator but can't set this in the object. How do I decide how many bytes I need?
Thanks, Jim

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Import from MATLAB 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by