This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

How recieve string into controller

Hi everybody!

I work with Keil C251 uVision2.
in my programm i want to recieve string into controller. i have an array of characters incom[20], which i use as a buffer for recieving string. i recieve current byte into variable bufIn and then replace it into my array:

 1.  	strcat(incom, bufIn);

or
 2.     incom[i] = bufIn;
	i++;
in 2 cases before (1) & (2) i test with my routine Print(unsigned char) and can see that every next byte is recieved into bufIn variable, and after (1) & (2) i can see that byte is stored into the next byte of incom[] array.
But after recieving the whole string while testing array:
for(i = 0; i < 20; i++)
     Print(incom[i]);
i can see the empty string with only the last recieved byte on the last place, for example: " a".
If anybody can help me to recieve the whole string?
And else. Why i can't create any array of characters more than 20 symbols?
Thaks.
Gennady.

  •  1.  	strcat(incom, bufIn);
    The standard strcat() library function needs both its arguments to be strings (ie, NULL-terminated)

    Are you having some sort of integer-promotion problem?
    ie, somewhere your char gets converted to an int then, when it goes back to a char, you get only the high-order byte - which is zero (ie, NULL)

    Why i can't create any array of characters more than 20 symbols?

    That shouldn't be a problem - what are your symptoms?


  • >1. strcat(incom, bufIn);
    >2. incom[i] = bufIn;
    > i++;

    How you declare "bufIn" ?

    Function strcat need "pointer to char" (not a "char") as second parameter.
    This must be NULL-terminated string with length less than unused area in "incom" !!!
    So, your code probably have a bug or bug's.


    I think this is not C251-specific question.



    >Why I can't create any array of
    >characters more than 20 symbols?

    Why not ? No problem.

    char       Buf[32]; // default memory space
    // or
    char idata Buf[64]; 
    // or
    char xdata Buf[1000]; 
    // or
    char edata Buf[32]; // C251-native addressing
    

    (You can receive error from linker if no free space in memory).

    Your question unclear,
    so no ideas because no source code.




  • Thank You for anser.
    i thik i've understood my problem.
    Thanks to all.

  • Sorry. I didn't found my mistake.
    my code is:

    bit Input = 0;
    int i = 0;
    unsigned char incom[18] = "";                  			
    unsigned char bufIn[2] = "";
    ....
    int PrntTo (unsigned char ch)
    {
    	TI = 0;
    	SBUF = ch;
    	while(!TI);
    }
    
    void port_io(void) interrupt 4 using 2 
    {  
    	 if (RI)
    	{	
    		bufIn[0] = SBUF;
    		Input = 1;
    	}
    }
    ...
    void main (void)  
    { 
    ...
    	while(1)
    	{	
    		memset (incom, 0, 18);
    		...
    		while (Input)
    		{
    			PrntTo(bufIn[0]);
    			RI = 0;
    			Input = 0;
    
    			strcat(incom, bufIn);
    			i++;
    			if(i == 17)
    			{
    				for(i = 0; i < 17; i++)
    					PrntTo(incom[i]);
    				i = 0;
    			}
    		}     
    	}
    ...
    }   
    

    I made some changes and now, when i send string "12345678901234567", in debugger i recieve the empty string with the last symbol:"7" at the first plase.
    Where is (or are) my problem(s)?

  • Your code includes several handling errors. I included my remarks in the code section.

    bit Input = 0;
    int i = 0;
    /* initialized variables blow up the code size. Try to avoid them. */
    unsigned char incom[18] = "";                  			
    /* The buffer must have a size of 19 if you want to receive 18 characters. Since you handle the buffer as string, a trailing 0x00 is inserted. */
    unsigned char bufIn[2] = "";
    /* The second byte of bufIn is never set to 0x00. This must be done for the strcat()-function. */
    ....
    int PrntTo (unsigned char ch)
    {
    	TI = 0;
    	SBUF = ch;
    	while(!TI);
    /* TI generates in interrupt here. You must handle this IRQ somehow. */
    }
    
    void port_io(void) interrupt 4 using 2 
    {  
    	 if (RI)
    	{	
    		bufIn[0] = SBUF;
    /* RI should be cleared immediately after reading SBUF. Otherwise you may lose a character. */
    		Input = 1;
    	}
    /* Deal here to clear the TI-Interrupt-request. Easiest way: TI =0; */
    }
    ...
    void main (void)  
    { 
    ...
    	while(1)
    	{	
    		memset (incom, 0, 18);
    /* use sizeof instead of 18. This applies to all occurences of the buffer's size. */
    		...
    		while (Input)
    		{
    			PrntTo(bufIn[0]);
    			RI = 0;
    			Input = 0;
    /* Setting this bit to 0 forces the controller to exit the while-Loop. Then the while(1)-loop is executed again, which clears your buffer again. All characters in the buffer are lost.... */
    			strcat(incom, bufIn);
    			i++;
    			if(i == 17)
    			{
    				for(i = 0; i < 17; i++)
    					PrntTo(incom[i]);
    				i = 0;
    			}
    		}     
    	}
    ...
    }   
    
    HHK

  • Your code not full...
    I still don't understand it.
    No hardware initialization sequence.
    (serial port, interrupts, timers)

    No sense to comment all of your code, just at first look:

    - you must clear RI manually

     if (RI)
     {	
       RI = 0; // !!!
    

    - you must process TI serial interrupt
    during PrntTo() or disable serial interrupts.
    (don't mix interrupt-driven
    serial input with
    "while(!TI);" sending method
    You need anything like:
    void port_io(void) interrupt 4 using 2 
    {  
     if( TI ) {
         TI = 0;
        // etc
     }
     if (RI) {
         RI = 0;
        // etc
     }
    }
    

    You can easy locate all of your problems via step-by-step debugging
    after Stop on Breakpoint in line

     void port_io(void) interrupt 4 using 2 
    


    Read "Application Note"
    http://www.keil.com/support/docs/1653.htm

  • >The buffer must have a size of 19 if
    >you want to receive 18 characters

    :)
    This guy "want" to receive 17 (I think :)

      i++;
      if(i == 17)
      {
        for(i = 0; i < 17; i++)