Transmit bitstream through serial port

14 vues (au cours des 30 derniers jours)
Benjamin
Benjamin le 14 Mai 2023
Commenté : Walter Roberson le 15 Mai 2023
Hi Matlab enthusiats,
I`m currently trying to send data (calculated from matlab) via the serial port of my computer to a board with a small microcontroller on it. The serial communication works in principle but I first want to convert the numbers from float to fixed point numbers for easier handling on the microcontroller. The transformation to fixed point works as well but as I`m trying to send the data matlab want`s a precision variable in the write command which yields to matlab interpreting the data and converting each single bit to a number of the given precision type.
So my question is there a possible way to directly transmit a bitstream (chopped down to 8 bits packages) via the serial port? (I know I can run the char array through a for loop and convert it to an integer number and transmit that but i was wondering if there would be an easier solution).
Thanks in advance!
Greetings
Benjamin
PS: My test code:
device = serialport("COM5",9600)
q = quantizer('fixed', 'nearest', 'saturate', [16 8]);
write(device,num2bin(q,pi),"uint8");

Réponses (2)

Shaik
Shaik le 14 Mai 2023
Hi,
% Open the serial port
device = serialport("COM5", 9600);
% Convert the floating-point number to fixed point
q = quantizer('fixed', 'nearest', 'saturate', [16 8]);
fixedData = num2bin(q, pi);
% Convert the bitstream to uint8
binaryData = reshape(fixedData.', 1, []);
binaryData = binaryData - '0'; % Convert from char '0'/'1' to numeric 0/1
binaryData = uint8(binaryData);
% Transmit the bitstream
fwrite(device, binaryData);
% Close the serial port
delete(device);
clear device;
  3 commentaires
Shaik
Shaik le 15 Mai 2023
% Open serial port connection
s = serialport("COM1", 9600); % Replace "COM1" with the correct port and 9600 with the desired baud rate
% Data to transmit
value = int16(1234);
% Convert the value to bytes
bytes = typecast(value, 'uint8');
% Transmit the bytes over the serial port
write(s, bytes);
% Close the serial port connection
delete(s);
clear s;
Walter Roberson
Walter Roberson le 15 Mai 2023
The question has to do specifically with having used the Fixed Point toolbox to create a representation of the data. The example given was to represent pi in 16 bits with 8 of the bits being fraction. You cannot just int16() a floating point number and typecast() to uint8 .
In the particular case of [16 8] you might be able to use typecast(int16(VALUE*256), 'uint8') -- though it would be a good idea to check the boundary conditions to be sure that values close to the boundaries are treated the same way by this code as compared to the code I show using num2int()

Connectez-vous pour commenter.


Walter Roberson
Walter Roberson le 14 Mai 2023
bytes = typecast(swapbytes(int16(num2int(q,VALUES))), 'uint8')
Note: this has been deliberately arranged to have the high-order bytes first -- for example, [3 36] in transmission order corresponding to 3*256 + 36 = 804. The default, without swapbytes, is to use "in-memory" order, and in-memory order for all supported CPUs is currently "little-endian", low byte followed by high byte in memory. If you are transmitting to another MATLAB system with the intention of reconstructing the fixed-point number then the swapbytes adds overhead that might not be technically necessary (but in my experience, "big-endian" is notably easier to debug than "little-endian")

Produits


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by