TA的每日心情 | 奋斗 2023-7-8 16:17 |
---|
签到天数: 971 天 连续签到: 1 天 [LV.10]以坛为家III
|
GD32 F150R8 与 PC端LabView 通信之VISA串口通信
https://www.cirmall.com/bbs/foru ... 9&fromuid=23447
实现了基本的通讯。
[GD32F150R8]02、与Labview 之VISA 串口通信2
https://www.cirmall.com/bbs/foru ... 5&fromuid=23447
对其进行扩展,采用command line 的方法,当大型任务的时候,就是也以command line 的方法进行扩展
最终以
UIF_CMD UIF_CMDTAB[] = {UIF_CMDS_ALL};
与
UIF_SETCMD UIF_SETCMDTAB[]={
{"led",0,1,uif_cmd_led,"set led"}
};
的方法对任务不断的扩展。
但在上述两例中,虽然实现了Labview有通信,但是不实用。原因在于查询的方法接收数据,无限的等待是无法接收的。
对于查询方式发送,一般的情况下是可以接受的。
如果从实用的角度上看,就要把查义接收数据,修改成中断。 这样采用了FIFO进行实现,代码如下:- /***************************************************************************************/
- char sFifoUart2RxBuffer[(128)] = {0};
- uint8_t sFifoUart2RxTail = 0;
- uint8_t sFifoUart2RxHead = 0;
- uint8_t sFifoUart2RxCounter = 0;
- bool Fifo_Uart2Rx_Add_Data(char Data)
- {
- USART_INT_Set(USART1,USART_INT_RBNE,DISABLE);
- if ((sFifoUart2RxTail == sFifoUart2RxHead)
- && (sFifoUart2RxCounter != 0))
- {
- USART_INT_Set(USART1,USART_INT_RBNE,ENABLE);
- return false;
- }
-
- sFifoUart2RxBuffer[sFifoUart2RxHead] = Data;
- sFifoUart2RxHead = (sFifoUart2RxHead == (128 - 1)) ?
- 0 : (sFifoUart2RxHead + 1);
- sFifoUart2RxCounter++;
- USART_INT_Set(USART1,USART_INT_RBNE,ENABLE);
- return true;
- }
- bool Fifo_Uart2Rx_Get_Data(char *pData)
- {
- USART_INT_Set(USART1,USART_INT_RBNE,DISABLE);
- if (((sFifoUart2RxTail == sFifoUart2RxHead)
- && (sFifoUart2RxCounter == 0))
- || (pData == (char *)0))
- {
- USART_INT_Set(USART1,USART_INT_RBNE,ENABLE);
- return false;
- }
-
- (*pData) = sFifoUart2RxBuffer[sFifoUart2RxTail];
- sFifoUart2RxTail = (sFifoUart2RxTail == (128 - 1)) ?
- 0 : (sFifoUart2RxTail + 1);
- sFifoUart2RxCounter--;
- USART_INT_Set(USART1,USART_INT_RBNE,ENABLE);
- return true;
- }
- bool Fifo_Uart2Rx_Reset(void)
- {
- USART_INT_Set(USART1,USART_INT_RBNE,DISABLE);
- sFifoUart2RxCounter = 0 ;
- sFifoUart2RxTail = 0;
- sFifoUart2RxHead = 0;
- USART_INT_Set(USART1,USART_INT_RBNE,ENABLE);
- return false;
- }
复制代码- void EVB_Uart2Config(void)
- {
- NVIC_InitPara NVIC_InitStructure;
-
- USART_InitPara USART_InitStructure;
- GPIO_InitPara GPIO_InitStructure;
- /* Enable GPIOA clock */
- RCC_AHBPeriphClock_Enable( RCC_AHBPERIPH_GPIOA , ENABLE );
- /* Enable USART1 APB clock */
- RCC_APB1PeriphClock_Enable( RCC_APB1PERIPH_USART2 , ENABLE );
- /* USART1 Pins configuration **************************************************/
- //GPIO_DeInit( GPIOA );
- /* Configure the GPIO ports */
- /* Connect pin to Periph */
- GPIO_PinAFConfig( GPIOA , GPIO_PINSOURCE2, GPIO_AF_1 );
- GPIO_PinAFConfig( GPIOA , GPIO_PINSOURCE3, GPIO_AF_1 );
- /* Configure pins as AF pushpull */
- GPIO_InitStructure.GPIO_Pin = GPIO_PIN_2 | GPIO_PIN_3;
- GPIO_InitStructure.GPIO_Mode = GPIO_MODE_AF;
- GPIO_InitStructure.GPIO_Speed = GPIO_SPEED_50MHZ;
- GPIO_InitStructure.GPIO_OType = GPIO_OTYPE_PP;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PUPD_NOPULL;
- GPIO_Init( GPIOA , &GPIO_InitStructure);
- USART_DeInit( USART2 );
- USART_InitStructure.USART_BRR = 115200;
- USART_InitStructure.USART_WL = USART_WL_8B;
- USART_InitStructure.USART_STBits = USART_STBITS_1;
- USART_InitStructure.USART_Parity = USART_PARITY_RESET;
- USART_InitStructure.USART_HardwareFlowControl = USART_HARDWAREFLOWCONTROL_NONE;
- USART_InitStructure.USART_RxorTx = USART_RXORTX_RX | USART_RXORTX_TX;
- USART_Init(USART2, &USART_InitStructure);
- Fifo_Uart2Rx_Reset();
-
- NVIC_PRIGroup_Enable(NVIC_PRIGROUP_0);
- NVIC_InitStructure.NVIC_IRQ = USART2_IRQn;
- NVIC_InitStructure.NVIC_IRQPreemptPriority = 0;
- NVIC_InitStructure.NVIC_IRQSubPriority = 3;
- NVIC_InitStructure.NVIC_IRQEnable = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
-
- USART_ClearINTBitState(USART2,USART_INT_RBNE);
- USART_INT_Set(USART2,USART_INT_RBNE,ENABLE);
- /* USART enable */
- USART_Enable(USART2, ENABLE);
-
- }
复制代码- bool UsartGetFifoByte(char* c)
- {
- return Fifo_Uart2Rx_Get_Data(c);
- }
复制代码 当FIFO为空是返回false ,成功时返回true
这样就有效地提高了实用的价值
|
评分
-
查看全部评分
|