Beijer Electronics (formerly QSI Corporation)

Manufacturer of Mobile Data and Human Machine Interface Terminals.
It is currently Wed Nov 22, 2017 5:48 am

All times are UTC - 7 hours




Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: Mon Nov 17, 2008 10:01 am 
Offline
User avatar

Joined: Thu May 17, 2007 7:09 am
Posts: 47
Location: Cape Cod, MA
Hi,

I was wondering if there was some Qlarity idiom for reading bytes of serial data into an object that is being used as an analog for a C structure. I have a communications protocol defined between my G-70 and a target hardware platform. The various message bodies for the protocol messages are defined on the target as C structs.

Now, I have a perl script that goes over all my header files and converts my enums to Qlarity enums (preserving comments) so that I can just cut and paste the converted header file. I'd like to do the same thing with structs... convert them to singleton instances of non-drawable objects containing member variables of the proper sizes with the same names on both platforms. The trick is receiving the serial data as a stream of bytes and then mapping them into these defined structures. As far as I can tell, there is no C-like mechanism for just popping the bytes into the memory addresses of the object storage. Nor is there any way to discover what the field names are for a given structure and iterate over them, using FromBytes() to populate them.

Has anyone solved this problem in a fairly generic way? Right now I'm just using a series of custom FromBytes() calls for every different message body, and I cut and past the C struct into my Qlarity code as a comment, so there's a reference for anyone looking at the code. Obviously this creates a clone-n-own sustainability problem. Any help appreciated.

--Rob


Top
 Profile  
 
PostPosted: Mon Nov 17, 2008 10:17 am 
Offline
QSI Support
QSI Support
User avatar

Joined: Wed Mar 08, 2006 12:25 pm
Posts: 881
Location: Salt Lake City, Utah
I am guessing you are looking for something akin to the C paradigm of:

Code:
struct DataStuff dataStuff;
unsigned char *dataStream;
int count = 0;

dataStream = GetSerialData(&count);
assert(count == sizeof(dataStuff));
memcpy(&dataStuff, dataStream, sizeof(dataStuff));
free(dataStream);


Unfortunately there is no equivilent in Qlarity. One of your major problems is that you have no guarantee that Qlarity would lay out an object's memory in the same way as a C struct. In fact I can guarantee that for all non-trivial objects it would not.

Typically I solve the problem with Serialize and Deserialize methods of the object. Not quite generic but it works. We don't have enough reflection in place for an object to query its properties in order of declaration and do something with that.

Code:
define object type DataStuff
dim p1 as integer
dim p2 as unibyte
dim p3 as byte

func Serialize() returns byte[]
    return GetBytes(p1, false) + GetBytes(p2, false) + GetBytes(p3, false)
endfunc

func DeSerialize(data[] as reference to byte)
    fromBytes(p1, mid(data, 0, 4), false)
    fromBytes(p2, mid(data, 4, 2), false)
    fromBytes(p3, mid(data, 6, 1), false)
endfunc

enddef


I suppose if you are already scanning your C headers, you could easily enough have your perl script automatically generate equivalent Serialize/Deserialize functions that could be #included into your Qlarity application.

_________________
Jeremy
http://www.beijerinc.com


Top
 Profile  
 
PostPosted: Mon Nov 17, 2008 10:51 am 
Offline
User avatar

Joined: Thu May 17, 2007 7:09 am
Posts: 47
Location: Cape Cod, MA
Yes I suppose I could generate those functions easily enough. Thanks for the suggestion! The major drawback is that it will bloat the code image size, so I'll have to see how much of an impact that will be.


Top
 Profile  
 
PostPosted: Mon Nov 17, 2008 11:07 am 
Offline
QSI Support
QSI Support
User avatar

Joined: Wed Mar 08, 2006 12:25 pm
Posts: 881
Location: Salt Lake City, Utah
Unless you are talking about hundreds of object types the bloat should be minimal. The code to call functions is fairly compact and there is only one copy of the function shared among all objects of the same type.

_________________
Jeremy
http://www.beijerinc.com


Top
 Profile  
 
PostPosted: Tue Nov 18, 2008 5:30 am 
Offline
User avatar

Joined: Thu May 17, 2007 7:09 am
Posts: 47
Location: Cape Cod, MA
Well there are a good number of unique structs to implement (in the dozens) and there would just be a singleton instance of each one. Since there is no stack-based limited scope creation/destruction of object instances, I'll just have to create one instance of each type at program load and then use each instance where appropriate.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 7 hours


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 post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group