UART Problems

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

Hi.

   

I have a problem with reading from a UART. I have attached a  code file.

   

When I try to receive  data using GetChar() function,. I can't  read more than 3 bytes.

   

I am running a for loop to get all the bytes in a buffer.  Anybody who have a solution?

0 Likes
1 Solution
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

You should concentrate on receiving a complete string before you make any other actions. The logic behind your do-while loop will start to send repeatedly when part of your message is received. Using _GetRxBufferSize is quite correct and will work as expected.

   

 

   

Bob

View solution in original post

0 Likes
18 Replies
Anonymous
Not applicable

No ideas out there? 😉

   

Best regards -

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Can you post your complete project, so that we all can have a look at all of your settings? To do so, use
Creator->File->Create Workspace Bundle (minimal)
and attach the resulting file.

   

 

   

Bob

0 Likes
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

Yes of course..

   

I think the main problem lies in  SendATcommand2. I have look at solutions such as circular buffers etc to read a string - but I am not sure. It might also be something totally different.  

   

Thank you.  

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

You should concentrate on receiving a complete string before you make any other actions. The logic behind your do-while loop will start to send repeatedly when part of your message is received. Using _GetRxBufferSize is quite correct and will work as expected.

   

 

   

Bob

0 Likes
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

Ok, I have now made a functionality which reads all bytes first (I have test this in main, it works flawless, but does not work in the sendATcommand function), and then goes to the do while statement (comparing statement). But it unfortunately does not work.

   

(look at the sendATcommand2)

   

Thank you. 

0 Likes
Anonymous
Not applicable

By the way , GPRS_1_PutString(ATcommand), should be in the while(1) statement. 😉 It should send out repeatedly until it receive the correct answer.  

   

Best regards. 

0 Likes
Anonymous
Not applicable

 while(1) {
    GPRS_1_PutString(ATcommand);
    CyDelay(2000); 
    if(GPRS_1_GetRxBufferSize() != 0) {

   

       
        response = GPRS_1_GetChar();
        if(response =='\r') 
        break; 
        i++; 
    }
  

0 Likes
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

Hi again:

   

I have made some research and I find out it probably would be best to use an interrupt routine, to get a string.

   

I have changed the code, so it gets all the bytes, when Rx is interrupted. The code does not work, I am not sure how to call an interrupt routine from a function (sendATcommand2) .  

   

Best regards. Hope you can help me 😉

0 Likes
lock attach
Attachments are accessible only for community members.
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Have a look into attached archive.

   

 

   

Bob

0 Likes
Anonymous
Not applicable

Thanks for the response.

   

I have read your comments.   What the code should do is .

   

1. Send ATcommand.   (line 125)

   

2.  Get back the response (line 104)

   

3. Compare the response with the ATcommand. (line 131, 138)

   

4. If there is a match break (continue to next statement)  

   

5. if there is not a match  repeat the process (SendATcommand again, this why I use else to break). (line 143)

   

By the way, the ReadString function repeat sending <0>, even though I expect OK back. (using bray terminal). 

0 Likes
lock attach
Attachments are accessible only for community members.
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Oops, sorry. Sewed with a too hot needle..

   

 

   

Bob

0 Likes
Anonymous
Not applicable

Hi Bob. 

   

Haha, that's Ok.  

   

It seems that, when I put a character in the buffer - it never leaves the ReadString function. It reads the characters and returns back to GPRS_1_GetRxBuffersize() wait. 

   

What if I am waiting for a Connect OK, to be received should I use \n or something else.

   

Best Regards. 

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

I would suggest you to check what string exactly is received using the debugger. Whether it is a \n or a \r or both (or none) you will see. You may even alter the ReadString() to have the delimiter as a parameter in case you need that. At least I would #define the delimiter for easiest adaption.

   

 

   

Bob

Anonymous
Not applicable

Hi Again

   

I have check with the debugger - it returns /r.  This part works now.

   

I have difficulties  writing a code, which keeps sending ATCommand, until the correct answer has been received. .

   

AT the moment it first sends ATcommand (line  137) 

   

and then waits until received data.   How can I make it keep sending ATcommand until the right answer has been received. 

   

Thanks . 😉

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

You can do that exactly as you wrote:

   

Repeat
Send AT-command
until answer satisfies

   

Since C-language has no Repeat-Until statement you need a small modification

   

Do
Send AT-Command
while Not(answer satisfies)
 

   

😉

   

Bob

0 Likes
Anonymous
Not applicable

 Hi.

   

I found a much easier way to do the job.

   

I have a question more (if thats Ok).  I want to use a Timer, to  count the time.

   

I want to use a timer in this task.

   

Like: if (strstr(resp, "<EOM>") != NULL && Timer_1_ReadCounter() > 929496720 )

   

{

   

  do this....

   

}  

   

else do something else.

   

if I get <EOM> and the time is under 17.896s --  it should execute the statement.  At the moment it doesnt work. 

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

We cant tell with just this code snippet what the timer config is

   

and when you init it and what you do if it rolls over.....

   

 

   

Consider posting your project, makes life easier to troubleshoot.

   

 

   

“File”                                                             Creator

   

“Create Workspace Bundle”

   

 

   

 

   

Regards, Dana.

0 Likes
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

Ok, sorry for that..  The timer is implemented in line 211. 

0 Likes