Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Help me with my C algorithm

Status
Not open for further replies.
Re: C Algorithm Help Needed

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

    Points: 2
    Helpful Answer Positive Rating
Re: C Algorithm Help Needed

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.
 

Re: C Algorithm Help Needed

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.



May be true, I do not know about CCS, but HI-TECH looks OK. Even though the code can not be optimized that much, a bit of optimization can be done if it is required.

Code:
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); 
}

But the code still looks like having a bug, i is incrimented 2 times for every block (with ECHO == 0x0A, and ECHO == 0x0D) :!::?:

Check the following code :idea:


Code:
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); 
}
 

    glenjoy

    Points: 2
    Helpful Answer Positive Rating
Re: C Algorithm Help Needed

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.
 

    glenjoy

    Points: 2
    Helpful Answer Positive Rating
Re: C Algorithm Help Needed

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.

Thanks.
 

Re: C Algorithm Help Needed

glenjoy wrote:
(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.

This forum is for discussion. Please any one feel free to ask about any cryptic coding.

Regards,
Amr.
 

    glenjoy

    Points: 2
    Helpful Answer Positive Rating
Re: C Algorithm Help Needed

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[]?

Thanks.
 

Re: C Algorithm Help Needed

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.
 

    glenjoy

    Points: 2
    Helpful Answer Positive Rating
Re: C Algorithm Help Needed

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.

Hi,

Can you elaborate how thsaid code works?

Thanks.

Glenjoy.
 

Re: C Algorithm Help Needed

glenjoy,
I am awfully sorry.
There are two brackets missing.
The intended line should be like that
Code:
if((DATA[j] = getc()) == 0x0A)
not
Code:
if(DATA[j] = getc() == 0x0A)

Regards,
Amr

Added after 10 minutes:

glenjoy wrote:
Can you elaborate how thsaid code works?

Lets look at the code:
Code:
if((DATA[j] = getc()) == 0x0A)

According to precednce rules in C, the statements between brackets are executed first.
Code:
DATA[j] = getc()

Then this stored value is tested with the value 0x0A. As long as testing of the If conditional is executed the reading of stream is done & stored till finding the sequence 0X0A. If found the code cheks that the pre-stored value is 0X0D to increment the "i" variable.

One final issue about my code is what if the sequence 0x0D0X0A came within the message body. This vodes handles pretty well if the sequence 0x0D or 0x0A came seldomly in the message body.

Regards,
Amr.
 

    glenjoy

    Points: 2
    Helpful Answer Positive Rating
C Algorithm Help Needed

Code:
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.

Rewrite the awkward ?: NULL expression as an ordinary "if" statement.

Elsewhere ...

Need to allocate space for the DATA buffer.

Need to check for and prevent DATA buffer overrun in case of long message.
 

    glenjoy

    Points: 2
    Helpful Answer Positive Rating
Re: C Algorithm Help Needed

echo47 wrote:
If the first received character happens to be 0x0A, then j will be zero and DATA[j-1] could cause a memory access violation.

I totally agree but look at the first request by glenjoy. It seems that she is sure that the first byte recieved will be 0x0D.

He also wrote :
Need to allocate space for the DATA buffer.

Need to check for and prevent DATA buffer overrun in case of long message.

These are necessary for reliable code. I was wondering from the first how u can use an array in C language without knowing its size.

Regards,
Amr.
 

    glenjoy

    Points: 2
    Helpful Answer Positive Rating
C Algorithm Help Needed

You can't safely use a C array without knowing its size.

That BYTE DATA[] is an incomplete declaration - it's missing the size expression. My compiler aborts with an error message.
 

    glenjoy

    Points: 2
    Helpful Answer Positive Rating
Re: C Algorithm Help Needed

I would simply treat the 0x0D-0x0A pair as an end-of-line indicator. Just ignore empty lines.

Here is some more information on the modem commands.

www.modem.com/general/extendat.html

The link to Command Response and Result Codes appears to hint that the last line will have a result code if result codes are enabled. Check to see if that is the case. If my guess is wrong, then you will probably need to use a timeout, or stay with command-specific response handlers.
 

    glenjoy

    Points: 2
    Helpful Answer Positive Rating
Re: C Algorithm Help Needed

Hi,

Thanks for the replies, now I decided just to use an individual subroutine for each of the stream of data.

Now, I discovered a new bug, if the message goes like this [0x0D][0x0A][MESSAGE][0x0D][0x0A][MESSAGE with 0x0A][0x0D][0x0A] this function crashes.

Code:
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 );


I tried this;

Code:
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 );

Still the program crashes.

Btw, how to compare two arrays or strings with N length without using STRING.H's string compare.

Thanks.
 

Re: C Algorithm Help Needed

Hi glenjoy,

Check the following code for strncmp function. This function returns 0 if both the strings are equal below length characters, otherwise -1.

change it if you have any specific requirements.

Code:
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;
}

Program looks OK, check the total bytes of data is less than 90

Cheers
 

    glenjoy

    Points: 2
    Helpful Answer Positive Rating
C Algorithm Help Needed

I don't see any reason why an extra 0x0A would cause a crash.
If your messages exceed 90 characters, then buffer overflow could cause a crash.

Seems strange to wait for kbhit(), and then immediately check it again.
 

    glenjoy

    Points: 2
    Helpful Answer Positive Rating
Re: C Algorithm Help Needed

I do not know if why this code makes the systems crash,

Code:
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 );

All I know is that this code does not make the system crash:

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);

I just edited some of the parameters,

Code:
         if((DATA_MESSAGE[data_rx_i] = getc()) == 0x0A ) 
            (DATA_MESSAGE[data_rx_i-1] == 0x0D)?(data_rx_j+=2):'\0'; 
            data_rx_i++;

As for the kbhit() and reset_cpu(), I am planning now to remove those two for a more comportable portability of this the source to other micros.

Thanks.
 

Re: C Algorithm Help Needed

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.
 

    glenjoy

    Points: 2
    Helpful Answer Positive Rating
Re: C Algorithm Help Needed

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.

This may not be the case..

if (DATA_RX_ECHO == 0x0A) is true then only next condition will be checked.
As per the message 0x0A will follow 0x0D. so it would not be a problem.

Upto my understanding of the code can only crash / hang
1) 0x0D is received first when this function is called, DATA_MESSAGE[-1] may point to not allocated address.
2) When the byte count comming in the sequence is > 90
3) It can hang when there is no sufficient number of 0x0d, 0x0A seq. no timeouts.


Cheers
 

    glenjoy

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top