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에서 Code Generation, GPU, and Third-Party Support에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by