<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://community.arm.com/utility/feedstylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>How recieve string into controller</title><link>https://community.arm.com/developer/tools-software/tools/f/keil-forum/14366/how-recieve-string-into-controller</link><description> 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</description><dc:language>en-US</dc:language><generator>Telligent Community 10</generator><item><title>RE: How recieve string into controller</title><link>https://community.arm.com/thread/85057?ContentTypeID=1</link><pubDate>Wed, 13 Jun 2001 07:00:17 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:ee2e44a7-6478-43f3-a634-ce1d7970899e</guid><dc:creator>Sergey Dymchishin</dc:creator><description>&lt;p&gt;&amp;gt;The buffer must have a size of 19 if &lt;br /&gt;
&amp;gt;you want to receive 18 characters&lt;br /&gt;
&lt;br /&gt;
:)&lt;br /&gt;
This guy &amp;quot;want&amp;quot; to receive 17 (I think :)&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;
  i++;
  if(i == 17)
  {
    for(i = 0; i &amp;lt; 17; i++)
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How recieve string into controller</title><link>https://community.arm.com/thread/84106?ContentTypeID=1</link><pubDate>Wed, 13 Jun 2001 06:42:19 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:42eaa7bf-0cc8-47cf-84c6-c15708036d76</guid><dc:creator>Sergey Dymchishin</dc:creator><description>&lt;p&gt;Your code not full...&lt;br /&gt;
I still don&amp;#39;t understand it.&lt;br /&gt;
No hardware initialization sequence.&lt;br /&gt;
(serial port, interrupts, timers)&lt;br /&gt;
&lt;br /&gt;
No sense to comment all of your code, just at first look:&lt;br /&gt;
&lt;br /&gt;
- you must clear RI manually&lt;br /&gt;
&lt;pre&gt;
 if (RI)
 {	
   RI = 0; // !!!
&lt;/pre&gt;
&lt;br /&gt;
- you must process TI serial interrupt &lt;br /&gt;
during PrntTo() or disable serial interrupts.&lt;br /&gt;
(don&amp;#39;t mix interrupt-driven&lt;br /&gt;
serial input with &lt;br /&gt;
&amp;quot;while(!TI);&amp;quot; sending method&lt;br /&gt;
You need anything like:&lt;br /&gt;
&lt;pre&gt;
void port_io(void) interrupt 4 using 2 
{  
 if( TI ) {
     TI = 0;
    // etc
 }
 if (RI) {
     RI = 0;
    // etc
 }
}
&lt;/pre&gt;
&lt;br /&gt;
You can easy locate all of your problems via step-by-step debugging&lt;br /&gt;
after Stop on Breakpoint in line&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;
 void port_io(void) interrupt 4 using 2 
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
Read &amp;quot;Application Note&amp;quot;&lt;br /&gt;
&lt;a href="http://www.keil.com/support/docs/1653.htm"&gt;http://www.keil.com/support/docs/1653.htm&lt;/a&gt; &lt;br /&gt;
&lt;br /&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How recieve string into controller</title><link>https://community.arm.com/thread/84109?ContentTypeID=1</link><pubDate>Wed, 13 Jun 2001 06:24:52 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:22e15737-8b20-42c2-b499-8c582d78a53d</guid><dc:creator>Hannes</dc:creator><description>&lt;p&gt;Your code includes several handling errors. I included my remarks in the code section.&lt;br /&gt;
&lt;pre&gt;
bit Input = 0;
int i = 0;
/* initialized variables blow up the code size. Try to avoid them. */
unsigned char incom[18] = &amp;quot;&amp;quot;;                  			
/* 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] = &amp;quot;&amp;quot;;
/* 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&amp;#39;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 &amp;lt; 17; i++)
					PrntTo(incom[i]);
				i = 0;
			}
		}     
	}
...
}   
&lt;/pre&gt;
HHK&lt;br /&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How recieve string into controller</title><link>https://community.arm.com/thread/53715?ContentTypeID=1</link><pubDate>Wed, 13 Jun 2001 04:38:34 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:119e0038-96fd-4349-9116-1bba1949cec0</guid><dc:creator>Mishiev Gennady</dc:creator><description>&lt;p&gt;Sorry. I didn&amp;#39;t found my mistake.&lt;br /&gt;
my code is:&lt;br /&gt;
&lt;pre&gt;
bit Input = 0;
int i = 0;
unsigned char incom[18] = &amp;quot;&amp;quot;;                  			
unsigned char bufIn[2] = &amp;quot;&amp;quot;;
....
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 &amp;lt; 17; i++)
					PrntTo(incom[i]);
				i = 0;
			}
		}     
	}
...
}   
&lt;/pre&gt;
&lt;br /&gt;
I made some changes and now, when i send string &lt;b&gt;&amp;quot;12345678901234567&amp;quot;&lt;/b&gt;, in debugger i recieve the empty string with the last symbol:&lt;b&gt;&amp;quot;7&amp;quot;&lt;/b&gt; at the first plase.&lt;br /&gt;
Where is (or are) my problem(s)?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How recieve string into controller</title><link>https://community.arm.com/thread/53714?ContentTypeID=1</link><pubDate>Wed, 13 Jun 2001 03:58:29 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:ae4370a8-8163-40a2-a8a7-bc8df5e76367</guid><dc:creator>Mishiev Gennady</dc:creator><description>&lt;p&gt;Thank You for anser.&lt;br /&gt;
i thik i&amp;#39;ve understood my problem.&lt;br /&gt;
Thanks to all.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How recieve string into controller</title><link>https://community.arm.com/thread/37596?ContentTypeID=1</link><pubDate>Wed, 13 Jun 2001 03:35:11 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:4a5d4c31-7a59-41d2-9d4d-59e90736761d</guid><dc:creator>Sergey Dymchishin</dc:creator><description>&lt;p&gt;&amp;gt;1. strcat(incom, bufIn);&lt;br /&gt;
&amp;gt;2. incom[i] = bufIn;&lt;br /&gt;
&amp;gt;   i++;&lt;br /&gt;
&lt;br /&gt;
How you declare &amp;quot;bufIn&amp;quot; ?&lt;br /&gt;
&lt;br /&gt;
Function &lt;b&gt;strcat&lt;/b&gt; need &amp;quot;pointer to char&amp;quot; (not a &amp;quot;char&amp;quot;) as second parameter.&lt;br /&gt;
This must be NULL-terminated string with length less than unused area in &amp;quot;incom&amp;quot; !!!&lt;br /&gt;
So, your code probably have a bug or bug&amp;#39;s.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
I think this is not C251-specific question. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;gt;Why I can&amp;#39;t create any array of &lt;br /&gt;
&amp;gt;characters more than 20 symbols?&lt;br /&gt;
&lt;br /&gt;
Why not ? No problem.&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;
char       Buf[32]; // default memory space
// or
char idata Buf[64]; 
// or
char xdata Buf[1000]; 
// or
char edata Buf[32]; // C251-native addressing
&lt;/pre&gt;
&lt;br /&gt;
(You can receive error from linker if no free space in memory).&lt;br /&gt;
&lt;br /&gt;
Your question unclear, &lt;br /&gt;
so no ideas because no source code.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How recieve string into controller</title><link>https://community.arm.com/thread/37591?ContentTypeID=1</link><pubDate>Wed, 13 Jun 2001 03:22:12 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:5c4191ab-1d66-4bdb-b586-1bc9288039d0</guid><dc:creator>Andrew Neil</dc:creator><description>&lt;p&gt;&lt;pre&gt; 1.  	strcat(incom, bufIn);&lt;/pre&gt;
The standard strcat() library function needs &lt;b&gt;both&lt;/b&gt; its arguments to be &lt;b&gt;strings&lt;/b&gt; (&lt;i&gt;ie&lt;/i&gt;, NULL-terminated)&lt;br /&gt;
&lt;br /&gt;
Are you having some sort of integer-promotion problem?&lt;br /&gt;
&lt;i&gt;ie&lt;/i&gt;, somewhere your &lt;b&gt;char&lt;/b&gt; gets converted to an &lt;b&gt;int&lt;/b&gt; then, when it goes back to a &lt;b&gt;char&lt;/b&gt;, you get only the high-order byte - which is zero (&lt;i&gt;ie&lt;/i&gt;, NULL) &lt;br /&gt;
&lt;br /&gt;
&lt;i&gt;Why i can&amp;#39;t create any array of characters more than 20 symbols?&lt;/i&gt;&lt;br /&gt;
&lt;br /&gt;
That shouldn&amp;#39;t be a problem - what are your symptoms?&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>