I'm using a Teensy to read CAN messages and I'm wanting to then transmit the message on another CAN ID.
On 0x319 I'm reading bits 7,6,5 and 4 on Byte 0.
I'm then wanting to transmit this on 0x35C again on Byte 0 as the same bits. (I will also be wanting to change the bits to be written to so hence asking this way).
I've made an attempt but somehow I'm not writing it correctly. Help please.
#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 msgcan1;
CAN_message_t msgcan2;
CAN_message_t txmsg1;
CAN_message_t txmsg2;
char msgString[128]; // Array to store serial string
long unsigned int rxId;
int ChgEn;
int DisEn;
int ReqForceChg1;
int ReqForceChg2;
void setup()
{
Serial.begin(115200); // start serial for output
can1.begin(); //CAN 1
can1.setBaudRate(500000);
can1.setMBFilter(ACCEPT_ALL);
can1.distribute();
can1.mailboxStatus();
can2.begin(); //CAN 2
can2.setBaudRate(500000);
can2.setMBFilter(ACCEPT_ALL);
can2.distribute();
can2.mailboxStatus();
}
void loop()
{
if (can1.read(msgcan1))
{
rxId = (msgcan1.id);
{
Serial.print ("CAN 1 ");
Serial.print("MB: "); Serial.print(msgcan1.mb);
Serial.print(" OVERRUN: "); Serial.print(msgcan1.flags.overrun);
Serial.print(" ID: 0x"); Serial.print(msgcan1.id, HEX );
Serial.print(" EXT: "); Serial.print(msgcan1.flags.extended );
Serial.print(" LEN: "); Serial.print(msgcan1.len);
Serial.print(" DATA: ");
for ( uint8_t i = 0; i < msgcan1.len ; i++ )
{
sprintf(msgString, " 0x%.2X", msgcan1.buf[i]);
Serial.print(msgString);
}
Serial.println();
}
if (rxId == 0x319)
{
ChgEn = bitRead(msgcan1.buf[0], 7);
DisEn = bitRead(msgcan1.buf[0], 6);
ReqForceChg1 = bitRead(msgcan1.buf[0], 5);
ReqForceChg2 = bitRead(msgcan1.buf[0], 4);
unsigned int value = 0xFFFF;
txmsg2.buf[0] = bitWrite(value, 7, ChgEn) * 64 + bitWrite(value, 6, DisEn) * 32 + bitWrite(value, 5, ReqForceChg1) * 16 + bitWrite(value, 5, ReqForceChg2) * 8;
txmsg2.buf[1] = 0x00;
txmsg2.id = 0x35C;
txmsg2.len = 2;
can2.write( txmsg2); // send message out on CAN2 interface to SMA
//319
Serial.println();
Serial.print("Charge enable = ");
Serial.println(ChgEn);
Serial.print("Discharge enable = ");
Serial.println(DisEn);
Serial.print("Request Forced Charge 1 = ");
Serial.println(ReqForceChg1);
Serial.print("Request Forced Charge 2 = ");
Serial.println(ReqForceChg2);
Serial.println();
}
}
}