prob with struct and byte order

Steve Holden steve at holdenweb.com
Mon Jul 24 13:05:46 EDT 2006


nephish at xit.net wrote:
> hello there, all.
> 
> i have a difficult app that connects to a server to get information for
> our database here.
> this server is our access point to some equipment in the field that we
> monitor.
> 
> the messages come in over a socket connection. And according to their
> (very limited) documentation, are set up in a particular order. like
> this
> 
> STX [length indicator] [message type] [message body] ENX
> 
> length indicator = length of the message body in 4 bytes
> message type = the code for what type of message is being sent
>    for example 200 = login
> message body = the actual command to send to the server
> 
> STX and ENX are the values assigned to start and stop a message.
> they are literally 'STX' and 'ENX'
> 
I believe you, but this is clearly a perversion of the single-character 
"start of transmission" and "end of transmission" that are part of the 
ASCII alphabet. So the protocol designer may have been inexperienced, 
and the documentation may be confused ...
> 
> when i capture a message i can print it to the screen and the 'STX' at
> the beginning and the 'ENX' at the end are discernable, but the rest of
> the message looks like this..
> 
> \x00\x00\x00,\x00\x00\x00\e17758\x00\x00   and so on and so forth...
> with some commas and other symbols in there. (the e before the 17758
> has a little hat on it)
>  If i print it out to a text file, i get this...
> STXNULNULNULea17758NULLNULL etc..
> 
The "\x00" is the Python repr() of a single-byte string containing a 
null byte (i.e. a byte whose decimal value is zero).

> now the 17758 is significant because it is the actual unit number of
> the machine we want to monitor. I just dont know how to extract the
> real values out of this message.
> 
> anyone have an idea where to start? i have experimented with struct,
> but do not know enough about it. Does anyone know a good tutorial about
> learning byte codes and such. The docs on pythons website explain the
> module well, but i still do not know what to do with it. Or how to
> generate a message or pull apart these values to get a message out of
> what the server sends us. 
> 
Well if the string you are seeing (when you print it from Python) looks like

"STX\x00\x00\x00,\x00\x00\x00\e17758\x00\x00 ... ETX"

then it looks like the bytes you want can be obtained, supposing the 
string is stored in variable s, as s[12:17].

  >>> s = "STX\x00\x00\x00,\x00\x00\x00\e17758\x00\x00 ... ETX"
  >>> s[12:17]
'17758'
  >>>

You may need to juggle about with the indexes a little, but one of the 
joys of Python is that the interactive interpreter is so cooperative!

Good luck.

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC/Ltd          http://www.holdenweb.com
Skype: holdenweb       http://holdenweb.blogspot.com
Recent Ramblings     http://del.icio.us/steve.holden




More information about the Python-list mailing list