Reading Bytes from Arduino to MATLAB
I’ve been trying to read data from ESP32S-Dev module to MATLAB sending over the data in bytes to make the transmission quicker. As seen below, I convert float data into bytes and then write the data. MATLAB sees the data and then creates a 1×8 array for each float value instead of just 1 value for each float. I’ve tried other methods, shown in 2nd part of MATLAB code, and it creates 1 value for each float but their wildly off (7-34 orders of magnitude)
I’ve tried different baud rates and even asked for help from the almighty ChatGPT but have not been able to crack the code. Any thoughts.
Arduino IDE code:
#include <SPI.h>
const int CS_SiPM_pot = 34;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200); //Starting
delay(1000);
SPI.begin();
pinMode(CS_SiPM_pot, OUTPUT);
}
void loop() {
for (int d_pot2 = 55; d_pot2 <= 55; d_pot2 = d_pot2 + 1) //57
{
digitalWrite(CS_SiPM_pot, LOW); //LOW
delay(10); //10
SPI.transfer(d_pot2);
digitalWrite(CS_SiPM_pot, HIGH);
// put your main code here, to run repeatedly:
float newTmp[4] = { 01.00, 02.00, 03.00, 04.00 };
for (int i = 0; i < 4; i++) {
byte *byteData = (byte *)&newTmp[i];
Serial.write(byteData, sizeof(float)); // Send each float as 4 bytes
delay(10);
}
}
}
MATLAB code:
esp = serialport(‘COM5’,152000);
%%
flush(esp)
% Read 16 bytes of data (4 floats * 4 bytes per float)
numBytes = 16;
data = read(esp, numBytes, ‘uint8’);
% Convert each 4-byte sequence to a float
float1 = typecast(data(1:4)), ‘single’);
float2 = typecast(data(5:8), ‘single’);
float3 = typecast(data(9:12), ‘single’);
float4 = typecast(data(13:16), ‘single’);
disp([float1, float2, float3, float4]);
% Convert the bytes back to float values
floatValues = typecast(uint8(data), ‘single’);
% Display the result
disp(‘Float values:’);
disp(floatValues);
disp(‘Raw bytes received:’);
disp(data); % Print the raw bytes
Results from MATLAB:
Float1: 0 0 0 0 0 0 0 0
Float2: 0 3.7480 0 0 0 0 0 0
Float3: 0 2.1250 0 3.7480 0 0 0 0
Float4: 0 0 0 0 0 3.7480 0 0
data: 0 0 0 0 255 0 0 0 3 255 0 0 0 0 255 0I’ve been trying to read data from ESP32S-Dev module to MATLAB sending over the data in bytes to make the transmission quicker. As seen below, I convert float data into bytes and then write the data. MATLAB sees the data and then creates a 1×8 array for each float value instead of just 1 value for each float. I’ve tried other methods, shown in 2nd part of MATLAB code, and it creates 1 value for each float but their wildly off (7-34 orders of magnitude)
I’ve tried different baud rates and even asked for help from the almighty ChatGPT but have not been able to crack the code. Any thoughts.
Arduino IDE code:
#include <SPI.h>
const int CS_SiPM_pot = 34;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200); //Starting
delay(1000);
SPI.begin();
pinMode(CS_SiPM_pot, OUTPUT);
}
void loop() {
for (int d_pot2 = 55; d_pot2 <= 55; d_pot2 = d_pot2 + 1) //57
{
digitalWrite(CS_SiPM_pot, LOW); //LOW
delay(10); //10
SPI.transfer(d_pot2);
digitalWrite(CS_SiPM_pot, HIGH);
// put your main code here, to run repeatedly:
float newTmp[4] = { 01.00, 02.00, 03.00, 04.00 };
for (int i = 0; i < 4; i++) {
byte *byteData = (byte *)&newTmp[i];
Serial.write(byteData, sizeof(float)); // Send each float as 4 bytes
delay(10);
}
}
}
MATLAB code:
esp = serialport(‘COM5’,152000);
%%
flush(esp)
% Read 16 bytes of data (4 floats * 4 bytes per float)
numBytes = 16;
data = read(esp, numBytes, ‘uint8’);
% Convert each 4-byte sequence to a float
float1 = typecast(data(1:4)), ‘single’);
float2 = typecast(data(5:8), ‘single’);
float3 = typecast(data(9:12), ‘single’);
float4 = typecast(data(13:16), ‘single’);
disp([float1, float2, float3, float4]);
% Convert the bytes back to float values
floatValues = typecast(uint8(data), ‘single’);
% Display the result
disp(‘Float values:’);
disp(floatValues);
disp(‘Raw bytes received:’);
disp(data); % Print the raw bytes
Results from MATLAB:
Float1: 0 0 0 0 0 0 0 0
Float2: 0 3.7480 0 0 0 0 0 0
Float3: 0 2.1250 0 3.7480 0 0 0 0
Float4: 0 0 0 0 0 3.7480 0 0
data: 0 0 0 0 255 0 0 0 3 255 0 0 0 0 255 0 I’ve been trying to read data from ESP32S-Dev module to MATLAB sending over the data in bytes to make the transmission quicker. As seen below, I convert float data into bytes and then write the data. MATLAB sees the data and then creates a 1×8 array for each float value instead of just 1 value for each float. I’ve tried other methods, shown in 2nd part of MATLAB code, and it creates 1 value for each float but their wildly off (7-34 orders of magnitude)
I’ve tried different baud rates and even asked for help from the almighty ChatGPT but have not been able to crack the code. Any thoughts.
Arduino IDE code:
#include <SPI.h>
const int CS_SiPM_pot = 34;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200); //Starting
delay(1000);
SPI.begin();
pinMode(CS_SiPM_pot, OUTPUT);
}
void loop() {
for (int d_pot2 = 55; d_pot2 <= 55; d_pot2 = d_pot2 + 1) //57
{
digitalWrite(CS_SiPM_pot, LOW); //LOW
delay(10); //10
SPI.transfer(d_pot2);
digitalWrite(CS_SiPM_pot, HIGH);
// put your main code here, to run repeatedly:
float newTmp[4] = { 01.00, 02.00, 03.00, 04.00 };
for (int i = 0; i < 4; i++) {
byte *byteData = (byte *)&newTmp[i];
Serial.write(byteData, sizeof(float)); // Send each float as 4 bytes
delay(10);
}
}
}
MATLAB code:
esp = serialport(‘COM5’,152000);
%%
flush(esp)
% Read 16 bytes of data (4 floats * 4 bytes per float)
numBytes = 16;
data = read(esp, numBytes, ‘uint8’);
% Convert each 4-byte sequence to a float
float1 = typecast(data(1:4)), ‘single’);
float2 = typecast(data(5:8), ‘single’);
float3 = typecast(data(9:12), ‘single’);
float4 = typecast(data(13:16), ‘single’);
disp([float1, float2, float3, float4]);
% Convert the bytes back to float values
floatValues = typecast(uint8(data), ‘single’);
% Display the result
disp(‘Float values:’);
disp(floatValues);
disp(‘Raw bytes received:’);
disp(data); % Print the raw bytes
Results from MATLAB:
Float1: 0 0 0 0 0 0 0 0
Float2: 0 3.7480 0 0 0 0 0 0
Float3: 0 2.1250 0 3.7480 0 0 0 0
Float4: 0 0 0 0 0 3.7480 0 0
data: 0 0 0 0 255 0 0 0 3 255 0 0 0 0 255 0 arduino, esp32, matlab, serialport, float-to-bytes MATLAB Answers — New Questions