This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

请教一下,MSP432 如何能实现动态更改波特率?

需求是这样的:MCU 上电之后,UART2 默认9600 的波特率与终端设备通信,与此同时,UART0与上位机连接(固定的波特率参数),需要能在上位机上更改UART2串口的波特率参数,且不能重启MCU

我的初始化的 做法是这样的:上电设置默认参数;

void init_com2()
{

    UART_Params uartParams;

    UART_init();     

    /* 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.dataLength = UART_LEN_8;
    uartParams.stopBits = UART_STOP_ONE;
    uartParams.parityType=UART_PAR_EVEN;            /*增加一个偶校验;*/ //  UART_PAR_ODD 奇校验
    uartParams.baudRate = 9600;

    com_app2 = UART_open(Board_UART2, &uartParams);      /*用于将UART_Params结构初始化为其默认值的函数*/

    if (com_app2 == NULL) {
        /* UART_open() failed */
        while (1);
    }
}

此9600 波特率的状态下,能正常与终端设备进行通信,收发数据都没问题;

在收到上位机更改UART2参数的命令之后,我是这么做的:

    void Reset_com2()
{
    UART_Params uartParams;

    UART_readCancel(com_app2);      //停止串口2 的读写操作;
    UART_writeCancel(com_app2);

    if(com_app2)
    {
        UART_close(com_app2);      /*关闭串口2*/
        com_app2 = NULL;           //置空;
    }    

    UART_init();   //重新初始化参数

    /* 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.dataLength = get_data;     //传入数据位
    uartParams.stopBits = get_stop;     //传入停止位
    uartParams.parityType= get_veri;    //传入校验位
    uartParams.baudRate = Baud_Rate;    //传入波特率参数;
    //UART_enableModule(EUSCI_A0_BASE);

    com_app2 = UART_open(Board_UART2, &uartParams);      /*重新打开UART2串口*/

    if (com_app2 == NULL) {
        /* UART_open() failed */
        while (1);
    }

}

 

其中,get_date、get_stop、get_veri和Baud_Rate 参数的类型值是符合库文件声明,单步Debug过程中,也能看到该值已正确传入;

现在的现象是:无论我上位机对波特率作何更改(其他变量与初始保持一致的前提下),串口2 的波特率还是9600。

比如我在上位机上将UART2波特率改为115200,用串口助手也设置成115200,去模拟终端设备发送数据,MCU无法收到,但是将串口助手改为9600,MCU就能正常收到。

也就是说,上面 Reset_com2()  函数执行之后,没有改变掉原来的波特率设定,看了好久 TI 官方的库文件,也没有对这方面的详细说明,调试了两天仍没进展,希望 TI 工作人员能给一点思路