这个代码试了试不行
#include "bflb_uart.h"
#include "board.h"
// 定义UART设备
struct bflb_device_s *uartx;
// 初始化UART和RS485模式
void uart485_init() {
// 配置UART参数
struct bflb_uart_config_s cfg = {
.baudrate = 9600, // 波特率,与串口助手保持一致
.data_bits = UART_DATA_BITS_8,
.stop_bits = UART_STOP_BITS_1,
.parity = UART_PARITY_NONE,
.flow_ctrl = UART_FLOWCTRL_NONE, // 使用正确的常量名称
.tx_fifo_threshold = 0,
.rx_fifo_threshold = 1,
};
// 获取UART设备(使用串口1)
uartx = bflb_device_get_by_name("uart1");
// 初始化UART
bflb_uart_init(uartx, &cfg);
// 配置RS485模式
bflb_uart_feature_control(uartx, UART_CMD_SET_TX_RS485_EN, 1);
bflb_uart_feature_control(uartx, UART_CMD_SET_TX_RS485_POLARITY, 1); // 极性根据硬件调整
}
// 发送字节函数
void MDSSerialSendBytes(uint8_t *bytes, uint16_t num) {
for (uint16_t i = 0; i < num; i++) {
bflb_uart_putchar(uartx, bytes[i]);
}
}
// 发送测试指令
void send_test_command() {
// 测试指令内容,可自定义
uint8_t test_cmd[] = "M61 RS485 Test: Hello, PC!\r\n";
MDSSerialSendBytes(test_cmd, sizeof(test_cmd) - 1); // 减去字符串结束符
}
// Arduino初始化函数,替代main函数中的初始化部分
void setup() {
// 板级初始化
board_init();
// 初始化RS485
uart485_init();
}
// Arduino循环函数,替代main函数中的while(1)循环
void loop() {
// 发送测试指令
send_test_command();
// 延时1秒,循环发送
bflb_mtimer_delay_ms(1000);
}
|