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.

Parents
  • 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

Reply
  • 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

Children