On Sat, 25 Aug 2012 11:34:39 -0700 (PDT), 9bizy
<a.m.akingbulu-> declaimed the following in
gmane.comp.python.general:
> I am trying to unpack values from sensor data I am retrieving through a serial cable, but I get errors while using struct.unpack, how can I use struct.unpack to unload the data in a readable format?
>
> I checked the python documentation for struct and I can seen to find any argument for this.
>
Pardon "... I can seen to find..."?
> I have data = struct.unpack('char',data) but I still get errors
No surprise... You've asked for a single-character, a short integer,
and two unknown field types.
What about
"""
s char[] string
"""
and
"""
For the "s" format character, the count is interpreted as the size of
the string, not a repeat count like for the other format characters; for
example, '10s' means a single 10-byte string, while '10c' means 10
characters. For packing, the string is truncated or padded with null
bytes as appropriate to make it fit. For unpacking, the resulting string
always has exactly the specified number of bytes. As a special case,
'0s' means a single, empty string (while '0c' means 0 characters).
"""
(both from the 2.5 help file).
The struct module relies upon the user knowing the format of the data.
If your problem is that you have some null-terminated string data in a
variable width field, you will have to locate the position of the null
FIRST, and specify the appropriate "count" for the s format.
--
Wulfraed Dennis Lee Bieber AF6VN
HTTP://wlfraed.home.netcom.com/