Microchip
直播中

江根磊

7年用户 1041经验值
私信 关注
[问答]

UART字符串接收问题

大家好,当我尝试用UART接收字符串时,我正面临一个问题。我使用的是PIC18LF26K22和C183.47.Enter如果工作良好(单和字符串),并且当我只想接收单个字符时,它也能工作,但是我不能得到该字符串。

以上来自于百度翻译


      以下为原文

    Hello Everyone,

I'm facing with a problem, when I try to receive string with UART. I'm using a PIC18LF26K22 and C18 3.47.

Transmit if working fine(single and string), and also when i just want to receive a single char it's also working, but i cannot get that string back.

Any help are welcome
Thanks

My routines are the follows:

//Receive Text
void UART_Read_Text(char *Output, unsigned int length)
{
  unsigned int i;
  for(i=0;i   {
   Output = UART_Read();
  }
}

//Data Ready
char UART_Data_Ready()
{
  return PIR1bits.RC1IF;
}

//Transmit Text
void UART_Write_Text(char *text)
{
  int i;
  for(i=0;text!='';i++)
    UART_Write(text);
}

//Check if transmit empty
char UART_TX_Empty()
{
  return TXSTA1bits.TRMT1;
}

//Variables
char readurt[8];
char transurt[8] = {'R','D','O','P','U','0','0','0'};

//The main
    while(1)
    {
    if(UART_TX_Empty())
    {
     UART_Write_Text(transurt);
    }
      if(UART_Data_Ready())
      {
       UART_Read_Text(readurt, 8);
       if(readurt == "RDOPU000")
          {
           //Do some stuff
          }
       }

回帖(8)

王焕树

2019-3-21 13:08:16
我看到了代码中的几个基本错误,但是你从来没有确切地说明你是如何测试它的,以及当你做了什么时,你没有显示你的UARTARGWORKE()函数,你的UUARTARTWORKEWEXTY()函数发送直到它遇到null,但是你没有把一个字符串传递给它,只是一个8字节的数组。为了保证这一点,在最后一个字节之后,没有一个空。为了解决这个问题,CueToOWHICH将创建一个九字节数组,后面有一个null。如果它以前工作过,那么数组后面的字节就是0,那就是运气。接下来,尝试将8个字节接收到一个8字节数组,这样。同样,不能保证终止null。然后尝试将数组与字符串进行比较。你不能在C中这样做,你必须使用StcMP函数SO TooToT,但是在Read UART数组的末尾需要一个NULL,所以你应该使它长九字节,并且在第九字节中添加一个NULL(Read UART(8))。X PIN,因为你正在检查刚刚发送的字符串。这是行不通的,因为你正在做所有的发送,然后做所有的接收。USAT没有8字符FIFO,因此您将溢出USAR接收缓冲器。如果不能经常检查USAT接收,则需要为RCIF设置中断服务,并将字符添加到中断服务中的缓冲区中。

以上来自于百度翻译


      以下为原文

    I see a few basic errors in your code, but then you never state exactly how you are testing it, and what happens when you do.
You don't show your UART_Write() function
Your UART_Write_Text() function is sending until it encounters a NULL, but you have not passed a string to it, just an array of 8 bytes, which you have done nothing to make sure there is a NULL after the last byte.
To fix that, change
char transurt[8] = {'R','D','O','P','U','0','0','0'};
to
char transurt[] = "RDOPU000";
Which will create a nine byte array, with a trailing NULL.
If it worked before, it was just luck that the byte following your array was a zero.
 
Next, you try to receive 8 bytes into an 8 byte array, so again there is no guarantee of a terminating NULL.
You then try to compare your array with a string. You can't do that in C, you must use the strcmp function
so change
if(readurt == "RDOPU000")
to
if(strcmp(readurt,"RDOPU000"))
however, that requires a NULL at the end of the readuart array, so you should make it nine bytes long, and add a NULL in the ninth byte (readuart[8])
 
Lastly, you never say this, but I'm guessing you have the TX pin connected to the RX pin, as you are checking for the string you just sent.
This isn't going to work, because you are doing all the sending, then doing all the receiving. The USART doesn't have an 8 character FIFO, so you are going to be overflowing the USART receive buffer.
If you can't be constantly checking for USART receive, then you need to set up an interrupt service for RCIF, and add the characters to a buffer in the interrupt service.
 
 
 
 
举报

h1654155275.5651

2019-3-21 13:21:39
亲爱的QHB,这是UARTHARI写函数:而且,我从未使用过UART中的第九位函数。你能帮助我吗?销钉没有相互连接。两者都连接到我的PC & lt;-gt;pic。问候,Sty。

以上来自于百度翻译


      以下为原文

    Dear qhb,
 
This is the UART_Write function:
 void UART_Write(char data)
{
  while(!TXSTA1bits.TRMT1);
  TXREG1 = data;
}
 
Also, I never used 9th bit function in UART. Can you help my in that? The pins are not connected into each other. both are connected to my PC <-> PIC.
 
Regards,
Sty
举报

李兆峰

2019-3-21 13:40:57
标志名称应该是

以上来自于百度翻译


      以下为原文

    The flag name should be
  while(!TXSTA1bits.TRMT); // not TRMT1
but I suppose this is just a typo in your post above
举报

王焕树

2019-3-21 13:58:24
这样做很有效,但效率很低。每个字节之间会有一个空隙。这个版本会连续发送:你想用它来做什么?我不认为这里需要一个九位模式,它通常用于与多个从属设备交谈时,你从来没有提到过你是如何测试这个的,所以我不得不猜测。你的接收代码只要在调用你的字符之间不超过两个字符就可以工作。接收函数。

以上来自于百度翻译


      以下为原文

   
That will work, but is inefficient. There will be a gap between each byte sent.
This version will send continuously:
 void UART_Write(char data)
{
  while(!PIR1.TX1IF);    //wait until TXBUF is empty
  TXREG1 = data;    //send the character
}
 

What do you want to use it for?
I don't see any need for a nine bit mode here, that is usually used for addressing when talking to multiple slave devices.
 

You never mentioned how you were testing this, so I had to guess.
Your receive code will work so long as you don't receive more than two characters between calls to your character receive function.
 
举报

更多回帖

发帖
×
20
完善资料,
赚取积分