Read data from serial device
1 回表示 (過去 30 日間)
古いコメントを表示
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 件のコメント
回答 (0 件)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!