Effacer les filtres
Effacer les filtres

Read data from serial device

1 vue (au cours des 30 derniers jours)
Jim O'Doherty
Jim O'Doherty le 13 Août 2013
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 (i.e. just the 4 data values) for each second? Basically I just want to write the stream to an excel file when a toggle state is on and stop it when its off. 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? And how do I make sure I start from the ÿ charachter?
Thanks, Jim

Réponses (1)

Walter Roberson
Walter Roberson le 13 Août 2013
Supply a format to your fscanf()

Catégories

En savoir plus sur Data Import from MATLAB 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