Libstock
It is currently 21 Oct 2014 01:46

All times are UTC + 1 hour




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: UART1_Read_Text() help
PostPosted: 10 Sep 2009 11:34 
Offline

Joined: 15 Jun 2009 11:49
Posts: 15
Hi I have been trying to see how the UART1_Read_Text() function works and I can not realy get it to work. I want to read a string that is a long number for example 4100000000, and then convert long to a byte array and then print it out with UART1_Write().

int i=0;
char output[11];
unsigned long tempLong=0;
char uartTmpArray[4];
 while(i==0){
                if (UART1_Data_Ready()==1) {               // If data is received,
                        UART1_Read_Text(output, '\0', 11); //reads text until '\0' is found
                 i=1;
                }
        }
tempLong=atol(output);
uartTmpArray[0] = tempLong & 0xFF;
uartTmpArray[1] = (tempLong & 0xFF00) >> 8;
uartTmpArray[2] = (tempLong & 0xFF0000) >> 16;
uartTmpArray[3] = (tempLong & 0xFF000000) >> 24;
i=0;
while(i<4){
UART1_Write(uartTmpArray[i]);
i++;
}


can some help me to get this to work?


Top
 Profile  
 
 Post subject:
PostPosted: 16 Sep 2009 07:49 
Offline
mikroElektronika team
User avatar

Joined: 17 Aug 2009 16:51
Posts: 1741
Location: Belgrade
Hi,
You probably had some issues with the delimiter because if you take a look at the function prototype, which you can find in your compiler's Help file:
void UART1_Read_Text(char *Output, char *Delimiter, char Attempts);

you'll see thet delimiter is a string. I suggest that you use "\n" instead of '\n'. Also, after completion of UART1_Write(), you always have i>0 and the every other iteration you skip the while loop. Of course, this posted code doesn't have clearly defined structure, so this could only be a part of a bigger code, so maybe it is differently integrated as a whole. What I suggest is to take a look at this suggested code, and try rearranging your branch and loop statements, and of course set the delimiter as string to ensure everything works fine:
const InSize = 11;
char StopChar[] = "\n";
char input[InSize];
int i;

unsigned long tempLong=0;
char uartTmpArray[4];
char Received = 0;

void main(){
  UART1_Init(9600);
  UART1_Write_Text("INIT");
  while (1){
    if (!Received){
      if(UART1_Data_Ready()==1){
        UART1_Read_Text(input, StopChar, InSize);
        Received = 1;
      }
    }
    else{
      Received = 0;
      tempLong = atol(input);
      uartTmpArray[0] = tempLong & 0xFF;
      uartTmpArray[1] = (tempLong & 0xFF00) >> 8;
      uartTmpArray[2] = (tempLong & 0xFF0000) >> 16;
      uartTmpArray[3] = (tempLong & 0xFF000000) >> 24;
      i=0;
      for (i=0; i<4; i++)
        UART1_Write(uartTmpArray[i]);
    }
  }
}


Best regards,
Aleksandar

_________________
mikroElektronika Product Manager


Top
 Profile  
 
 Post subject:
PostPosted: 17 Sep 2009 14:58 
Offline

Joined: 15 Jun 2009 11:49
Posts: 15
Thanks for your help Aleksandar.

One question is it recommended to use \n instead of \0 or is it up to the user?

Thanks again for your help.

Best regards Landis2


Top
 Profile  
 
 Post subject:
PostPosted: 18 Sep 2009 12:15 
Offline
mikroElektronika team
User avatar

Joined: 17 Aug 2009 16:51
Posts: 1741
Location: Belgrade
Hi,
It is up to user, but I would personally use some other character and not '\0', just in case of the UART connection breakup.

Best regards,
Aleksandar

_________________
mikroElektronika Product Manager


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: Exabot [Bot] and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group