CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

C Algorithm Help Needed

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
glenjoy



Joined: 18 Oct 2004
Posts: 21

View user's profile Send private message

C Algorithm Help Needed
PostPosted: Mon Aug 08, 2005 12:25 am     Reply with quote

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.
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Mon Aug 08, 2005 2:54 am     Reply with quote

Use gets() to read the data stream up to and including the CR\LF combination.

Study the literature about 'state machines' to learn about how to effectively process the messages.
Humberto



Joined: 08 Sep 2003
Posts: 1215
Location: Buenos Aires, La Reina del Plata

View user's profile Send private message

PostPosted: Mon Aug 08, 2005 6:16 am     Reply with quote

Quote:

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


IMO it´s not usual nor well defined this format, it is not a formal protocol,
just a kind of string delimiter.
[0x0D][0x0A][message][0x0D][0x0A]
It will have a pause after the delimiters to flush the buffer.
You must copy the whole buffer before the next [0x0A] if not, it will overwrited.

Anyway this code will do the job, it´s not tested.
Code:

#define Rx_size 20  // I don´t know the message long
int8 Rx_buffer[Rx_size];
int8 index, mssge;

#int_RDA
void rcver_int_handler()
{
int8 rcved_char;

      rcved_char = getc();
      if( rcved_char == 0x0A )
        { index = 0; mssge = TRUE; }
      if( rcved_char == 0x0D )
        { mssge = FALSE; }
      if( mssge )
        { Rx_buffer[index] = rcved_char; index++; }
}



Humberto
kender



Joined: 09 Aug 2004
Posts: 768
Location: Silicon Valley

View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger

PostPosted: Mon Aug 08, 2005 3:50 pm     Reply with quote

Humberto wrote:

IMO it’s not usual nor well defined this format, it is not a formal protocol,
just a kind of string delimiter.


Humberto, don't be so harsh. It is a protocol. A protocol for transferring strings. What would be a usual format? (This is not just rhetoric question.) However, I have to comment that our friend GlenJoy didn’t mention one important thing: the [message] should not contain 0x0A and 0x0D, or it should have an escape sequence for these bytes.

Humberto's algorithm can be made more robust, if you have 2 (or more) receive buffers instead of one. (some times it’s called “ping-pong buffers”) While one is being processed by your main() thread, the other one is filled by your RDA ISR. That way, you will have more time to process the data. Of course the degree of success of such an improvement depends on the duty cycle of your communication stream.

Code:

int8  index, mssge, newdata;

#int_RDA
void rcver_int_handler()
{
      int8 rcved_char;
      int8* pTemp;      // used for swapping

      rcved_char = getc();

      // some validation code goes here.  e.g. [numbers,characters,0x0A,0x0D only
     
      switch (rcved_char)
      {
         case (0x0A):
         {
            index = 0;
            mssge = TRUE;
         }
         case (0x0D):
         {
            mssge = FALSE;

            // swap the old and the new buffers
            pTemp = pOldBuff;
            pOldbuff = pNewBuff;
            pNewBuff = pTemp;
           
            newdata = TRUE;// flag that you have a whole new message
         }
         default:
            if (mssge)
            {
               pNewBuff[index] = rcved_char;
               ++index;
            }
            else
            {
               // out-of protocol character. 
               // some whaco had connected the serial cable God only knows where.
               // alert the user, or just ignore.
            }
      }
}


void main()
{
   pOldBuff = &Rx_buffer0;
   pNewBuff = &Rx_buffer1;
   newdata = 0;
   mssage = FALSE;
   
   if (newdata)
   {
      // process data in the OldBuff
      newdata = FALSE;
   }
}
Humberto



Joined: 08 Sep 2003
Posts: 1215
Location: Buenos Aires, La Reina del Plata

View user's profile Send private message

PostPosted: Tue Aug 09, 2005 12:32 pm     Reply with quote

Quote:

Humberto, don't be so harsh.

Kender, please keep the conversation civil and respectful.
I feel your comments very agressive, be aware that I will not admit any kind of judment from you nor from anybody. English is not my natural languaje so you would be aware that inadvertidely I can use some words that will sound not so fair-spoken for yours.
Just post your code and keep your unasked opinion regarding another people.


Regarding the point if it is a protocol or not, I posted my thread and I tried to be prudent and started the phrase with IMO (in my opinion) being in advice that such format is an opinable matter.


Quote:

What would be a usual format?


Protocol from Wikipedia:
"It is difficult to generalize about protocols because they vary so greatly in purpose and sophistication.
Most protocols specify one or more of the following behaviors:
- Detection of the underlying physical connection (wired or wireless), or the existence of the other endpoint or node
- Handshaking
- Negotiation of various connection characteristics
- How to start and end a message
- How to format a message
- What to do with corrupted or improperly formatted messages (error correction)
- How to detect unexpected loss of the connection, and what to do next
- Termination of the session or connection"

In computing, a protocol is a convention or standard that controls or enables the connection, communication, and data transfer between two computing endpoints.

In the field of telecommunications, a communications protocol is the set of standard rules for data representation, signalling, authentication, and error detection required to send information over a communications channel."


I´m still consider that the format [message][0x0D][0x0A] it is very primitive to be considered a real protocol, unless it is transmitted in burst of strings with a marked pause at the end.
One of the advantage of using a protocol is that it has enough self contained information that the receiver knows exactly where and when is the beggining of a message, that I can´t determine clearly in the preceding example.

Humberto
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group