1

This question may be a duplicate of this question or this question

I am using C and trying to communicate with a device which works on serial communication. I am reading a single byte and storing it in an array and then checking the array element to '\0' (0x00). If it's '\0' then that means the data is now not available.

volatile int rxFlag = 0; volatile int index = 0; void function() //event based function, whenever a byte is received { Rx[index] = ReadUSART(); //reading a byte if(Rx[index] == '\0' ) //checking if its null { rxFlag = 1; //raise a flag } index++; //increment index } 

Below is the screenshot of the Rx:

enter image description here

In the image you can see the from Rx[0]-Rx[24] it contains data but Rx[25] is a null terminator (0x00) and the rxFlag should be set to 1. But it is not set.

What is the another way of checking if the array element is '\0'.

7
  • Have you tried adding simple debug code to see what Rx[25] outputs? Commented Aug 2, 2016 at 6:04
  • What is the index value at the time you did the screenshot? It seems you received 25 chars, so last checked is Rx[24].... Commented Aug 2, 2016 at 6:09
  • You can simply check if (!Rx[index]) rxFlag = 1; as the nul-character '\0' has the numeric value of 0. Commented Aug 2, 2016 at 6:17
  • I am sorry. I have used the wrong code. its not a while(1) it a function which is an event based and is fired whenever the data is received. the problem I am facing is because when the byte received is the last byte, it is then received and index is incremented but as it was the last byte so the event is not fired again and thus it doesnt check for null char. Commented Aug 2, 2016 at 6:22
  • Could you show us the declaration of rxFlag? Commented Aug 2, 2016 at 6:23

1 Answer 1

3

You have a typo when assigning rxFlag.

rxFlag == 1; 

should be

rxFlag = 1; 
Sign up to request clarification or add additional context in comments.

5 Comments

Good catch, it's funny how fallible the human mind is to glancing right over a '==' in place of a '='.
We've all been there :)
Your code looks fine from here, although I don't see what is reading/reacting to rxFlag.
The volatile statements are not necessary, unless you have something exotic like an interrupt routine updating them, in which case the compiler may do some unwanted optimisation without the volatile.
@PaulHK I have used the volatile because its inside the interrupt.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.