hello i have completely configure UART to receive and send data using serial port. in this configure below is my code
Code:
int main (void)
{
initAll() ; // configure uart and all other parameter
while(1)
{
if(!strncmp(C, "{3QFG}", sizeof("{3QFG}")))
{
mLED_1_Off();
}
else if(!strncmp(C, "{START}", sizeof("{START}")))
{
mLED_1_Off();
}
}
}
All this working properly with predefined command using serial port means in serial port i have send {3QFG} , {START} all command send and working completely.
But Now problem is suppose i send unknown command with unknown length that time My controller serial port is hang. so how to handle this stuff i didn't know. please give me advise to resolve this problem.
It is a bad way of coding. You have to check the flag that means that string was received and only after that string can be compared. Now you trying to compare the string even if it is receiving.
No, this is incorrect also. Flag should be set in timer interrupt, when timeout is over.
Every received byte occurs the interupt, where you store it and clear timer value. If byte not received, timer interupt sets the flag.
Another way is to catch up combination of 0x13 and 0x10 bytes being received - this is 'new string' means. You checking input byte, if it is 0x10, checking previos, if it is 0x13, set received flag, block the receiver or switch the pointer to another buffer (double buffer)...
There is a problem with using the 'sizeof' operator but it is not the one that aProgrammer mentions.
Sizeof will return a value that will include the trailing null character that the C compiler kindly adds to the double-quoted string equivalent character array. Therefore the 'sizeof' will return the size of the array which is 1 more than the number of characters.
If you want to use this approach then you need to use 'strlen' which shifts the length calculation from compile-tine to runtime, or use 'sizeof()-1'.
Susan
Now another thing i would to know please help me .
suppose i have send using serial port command {7Uxxx} this type of command and where xxx is the range 000 to 999
then i send back to this command then what can i do below is my first method is there any other method. to give back this command with [7Uxxx].