现在问题是这样的,比如我单片机的串口波特率设置了19200,串口调试助手设置9600,点发送几个字符,然后再把助手的波特率切换到19200,单片机的串口就无法接收了,但是可以发送,这种情况下,只有printf一次之后,才能再次接收。
以下是我的串口的配置:
UART_Params_init(&uartParams);
uartParams.readMode = UART_MODE_CALLBACK;
uartParams.readCallback = Uart_ReadCallback;
uartParams.writeCallback = Uart_WriteCallback;
uartParams.writeMode = UART_MODE_CALLBACK;
uartParams.writeDataMode = UART_DATA_BINARY;
uartParams.readDataMode = UART_DATA_BINARY;
uartParams.readReturnMode = UART_RETURN_FULL;
uartParams.readEcho = UART_ECHO_OFF;
uartParams.baudRate = 9600;
uartParams.readTimeout = 50000/Clock_tickPeriod;
uart = UART_open(Board_UART0, &uartParams);
UART_control(uart,32,0);
if (uart == NULL) {
System_abort("Error opening the UART");
}
Felix ZF:
建议你在线调试一下,看看收发两端波特率不一致时,接收到的是什么内容,以及代码运行是否正常
Viki Shi:
“比如我单片机的串口波特率设置了19200,串口调试助手设置9600”为什么把双方设置得不一样?
“只有printf一次之后,才能再次接收” printf后清理了buffer,查一下是不是buffer满了的问题还是之前的判断,怀疑是下位机跟上位机应答过程中,切换波特率导致的响应失败
FreePom:
回复 Viki Shi:
怎么查看buffer?
FreePom:
回复 Viki Shi:
后来我调试了一下,发现具体情况是这样的,如果连乱码都接收不到的情况下才会造成切换回去read不到,如果接收到乱码就没有问题,有些波特率会出现,概率很大,要这样操作是因为我的上位机是要自动去扫描波特率
FreePom:
回复 Viki Shi:
如果收不到误码,软件这边什么反应都没有
Felix ZF:
回复 FreePom:
你使用的是UART_MODE_BLOCKING模式吧?
应该是卡在UART_read()里了,串口没有收到指定长度的数据,一直处于等待数据状态。
你可以将readTimeout设置成一个指定的时间,比如100000,不要设为UART_WAIT_FOREVER,试一下。
或是使用下面的partial read模式。
Receive with Return Partial
This use case will read in UART_MODE_BLOCKING until the wanted amount of bytes is received or until a started reception is inactive for a 32-bit period. This UART_read() call can also be used when unknown amount of bytes shall be read. Note: The partial return is also possible in UART_MODE_CALLBACK mode.
UART_Handle handle;
UART_Params params;
uint8_t rxBuf[100]; // Receive buffer// Init UART and specify non-default parameters
UART_Params_init(¶ms);
params.baudRate = 9600;
params.writeDataMode = UART_DATA_BINARY;// Open the UART and initiate the partial read
handle = UART_open(Board_UART, ¶ms);
// Enable RETURN_PARTIAL
UART_control(handle, UARTCC26XX_CMD_RETURN_PARTIAL_ENABLE, NULL);
// Begin read
int rxBytes = UART_read(handle, rxBuf, 100));
FreePom:
回复 Felix ZF:
我配置程序贴出来在问题下面,你可以帮我看看吗
Felix ZF:
回复 FreePom:
我看你已经设置了uartParams.readTimeout = 50000/Clock_tickPeriod;
不过,timeout只在UART_MODE_BLOCKING
你把uartParams.readMode设成UART_MODE_BLOCKING,其他设置不变,试一下。General timeout
A timeout value can only be specified for reads and writes in UART_MODE_BLOCKING. If a timeout occurs during a read when in UART_MODE_BLOCKING, the number of bytes received will be returned and the UART_Object.status will be set to UART_TIMED_OUT. After a read timeout, RX will remain on, but device is allowed to enter standby. For more details see Power Management chapter below.In UART_MODE_CALLBACK there is no timeout and the application must call UART_readCancel() or UART_writeCancel() to abort the operation.
FreePom:
回复 Felix ZF:
改mode不行,改成blocking也不行,感觉是不是底层做了什么操作,一直在等,
Felix ZF:
回复 FreePom:
我这边试下来是可以的啊
/*
* ======== mainThread ========
*/
void *mainThread(void *arg0)
{
char input;
const char echoPrompt[] = "Echoing characters:\r\n";
UART_Handle uart;
UART_Params uartParams;/* Call driver init functions */
GPIO_init();
UART_init();/* Configure the LED pin */
GPIO_setConfig(Board_GPIO_LED0, GPIO_CFG_OUT_STD | GPIO_CFG_OUT_LOW);/* Turn on user LED */
GPIO_write(Board_GPIO_LED0, Board_GPIO_LED_ON);/* Create a UART with data processing off. */
UART_Params_init(&uartParams);
uartParams.writeDataMode = UART_DATA_BINARY;
uartParams.readDataMode = UART_DATA_BINARY;
uartParams.readReturnMode = UART_RETURN_FULL;
uartParams.readEcho = UART_ECHO_OFF;
uartParams.baudRate = 9600;
uartParams.readTimeout = 5000;uart = UART_open(Board_UART0, &uartParams);
if (uart == NULL) {
/* UART_open() failed */
while (1);
}UART_write(uart, echoPrompt, sizeof(echoPrompt));
/* Loop forever echoing */
while (1) {
UART_read(uart, &input, 1);
UART_write(uart, &input, 1);
}
}