Parsing data from serial port
2 次查看(过去 30 天)
显示 更早的评论
Hi,
I communicate with a DsPIC
i have a constant format for a message:
HEADER1 HEADER2 OPCODE DATA CHECKSUM (no spaces)
in my MATLAB GUI i want to read the incoming message, chk if i get the HEADER1 I EXPCT, then chk to see i get HEADER2 i expect , if so: get OPCODE and DATA and do with that something
how can i split my message?
thought about switch;case
wanted to know if there is something more "elegant"
Thanks
2 个评论
Walter Roberson
2016-4-11
Is that a text format or a numeric format? are the headers variable length? How can you tell when HEADER1 stops and HEADER2 begins?
采纳的回答
Walter Roberson
2016-4-11
You need a Finite State Machine. The Mathworks tools for that are http://www.mathworks.com/discovery/finite-state-machine.html but you probably do not need anywhere near the full power of that.
current_state = 0;
while true
this_input = fread(s, 1, 'uint8');
if feof(s); break; end
if state == 0 && this_input == 88 %0x55
current_state = 1;
elseif current_state == 1 && this_input == 170 %0xAA
current_state = 2;
now read the opcode and data and so on.
When you are through with it
current_state = 0;
else
current_state = 0;
end
end
6 个评论
Walter Roberson
2016-4-14
"persistent". Or a shared variable if you need to be able to reset the state.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Properties 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!