C++ parser
-
Hello, I would to develop an application in c++ to decode some messages. The user can specify the value of each field and when a message arrives, it is decoded with the rules first specified. e.g. I have a message formed by 16 bit and at most I can have 32 messages in the same frame. The user can add the rules to decode the frame. Beginning the uses must specify the fields that form the frame and when the frame arrive, the application fills the fields specified by the user. How can i implement this application? is there some tool that simplifies the development? Thank You, Andrea.
-
Hello, I would to develop an application in c++ to decode some messages. The user can specify the value of each field and when a message arrives, it is decoded with the rules first specified. e.g. I have a message formed by 16 bit and at most I can have 32 messages in the same frame. The user can add the rules to decode the frame. Beginning the uses must specify the fields that form the frame and when the frame arrive, the application fills the fields specified by the user. How can i implement this application? is there some tool that simplifies the development? Thank You, Andrea.
The word message is overused in software, so that it doesn't mean anything at all outside its context. I guess you are talking about a couple of bytes which come from a certain source (e.g. a serial port). AFAIK there's no library which could help you for that. However, this shouldn't be too complex to implement yourself. But it depends also what you have to do exactly: how does this parsing works ? Can you give a concrete example (with a concrete message and concrete rules) ?
Cédric Moonen Software developer
Charting control [v3.0] OpenGL game tutorial in C++ -
The word message is overused in software, so that it doesn't mean anything at all outside its context. I guess you are talking about a couple of bytes which come from a certain source (e.g. a serial port). AFAIK there's no library which could help you for that. However, this shouldn't be too complex to implement yourself. But it depends also what you have to do exactly: how does this parsing works ? Can you give a concrete example (with a concrete message and concrete rules) ?
Cédric Moonen Software developer
Charting control [v3.0] OpenGL game tutorial in C++thanks for your answer. A message is represented as Int16 type. The user, at run-time, can specify e.g (rule): bit1 :on/off field bit2 - bit5: address bit 6 - 11: sub address and so on... another rule could be: bit1: T/R field bit2 - bit10 telemetry and so on... When i write code don't know the rules. When the rules have been specified, the user select a particular rule and when arrive a message, the specified fields are filled according to the selected rule. Thank You.
-
Hello, I would to develop an application in c++ to decode some messages. The user can specify the value of each field and when a message arrives, it is decoded with the rules first specified. e.g. I have a message formed by 16 bit and at most I can have 32 messages in the same frame. The user can add the rules to decode the frame. Beginning the uses must specify the fields that form the frame and when the frame arrive, the application fills the fields specified by the user. How can i implement this application? is there some tool that simplifies the development? Thank You, Andrea.
You may embed a scripting language in your application, like (the excellent) Lua. Then, users can formulate the decoding rules by implementing functions (or simple data structures) using the hosted language. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
thanks for your answer. A message is represented as Int16 type. The user, at run-time, can specify e.g (rule): bit1 :on/off field bit2 - bit5: address bit 6 - 11: sub address and so on... another rule could be: bit1: T/R field bit2 - bit10 telemetry and so on... When i write code don't know the rules. When the rules have been specified, the user select a particular rule and when arrive a message, the specified fields are filled according to the selected rule. Thank You.
Andrea Di Domenico wrote:
the specified fields are filled according to the selected rule.
I still don't get what those fields are. Are they simple variables ? Any particular type ? As Carlo suggested, you might take a look at a scripting language, although this is perhaps a bit overkill for your case (that's what I'm trying to figure by asking you so many questions).
Cédric Moonen Software developer
Charting control [v3.0] OpenGL game tutorial in C++ -
Hello, I would to develop an application in c++ to decode some messages. The user can specify the value of each field and when a message arrives, it is decoded with the rules first specified. e.g. I have a message formed by 16 bit and at most I can have 32 messages in the same frame. The user can add the rules to decode the frame. Beginning the uses must specify the fields that form the frame and when the frame arrive, the application fills the fields specified by the user. How can i implement this application? is there some tool that simplifies the development? Thank You, Andrea.
I am not sure how experienced you are in C++ but one thing you will find is that the address operator (&) does not work with bits of a byte. This will be a very important factor since you are configuring which bits to peel apart. One thing I have done to help with this is I made functions to unpack the bits of a byte into an array of bytes and to also do the converse - pack the LSB of an array of bytes into one byte. Here is what the unpacker looks like :
void BitUnpack( uchar byteArray[], uchar bits )
{
for( uint n = 0; n < 8; ++n )
{
uchar mask = 1 << n;
byteArray[n] = ( bits & mask ) ? 1 : 0;
}
}This will give you an array of bytes where each one corresponds to the value of the bit at that location. This means that byteArray[2] will be 1 if the 3rd bit is set and it will be 0 if not. If you unpack all of the bits of your message into array of bytes it will be easy to then repack, say, bits 3-7 into one value and so on. Note that the function above unpacks just one byte. If you call this repeatedly you can unpack all of the bytes you receive into arrays for processing later. BTW - you may want to tweak the packer function to have it pack arbitrary numbers of bits to make things easier for you later. Here's the packer I use :
void BitPack( uchar byteValue, uchar byteArray[] )
{
for( uint n = 0; n < 8; ++n )
{
if( byteArray[n] )
{
uchar mask = 1 << n;
byteValue |= mask;
}
}
}If you always work exclusively with 16-bit values then change the uchars to ushorts and the 8s to 16s and you should be ready to go. I should mention that I define the uchar, ushort, uint, and ulong types for myself but you don't have to use those since VC++ already defines the capitalized versions of these types. What this all comes down to is sorting out the bits and then reassembling them later. I hope these functions give you some ideas on how to approach this.
modified on Tuesday, October 26, 2010 6:13 PM
-
Hello, I would to develop an application in c++ to decode some messages. The user can specify the value of each field and when a message arrives, it is decoded with the rules first specified. e.g. I have a message formed by 16 bit and at most I can have 32 messages in the same frame. The user can add the rules to decode the frame. Beginning the uses must specify the fields that form the frame and when the frame arrive, the application fills the fields specified by the user. How can i implement this application? is there some tool that simplifies the development? Thank You, Andrea.
Hi Andrea, Have a look at std::valarray<bool>[^] and the associated std::slice[^]. Not much more out there to help you. cheers, AR
When the wise (person) points at the moon the fool looks at the finger (Chinese proverb)
-
I am not sure how experienced you are in C++ but one thing you will find is that the address operator (&) does not work with bits of a byte. This will be a very important factor since you are configuring which bits to peel apart. One thing I have done to help with this is I made functions to unpack the bits of a byte into an array of bytes and to also do the converse - pack the LSB of an array of bytes into one byte. Here is what the unpacker looks like :
void BitUnpack( uchar byteArray[], uchar bits )
{
for( uint n = 0; n < 8; ++n )
{
uchar mask = 1 << n;
byteArray[n] = ( bits & mask ) ? 1 : 0;
}
}This will give you an array of bytes where each one corresponds to the value of the bit at that location. This means that byteArray[2] will be 1 if the 3rd bit is set and it will be 0 if not. If you unpack all of the bits of your message into array of bytes it will be easy to then repack, say, bits 3-7 into one value and so on. Note that the function above unpacks just one byte. If you call this repeatedly you can unpack all of the bytes you receive into arrays for processing later. BTW - you may want to tweak the packer function to have it pack arbitrary numbers of bits to make things easier for you later. Here's the packer I use :
void BitPack( uchar byteValue, uchar byteArray[] )
{
for( uint n = 0; n < 8; ++n )
{
if( byteArray[n] )
{
uchar mask = 1 << n;
byteValue |= mask;
}
}
}If you always work exclusively with 16-bit values then change the uchars to ushorts and the 8s to 16s and you should be ready to go. I should mention that I define the uchar, ushort, uint, and ulong types for myself but you don't have to use those since VC++ already defines the capitalized versions of these types. What this all comes down to is sorting out the bits and then reassembling them later. I hope these functions give you some ideas on how to approach this.
modified on Tuesday, October 26, 2010 6:13 PM
Congrats! You invented std::bitset[^] :)
When the wise (person) points at the moon the fool looks at the finger (Chinese proverb)
-
Congrats! You invented std::bitset[^] :)
When the wise (person) points at the moon the fool looks at the finger (Chinese proverb)
-
I doubt it. Those two functions were first written more than twenty years ago. :rolleyes:
Hi Rick,
Rick York wrote:
Those two functions were first written more than twenty years ago
Dont' be ashamed to be a precursor :) C++ improved a lot from this time, and is improving again with C++0x. cheers, AR
When the wise (person) points at the moon the fool looks at the finger (Chinese proverb)
-
Hi Rick,
Rick York wrote:
Those two functions were first written more than twenty years ago
Dont' be ashamed to be a precursor :) C++ improved a lot from this time, and is improving again with C++0x. cheers, AR
When the wise (person) points at the moon the fool looks at the finger (Chinese proverb)
Thank you fro the answer