Supply a format to your fscanf()
Read data from serial device
4 次查看(过去 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 (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
0 个评论
回答(1 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Point Cloud Processing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!