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

I think there is another case.
What i am saying in the buggy case the function will finish while there is input coming. I think this may crash the system. As there will be interrupt not handled.

If glenjoy tried my modification & it did not crash then I am correct.

Regards,
Amr
 

    glenjoy

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

amraldo said:
I think there is another case.
What i am saying in the buggy case the function will finish while there is input coming. I think this may crash the system. As there will be interrupt not handled.

If glenjoy tried my modification & it did not crash then I am correct.

Regards,
Amr

Yes, I tried the modification and did not crashed the system.

But my problem is, how come the buggy routine crashes?

1. The string element is not more than 90
2. The subroutine does not really look for a sequence but rather in the appearance of 0x0D for it to increment the the counter for do-while loop.
3. The sequence of data is still the same aside from that a 0x0A was inserted in the message.

Anyway, I am now looking in optimizing this routine:

Code:
void out_buffer(char *STRING_BUFFER, char STRING_ENTRY)
{
   BYTE OUT_ECHO, out_i;

   for(out_i=0 ; STRING_BUFFER[out_i] != '\0' ; out_i++ )
      {

         putc(STRING_BUFFER[out_i]);

         while(!kbhit());

         if(kbhit())
            {
               OUT_ECHO = getc();
               if(OUT_ECHO != STRING_BUFFER[out_i])
                  {
                  reset_cpu();
                  }
            }
      }

   if(STRING_ENTRY == 0x0D)
      {
         putc(0x0D);

         while(!kbhit());

         if(kbhit())
            {
               OUT_ECHO = getc();
               if(OUT_ECHO != 0x0D)
                  {
                  reset_cpu();
                  }
               else
                  {
                  get_reply_ok();
                  }
            }
         }
   else
      if(STRING_ENTRY == 0x1A)
         {
            putc(0x1A);

            while(!kbhit());

            if(kbhit())
               {
                  OUT_ECHO = getc();
                  if(OUT_ECHO != 0x1A)
                     {
                     reset_cpu();
                     }
                  else
                     {
                     message_report();
                     }
               }
         }
   else
      if(STRING_ENTRY == 0x3E)
         {
            putc(0x0D);

            while(!kbhit());

            if(kbhit())
               {
                  OUT_ECHO = getc();
                  if(OUT_ECHO != 0x0D)
                     {
                     reset_cpu();
                     }
                  else
                     {
                     ready_tx();
                     }
               }
         }
}

Is there still a possibility to still improve this one? I am looking for techniques in C programming.
 

Re: C Algorithm Help Needed

glenjoy wrote:
how come the buggy routine crashes?

Why the original code is buggy? Easy to answer. I think I was correct.
Look at the input sequence you said ot caused the bug:
[0x0D][0x0A][message][0x0D][0x0A][message][0x0A][0x0D][0x0A]
When your code reaches the last 0x0D it incremneted the variable data_rx_j so the function stops while there is still unrecievd byte the last 0x0A (the one after the red one). So the system crashes as it does not know how to deal with it. data_rx_j should count of occuring 0x0D0x0A succesively not only 0x0D

Added after 24 minutes:

Suggested optimization is to modify every:
Code:
while(!kbhit()); //wait for a  keyboard hit
if(kbhit())     //check if there is a keyboard hit
{
........
}
TO
Code:
while(!kbhit());//wait for a  keyboard hit
........


There is another optimization but you must check the following code statements:
Code:
else 
      if(STRING_ENTRY == 0x3E) 
         { 
            putc(0x0D); 

            while(!kbhit()); 

            if(kbhit()) 
               { 
                  OUT_ECHO = getc(); 
                  if(OUT_ECHO != 0x0D) 
                     { 
                     reset_cpu(); 
                     } 
                  else 
                     { 
                     ready_tx(); 
                     } 
               } 
         }

particulary these two lines
Code:
putc(0x0D); 
...........
if(OUT_ECHO != 0x0D)
If it is wrong & it is actually
Code:
putc(0x3E);
.................
if(OUT_ECHO != 0x3E)
as I hope there will be a great optimization.

Regards,
Amr.
 

Re: C Algorithm Help Needed

I also agree with amraldo :D
No need of if(kbhit())
putc(0x0D) in place of putc(0x3E)
by considering amraldo is correct, code can be written the follwing way.

Code:
void out_buffer(char *STRING_BUFFER, char STRING_ENTRY) 
{ 
   BYTE OUT_ECHO, out_i; 

   for(out_i=0 ; STRING_BUFFER[out_i] != '\0' ; out_i++ ) 
      { 

         putc(STRING_BUFFER[out_i]); 

         while(!kbhit()); 

         OUT_ECHO = getc(); 
         if(OUT_ECHO != STRING_BUFFER[out_i]) 
           { 
              reset_cpu(); 
           } 
      } 
      
      
   putc(STRING_ENTRY); 
   while(!kbhit()); 
   OUT_ECHO = getc(); 
   
   if(OUT_ECHO != STRING_ENTRY)
   {
       reset_cpu(); 
   }
   else if(STRING_ENTRY == 0x0D) 
   {
       get_reply_ok(); 
   }
   else if(STRING_ENTRY == 0x1A) 
   {
       message_report(); 
   }
   else if(STRING_ENTRY == 0x3E) 
   {
       ready_tx(); 
   }  
}


Cheers
 

Re: C Algorithm Help Needed

Hi,

Is this the same
Code:
               OUT_ECHO = getc();
               if(OUT_ECHO != STRING_BUFFER[out_i])

with

Code:
               if((OUT_ECHO = getc())!= STRING_BUFFER[out_i])

I am now planning to remove kbhit() on the sourcecode because some compilers in C does not have a kbhit() function.

As I see it, while(!kbhit()) does nothing in the function but just wait for a character entry then goes into the next statement.

So it will be just like the getc() that will wait for a character entry before it will go into the next statement.

As I see it, kbhit() is useful only if you will accompany it with a timeout say,

Code:
LONG TIME_OUT;

while(!kbhit() && (++TIME_OUT < 0xFFFF));
{

}

Tell me if my analysis is wrong.

Added after 10 minutes:

echo47 said:
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.

I think the solution to this is to initialize, j = 1 not j=0.
 

Re: C Algorithm Help Needed

glenjoy wrote:

Hi,

Is this the same

Code:
OUT_ECHO = getc(); 
if(OUT_ECHO != STRING_BUFFER[out_i])
with
Code:
if((OUT_ECHO = getc())!= STRING_BUFFER[out_i])
Yes, it is. Welcome to the cryptic coding.:D

Also wrote
So it will be just like the getc() that will wait for a character entry before it will go into the next statement.
I think getc() deals with streams. So if you removed khbit() & used getc() only & there were no input, I think your system will crash. The
Code:
while(!khbit()) ;
is useful anyway.

Also wrote:
I think the solution to this is to initialize, j = 1 not j=0.
If you guarantee that your first byte is not 0x0A, there is no need for that. As I remeber that 0X0D 0X0A are equivalent to enter & I think there is no need for that.

Regards,
Amr.
 

    glenjoy

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

amraldo said:
glenjoy wrote:

Hi,

Is this the same

Code:
OUT_ECHO = getc(); 
if(OUT_ECHO != STRING_BUFFER[out_i])
with
Code:
if((OUT_ECHO = getc())!= STRING_BUFFER[out_i])
Yes, it is. Welcome to the cryptic coding.:D

Also wrote
So it will be just like the getc() that will wait for a character entry before it will go into the next statement.
I think getc() deals with streams. So if you removed khbit() & used getc() only & there were no input, I think your system will crash. The
Code:
while(!khbit()) ;
is useful anyway.

Also wrote:
I think the solution to this is to initialize, j = 1 not j=0.
If you guarantee that your first byte is not 0x0A, there is no need for that. As I remeber that 0X0D 0X0A are equivalent to enter & I think there is no need for that.

Regards,
Amr.

Hi,

In the compiler I am using, it is stated like this in the header file,

Code:
#define getc getch
#define fgetc getch
#define getchar getch
#define putc putchar
#define fputc putchar
#define fgets gets
#define fputs puts

So I think getc() in the said compiler is just the same as getch(), can this be a valid reason to remove the kbhit()?

Thanks.

Glenjoy
 

Re: C Algorithm Help Needed

glenjoy wrote:

So I think getc() in the said compiler is just the same as getch(), can this be a valid reason to remove the kbhit()?

Yes, it is. getch() suspends operation till a key hit.

Regards,
Amr.
 

    glenjoy

    Points: 2
    Helpful Answer Positive Rating
C Algorithm Help Needed

How to copy the whole content of a structure into a single memory?

Thanks.

Glenjoy
 

C Algorithm Help Needed

Please clarify "single memory"

You can copy structs just like you copy variables, by using the "=" operator.
 

Re: C Algorithm Help Needed

Hi,
You can assign the pointer of the structure to the pointer of the memory. but take care of the memory organization: Is it a little endian or big endian?

Regards,
Amr.
 

Re: C Algorithm Help Needed

I got errors, say this example:

Code:
struct test {

 char bit_0 : 1;
 char bit_1 : 1;
 char bit_2 : 1;
 char bit_3 : 1;
 char bit_4 : 1;
 char bit_5 : 1;
 char bit_6 : 1;
 char bit_7 : 1;

} test_pins;

char buffer;

buffer = test_pins;
 

Re: C Algorithm Help Needed

Yes, that's an incompatible assignment.

You didn't say what you are trying to achieve. Maybe change char buffer; to struct test buffer;

Or maybe change buffer = test_pins; to buffer = *(char*)&test_pins; but that can have unexpected results depending on how your compiler stores data in a struct.

You will have better portability by changing those eight struct members from char to unsigned because some compilers assume char is signed, and I'm guessing you don't want the values to be 0 and -1.

As you learn C, you will have many questions. This FAQ has many answers:
https://www.eskimo.com/~scs/C-faq/top.html
 

Re: C Algorithm Help Needed

echo47 wrote:
Or maybe change buffer = test_pins; to buffer = *(char*)&test_pins;
Thats a good suggestion but you should know the memory organization of your machine(compiler).


Regards,
Amr.
 

C Algorithm Help Needed

You must have read my message just before I edited it. ;)
 

Re: C Algorithm Help Needed

You did not see my message at 13 August 2005 12:00 :!:

Regards,
Amr.
 

C Algorithm Help Needed

I saw your message. It is fine.
Or maybe you were talking to glenjoy. Gets confusing here sometimes. ;)
 

Re: C Algorithm Help Needed

What is the best way to compare strings, I have this example of a stored data in an array

Code:
unsigned char DATA[10] = "AD ADC1"

Now I want to compare the said array to another array of data and it the result matches, it will excute a certain case statement.

Wjat is the best way to do this.
 

C Algorithm Help Needed

If they are null-terminated characters strings such as your "AD ADC1" example, the easy way to compare them is with strcmp or strncmp.

If they are blocks of data without null-terminators, use memcmp.

Don't forget #include <strings.h>
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top