Help - Can Bus Sender

Hello

I'm testing the TJA1050 can bus module:
image

But I can't do a simple transmission, my code stops at

Serial.println("Sending package...");

It's just a test example I wanted to do, but for some reason nothing is sent.

Follow the codes and schema.

I use the following library: GitHub - sandeepmistry/arduino-CAN: An Arduino library for sending and receiving data using CAN bus.

Sender:

#include <CAN.h> //Inclui a biblioteca CAN

void setup() {
   Serial.begin(9600);
   Serial.println("Transmissor CAN");
    if (!CAN.begin(500E3)) {
    Serial.println("Falha ao iniciar o controlador CAN"); //caso não seja possível iniciar o controlador
  }
}

void loop() {
  // Usando o CAN 2.0 padrão
  //Envia um pacote: o id tem 11 bits e identifica a mensagem (prioridade, evento)
  //o bloco de dados deve possuir até 8 bytes
  Serial.println("Enviando pacote...");
  CAN.beginPacket(0x12); //id 18 em hexadecimal
  CAN.write('h'); //1º byte
  CAN.write('e'); //2º byte
  CAN.write('l'); //3º byte
  CAN.write('l'); //4º byte
  CAN.write('o'); //5º byte
  CAN.endPacket(); //encerra o pacote para envio
  Serial.println("Enviado.");
  delay(1000);

  //Usando CAN 2.0 Estendido
  //Envia um pacote: o id tem 29 bits e identifica a mensagem (prioridade, evento)
  //o bloco de dados deve possuir até 8 bytes
  Serial.println("Enviando pacote estendido...");
  CAN.beginExtendedPacket(0xabcdef); //id 11259375 decimal ( abcdef em hexa) = 24 bits preenchidos até aqui
  CAN.write('w'); //1º byte
  CAN.write('o'); //2º byte
  CAN.write('r'); //3º byte
  CAN.write('l'); //4º byte
  CAN.write('d'); //5º byte
  CAN.endPacket(); //encerra o pacote para envio
  Serial.println("Enviado.");
  delay(1000);
}

Receiver:

#include <CAN.h> //Inclui a biblioteca CAN

void setup() {
  Serial.begin(9600); //inicia a serial para debug
  Serial.println("Receptor CAN");
  // Inicia o barramento CAN a 500 kbps
  if (!CAN.begin(500E3)) {
    Serial.println("Falha ao iniciar o controlador CAN"); //caso não seja possível iniciar o controlador
  }
}

void loop() {
  // Tenta verificar o tamanho do acote recebido
  int packetSize = CAN.parsePacket();
  if (packetSize) {
    // Se temos um pacote
    Serial.println("Recebido pacote. ");
    if (CAN.packetExtended()) { //verifica se o pacote é estendido
      Serial.println("Estendido");
    }
    if (CAN.packetRtr()) {
      //Verifica se o pacote é um pacote remoto (Requisição de dados), neste caso não há dados
      Serial.print("RTR ");
    }
    Serial.print("Pacote com id 0x");
    Serial.print(CAN.packetId(), HEX);
    if (CAN.packetRtr()) {                      //se o pacote recebido é de requisição, indicamos o comprimento solicitado
      Serial.print(" e requsitou o comprimento ");
      Serial.println(CAN.packetDlc()); //obtem o DLC (Data Length Code, que indica o comprimento dos dados)
    } else {
      Serial.print(" e comprimento "); // aqui somente indica o comprimento recebido
      Serial.println(packetSize);
      //Imprime os dados somente se o pacote recebido não foi de requisição
      while (CAN.available()) {
        Serial.print((char)CAN.read());
      }
      Serial.println();
    }
    Serial.println();
  }
}

Could someone tell me why it doesn't work?

Thank you!

this CAN transceiver does not operate at 3.3V. see its datasheet
image

try procuring the TI SN65HVD230. as suggested in that GIT library.

hope that helps....

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.