Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[PIC] Unknown string Problem?

Status
Not open for further replies.

nick703

Advanced Member level 1
Joined
Oct 17, 2011
Messages
422
Helped
21
Reputation
44
Reaction score
22
Trophy points
1,298
Location
surat
Activity points
3,987
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.
 

Hi,

I'd use the "}" as delimiter. Every time you receive this you may do the string compare.

If your send_string is always 6 bytes in length, then you should use the "}" and the previosly sent 5 bytes for your string compare.

Klaus
 

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.
 

Thanks for reply

@KlausST
there is no limit suppose some unkwon string length i between 2 to 10 character.


@Easyrider83
that means every time i set flag and then i have to compared like that
Code:
int main (void)
{
  initAll() ;  // configure uart and all other parameter

  while(1)
  {
      if(!strncmp(C, "{3QFG}", sizeof("{3QFG}")))
      {
       	  mLED_1_Off(); 
          bPassword = TRUE;
      }
      else if(!strncmp(C, "{START}", sizeof("{START}"))&& bPassword)
      {
           mLED_1_Off();
      }
       else if(!strncmp(C, "{POWER}", sizeof("{POWER}"))&& bPassword)
      {
           mLED_1_Off();
      }
  }
}

That's way i have trying if m i wrong then tell me.
 

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)...
 
I think sizeof("string")
don't work like you expect,
it return the size of a pointer, NOT THE SIZE OF THE STRING.

it is the same that :

const char * str = "string";

you can check than
sizeof(str) == sizeof("any string")
is true

you understand ?
 

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
 

Thanks a lot to all Now problem is solve.

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].

Code:
if((strlen(CommandFrame)== 7) && (!strncmp(CommandFrame,"{7U" , 3) && (CommandFrame[6]=='}')))
{
char TempA = (CommandFrame[3] - 0x30), TempB = (CommandFrame[4] - 0x30), TempC = (CommandFrame[5] - 0x30);
int FinalReadA = (TempA * 100) + (TempB * 10) + TempC;
char MessageFrame[8];
				
	MessageFrame[0] = '[';
	MessageFrame[1] = '7';
	MessageFrame[2] = 'U';
	MessageFrame[3] = TempA + 0x30;
	MessageFrame[4] = TempB + 0x30;
	MessageFrame[5] = TempC + 0x30;
	MessageFrame[6] = ']';
	MessageFrame[7] = '|';
	MessageFrame[8] = '\0';

	UART2_txArray(MessageFrame,strlen(MessageFrame));
}
 

Hi,

this needs an extra check, so it differs from the other checks.

Check on "{7U" only.

If this is true, then go to a function that tests on
* three bytes, each in the range of 0.30..0x39
* and one byte = "}"

Klaus
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top