I am trying to read a UART serial stream, and the data comes in as hex
(I think). I’m not actually sure.
Doesn’t look like it. But the bytes you’re receiving: some of them are
_rendered in hex when you print the bytes object.
Below is a short snippet of what I am getting.
This is repr(some_bytes_object). That is printed as a valid Python bytes
literal, just as you’d compose one in a programme, in a string-like
format. The leading b’ says that it is a bytes literal “string”.
See this documentation:
including the escape sequence table.
I’m pretty sure the “” is a delimiter.
I don’t see a " above. The leading b’ says “bytes string” and the final
’ is the end of the string.
But remember that your bytes are only being printed in a string-like
way. They may well not be text of any kind.
And I believe the \n means new line, \t means tab. So those are ascii.
Those are byte values which happen to correspond to some ASCII values. A
bytes literal is usually used by programmers when there’s composing
bytes that contain some text. But an arbittrary bytes object need not,
and nothing in your literal above looks very texty to me.
I am also guessing that anything starting with \x is a hex value. But
that’s where I get lost. Hex \x9c is convertible. But what is \xfdXh?
“\xfdXh” is “\xfd” then “X” then “h”. Or rather, byte values which can
be written that way.
There’s no such thing as a “hex value”, except that it is a byte value
written in hex because there’s no ASCII equivalent character.
However, these do not look like text to me. So you should consider their
[9, 156, 29, 200, 76, 88, 9, 253, 88, 104, 9, 191, 30, 138, 94, 88, 123, 124, 190, 133]
If anyone can help, or know if there is a python command to parse this,
thanks in advance.
Parsing this data stream requires knowing the data format it is using,
which could be anyway. And since you’re reading from a UART, that
includes stuff like: reading the wrong serial protocol. If your bit rate
is wrong, or the characters per byte are wrong, or the stop or parity
bits are wrong, you could be reading complete garbage, let alone
decoding what should be coming in.
- are you sure you have the correct bit rate (eg 19200, or whatever),
bits per byte, presence of start/stop/parity bits? These need to be
correct to even get the correct binary data.
- what else do you know about the source of the data and what should be
Given those, you can start decoding the stream. But you need to answer
those questions first. Then start with the “struct” builtin module.
There are other things available if that is not enough.
Cameron Simpson email@example.com