0

Python を使用して、RS232 シリアル ダイアログを介して計測器からデータ転送を行っています。データは ASCII 形式で送信されます。たとえば、これはPythonを使用して計測器から ASCII で取得したものです。

'\x02S20390908127F010102F3004000900300000000000000000000000000000300A500000000000202020202020206070505050505050000000707070707070707020700000000000000000000000000000000000000008000800089237A0715047E000000000000000000000000000000000000000000000000000000000000000000000000005350374646303030\x1732'

ハイパーターミナル (Docklight)を使用した同じデータ転送でこれが得られ、ハイパーターミナルで取得した 16 進数表現は次のとおりです。

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 

私の質問は、Pythonを使用して、取得した ascii データから同じ 16 進表現を取得するにはどうすればよいかということです。この種のものに関しては、私は非常に初心者です。前もって感謝します。

4

2 に答える 2

1

説明: 各文字をバイトに変換しord、2 桁の 16 進数としてフォーマットし、それをリストにフィードします。次に、区切り文字としてスペースを使用してリストを結合します。

>>> 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'
于 2012-08-10T16:11:58.183 に答える
0

これが一番簡単な方法のようです...

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:] + ' '
于 2012-08-10T16:16:07.690 に答える