readpacket.m
来自「基于串行接口的GPS驱动程序」· M 代码 · 共 35 行
M
35 行
function [id, data] = ReadPacket(gps, type)
% READPACKET Read a single packet from the device
global pid_ack_byte pid_nak_byte
if (nargin < 2)
type = 'uint8';
end
% Initialize output data
data = [];
% Read the packet header (id and size)
[id, sz] = ReadPacketHeader(gps);
% Read data, if non-zero size
if (sz > 0)
[data, d] = ReadData(gps, id, sz / sizeof(type), type);
end
% Read checksum and packet terminator
cksum = ReadPacketTerminator(gps);
% Verify checksum
csum = ComputeChecksum(id, d);
if (csum ~= cksum)
WritePacket(gps, pid_nak_byte, id);
error(['Checksum mismatch. Got ', num2str(cksum), ', expecting ', num2str(csum)]);
end
% Send ACK, unless we just read an ACK or NAK
if ((pid_ack_byte ~= id) && (pid_nak_byte ~= id))
WritePacket(gps, pid_ack_byte, id);
end
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?