所以,我一直在尝试测试 esp32-s2 芯片中称为“twai”的 CAN 总线通信协议。
我有芯片 - Arduino 线 - 可以收发器(SN65HVD230)。Rx 连接到端口 4 Tx 连接到端口 5
消息的接收不起作用,并且知道为什么会显示以下输出。
Failed to receive message
Message queued for transmission
Failed to receive message
Message queued for transmission
Failed to receive message
Message queued for transmission
Failed to receive message
Message queued for transmission
Failed to receive message
Message queued for transmission
Failed to receive message
Failed to queue message for transmission
Failed to receive message
Failed to queue message for transmission
Failed to receive message
Failed to queue message for transmission
Failed to receive message
Failed to queue message for transmission
Failed to receive message
Failed to queue message for transmission
Failed to receive message
Failed to queue message for transmission
Failed to receive message
Failed to queue message for transmission
Failed to receive message
Failed to queue message for transmission
Failed to receive message
Failed to queue message for transmission
Failed to receive message
Failed to queue message for transmission
Failed to receive message
Failed to queue message for transmission
Failed to receive message
Failed to queue message for transmission
Failed to receive message
Failed to queue message for transmission
Failed to receive message
Failed to queue message for transmission
这是我使用的代码,我知道它有什么问题,我从 espressif 链接获得了命令
https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/twai.html
它应该类似于常规esp32芯片中的CAN接口。
这是使用的代码
#include "driver/gpio.h"
#include "driver/twai.h"
void setup()
{
Serial.begin(115200);
// Initialize configuration structures using macro initializers
twai_general_config_t g_config = TWAI_GENERAL_CONFIG_DEFAULT(GPIO_NUM_5, GPIO_NUM_4, TWAI_MODE_NORMAL);
twai_timing_config_t t_config = TWAI_TIMING_CONFIG_250KBITS();
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;
}
}
void loop() {
//Configure message to transmit
twai_message_t message;
message.identifier =0x12;
message.extd =1;
message.data_length_code = 8;
for (int i = 0; i < 8; i++) {
message.data[i] = 0;
}
//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");
}
delay(2000);
twai_message_t messagerx;
if (twai_receive(&messagerx, pdMS_TO_TICKS(10000)) == ESP_OK) {
printf("Message received\n");
} else {
printf("Failed to receive message\n");
return;
}
//Process received message
if (messagerx.extd) {
printf("Message is in Extended Format\n");
} else {
printf("Message is in Standard Format\n");
}
printf("ID is %d\n", messagerx.identifier);
if (!(messagerx.rtr)) {
for (int i = 0; i < messagerx.data_length_code; i++) {
printf("Data byte %d = %d\n", i, message.data[i]);
}
}
}