samm_flynn
Active member
I am using a teensy 4.1
I am trying to calculate CRC32 of a large float array, when I upload the code using arduino IDE, the result matches with the CRC32 calculated in python
how ever, when I upload the same code using platformio, the CRC is different.
I also get a warning -
arduino/platformio code -
Python code -
I am trying to calculate CRC32 of a large float array, when I upload the code using arduino IDE, the result matches with the CRC32 calculated in python
how ever, when I upload the same code using platformio, the CRC is different.
Code:
"""
python : CRC32 of float array: 0x6CCB2E56
arduino : CRC32 of float array: 0x6CCB2E56
pio : CRC32 of float array: 0x6E3BCDC6
"""
I also get a warning -
Code:
C:/Users/___/OneDrive/Documents/PlatformIO/Projects/250222-170929-teensy41/src/FastCRC_CRC32.ino: In function 'void loop()':
C:/Users/___/OneDrive/Documents/PlatformIO/Projects/250222-170929-teensy41/src/FastCRC_CRC32.ino:33:100: warning: conversion from 'unsigned int' to 'uint16_t' {aka 'short unsigned int'} changes value from '511872' to '53120' [-Woverflow]
33 | Serial.printf("CRC32 of float array: 0x%08X\n", CRC32.crc32(reinterpret_cast<uint8_t*>(arr), sizeof(arr)));
arduino/platformio code -
C++:
#include <Arduino.h>
#include <FastCRC.h>
#define N 21328
#define M 6
DMAMEM float arr[N][M]; // Large float array
bool hasRun = false; // Flag to ensure it runs only once per connection
void setup()
{
Serial.begin(115200); // Initialize serial communication
for (int i = 0; i < N; i++)
{
for (int j = 0; j < M; j++)
{
arr[i][j] = static_cast<float>(i * M + j); // Matches np.arange()
}
}
}
void loop()
{
if (Serial.dtr()) // When a Python script opens the serial port
{
if (!hasRun)
{
FastCRC32 CRC32;
hasRun = true; // Set flag to prevent multiple runs during connection
Serial.printf("CRC32 of float array: 0x%08X\n", CRC32.crc32(reinterpret_cast<uint8_t*>(arr), sizeof(arr)));
}
}
else
{
hasRun = false; // Reset when Python disconnects
}
}
Python code -
Python:
import binascii
import numpy as np
import serial
SERIAL_PORT = "COM8"
BAUD_RATE = 4608000
def compute_crc32(data: bytes) -> int:
return binascii.crc32(data) & 0xFFFFFFFF
# Example Usage
if __name__ == "__main__":
# Example 1: Compute CRC32 of a float array (N x M)
N, M = 21328, 6 # Same as C++
float_arr = np.arange(N * M, dtype=np.float32).reshape(N, M)
crc_float_array = compute_crc32(float_arr.tobytes())
print(f"python : CRC32 of float array: 0x{crc_float_array:08X}")
with serial.Serial(SERIAL_PORT, BAUD_RATE, timeout=0.1) as teensy:
recv = teensy.readline()
print(f"teensy : {recv.decode()}")
"""
python : CRC32 of float array: 0x6CCB2E56
arduino : CRC32 of float array: 0x6CCB2E56
pio : CRC32 of float array: 0x6E3BCDC6
"""