CAN communication between two teensy 4.1

Hello,
I'm trying to communicate two teensy using CAN communication.
I connected VP230 to CAN1 to the first teensy using pins 22-23 for CTX1 and CRX1, and I connected the the CANL and CANH of the VP230 IC to CANL and CANH of a second IC that is connected to the second Teensy 4.1 board.
i checked with the oscilloscope and saw the CAN message from the first Teensy and after conversion to CANL and CANH and the CAN message is available, but when I connect the second Teensy to Read from it using also CAN1, the message on the oscilloscope disappear. My problem is i can't read the CAN message that I'm sending.
Can anyone help me to solve this problem or tell me if i have a connection mistake or coding mistake?

thank you in advance

This is my sender code:

#include <FlexCAN_T4.h>
FlexCAN_T4<CAN1, RX_SIZE_256, TX_SIZE_16> Can1;
#define sw1 35
#define sw2 36
void setup(void) {
Serial.begin(115200);
delay(400);

Can1.begin();
Can1.setBaudRate(1000000);
Can1.setMaxMB(16);
Can1.enableFIFO();
Can1.enableFIFOInterrupt();
Can1.onReceive(canSniff);
Can1.mailboxStatus();
Can1.enhanceFilter(MB6);

}

void canSniff(const CAN_message_t &msg) {
Serial.print("MB ");
Serial.print(msg.mb);
Serial.print(" OVERRUN: ");
Serial.print(msg.flags.overrun);
Serial.print(" LEN: ");
Serial.print(msg.len);
Serial.print(" EXT: ");
Serial.print(msg.flags.extended);
Serial.print(" TS: ");
Serial.print(msg.timestamp);
Serial.print(" ID: ");
Serial.print(msg.id, HEX);
Serial.print(" Buffer: ");
for ( uint8_t i = 0; i < msg.len; i++ ) {
Serial.print(msg.buf, HEX); Serial.print(" ");
}
Serial.println();
}

void loop() {

CAN_message_t msg;
msg.id = 0x1;
Can1.events();

enableMode();
delay(1000);
CW();
delay(1000);
CCW();
delay(1000);
Exit();
delay(1000);

}

void enableMode()
{
Serial.println("Enter Motor Mode");
CAN_message_t msg;
msg.id = 0x88;
msg.buf[0] = 0xFF;
msg.buf[1] = 0xFF;
msg.buf[2] = 0xFF;
msg.buf[3] = 0xFF;
msg.buf[4] = 0xFF;
msg.buf[5] = 0xFF;
msg.buf[6] = 0xFF;
msg.buf[7] = 0xFC;
Can1.write(msg);
}

void CW()
{
Serial.println("CW");
CAN_message_t msg;
msg.id = 0x1;
msg.buf[0] = 0x85;
msg.buf[1] = 0x52;
msg.buf[2] = 0x7F;
msg.buf[3] = 0xF0;
msg.buf[4] = 0x51;
msg.buf[5] = 0x11;
msg.buf[6] = 0xE7;
msg.buf[7] = 0xFF;
Can1.write(msg);
}

void CCW()
{
Serial.println("CCW");
CAN_message_t msg;
msg.id = 0x1;
msg.buf[0] = 0x7A;
msg.buf[1] = 0xAC;
msg.buf[2] = 0x7F;
msg.buf[3] = 0xF0;
msg.buf[4] = 0x51;
msg.buf[5] = 0x11;
msg.buf[6] = 0xE7;
msg.buf[7] = 0xFF;
Can1.write(msg);
}


void Exit()
{
Serial.println("Exit");
CAN_message_t msg;
msg.id = 0x1;
msg.buf[0] = 0xFF;
msg.buf[1] = 0xFF;
msg.buf[2] = 0xFF;
msg.buf[3] = 0xFF;
msg.buf[4] = 0xFF;
msg.buf[5] = 0xFF;
msg.buf[6] = 0xFF;
msg.buf[7] = 0xFD;
Can1.write(msg);
}

Receiver code:

#include <FlexCAN_T4.h>

#define debug(msg) Serial.print("["); Serial.print(__FILE__); Serial.print("::"); Serial.print(__LINE__); Serial.print("::"); Serial.print(msg); Serial.println("]");
void debug_pause(void)
{
Serial.print("Paused...");
while (!Serial.available());
while (Serial.available()) Serial.read();
Serial.println("Restarted.");
}

FlexCAN_T4<CAN1, RX_SIZE_256, TX_SIZE_16> Can1;
FlexCAN_T4<CAN2, RX_SIZE_256, TX_SIZE_16> Can2;

static CAN_message_t msg;
static uint8_t hex[17] = "0123456789abcdef";

// -------------------------------------------------------------
static void hexDump(uint8_t dumpLen, uint8_t *bytePtr)
{
uint8_t working;
while( dumpLen-- ) {
working = *bytePtr++;
Serial.write( hex[ working>>4 ] );
Serial.write( hex[ working&15 ] );
}
Serial.write('\r');
Serial.write('\n');
}


// -------------------------------------------------------------
void setup(void)
{
Serial.begin(115200);
int iSerialTimeout = 1000000;
delay(100);
while (!Serial && (iSerialTimeout-- != 0));
debug (F("start setup"));


Can1.setBaudRate(1000000); // I like to set the baud rates just to be on the safe side
Can2.setBaudRate(1000000);
Can1.begin();
Can2.begin();
// t4 missing msg.ext = 0;
msg.id = 0x100;
msg.len = 8;
msg.flags.extended = 0;
msg.flags.remote = 0;
msg.flags.overrun = 0;
msg.flags.reserved = 0;
msg.buf[0] = 10;
msg.buf[1] = 20;
msg.buf[2] = 0;
msg.buf[3] = 100;
msg.buf[4] = 128;
msg.buf[5] = 64;
msg.buf[6] = 32;
msg.buf[7] = 16;
debug (F("setup complete"));
//debug_pause();
}


// -------------------------------------------------------------
void loop(void)
{
msg.buf[0]++; //since you are in a loop i just incremented each time in stead of doing it 5 times
Can1.write(msg); //you could do it your way as well
Serial.println("T4.0cantest - Repeat: Read bus2, Write bus1");
CAN_message_t inMsg;
if (Can2.read(inMsg)!=0) // Changed this to if as as opposed to while - the way you had it just gets stuck since you haven't even sent a message yet
{
Serial.print("W RD bus 2: "); hexDump(8, inMsg.buf);
}
delay(20);
}
 
Code:
#include <FlexCAN_T4.h>
FlexCAN_T4<CAN1, RX_SIZE_256, TX_SIZE_16> Can1;
#define sw1 35
#define sw2 36
void setup(void) {
	Serial.begin(115200);
	delay(400);

	Can1.begin();
	Can1.setBaudRate(1000000);
	Can1.setMaxMB(16);
	Can1.enableFIFO();
	Can1.enableFIFOInterrupt();
	Can1.onReceive(canSniff);
	Can1.mailboxStatus();
	Can1.enhanceFilter(MB6);

}

void canSniff(const CAN_message_t& msg) {
	Serial.print("MB ");
	Serial.print(msg.mb);
	Serial.print(" OVERRUN: ");
	Serial.print(msg.flags.overrun);
	Serial.print(" LEN: ");
	Serial.print(msg.len);
	Serial.print(" EXT: ");
	Serial.print(msg.flags.extended);
	Serial.print(" TS: ");
	Serial.print(msg.timestamp);
	Serial.print(" ID: ");
	Serial.print(msg.id, HEX);
	Serial.print(" Buffer: ");
	for (uint8_t i = 0; i < msg.len; i++) {
		Serial.print(msg.buf[i], HEX); Serial.print(" ");
	}
	Serial.println();
}

void loop() {

	CAN_message_t msg;
	msg.id = 0x1;
	Can1.events();

	enableMode();
	delay(1000);
	CW();
	delay(1000);
	CCW();
	delay(1000);
	Exit();
	delay(1000);

}

void enableMode()
{
	Serial.println("Enter Motor Mode");
	CAN_message_t msg;
	msg.id = 0x88;
	msg.buf[0] = 0xFF;
	msg.buf[1] = 0xFF;
	msg.buf[2] = 0xFF;
	msg.buf[3] = 0xFF;
	msg.buf[4] = 0xFF;
	msg.buf[5] = 0xFF;
	msg.buf[6] = 0xFF;
	msg.buf[7] = 0xFC;
	Can1.write(msg);
}

void CW()
{
	Serial.println("CW");
	CAN_message_t msg;
	msg.id = 0x1;
	msg.buf[0] = 0x85;
	msg.buf[1] = 0x52;
	msg.buf[2] = 0x7F;
	msg.buf[3] = 0xF0;
	msg.buf[4] = 0x51;
	msg.buf[5] = 0x11;
	msg.buf[6] = 0xE7;
	msg.buf[7] = 0xFF;
	Can1.write(msg);
}

void CCW()
{
	Serial.println("CCW");
	CAN_message_t msg;
	msg.id = 0x1;
	msg.buf[0] = 0x7A;
	msg.buf[1] = 0xAC;
	msg.buf[2] = 0x7F;
	msg.buf[3] = 0xF0;
	msg.buf[4] = 0x51;
	msg.buf[5] = 0x11;
	msg.buf[6] = 0xE7;
	msg.buf[7] = 0xFF;
	Can1.write(msg);
}


void Exit()
{
	Serial.println("Exit");
	CAN_message_t msg;
	msg.id = 0x1;
	msg.buf[0] = 0xFF;
	msg.buf[1] = 0xFF;
	msg.buf[2] = 0xFF;
	msg.buf[3] = 0xFF;
	msg.buf[4] = 0xFF;
	msg.buf[5] = 0xFF;
	msg.buf[6] = 0xFF;
	msg.buf[7] = 0xFD;
	Can1.write(msg);
}
Code:
//Receiver code :

#include <FlexCAN_T4.h>

#define debug(msg) Serial.print("["); Serial.print(__FILE__); Serial.print("::"); Serial.print(__LINE__); Serial.print("::"); Serial.print(msg); Serial.println("]");
void debug_pause(void)
{
	Serial.print("Paused...");
	while (!Serial.available());
	while (Serial.available()) Serial.read();
	Serial.println("Restarted.");
}

FlexCAN_T4<CAN1, RX_SIZE_256, TX_SIZE_16> Can1;
FlexCAN_T4<CAN2, RX_SIZE_256, TX_SIZE_16> Can2;

static CAN_message_t msg;
static uint8_t hex[17] = "0123456789abcdef";

// -------------------------------------------------------------
static void hexDump(uint8_t dumpLen, uint8_t* bytePtr)
{
	uint8_t working;
	while (dumpLen--) {
		working = *bytePtr++;
		Serial.write(hex[working >> 4]);
		Serial.write(hex[working & 15]);
	}
	Serial.write('\r');
	Serial.write('\n');
}


// -------------------------------------------------------------
void setup(void)
{
	Serial.begin(115200);
	int iSerialTimeout = 1000000;
	delay(100);
	while (!Serial && (iSerialTimeout-- != 0));
	debug(F("start setup"));


	Can1.setBaudRate(1000000); // I like to set the baud rates just to be on the safe side
	Can2.setBaudRate(1000000);
	Can1.begin();
	Can2.begin();
	// t4 missing msg.ext = 0;
	msg.id = 0x100;
	msg.len = 8;
	msg.flags.extended = 0;
	msg.flags.remote = 0;
	msg.flags.overrun = 0;
	msg.flags.reserved = 0;
	msg.buf[0] = 10;
	msg.buf[1] = 20;
	msg.buf[2] = 0;
	msg.buf[3] = 100;
	msg.buf[4] = 128;
	msg.buf[5] = 64;
	msg.buf[6] = 32;
	msg.buf[7] = 16;
	debug(F("setup complete"));
	//debug_pause();
}


// -------------------------------------------------------------
void loop(void)
{
	msg.buf[0]++; //since you are in a loop i just incremented each time in stead of doing it 5 times
	Can1.write(msg); //you could do it your way as well
	Serial.println("T4.0cantest - Repeat: Read bus2, Write bus1");
	CAN_message_t inMsg;
	if (Can2.read(inMsg) != 0) // Changed this to if as as opposed to while - the way you had it just gets stuck since you haven't even sent a message yet
	{
		Serial.print("W RD bus 2: "); hexDump(8, inMsg.buf);
	}
	delay(20);
}
Can you post your code between code tags, using the # button.
It makes it much more readable and you are more likely to get some help.
 
#########################
#include <FlexCAN_T4.h>
FlexCAN_T4<CAN1, RX_SIZE_256, TX_SIZE_16> Can1;
#define sw1 35
#define sw2 36
void setup(void) {
Serial.begin(115200);
delay(400);

Can1.begin();
Can1.setBaudRate(1000000);
Can1.setMaxMB(16);
Can1.enableFIFO();
Can1.enableFIFOInterrupt();
Can1.onReceive(canSniff);
Can1.mailboxStatus();
Can1.enhanceFilter(MB6);

}

void canSniff(const CAN_message_t &msg) {
Serial.print("MB ");
Serial.print(msg.mb);
Serial.print(" OVERRUN: ");
Serial.print(msg.flags.overrun);
Serial.print(" LEN: ");
Serial.print(msg.len);
Serial.print(" EXT: ");
Serial.print(msg.flags.extended);
Serial.print(" TS: ");
Serial.print(msg.timestamp);
Serial.print(" ID: ");
Serial.print(msg.id, HEX);
Serial.print(" Buffer: ");
for ( uint8_t i = 0; i < msg.len; i++ ) {
Serial.print(msg.buf, HEX); Serial.print(" ");
}
Serial.println();
}

void loop() {

CAN_message_t msg;
msg.id = 0x1;
Can1.events();

enableMode();
delay(1000);
CW();
delay(1000);
CCW();
delay(1000);
// Exit();
// delay(1000);

}

void enableMode()
{
Serial.println("Enter Motor Mode");
CAN_message_t msg;
msg.id = 0x88;
msg.buf[0] = 0xFF;
msg.buf[1] = 0xFF;
msg.buf[2] = 0xFF;
msg.buf[3] = 0xFF;
msg.buf[4] = 0xFF;
msg.buf[5] = 0xFF;
msg.buf[6] = 0xFF;
msg.buf[7] = 0xFC;
Can1.write(msg);
}

void CW()
{
Serial.println("CW");
CAN_message_t msg;
msg.id = 0x1;
msg.buf[0] = 0x85;
msg.buf[1] = 0x52;
msg.buf[2] = 0x7F;
msg.buf[3] = 0xF0;
msg.buf[4] = 0x51;
msg.buf[5] = 0x11;
msg.buf[6] = 0xE7;
msg.buf[7] = 0xFF;
Can1.write(msg);
}

void CCW()
{
Serial.println("CCW");
CAN_message_t msg;
msg.id = 0x1;
msg.buf[0] = 0x7A;
msg.buf[1] = 0xAC;
msg.buf[2] = 0x7F;
msg.buf[3] = 0xF0;
msg.buf[4] = 0x51;
msg.buf[5] = 0x11;
msg.buf[6] = 0xE7;
msg.buf[7] = 0xFF;
Can1.write(msg);
}


void Exit()
{
Serial.println("Exit");
CAN_message_t msg;
msg.id = 0x1;
msg.buf[0] = 0xFF;
msg.buf[1] = 0xFF;
msg.buf[2] = 0xFF;
msg.buf[3] = 0xFF;
msg.buf[4] = 0xFF;
msg.buf[5] = 0xFF;
msg.buf[6] = 0xFF;
msg.buf[7] = 0xFD;
Can1.write(msg);
}

################################

#include <FlexCAN_T4.h>
#include <isotp.h>
isotp<RX_BANKS_16, 512> tp; /* 16 slots for multi-ID support, at 512bytes buffer each payload rebuild */

FlexCAN_T4<CAN1, RX_SIZE_256, TX_SIZE_16> Can1;

void myCallback(const ISOTP_data &config, const uint8_t *buf) {
Serial.print("ID: ");
Serial.print(config.id, HEX);
Serial.print("\tLEN: ");
Serial.print(config.len);
Serial.print("\tFINAL ARRAY: ");
for ( int i = 0; i < config.len; i++ ) {
Serial.print(buf, HEX);
Serial.print(" ");
} Serial.println();
}

void canSniff(const CAN_message_t &msg) {
Serial.print("MB "); Serial.print(msg.mb);
Serial.print(" OVERRUN: "); Serial.print(msg.flags.overrun);
Serial.print(" LEN: "); Serial.print(msg.len);
Serial.print(" EXT: "); Serial.print(msg.flags.extended);
Serial.print(" TS: "); Serial.print(msg.timestamp);
Serial.print(" ID: "); Serial.print(msg.id, HEX);
Serial.print(" BUS: "); Serial.print(msg.bus);
Serial.print(" Buffer: ");
for ( uint8_t i = 0; i < msg.len; i++ ) {
Serial.print(msg.buf, HEX); Serial.print(" ");
} Serial.println();
}

void setup() {
Serial.begin(115200); delay(400);
Can1.begin();
Can1.setClock(CLK_60MHz);
Can1.setBaudRate(95238);
Can1.setMaxMB(16);
Can1.enableFIFO();
Can1.enableFIFOInterrupt();
// Can1.onReceive(canSniff);
tp.begin();
tp.setWriteBus(&Can1); /* we write to this bus */
tp.onReceive(myCallback); /* set callback */
}

void loop() {
static uint32_t sendTimer = millis();
if ( millis() - sendTimer > 1000 ) {
uint8_t buf[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5 };
const char b[] = "01413AAAAABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
ISOTP_data config;
config.id = 0x666;
config.flags.extended = 0; /* standard frame */
config.separation_time = 10; /* time between back-to-back frames in millisec */
tp.write(config, buf, sizeof(buf));
tp.write(config, b, sizeof(b));
sendTimer = millis();
}
}
####################################
 
#########################
#include <FlexCAN_T4.h>
FlexCAN_T4<CAN1, RX_SIZE_256, TX_SIZE_16> Can1;
#define sw1 35
#define sw2 36
void setup(void) {
Serial.begin(115200);
delay(400);

Can1.begin();
Can1.setBaudRate(1000000);
Can1.setMaxMB(16);
Can1.enableFIFO();
Can1.enableFIFOInterrupt();
Can1.onReceive(canSniff);
Can1.mailboxStatus();
Can1.enhanceFilter(MB6);

}

void canSniff(const CAN_message_t &msg) {
Serial.print("MB ");
Serial.print(msg.mb);
Serial.print(" OVERRUN: ");
Serial.print(msg.flags.overrun);
Serial.print(" LEN: ");
Serial.print(msg.len);
Serial.print(" EXT: ");
Serial.print(msg.flags.extended);
Serial.print(" TS: ");
Serial.print(msg.timestamp);
Serial.print(" ID: ");
Serial.print(msg.id, HEX);
Serial.print(" Buffer: ");
for ( uint8_t i = 0; i < msg.len; i++ ) {
Serial.print(msg.buf, HEX); Serial.print(" ");
}
Serial.println();
}

void loop() {

CAN_message_t msg;
msg.id = 0x1;
Can1.events();

enableMode();
delay(1000);
CW();
delay(1000);
CCW();
delay(1000);
// Exit();
// delay(1000);

}

void enableMode()
{
Serial.println("Enter Motor Mode");
CAN_message_t msg;
msg.id = 0x88;
msg.buf[0] = 0xFF;
msg.buf[1] = 0xFF;
msg.buf[2] = 0xFF;
msg.buf[3] = 0xFF;
msg.buf[4] = 0xFF;
msg.buf[5] = 0xFF;
msg.buf[6] = 0xFF;
msg.buf[7] = 0xFC;
Can1.write(msg);
}

void CW()
{
Serial.println("CW");
CAN_message_t msg;
msg.id = 0x1;
msg.buf[0] = 0x85;
msg.buf[1] = 0x52;
msg.buf[2] = 0x7F;
msg.buf[3] = 0xF0;
msg.buf[4] = 0x51;
msg.buf[5] = 0x11;
msg.buf[6] = 0xE7;
msg.buf[7] = 0xFF;
Can1.write(msg);
}

void CCW()
{
Serial.println("CCW");
CAN_message_t msg;
msg.id = 0x1;
msg.buf[0] = 0x7A;
msg.buf[1] = 0xAC;
msg.buf[2] = 0x7F;
msg.buf[3] = 0xF0;
msg.buf[4] = 0x51;
msg.buf[5] = 0x11;
msg.buf[6] = 0xE7;
msg.buf[7] = 0xFF;
Can1.write(msg);
}


void Exit()
{
Serial.println("Exit");
CAN_message_t msg;
msg.id = 0x1;
msg.buf[0] = 0xFF;
msg.buf[1] = 0xFF;
msg.buf[2] = 0xFF;
msg.buf[3] = 0xFF;
msg.buf[4] = 0xFF;
msg.buf[5] = 0xFF;
msg.buf[6] = 0xFF;
msg.buf[7] = 0xFD;
Can1.write(msg);
}

################################

#include <FlexCAN_T4.h>
#include <isotp.h>
isotp<RX_BANKS_16, 512> tp; /* 16 slots for multi-ID support, at 512bytes buffer each payload rebuild */

FlexCAN_T4<CAN1, RX_SIZE_256, TX_SIZE_16> Can1;

void myCallback(const ISOTP_data &config, const uint8_t *buf) {
Serial.print("ID: ");
Serial.print(config.id, HEX);
Serial.print("\tLEN: ");
Serial.print(config.len);
Serial.print("\tFINAL ARRAY: ");
for ( int i = 0; i < config.len; i++ ) {
Serial.print(buf, HEX);
Serial.print(" ");
} Serial.println();
}

void canSniff(const CAN_message_t &msg) {
Serial.print("MB "); Serial.print(msg.mb);
Serial.print(" OVERRUN: "); Serial.print(msg.flags.overrun);
Serial.print(" LEN: "); Serial.print(msg.len);
Serial.print(" EXT: "); Serial.print(msg.flags.extended);
Serial.print(" TS: "); Serial.print(msg.timestamp);
Serial.print(" ID: "); Serial.print(msg.id, HEX);
Serial.print(" BUS: "); Serial.print(msg.bus);
Serial.print(" Buffer: ");
for ( uint8_t i = 0; i < msg.len; i++ ) {
Serial.print(msg.buf, HEX); Serial.print(" ");
} Serial.println();
}

void setup() {
Serial.begin(115200); delay(400);
Can1.begin();
Can1.setClock(CLK_60MHz);
Can1.setBaudRate(95238);
Can1.setMaxMB(16);
Can1.enableFIFO();
Can1.enableFIFOInterrupt();
// Can1.onReceive(canSniff);
tp.begin();
tp.setWriteBus(&Can1); /* we write to this bus */
tp.onReceive(myCallback); /* set callback */
}

void loop() {
static uint32_t sendTimer = millis();
if ( millis() - sendTimer > 1000 ) {
uint8_t buf[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5 };
const char b[] = "01413AAAAABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
ISOTP_data config;
config.id = 0x666;
config.flags.extended = 0; /* standard frame */
config.separation_time = 10; /* time between back-to-back frames in millisec */
tp.write(config, buf, sizeof(buf));
tp.write(config, b, sizeof(b));
sendTimer = millis();
}
}
####################################

Can you post your code between code tags, using the # button.
It makes it much more readable and you are more likely to get some help.
 
sender
Code:
#include <FlexCAN_T4.h>
FlexCAN_T4<CAN1, RX_SIZE_256, TX_SIZE_16> Can1;
#define sw1 35
#define sw2 36
void setup(void) {
  Serial.begin(115200);
  delay(400);

  Can1.begin();
  Can1.setBaudRate(1000000);
  Can1.setMaxMB(16);
  Can1.enableFIFO();
  Can1.enableFIFOInterrupt();
  Can1.onReceive(canSniff);
  Can1.mailboxStatus();
  Can1.enhanceFilter(MB6);

}

void canSniff(const CAN_message_t &msg) {
  Serial.print("MB ");
  Serial.print(msg.mb);
  Serial.print(" OVERRUN: ");
  Serial.print(msg.flags.overrun);
  Serial.print(" LEN: ");
  Serial.print(msg.len);
  Serial.print(" EXT: ");
  Serial.print(msg.flags.extended);
  Serial.print(" TS: ");
  Serial.print(msg.timestamp);
  Serial.print(" ID: ");
  Serial.print(msg.id, HEX);
  Serial.print(" Buffer: ");
  for ( uint8_t i = 0; i < msg.len; i++ ) {
    Serial.print(msg.buf[i], HEX); Serial.print(" ");
  }
  Serial.println();
}

void loop() {

  CAN_message_t msg;
  msg.id = 0x1;
  Can1.events();

  enableMode();
  delay(1000);
  CW();
   delay(1000);
  CCW();
  delay(1000);
//  Exit();
//  delay(1000);

}

void enableMode()
{
  Serial.println("Enter Motor Mode");
  CAN_message_t msg;                   
  msg.id = 0x88;
  msg.buf[0] = 0xFF;
  msg.buf[1] = 0xFF;
  msg.buf[2] = 0xFF;
  msg.buf[3] = 0xFF;
  msg.buf[4] = 0xFF;
  msg.buf[5] = 0xFF;
  msg.buf[6] = 0xFF;
  msg.buf[7] = 0xFC;
  Can1.write(msg);
}

void CW()
{
  Serial.println("CW");
  CAN_message_t msg;
  msg.id = 0x1;
  msg.buf[0] = 0x85;
  msg.buf[1] = 0x52;
  msg.buf[2] = 0x7F;
  msg.buf[3] = 0xF0;
  msg.buf[4] = 0x51;
  msg.buf[5] = 0x11;
  msg.buf[6] = 0xE7;
  msg.buf[7] = 0xFF;
  Can1.write(msg);
}

void CCW()
{
  Serial.println("CCW");
  CAN_message_t msg;
  msg.id = 0x1;
  msg.buf[0] = 0x7A;
  msg.buf[1] = 0xAC;
  msg.buf[2] = 0x7F;
  msg.buf[3] = 0xF0;
  msg.buf[4] = 0x51;
  msg.buf[5] = 0x11;
  msg.buf[6] = 0xE7;
  msg.buf[7] = 0xFF;
  Can1.write(msg);
}


void Exit()
{
  Serial.println("Exit");
  CAN_message_t msg;
  msg.id = 0x1;
  msg.buf[0] = 0xFF;
  msg.buf[1] = 0xFF;
  msg.buf[2] = 0xFF;
  msg.buf[3] = 0xFF;
  msg.buf[4] = 0xFF;
  msg.buf[5] = 0xFF;
  msg.buf[6] = 0xFF;
  msg.buf[7] = 0xFD;
  Can1.write(msg);
}
 
Receiver:
Code:
#include <FlexCAN_T4.h>
#include <isotp.h>
isotp<RX_BANKS_16, 512> tp; /* 16 slots for multi-ID support, at 512bytes buffer each payload rebuild */

FlexCAN_T4<CAN1, RX_SIZE_256, TX_SIZE_16> Can1;

void myCallback(const ISOTP_data &config, const uint8_t *buf) {
  Serial.print("ID: ");
  Serial.print(config.id, HEX);
  Serial.print("\tLEN: ");
  Serial.print(config.len);
  Serial.print("\tFINAL ARRAY: ");
  for ( int i = 0; i < config.len; i++ ) {
    Serial.print(buf[i], HEX);
    Serial.print(" ");
  } Serial.println();
}

void canSniff(const CAN_message_t &msg) {
  Serial.print("MB "); Serial.print(msg.mb);
  Serial.print(" OVERRUN: "); Serial.print(msg.flags.overrun);
  Serial.print(" LEN: "); Serial.print(msg.len);
  Serial.print(" EXT: "); Serial.print(msg.flags.extended);
  Serial.print(" TS: "); Serial.print(msg.timestamp);
  Serial.print(" ID: "); Serial.print(msg.id, HEX);
  Serial.print(" BUS: "); Serial.print(msg.bus);
  Serial.print(" Buffer: ");
  for ( uint8_t i = 0; i < msg.len; i++ ) {
    Serial.print(msg.buf[i], HEX); Serial.print(" ");
  } Serial.println();
}

void setup() {
  Serial.begin(115200); delay(400);
  Can1.begin();
  Can1.setClock(CLK_60MHz);
  Can1.setBaudRate(95238);
  Can1.setMaxMB(16);
  Can1.enableFIFO();
  Can1.enableFIFOInterrupt();
  //  Can1.onReceive(canSniff);
  tp.begin();
  tp.setWriteBus(&Can1); /* we write to this bus */
  tp.onReceive(myCallback); /* set callback */
}

void loop() {
  static uint32_t sendTimer = millis();
  if ( millis() - sendTimer > 1000 ) {
    uint8_t buf[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5 };
    const char b[] = "01413AAAAABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    ISOTP_data config;
    config.id = 0x666;
    config.flags.extended = 0; /* standard frame */
    config.separation_time = 10; /* time between back-to-back frames in millisec */
    tp.write(config, buf, sizeof(buf));
    tp.write(config, b, sizeof(b));
    sendTimer = millis();
  }
}
 
Also I tried this code for receiver but still didn't work
Code:
#include <FlexCAN_T4.h>

FlexCAN_T4<CAN1, RX_SIZE_256, TX_SIZE_16> can1;
FlexCAN_T4<CAN2, RX_SIZE_256, TX_SIZE_16> can2;
CAN_message_t msg;

void setup(void) {
  can1.begin();
  can1.setBaudRate(1000000);
  can2.begin();
  can2.setBaudRate(250000);
}

void loop() {
  

  if ( can1.read(msg) ) {
    Serial.print("CAN1 "); 
    Serial.print("MB: "); Serial.print(msg.mb);
    Serial.print("  ID: 0x"); Serial.print(msg.id, HEX );
    Serial.print("  EXT: "); Serial.print(msg.flags.extended );
    Serial.print("  LEN: "); Serial.print(msg.len);
    Serial.print(" DATA: ");
    for ( uint8_t i = 0; i < 8; i++ ) {
      Serial.print(msg.buf[i]); Serial.print(" ");
    }
    Serial.print("  TS: "); Serial.println(msg.timestamp);
  }
  else if ( can2.read(msg) ) {
    Serial.print("CAN2 "); 
    Serial.print("MB: "); Serial.print(msg.mb);
    Serial.print("  ID: 0x"); Serial.print(msg.id, HEX );
    Serial.print("  EXT: "); Serial.print(msg.flags.extended );
    Serial.print("  LEN: "); Serial.print(msg.len);
    Serial.print(" DATA: ");
    for ( uint8_t i = 0; i < 8; i++ ) {
      Serial.print(msg.buf[i]); Serial.print(" ");
    }
    Serial.print("  TS: "); Serial.println(msg.timestamp);
  }
}
 
Back
Top