Does MATLAB support UBX protocol ?

24 visualizzazioni (ultimi 30 giorni)
Samuel Olufemi Taiwo
Samuel Olufemi Taiwo il 26 Mar 2021
Risposto: Abhimenyu il 11 Nov 2024 alle 7:09
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 Commenti
Aditya Patil
Aditya Patil il 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.
Samuel Olufemi Taiwo
Samuel Olufemi Taiwo il 7 Apr 2021
I am trying to read streams of binary data in from a device via the serial port.
My short code is shown below:
function gnssbinary
device = serialport("COM4", 115200);
data = read(device,50000, 'uint64');
disp(dec2hex(data))
end
  1. The information I am looking or have header, Class and and ID of 0 x b5 , 0 x 62 , 0 x 02 and 0 x 15 in that order and the following messages should be the real information I need.
  2. I arbitrary used 5000 as the number of values (in the read syntax) to be read because it is like looking for a needle in a hay stack
  3. I have been able to get 0 x b5 , 0 x 62 , 0 x 02 from the binary but not yet what I am looking for which should still have 0 x 15.
  4. how do I program the MATLAB to keep identify the messahe identifier in (1) above, read the messages that come after and ignore the rest until it encounters the same message identifer again and repeat the process?

Accedi per commentare.

Risposte (1)

Abhimenyu
Abhimenyu il 11 Nov 2024 alle 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:
  1. 'Use Callbacks for Serial Port Communication': https://www.mathworks.com/help/matlab/import_export/use-events-and-callbacks-for-serial-port-communication.html
  2. 'configureCallback': https://www.mathworks.com/help/matlab/ref/serialport.configurecallback.html
I hope this helps!

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by