如何使用UART接收数据进行进一步处理,而不仅仅是在嵌入式C中回显?

这是我测试环境中的代码。它只是简单地将输入的数据回复(回送)到通过串行端口连接的终端应用程序。到目前为止一切顺利。

现在,我需要根据需求/协议使用该数据和流程。 但是,正如我期望的那样,我正在努力使用数据UART0Buffer。 (变量UART0Buffer填充在UART中断中)

我发送例如终端上的“ 7E0007AA010203040506CC” 并且出于某种原因,代码按四个步骤将其按顺序返回给终端应用程序

  

1通过进程#1

37

  

2通过流程#2

00

  

3通过进程#1

30 30 30 37 41 41 30 31 30 32 30 33 30 34 30

  

4通过流程#2

A1

您还可以注意到,答复(回显)的数据与我从终端发送的数据不同。

此外,如果我完全删除“ Process#2”行, 我可以得到适当的回声;

37 45 30 30 30 37 41 41 30 31 30 32 30 33 30 34 30 35 30 36 43 43

我在这里真正想念的是什么?

int main()
{
    SystemInit();       /* Clock and PLL configuration */   
    UARTInit(0,57600); /* baud rate setting */

    while(1)
    {
        if ( UART0Count != 0 )
        {
            LPC_UART0->IER = IER_THRE | IER_RLS;            /* Disable RBR */

            UARTSend( 0,(uint8_t *)UART0Buffer,UART0Count ); // Process #1

            UART0Count = 0;

// I need to call a function here in order to process data in UART0Buffer
// like checking/using STX,CHKSUM and DATA.

            // A test,if I can manage to use the data in pieces
            UARTSend( 0,(uint8_t *)UART0Buffer[0],1); // Process #2

            LPC_UART0->IER = IER_THRE | IER_RLS | IER_RBR;  /* Re-enable RBR */
        }
    }
}

void UARTSend( uint32_t portNum,uint8_t *BufferPtr,uint32_t Length )
{
  if ( portNum == 0 )
  {
    while ( Length != 0 )
    {
      /* THRE status,contain valid data */
      while ( !(UART0TxEmpty & 0x01) ); 
      LPC_UART0->THR = *BufferPtr;
      UART0TxEmpty = 0; /* not empty in the THR until it shifts out */
      BufferPtr++;
      Length--;
    }
  }
}

编辑: 由于dude已经解释了“进程#2”的错误用法,请忽略它。它以某种方式破坏了事情。但是,我的问题仍然存在。尽管我可以通过“ Process#1”得到回显,但是我无法读取和使用数据!任何指针将不胜感激。

编辑:

接收:

void UART0_IRQHandler (void) 
{
  uint8_t IIRValue,LSRValue;
  uint8_t Dummy = Dummy;

  IIRValue = LPC_UART0->IIR;

  IIRValue >>= 1;           /* skip pending bit in IIR */
  IIRValue &= 0x07;         /* check bit 1~3,interrupt identification */
  if ( IIRValue == IIR_RLS )        /* Receive Line Status */
  {
    LSRValue = LPC_UART0->LSR;
    /* Receive Line Status */
    if ( LSRValue & (LSR_OE|LSR_PE|LSR_FE|LSR_RXFE|LSR_BI) )
    {
      /* There are errors or break interrupt */
      /* Read LSR will clear the interrupt */
      UART0Status = LSRValue;
      Dummy = LPC_UART0->RBR;       /* Dummy read on RX to clear 
                            interrupt,then bail out */
      return;
    }
    if ( LSRValue & LSR_RDR )   /* Receive Data Ready */            
    {
      /* If no error on RLS,normal ready,save into the data buffer. */
      /* Note: read RBR will clear the interrupt */
      UART0Buffer[UART0Count] = LPC_UART0->RBR;
      UART0Count++;
      if ( UART0Count == BUFSIZE )
      {
        UART0Count = 0;     /* buffer overflow */
      } 
    }
  }
  else if ( IIRValue == IIR_RDA )   /* Receive Data Available */
  {
    /* Receive Data Available */
    UART0Buffer[UART0Count] = LPC_UART0->RBR;
    UART0Count++;
    if ( UART0Count == BUFSIZE )
    {
      UART0Count = 0;       /* buffer overflow */
    }
  }
  else if ( IIRValue == IIR_CTI )   /* Character timeout indicator */
  {
    /* Character Time-out indicator */
    UART0Status |= 0x100;       /* Bit 9 as the CTI error */
  }
  else if ( IIRValue == IIR_THRE )  /* THRE,transmit holding register empty */
  {
    /* THRE interrupt */
    LSRValue = LPC_UART0->LSR;      /* Check status in the LSR to see if
                                    valid data in U0THR or not */
    if ( LSRValue & LSR_THRE )
    {
      UART0TxEmpty = 1;
    }
    else
    {
      UART0TxEmpty = 0;
    }
  }

}
dongjian_011 回答:如何使用UART接收数据进行进一步处理,而不仅仅是在嵌入式C中回显?

您基本上是在设计串行消息驱动程序。将设计分为几层。

最底层是UART驱动程序,用于将字符移入和移出硬件UART。 UART驱动程序封装了有关如何与UART硬件接口的所有知识。该层不知道帧或消息,只知道字符。当发送字符可用且UART准备发送时,它将发送字符复制到TX寄存器。当一个可用时,它将从UART RX寄存器复制一个接收到的字符。这些动作通常在UART中断服务程序中实现。通常,RAM循环缓冲区用于存储TX和RX字符。

下一层用于框架字符。该层负责标识界定帧的字符。该层封装了所有关于字符的知识,这些知识被划分为帧。通常将其实现为状态机,当RX缓冲区中的字符可用时,该状态机将被重复调用。状态机从RX缓冲区获取下一个字符,并根据当前状态对其进行处理。状态可能包括,例如,waiting_for_start_of_frame,copying_body_of_frame和waiting_for_end_of_frame。如果帧协议包括帧检查序列,则该层执行检查。该层不会尝试解释帧的含义,而只是形成完整的帧以供下一层使用。

如果您的串行消息足够大,可以跨越多个帧,那么您可能在这里有一层将帧缝在一起成为消息的层。或者,如果每个消息都包含在一个框架中,那么您可能不需要此层。

最顶层是实际解释消息并执行适当操作的层。该层对UART或帧定界符一无所知,因为所有这些知识都由较低层封装。

此答案有更多提示,Embedded C UART conventions

本文链接:https://www.f2er.com/3059020.html

大家都在问