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);
2. incom[i] = bufIn; i++;
for(i = 0; i < 20; i++) Print(incom[i]);
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; } } } ... }
>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++)