Отправка HEX-кода на проектор через rs232 - PullRequest
0 голосов
/ 12 сентября 2018

Я хочу управлять проекцией через RS232.Нужно будет выслать код 7E 30 30 30 30 20 31 0D.Но я не могу правильно написать код на Python.Я нахожу это решение, но выдает ошибку:

Traceback (most recent call last):
  File "C:\Python27\12.py", line 35, in <module>
    ser.write(cmd.decode('hex')+'\r\n')
  File "C:\Python27\lib\encodings\hex_codec.py", line 42, in hex_decode
    output = binascii.a2b_hex(input)
TypeError: Odd-length string

Код:

import serial
import time

port = "COM1"
ser = serial.Serial(port,9600,timeout=0.5)
print(ser.name + ' is open.')

def bits_to_hex(user64bit): # convert the 8-bit user input to hex
    numInt = int(user64bit, 2)
    strHex = hex(numInt)
    userHex = strHex[2:].zfill(2)
    # make sure the payload is a two-digit hex
    return userHex


while True:
    input = raw_input("Enter HEX cmd or 'exit'>> ")
    if input == 'exit':
        ser.close()
        print(port+' is closed.')
        exit()

    elif len(input) == 64:
    # user enters new register value, convert it into hex
        newRegisterValue = bits_to_hex(input)
        ser.write(newRegisterValue.decode('hex')+'\r\n')
        print('Saving...'+newRegisterValue)
        print('Receiving...')
        out = ser.read(1)
        for byte in out:
            print(byte) # present ascii

    else:
        cmd = input
        print('Sending...'+cmd)
        ser.write(cmd.decode('hex')+'\r\n')
        print('Receiving...')
        out = ser.read(1)
        for byte in out:
            print(byte) # present ascii
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...