August 17th, 2005, 10:59 PM
need help with serial & binary
i have an interesting project at work going on. here is the challenge.
i am using the serial module to read data from a serial input.
it comes in as a hex. i need to make it a binary and compare it bit by
bit to another byte. They have some weird way they set this up that i
have to compare these things with AND. in other words, if bit 1 is 1
AND bit 1 is 1 then the real value is 1...
long story short. is there a good way to compare bytes, bit by bit with
one of the modules of python. i want to know so i dont get halfway into
developing this and find that there is a much better way to do this
than by hand.
thanks for any suggestions.
August 18th, 2005, 01:03 AM
August 18th, 2005, 10:57 AM
this is cool. i need to know how to convert the weird ascii characters to a hex or integer or something i can work with.
how do i do that? i cant find anything about how to convert hex ascii characters to integers or anything else.
August 18th, 2005, 12:57 PM
>>> c = 'x' #ASCII Character
>>> i = ord(c)
>>> print i
120 #ord(c) -> ASCII Character code (decimal integer)
'0x78' #hex(number) -> Number in Hexadecimal
'0170' #oct(number) -> Number in Octal
>>> int(hex(i), 16)
120 #int(string, B) -> Convert string representation of number in base B to decimal integer, e.g. hex is base 16.
Comments on this post
August 18th, 2005, 03:44 PM
good, i am getting somewhere now.
one more quick question.
if it times out, how can i have it not error out, but just print an error or something.
i have to time this thing, 4 bytes will be comming in at a time, and if there is a timeout between bits,
it means they were bad and i need to start the read over when it times out.
is there a try - except senario that could work here?
and thanks for the tips
August 19th, 2005, 08:46 PM
Print error message:
Print custom error message:
except Exception, inst:
print 'An error occured.'
August 20th, 2005, 03:41 PM
If what times out?
Are you using PySerial? Sockets? COM Automation? A custom library?
August 21st, 2005, 09:08 PM
sorry for the confusion. i am using ord() and its giving me what i want, i was confused about what exactly i was getting on the PySerial.