【问题标题】:How to convert 7f => 02 hex如何转换 7f => 02 十六进制
【发布时间】:2017-04-07 15:40:37
【问题描述】:

我正在通过“Modbus RTU”与伺服系统交谈,但要了解究竟发生了什么,我需要通过如下所示的适当转换打印接收到的十六进制数字。

以这种方式转换接收值:

  1. hex 7f 转换为 bin 给出(01111111)
  2. next 否定给出的值(10000000)
  3. rol 2-place give(00000010)
  4. 然后返回十六进制给出(02)

这是在 home 命令后来自伺服的:

007f7d5fefff858f007f7d5f2fe0076f0000000000000000000000000000

这是我的代码:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import serial
import time
import numpy

port= serial.Serial('/dev/ttyS0', 9600, timeout=2,   
xonxoff=False, rtscts=False, dsrdtr=False) 
port.flushInput()
port.flushOutput()
time.sleep(0.01)
if port.isOpen():
    print(port.name + ' is open...')
    flag = port.isOpen()
    print flag
    #MODBUS_ACTIVE=b"\x01\x05\x04\x27\xFF\x00\x3D\x01"  
    #port.write(MODBUS_ACTIVE)
    #print    
    "sending=>MODBUS_ACTIVE<=>",MODBUS_ACTIVE.encode("hex")
    time.sleep(0.01)
    command=raw_input('Enter your command ie.home: ')
    if( command == 'home' ):
    HOME_INITIAL=b"\x01\x05\x04\x0B\x00\x00\xBD\x38"      
    #command home first time
    port.write(HOME_INITIAL)
    print "sending=>HOME",HOME_INITIAL.encode("hex")
    time.sleep(0.5)
    HOME_FINAL=b"\x01\x05\x04\x0B\xFF\x00\xFC\xC8"   
    #command home second time
    port.write(HOME_FINAL)
    time.sleep(0.5)
    #While True:
    rcv = port.read(30)
    print type(rcv)
    rcv = rcv.encode('hex')
    #rcv = int(rcv,16)
    #rcv = "{0:012b}".format(0xfff ^ rcv)
    #numpy.right_shift(rcv, 30)
    #format(rcv if rcv >= 0 else (1 << 16) + rcv, '016b'
    #binary_repr(rcv)
    #time.sleep(0.1)
    #rcv = bin(rcv^1)
    #print bin(str(rcv))
    #''.join(format(ord(c), '08b') for c in 'rcv')
    #rcv = str(rcv)
    #rcv = rcv >>= 1
    #print type(rcv)
    #print int('rcv')+1
    #print 'rcv'
    #int(''.join(bin(x)[:1:-1]), 2)
    #bin((i ^ (2 ** (i.bit_length()+1) - 1)))[3:]
    #bin((rcv ^ (2 ** (rcv.bit_length()+1) - 1)))[3:]
    #bin((int(s, 2) ^ (2**(len(s)+1) - 1)))[3:]
    #bin((int(rcv, 16) ^ (2**(len(rcv)+1) - 1)))[3:]
    #s.replace('1', '2').replace('0', '1').replace('2', '0')
    #rcv.replace('1', '0').replace('0', '1').replace('2',     '0')
    #bin((rcv ^ (2 ** (rcv.bit_length()+1) - 1)))[3:]
    print ''.join('1' if x == '0' else '0' for x in 'rcv')
    #flip = {'1':'0', '0':'1'}; ''.join(flip[b] for b in s)
    #rcv = {'1':'0', '0':'1'}; ''.join(rcv[b] for b in rcv)
    #print "{0:012b}".format(2**12-1-a)


    #result = int(a, 16) ^ int(b, 16) # convert to integers     and xor them 
    #return '{:x}'.format(result)     # convert back to     hexadecimal
    #result = int(rcv, 16) ^ int(rcv, 16)
    #print rcv.encode('hex')
    #print format('rcv')
    #print (rcv)
    command=raw_input('Enter your command ie.home: ')

如您所见,我尝试了多种方法在屏幕上打印转换后的值,但效果不佳。

【问题讨论】:

    标签: python


    【解决方案1】:

    从代码中print 的不一致用法(有些带括号,有些不带括号)看来,您使用的是 Python 2.7。使用 encode 转换为 hex 可能不是一个好主意,它可能只适用于 Python 3。

    改为使用binascii 模块来转换从串口读取的字节。见binascii

    >>> binascii.hexlify('test')
    '74657374'
    

    如果您想自己进行位操作,可以使用下面的示例代码。请注意,这些位右移 6 位,而不是左移 2 位,因此您不必担心从单个字节进行整数转换。

    >>> abyte = 0x7f
    >>> bin(abyte)
    '0b1111111'
    >>> b = abs(~abyte)
    >>> bin(b)
    '0b10000000'
    >>> c = b >> 6
    >>> bin(c)
    '0b10'
    >>> hex(c)
    '0x2'
    

    【讨论】:

    • 感谢您的回复,所以当我输入 binascii.unhexlify('rcv') 时,总是会收到:binascii.unhexlify('rcv') TypeError: Odd-length string
    • @Grzesiek 如果您收到“奇数字符串”错误,可能是由于读取结束时出现换行符。尝试在来自port.read() 的rcv 上使用strip()。此链接可能会有所帮助:stackoverflow.com/questions/3731278/…
    • ...战斗但没有成功。
    猜你喜欢
    • 1970-01-01
    • 2015-09-13
    • 2013-10-08
    • 1970-01-01
    • 2018-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多