3
\$\begingroup\$

I am trying to communicate with PIC32 UART. I am able to transmit any data but getting problem in reading the data. In PIC32 there's a function putsUART2("") which can transmit any data and there's function getsUART1(5, myBuffer, 123);. Parameter's for this function are:

1)length: This is the length of the string to be received
2)buffer: This is the pointer to the location where the data
          received have to be stored.
3)uart_data_wait: This is the time-out count for which the module
                  has to wait before return.  If the time-out count is ‘N’,
                  the actual time out would be about (19 * N – 1) instruction cycles.

Now what I am doing in my code is

if((U2STAbits.URXDA)!=0)  //if data received,then
{
    getsUART2(5,RxBuffer , 123); //storing the data in RxBuffer
    putsUART2(">>Data Received\r\n");
    U2STAbits.URXDA = 0; 
    U2STAbits.OERR = 0;                     
    putsUART2(RxBuffer); //transmitting the data back to the terminal
}

Now when I type 'A', >>Data Received is printed on the terminal and I am getting back data in terminal. but as the size of the RxBuffer is 5, it only stores one character. Can anyone help me with this, I really want to type a string and get back it on terminal.

Please help, thanks.

\$\endgroup\$
3
\$\begingroup\$

Your code is activated as soon as the UART has received at least one character. Then it tries to retrieve as many characters from the UARTs buffer, with a maximum of 5 characters, and waiting a maximum of 122 * 19 instruction cycles. Apparently, that time limit is not enough to receive the next character, so you end up with only the one character that was fully received. If you want to receive more charcaters you will have to call this code again (and again...), or you must specify a timeout that is sufficient to receive all characters (but that requires that you know whether the sender sends all characters back-to-back, which in general you can't know).

\$\endgroup\$
  • \$\begingroup\$ Thanks for your answer. I am still confused with this time out, can you explain it a little more. \$\endgroup\$ – Aircraft Aug 14 '15 at 8:35
  • 2
    \$\begingroup\$ @CZAbhinav what hes saying is that your getsUART1(..) has a parameter for delay. It will wait (19 * N – 1) cycles to receive the LENGTH number of byes. What looks like is happening is that you arent waiting long enough. For example if it takes 1 second to receive 1 byte, and you need to receive 5 bytes, you should be waiting 5 seconds, but instead, you are leaving in like 1 second. So you never receive the other 4 bytes, and you only have 1 byte recorded. Increase your timeout delay, so that you are waiting long enough to receive your bytes. \$\endgroup\$ – efox29 Aug 14 '15 at 8:56
  • 1
    \$\begingroup\$ As I imply in the last sentence, increasing the delay to receive all characters is not a good design, unless you can be sure that the other side sends all the characters as fast as it can, which in general (epsecially when the other side is a PC) you can't be sure of. \$\endgroup\$ – Wouter van Ooijen Aug 14 '15 at 9:40
  • \$\begingroup\$ @WoutervanOoijen: What would be a better design? \$\endgroup\$ – bos Oct 17 at 9:16
  • \$\begingroup\$ Wait for as long as you would allow a message to take. This might in some cases be forever, in which case you would probably want to use polling, or some form of interrups or multithreading. \$\endgroup\$ – Wouter van Ooijen Oct 17 at 16:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.