#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2005
    Posts
    70
    Rep Power
    10

    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.
    sk <><
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2004
    Location
    Regensburg, Germany
    Posts
    147
    Rep Power
    16
    There are bitwise operations available for integers. See http://www.python.org/doc/current/li...tring-ops.html for details.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2005
    Posts
    70
    Rep Power
    10
    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.
    thanks
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    34
    Code:
    >>> c = 'x'         #ASCII Character
    >>> i = ord(c)
    >>> print i
    120                  #ord(c) -> ASCII Character code (decimal integer)
    >>> hex(i)
    '0x78'               #hex(number) -> Number in Hexadecimal
    >>> oct(i)         
    '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

    • Yegg` agrees
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2005
    Posts
    70
    Rep Power
    10
    good, i am getting somewhere now.
    thanks
    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
  10. #6
  11. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    Print error message:
    Code:
    try:
        code here
    except Exception, inst:
        print inst
    Print custom error message:
    Code:
    try:
        code here
    except:
        print 'An error occured.'
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    34
    if it times out, how can i have it not error out, but just print an error or something.
    If what times out?

    Are you using PySerial? Sockets? COM Automation? A custom library?
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2005
    Posts
    70
    Rep Power
    10
    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.
    thanks.

IMN logo majestic logo threadwatch logo seochat tools logo