Having troubel sending messages
Hi I am programming a micrcontroller to send messages to a chager, this is because I am currently in a formula student team and the charger will be used to charge the batteries. The problem is that I am sending the through the micro but they are not being sent because I have already tried to send the messages directly from my PC to the charger and it worked, so I can only imagine that my difficulties are inside the code. This is my code:
#include <Arduino.h>
#include <FlexCAN_T4.h>
FlexCAN_T4<CAN1, RX_SIZE_256, TX_SIZE_16> can1;
void setup() {
Serial.begin(9600);
can1.begin();
can1.setBaudRate(125000);
CAN_message_t msg; // power on all modules
msg.id = 0x02204000;
msg.len = 8;
msg.buf[0] = 0x10;
msg.buf[1] = 0x04;
msg.buf[2] = 0x00;
msg.buf[3] = 0x00;
msg.buf[4] = 0x00;
msg.buf[5] = 0x00;
msg.buf[6] = 0x00;
msg.buf[7] = 0x00;
can1.write(msg);
CAN_message_t msg2; // set an output reference of 100V
msg2.id = 0x02200000;
msg2.len = 8;
msg2.buf[0] = 0x10;
msg2.buf[1] = 0x02;
msg2.buf[2] = 0x00;
msg2.buf[3] = 0x00;
msg2.buf[4] = 0x00;
msg2.buf[5] = 0x06;
msg2.buf[6] = 0xED;
msg2.buf[7] = 0x70;
can1.write(msg2);
CAN_message_t msg3; // set an output reference of 10A
msg3.id = 0x02200000;
msg3.len = 8;
msg3.buf[0] = 0x10;
msg3.buf[1] = 0x03;
msg3.buf[2] = 0x00;
msg3.buf[3] = 0x00;
msg3.buf[4] = 0x00;
msg3.buf[5] = 0x00;
msg3.buf[6] = 0x27;
msg3.buf[7] = 0x10;
}
void loop() {
CAN_message_t msg4; //read output voltage of all modules
msg4.id = 0x02200000;
msg4.len = 8;
msg4.buf[0] = 0x12;
msg4.buf[1] = 0x00;
msg4.buf[2] = 0x00;
msg4.buf[3] = 0x00;
msg4.buf[4] = 0x00;
msg4.buf[5] = 0x00;
msg4.buf[6] = 0x00;
msg4.buf[7] = 0x00;
CAN_message_t message2;
if (can1.read(message2)) {
Serial.print("Received message with ID 0x");
Serial.print(message2.id, HEX);
Serial.print(": ");
for (int i = 0; i < message2.len; i++) {
Serial.print(message2.buf);
}
Serial.print('\n');
}
//Serial.println("teste");
delay(10);
}
@jmlima44 mine works like this:
Code:#include <FlexCAN_T4.h> CAN_message_t can_fwd_car; FlexCAN_T4<CAN2, RX_SIZE_32, TX_SIZE_32> can_master; void setup() { // put your setup code here, to run once: can_master.begin(); can_master.setBaudRate(125000); } void loop() { // put your main code here, to run repeatedly: if (can_master.read(can_fwd_car)) { // do something with the data in can_fwd_car } // rest of code }
Hi I am programming a micrcontroller to send messages to a chager, this is because I am currently in a formula student team and the charger will be used to charge the batteries. The problem is that I am sending the through the micro but they are not being sent because I have already tried to send the messages directly from my PC to the charger and it worked, so I can only imagine that my difficulties are inside the code. This is my code:
#include <Arduino.h>
#include <FlexCAN_T4.h>
FlexCAN_T4<CAN1, RX_SIZE_256, TX_SIZE_16> can1;
void setup() {
Serial.begin(9600);
can1.begin();
can1.setBaudRate(125000);
CAN_message_t msg; // power on all modules
msg.id = 0x02204000;
msg.len = 8;
msg.buf[0] = 0x10;
msg.buf[1] = 0x04;
msg.buf[2] = 0x00;
msg.buf[3] = 0x00;
msg.buf[4] = 0x00;
msg.buf[5] = 0x00;
msg.buf[6] = 0x00;
msg.buf[7] = 0x00;
can1.write(msg);
CAN_message_t msg2; // set an output reference of 100V
msg2.id = 0x02200000;
msg2.len = 8;
msg2.buf[0] = 0x10;
msg2.buf[1] = 0x02;
msg2.buf[2] = 0x00;
msg2.buf[3] = 0x00;
msg2.buf[4] = 0x00;
msg2.buf[5] = 0x06;
msg2.buf[6] = 0xED;
msg2.buf[7] = 0x70;
can1.write(msg2);
CAN_message_t msg3; // set an output reference of 10A
msg3.id = 0x02200000;
msg3.len = 8;
msg3.buf[0] = 0x10;
msg3.buf[1] = 0x03;
msg3.buf[2] = 0x00;
msg3.buf[3] = 0x00;
msg3.buf[4] = 0x00;
msg3.buf[5] = 0x00;
msg3.buf[6] = 0x27;
msg3.buf[7] = 0x10;
}
void loop() {
CAN_message_t msg4; //read output voltage of all modules
msg4.id = 0x02200000;
msg4.len = 8;
msg4.buf[0] = 0x12;
msg4.buf[1] = 0x00;
msg4.buf[2] = 0x00;
msg4.buf[3] = 0x00;
msg4.buf[4] = 0x00;
msg4.buf[5] = 0x00;
msg4.buf[6] = 0x00;
msg4.buf[7] = 0x00;
CAN_message_t message2;
if (can1.read(message2)) {
Serial.print("Received message with ID 0x");
Serial.print(message2.id, HEX);
Serial.print(": ");
for (int i = 0; i < message2.len; i++) {
Serial.print(message2.buf);
}
Serial.print('\n');
}
//Serial.println("teste");
delay(10);
}