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.

glenjoy

Banned
Joined
Jan 1, 2004
Messages
962
Helped
72
Reputation
146
Reaction score
20
Trophy points
1,298
Location
Philippines
Activity points
0
C Algorithm Help Needed

I have these data, it is formatted as [0x0D][0x0A][message][0x0D][0x0A][message][0x0D][0x0A] and [0x0D][0x0A][message][0x0D][0x0A]

What I need is to get all data and stop once the last 0x0A is received.

I had a solution already but my problem is that I use two algorithms to process each of the said data, one subroutine for [0x0D][0x0A][message][0x0D][0x0A][message][0x0D][0x0A] and another subroutine for [0x0D][0x0A][message][0x0D][0x0A].

Now what I need is to use only one subroutine in processing both said data.

Thanks.
 

Re: C Algorithm Help Needed

You can do this in one routine by having separate counters for the two frames, which look and count for the occurence of 0x0A after a special sequence of the string.
 

    glenjoy

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

eda_surfer said:
You can do this in one routine by having separate counters for the two frames, which look and count for the occurence of 0x0A after a special sequence of the string.

Care to give your example, what do you mean special sequence, I already gave the sequence and I think there is no special about it.

Btw, use of functions under string.h is not allowed, there is no null terminator in the said sequence.

The only one that marks the start is 0x0D and stop is 0x0E. problem is, at the start, 0x0D and 0x0A are sequentially given.

Your counter suggestion is only good for one subroutine but will not work for both.

If you think it will work, then show your source code or your routine flow.
 

C Algorithm Help Needed

If those are in-memory character strings with no terminator, then it is impossible to distinguish the two forms, unless there's something in [message] that tells you it's the last message.

Or are they disk files, or stdin? You can detect the end-of-file.

0x0E? Where is that?

You haven't described your two algorithms, so we can't help you convert them into one function.
 

    glenjoy

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

echo47 said:
If those are in-memory character strings with no terminator, then it is impossible to distinguish the two forms.
Or are they disk files? You can detect the end-of-file.

0x0E? Where is that?

You haven't described your two algorithms, so we can't help you convert them into one function.

Sorry, it is a typo, START is [0x0D] and STOP is [0x0A].

Here is the code for the sequence [0x0D][0x0A][message][0x0D][0x0A]

Code:
void data_A()
{
   BYTE i = 0, j = 0, ECHO, DATA[];
   do
   {
         ECHO = getc();
         if(ECHO != 0x0A && ECHO != 0x0D)
            {
             DATA[j] = ECHO;
             j++;
            }
         else
            {
             DATA[j] = ECHO;
             j++;
             i++;
            }
   } while(i<4);
}

This one is for the [0x0D][0x0A][message][0x0D][0x0A][message][0x0D][0x0A]

Code:
void DATA_B()
{

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

Now what I need is just one subroutine that will do both of the said routines.
 

C Algorithm Help Needed

I see you are inputting this text from stdin. Can you use the end-of-file flag to tell you that input has ended?

If not, then is there something in [message] that tells you it's the last message?

If not, then your problem seems unsolvable.
 

    glenjoy

    Points: 2
    Helpful Answer Positive Rating
C Algorithm Help Needed

No, there is none in the message that indicates that it is the end of it, the stop bytes indicator are same with the start bytes indicator.

If there is no single routine that can do both functions, I guess I will just ask if I can still improve the said routines.

Thanks.
 

C Algorithm Help Needed

Are you sure you can't use EOF (end-of-file)?
getc() returns EOF when no more input is available.

getc() returns an int, but you are putting it into a BYTE, which I'm guessing is an unsigned char. That could corrupt the EOF value.
 

    glenjoy

    Points: 2
    Helpful Answer Positive Rating
C Algorithm Help Needed

What do you mean if no more input is available?

Care to site an example?
 

Re: C Algorithm Help Needed

Your situation mirrors that of non-unique decodability in coding theory. Any code that is a prefix of another code is not uniquely decodable.
As such, there is no single function that can handle both data streams. On the other hand, it shows that the protocol used is a lousy one.
 

    glenjoy

    Points: 2
    Helpful Answer Positive Rating
C Algorithm Help Needed

Hi,

So you mean that the modem AT command protocol is a lousy one.

How come hyperterminal can decode such?
 

Re: C Algorithm Help Needed

glenjoy,

can you describe what you are trying to do??

One possible solution would be to buffer the messages and decide via a timeout of xxxx mS if the data received is finished or not.

So in the receiving routine (working in interrupt) all you have to do is reset the timeout timer with every character received to XXX mS and whenever the timer overflows your received message is "finished".

Could this be a solution for you??

best regards
 

    glenjoy

    Points: 2
    Helpful Answer Positive Rating
C Algorithm Help Needed

Hi,

I am getting data from a modem, and it is how the modem throws its data, I am just wondering if how can I make a micro receive a data as efficient as hyperterminal.
 

Re: C Algorithm Help Needed

I never stated that the modem AT command protocol is a lousy one, so stop putting words in my mouth. The protocol that you have showed is a lousy one.

I do not know the AT command protocol, but a quick search on the net reveals that they start with AT, and end with <Enter>. They are different from what you have. BTW, <0x0D 0x0A> is equivalent to <Enter>.

To demonstrate a simple example of non-unique decodability, assuming that "0", "01" and "001" are valid codes. An input stream 001 can mean "0"+"01" or they can mean "001". There is no unique decoding method, as "0" is a prefix of both "01" and "001".
 

    glenjoy

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

Hyperterminal is not "eficient" in any way it is just a "stupid" PC program whicj must not care for memory or resources as you must do in microcontroller programming.

I was asking you if there is no way to know if the numer of received messages is 1 or 2 or ...

Is this just an answer to something that you are initiating or where do the messages you want to receive come from???

Can you post an example log of hyperterminal??
 

    glenjoy

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

checkmate said:
I never stated that the modem AT command protocol is a lousy one, so stop putting words in my mouth. The protocol that you have showed is a lousy one.

I do not know the AT command protocol, but a quick search on the net reveals that they start with AT, and end with <Enter>. They are different from what you have. BTW, <0x0D 0x0A> is equivalent to <Enter>.

To demonstrate a simple example of non-unique decodability, assuming that "0", "01" and "001" are valid codes. An input stream 001 can mean "0"+"01" or they can mean "001". There is no unique decoding method, as "0" is a prefix of both "01" and "001".

LOL, AT Commands replies begins with 0x0D and 0x0A and also ends with that, so before commenting about lousy protocols, try to know the facts first.

Added after 4 minutes:

C-Man said:
Hyperterminal is not "eficient" in any way it is just a "stupid" PC program whicj must not care for memory or resources as you must do in microcontroller programming.

I was asking you if there is no way to know if the numer of received messages is 1 or 2 or ...

Is this just an answer to something that you are initiating or where do the messages you want to receive come from???

Can you post an example log of hyperterminal??

Hi,

There is a way to know if the message to be received is 1 or 2, it will depend on the AT command you executed.

For example, you got OK, it will give [0x0D][0x0A][O][K][0x0D][0x0A], if you execute a different command, it will reply for a stream of data having the other kind of format.

Thanks.

Glenjoy
 

Re: C Algorithm Help Needed

glenjoy said:
LOL, AT Commands replies begins with 0x0D and 0x0A and also ends with that, so before commenting about lousy protocols, try to know the facts first.
I normally do not like to squabble here as I feel that this is a technical forum for sharing. But I just blew it.
The initial protocol you gave is clearly non-uniquely decodable, and there is no indication, nor the added information to show that you were using the AT protocol, which besides what you described, has other conditions for packet framing.
And as I repeat again, I never said the AT command protocol is a lousy one. I'm saying the protocol you described is a lousy one.
I rest my case.
 

    glenjoy

    Points: 2
    Helpful Answer Positive Rating
C Algorithm Help Needed

Oh, you are talking to a modem. That's a continuous connection, so my EOF suggestion doesn't apply to your application. You probably need to analyze the modem response messages instead of simply counting carriage returns.

getc() returns EOF when the input stream ends. That occurs in the following command-line example after the program reaches the end of the data file:
myprogram.exe < mydata.txt

I'm curious how you got getc() working that way in your example. Normally, getc() requires a FILE pointer.

It also occurs if you are typing into myprogram.exe from the keyboard, and you press the end-of-input key (that's Ctrl-Z in Windows).

You may want to use some other input function besides getc(). If you call getc() when your modem has nothing to say, then you program gets stuck. That may not matter if you are doing multi-threading. Other choices depend on which operating system or programming environment you are using.
 

    glenjoy

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

Glenjoy,

I am just wondering about some things:
1. What if the message it self contained the pattern for starting & ending the: 0x0d & 0x0a?
2. Why do you save your start & end sequences in the message array?
3. getc is used to deal with streams (in other words files) & I think echo47 was correct about using EOF in your function.
4. Finally I think that both streams can be detected by a single function with or without getc but with caution in the first case.

Regards,
Amr.
 

    glenjoy

    Points: 2
    Helpful Answer Positive Rating
C Algorithm Help Needed

I had sniffed my serial ports for such patterns but there is none.

The start of file and end of file comprises of 0x0D and 0x0A.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top