obd2 twai

sasukiuchiwa
Posts: 3
Joined: Mon Apr 25, 2022 2:56 pm

obd2 twai

Postby sasukiuchiwa » Thu May 12, 2022 1:13 am

i m using obd2 can pins (6 /14)to communicate to my car i tried installing nd starting twai driver i transmiting data but recieving always fails
how to transmite obd data correctly? why i can't recieve data ?
resultat :
Driver installed
Driver started
Message queued for transmission
Failed to receive message


#include <stdio.h>
#include "driver/gpio.h"
#include "driver/twai.h"

#define GPIO_NUM_21 21
#define GPIO_NUM_22 22
void app_main(void)
{
//Initialize configuration structures using macro initializers
gpio_reset_pin(GPIO_NUM_21);
gpio_reset_pin(GPIO_NUM_22);
twai_general_config_t g_config = TWAI_GENERAL_CONFIG_DEFAULT(GPIO_NUM_21, GPIO_NUM_22, TWAI_MODE_NORMAL);
twai_timing_config_t t_config = TWAI_TIMING_CONFIG_500KBITS();
twai_filter_config_t f_config = TWAI_FILTER_CONFIG_ACCEPT_ALL();

//Install TWAI driver
if (twai_driver_install(&g_config, &t_config, &f_config) == ESP_OK) {
printf("Driver installed\n");
} else {
printf("Failed to install driver\n");
return;
}

//Start TWAI driver
if (twai_start() == ESP_OK) {
printf("Driver started\n");
} else {
printf("Failed to start driver\n");
return;
}
twai_message_t message;
twai_message_t message2;
message.identifier = 0xAAAA;
message.extd = 1;
// message.rtr = 1;
message.data_length_code = 4;
message.data[0] = 0x00;
message.data[1] = 0x01;
message.data[2] = 0x0C;
message.data[3] = 0x01;

//Queue message for transmission
if (twai_transmit(&message, pdMS_TO_TICKS(1000)) == ESP_OK) {
printf("Message queued for transmission\n");
} else {
printf("Failed to queue message for transmission\n");
}

if (twai_receive(&message, pdMS_TO_TICKS(10000)) == ESP_OK) {
printf("Message received\n");
} else {
printf("Failed to receive message\n");
return;
}

//Process received message
if (message.extd) {
printf("Message is in Extended Format\n");
} else {
printf("Message is in Standard Format\n");
}
printf("ID is %d\n", message.identifier);
if (!(message.rtr)) {
for (int i = 0; i < message.data_length_code; i++) {
printf("Data byte %d = %d\n", i, message.data);
}
}
if (twai_stop() == ESP_OK) {
printf("Driver stopped\n");
} else {
printf("Failed to stop driver\n");
return;
}

//Uninstall the TWAI driver
if (twai_driver_uninstall() == ESP_OK) {
printf("Driver uninstalled\n");
} else {
printf("Failed to uninstall driver\n");
return;
}
}

Who is online

Users browsing this forum: No registered users and 63 guests