void DATA(BYTE count)
{
BYTE j = 0, ECHO;
BYTE DATA[];
BYTE i = 0;
do
{
ECHO = getc();
if(ECHO != 0x0A && ECHO != 0x0D)
{
MESSAGE[j] = ECHO;
j++;
}
else
{
MESSAGE[j] = ECHO;
j++;
i++;
}
} while(i < count);
}
idlebrain said:What about the count 4 / 6 is passed as the argument.
DATA_A equals DATA(4)
DATA_B equals DATA(6)
Code:void DATA(BYTE count) { BYTE j = 0, ECHO; BYTE DATA[]; BYTE i = 0; do { ECHO = getc(); if(ECHO != 0x0A && ECHO != 0x0D) { MESSAGE[j] = ECHO; j++; } else { MESSAGE[j] = ECHO; j++; i++; } } while(i < count); }
glenjoy said:idlebrain said:What about the count 4 / 6 is passed as the argument.
DATA_A equals DATA(4)
DATA_B equals DATA(6)
Code:void DATA(BYTE count) { BYTE j = 0, ECHO; BYTE DATA[]; BYTE i = 0; do { ECHO = getc(); if(ECHO != 0x0A && ECHO != 0x0D) { MESSAGE[j] = ECHO; j++; } else { MESSAGE[j] = ECHO; j++; i++; } } while(i < count); }
Good idea, but now, I am more on think of how to optimize the source code, freaking CCS C got lots of bugs.
It cannot function very well anymore if about 50% of its RAM is used and has a problem finding its way back.
Maybe have to use ARM for my application.
Or study the use of HI-TECH.
void DATA(BYTE count)
{
BYTE j = 0, ECHO;
BYTE DATA[];
BYTE i = 0;
do
{
ECHO = getc();
MESSAGE[j] = ECHO;
j++;
if(ECHO != 0x0A && ECHO != 0x0D)
{
;
}
else
{
i++;
}
} while(i < count);
}
void DATA(BYTE count)
{
BYTE j = 0, ECHO;
BYTE DATA[];
BYTE i = 0;
do
{
ECHO = getc();
MESSAGE[j] = ECHO;
j++;
if(ECHO == 0x0A)
{
i++;
}
} while(i < count);
}
void DATA(BYTE count)
{
BYTE i =0; j=0;
BYTE DATA[];
do
{
if(DATA[j] = getc() == 0x0A )
(DATA[j-1] == 0x0D) ? i+=2 : NULL;
j++;
} while(i < count);
amraldo said:Here is another Optimized code (Please inform if buggy)
Code:void DATA(BYTE count) { BYTE i =0; j=0; BYTE DATA[]; do { if(DATA[j] = getc() == 0x0A ) (DATA[j-1] == 0x0D) ? i+=2 : NULL; j++; } while(i < count);
Regards,
Amr.
(DATA[j-1] == 0x0D) ? i+=2 : NULL; , let us not be too cryptic in the coding, maybe others also are interested on this routine and they might not be able to understand the line.
amraldo said:Here is another Optimized code (Please inform if buggy)
Code:void DATA(BYTE count) { BYTE i =0; j=0; BYTE DATA[]; do { if(DATA[j] = getc() == 0x0A ) (DATA[j-1] == 0x0D) ? i+=2 : NULL; j++; } while(i < count);
Regards,
Amr.
In the source code above? Are the 0x0D and 0x0A still stored in DATA[]?
if(DATA[j] = getc() == 0x0A)
amraldo said:glenjoy wrote:
In the source code above? Are the 0x0D and 0x0A still stored in DATA[]?
Yes they are still stored in the array above inthe following cryptic code section:
Code:if(DATA[j] = getc() == 0x0A)
Regards,
Amr.
if((DATA[j] = getc()) == 0x0A)
if(DATA[j] = getc() == 0x0A)
Can you elaborate how thsaid code works?
if((DATA[j] = getc()) == 0x0A)
DATA[j] = getc()
if((DATA[j] = getc()) == 0x0A)
(DATA[j-1] == 0x0D) ? i+=2 : NULL;
If the first received character happens to be 0x0A, then j will be zero and DATA[j-1] could cause a memory access violation.
Need to allocate space for the DATA buffer.
Need to check for and prevent DATA buffer overrun in case of long message.
void data_rx()
{
BYTE DATA_RX_ECHO, data_rx_i, data_rx_j;
BYTE DATA_MESSAGE[90];
data_rx_i = 0;
data_rx_j = 0;
do
{
while(!kbhit());
if(kbhit())
{
DATA_RX_ECHO = getc();
if((DATA_RX_ECHO != 0x0A) && (DATA_RX_ECHO!= 0x0D))
{
DATA_MESSAGE[data_rx_i] = DATA_RX_ECHO;
data_rx_i++;
}
else
{
DATA_MESSAGE[data_rx_i] = DATA_RX_ECHO;
data_rx_i++;
data_rx_j++;
}
}
else
{
reset_cpu();
}
} while( data_rx_j < 6 );
void data_rx()
{
BYTE DATA_RX_ECHO, data_rx_i, data_rx_j;
BYTE DATA_MESSAGE[90];
data_rx_i = 0;
data_rx_j = 0;
do
{
while(!kbhit());
if(kbhit())
{
DATA_RX_ECHO = getc();
if((DATA_RX_ECHO != 0x0A) && (DATA_RX_ECHO!= 0x0D))
{
DATA_MESSAGE[data_rx_i] = DATA_RX_ECHO;
data_rx_i++;
}
else
{
DATA_MESSAGE[data_rx_i] = DATA_RX_ECHO;
data_rx_i++;
if(DATA_RX_ECHO == 0x0D)
data_rx_j++;
}
}
else
{
reset_cpu();
}
} while( data_rx_j < 3 );
int mystrncmp(const char *s1, const char *s2, size_t maxlen)
{
int result = 0;
while(maxlen > 0)
{
if(*s1 == '\0' || *s2 == '\0')
break;
if(*s1 != *s2)
{
result = -1;
break;
}
s1++;s2++;
maxlen--;
}
return result;
}
void data_rx()
{
BYTE DATA_RX_ECHO, data_rx_i, data_rx_j;
BYTE DATA_MESSAGE[90];
data_rx_i = 0;
data_rx_j = 0;
do
{
while(!kbhit());
if(kbhit())
{
DATA_RX_ECHO = getc();
if((DATA_RX_ECHO != 0x0A) && (DATA_RX_ECHO!= 0x0D))
{
DATA_MESSAGE[data_rx_i] = DATA_RX_ECHO;
data_rx_i++;
}
else
{
DATA_MESSAGE[data_rx_i] = DATA_RX_ECHO;
data_rx_i++;
if(DATA_RX_ECHO == 0x0D)
data_rx_j++;
}
}
else
{
reset_cpu();
}
} while( data_rx_j < 3 );
void DATA(BYTE count)
{
BYTE i =0; j=0;
BYTE DATA[];
do
{
if(DATA[j] = getc() == 0x0A )
(DATA[j-1] == 0x0D) ? i+=2 : NULL;
j++;
} while(i < count);
if((DATA_MESSAGE[data_rx_i] = getc()) == 0x0A )
(DATA_MESSAGE[data_rx_i-1] == 0x0D)?(data_rx_j+=2):'\0';
data_rx_i++;
if((DATA_RX_ECHO == 0x0A)&&(DATA_MESSAGE[data_rx_i-1]==0x0D) data_rx_j++;
amraldo said:I do not know why exactly your code crahses but I think it is a better idea to modify your code as follows:
Code:if((DATA_RX_ECHO == 0x0A)&&(DATA_MESSAGE[data_rx_i-1]==0x0D) data_rx_j++;
I think that your code will stop working at the last 0X0D leaving the final 0x0A undealt with. In other words there are still input at they keboard while you do not handle by your function.
You should only increment the data_rx_j when you recieve the complete sequence 0x0D0x0Anot only 0x0D
Regards,
Amr.
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?