Does MATLAB support UBX protocol ?
24 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
data stream coming from my receiver has binary stream of data in ubx form.
The message structure of the ubx binary lines that I am interetsed in always start with b5 hexa.
how do I write the matlab to read the binary line and also is there any callback function for binary data stream?
2 commentaires
Aditya Patil
le 31 Mar 2021
Can you provide more details about the device and the format itself? That way the community will find it easier to help you.
Réponses (1)
Abhimenyu
le 11 Nov 2024 à 7:09
Hi Samuel,
I understand that you want to read and process the binary data stream from your receiver in MATLAB. You can use a combination of serial port reading and data parsing techniques.
Please find the below-mentioned example MATLAB code that continuously reads and processes the incoming binary data stream, identifying and handling the UBX messages as they arrive:
- Function gnssbinary: This function first initializes a serial port connection on COM4 with a baud rate of 115200 and then sets up a callback function readUBXData using the MATLAB's configureCallback function.
function gnssbinary
% Set up the serial port and configure callback
device = serialport("COM4", 115200);
configureCallback(device, "byte", 1, @readUBXData);
end
- Function readUBXData: This function uses a persistent variable buffer to accumulate incoming data, ensuring that partial messages can be buffered until complete. It then reads available data from the serial port and appends it to the buffer.
function readUBXData(device, ~)
persistent buffer;
if isempty(buffer), buffer = []; end
% Append new data to buffer
buffer = [buffer; read(device, device.NumBytesAvailable, 'uint8')];
- UBX Message Processing: This starts with a header search that looks for the UBX header (0xB5, 0x62) in the buffer. It then extracts message length and checks if the full message is available. Then the complete message is extracted, validated using checksum, and displayed if its valid.
% Process buffer for UBX messages
while length(buffer) >= 8
% Find UBX header
headerIndex = find(buffer(1:end-7) == 0xB5 & buffer(2:end-6) == 0x62, 1);
if isempty(headerIndex), return; end
% Validate message length and availability
lengthL = buffer(headerIndex+4);
lengthH = buffer(headerIndex+5);
msgLength = lengthL + lengthH * 256;
if length(buffer) < headerIndex + 7 + msgLength, return; end
% Extract and validate message
message = buffer(headerIndex:headerIndex+7+msgLength);
if validateChecksum(message(3:end-2), message(end-1:end))
disp('UBX Message Found:');
disp(dec2hex(message));
% Add message processing here
else
disp('Checksum failed. Message discarded.');
end
- Buffer Management: This removes the processed message from the buffer to prevent overflow and ensure efficient memory usage.
% Remove processed message from buffer
buffer(1:headerIndex+7+msgLength) = [];
end
end
- Function validateChecksum: This computes the checksum over the payload using a simple accumulation method and then compares the calculated checksum with the received checksum to ensure data integrity.
function isValid = validateChecksum(payload, checksum)
% Calculate and compare checksum
CK_A = sum(payload);
CK_B = sum(cumsum(payload));
isValid = mod(CK_A, 256) == checksum(1) && mod(CK_B, 256) == checksum(2);
end
This should effectively handle streaming UBX data while ensuring the integrity and correctness of the processed messages.
Please use the below-given MATLAB R2024b documentation links for more information:
- 'Use Callbacks for Serial Port Communication': https://www.mathworks.com/help/matlab/import_export/use-events-and-callbacks-for-serial-port-communication.html
- 'configureCallback': https://www.mathworks.com/help/matlab/ref/serialport.configurecallback.html
I hope this helps!
0 commentaires
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!