Allora eccomi tornato...dopo aver fatto alcune prove e capito un po' meglio tutta la questione sono riuscito a mandare delle float attraverso il CanBus. Il problema ora è che non capisco come salvare i dati in arrivo per convertirli da HEX a decimale e ri-salvarli in una float da poter usare.
Vi allego i codici del trasmettitore e ricevitore.
SENDER
#include <Arduino_CAN.h>
static uint32_t const CAN_ID = 0x01; //Indirizzo CAN
void setup() {
Serial.begin(115200); //Inizializza seriale
if (!CAN.begin(CanBitRate::BR_250k)) {
Serial.println("CAN.begin(...) failed.");
for (;;) {}
}
}
float msg_cnt = 22.30; //Contenuto messaggio
void loop() {
/* Assemble a CAN message with the format of
* 0xCA 0xFE 0x00 0x00 [4 byte message counter]
*/
uint8_t const msg_data[8] = {}; //Buffer messaggio
memcpy((void *)(msg_data), &msg_cnt, sizeof(msg_cnt)); //Copia contenuto messaggio nel buffer
CanMsg const msg(CanStandardId(CAN_ID), sizeof(msg_data), msg_data); //Spedisce messaggio
/* Transmit the CAN message, capture and display an
* error core in case of failure.
*/
if (int const rc = CAN.write(msg); rc < 0) {
Serial.print("CAN.write(...) failed with error code ");
Serial.println(rc);
for (;;) {}
}
/* Only send one message per second. */
delay(100);
}
RECEIVER
#include <Arduino_CAN.h>
/**************************************************************************************
* SETUP/LOOP
**************************************************************************************/
void setup() {
Serial.begin(115200);
while (!Serial) {}
if (!CAN.begin(CanBitRate::BR_250k)) {
Serial.println("CAN.begin(...) failed.");
for (;;) {}
}
}
void loop() {
if (CAN.available()) {
CanMsg const msg = CAN.read();
if (msg.getStandardId() == 0x00){
Serial.print("0x00 ");
Serial.println(msg);
}
if (msg.getStandardId()==0x01){
Serial.print("0x01 ");
Serial.println(msg);
}
}
}