www.pudn.com > SerialGPS.zip > ReadPacket.m
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