0

I am doing data transfer from an instrument over RS232 serial dialogue using Python. The data is coming in ASCII format. For example this is what i get in ASCII from the instrument using Python:

'\x02S20390908127F010102F3004000900300000000000000000000000000000300A500000000000202020202020206070505050505050000000707070707070707020700000000000000000000000000000000000000008000800089237A0715047E000000000000000000000000000000000000000000000000000000000000000000000000005350374646303030\x1732'

The same data transfer using Hyperterminal (Docklight) gave me this and the following is the hex representation i got in hyperterminal:

02 53 31 38 33 38 30 39 30 38 31 32 37 46 30 31 30 31 30 32 46 33 30 30 34 30 30 30 39 30 30 33 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 33 30 30 41 35 30 30 30 30 30 30 30 30 30 30 30 32 30 32 30 32 30 32 30 32 30 32 30 32 30 36 30 37 30 35 30 35 30 35 30 35 30 35 30 35 30 30 30 30 30 30 30 37 30 37 30 37 30 37 30 37 30 37 30 37 30 37 30 32 30 37 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 38 30 30 30 38 30 30 30 38 39 32 33 37 41 30 37 31 35 30 34 37 45 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 35 33 35 30 33 37 34 36 34 36 33 30 33 30 33 30 17 33 38 

My question is that how do I, using Python, get the same hex representation from the ascii data I got. I am very much of a noob when it comes to this kind of stuff. Thank you in advance.

dsolimano
  • 8,870
  • 3
  • 48
  • 63
Aasam Tasaddaq
  • 645
  • 4
  • 13
  • 23
  • Are the two quoted lines supposed to represent the same signal or merely similar ones? – DSM Aug 10 '12 at 16:02
  • They are supposed to be same. I checked the data in ascii format in docklight hyperterminal as well and its the same. – Aasam Tasaddaq Aug 10 '12 at 16:08
  • 2
    I can't get them to match, though. I find four differences: mine starts `02 53 32 30 33 39` whereas yours starts `02 53 31 38 33 38`, and mine ends with `32` while yours ends `38`. Since there are only four terms which differ, it's hard to see what the error could be. – DSM Aug 10 '12 at 16:14

2 Answers2

1

Explanation: convert each character into byte using ord then format as two digit hex number and feed it into a list. Then join the list using space as a separator.

>>> ll = ["%02X" % (ord(x)) for x in '\x02S20390908127F010102F3004000900300000000000000000000000000000300A500000000000202020202020206070505050505050000000707070707070707020700000000000000000000000000000000000000008000800089237A0715047E000000000000000000000000000000000000000000000000000000000000000000000000005350374646303030\x1732']
>>> ll           #as a list
['02', '53', '32', '30', '33', '39', '30', '39', '30', '38', '31', '32', '37', '46', '30', '31', '30', '31', '30', '32', '46', '33', '30', '30', '34', '30', '30', '30', '39', '30', '30', '33', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '33', '30', '30', '41', '35', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '32', '30', '32', '30', '32', '30', '32', '30', '32', '30', '32', '30', '32', '30', '36', '30', '37', '30', '35', '30', '35', '30', '35', '30', '35', '30', '35', '30', '35', '30', '30', '30', '30', '30', '30', '30', '37', '30', '37', '30', '37', '30', '37', '30', '37', '30', '37', '30', '37', '30', '37', '30', '32', '30', '37', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '38', '30', '30', '30', '38', '30', '30', '30', '38', '39', '32', '33', '37', '41', '30', '37', '31', '35', '30', '34', '37', '45', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '35', '33', '35', '30', '33', '37', '34', '36', '34', '36', '33', '30', '33', '30', '33', '30', '17', '33', '32']
>>> ' '.join(ll) # as a string
'02 53 32 30 33 39 30 39 30 38 31 32 37 46 30 31 30 31 30 32 46 33 30 30 34 30 30 30 39 30 30 33 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 33 30 30 41 35 30 30 30 30 30 30 30 30 30 30 30 32 30 32 30 32 30 32 30 32 30 32 30 32 30 36 30 37 30 35 30 35 30 35 30 35 30 35 30 35 30 30 30 30 30 30 30 37 30 37 30 37 30 37 30 37 30 37 30 37 30 37 30 32 30 37 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 38 30 30 30 38 30 30 30 38 39 32 33 37 41 30 37 31 35 30 34 37 45 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 35 33 35 30 33 37 34 36 34 36 33 30 33 30 33 30 17 33 32'
Maria Zverina
  • 10,863
  • 3
  • 44
  • 61
  • Thank you for your response, but could you please explain what this does? I am guessing you are converting each char into its ordinal, but how would that get the format i need. How would I go about getting the hex representation from here. Sorry I am new to all this programming. – Aasam Tasaddaq Aug 10 '12 at 16:15
  • @AasamTasaddaq sorry missed the hex requirement ... it's in there now :) – Maria Zverina Aug 10 '12 at 16:16
  • I used hex() function on the ordinals and it worked. Thank you! – Aasam Tasaddaq Aug 10 '12 at 16:21
  • This doesn't reproduce the OP's results, but I think that's okay, because the OP's results aren't actually the same signal despite what he said. – DSM Aug 10 '12 at 16:23
0

This seems to be the easiest way...

ascstr = 'x\02S20..."  #your string

#I think you need to get rid of the first 4 characters '\x02' looks like
#it is hex
ascstr = ascstr[4:] 
for c in mystring:
   hexstr = hexstr + hex(ord(c))[2:] + ' '
James Rice
  • 75
  • 1
  • 10