Sending hex over serial with python

元气小坏坏 提交于 2019-12-03 03:50:25
Vorsprung

Rewrite "thestring" as

thestring = "\x7E\xFF\x03\x00\x01\x00\x02\x0A\x01\xC8\x04\xD0\x01\x02\x80\x00\x00\x00\x00\x8E\xE7\z7E"

You won't need to pack it, you can say data=thestring and send it. This will only work if the various ids in the document match exactly what is on your equipment

You need to figure out how the python "struct" works, how to encode binary and how to put two 4 bit values into one 8 bit byte: clue, see the >> and << operators and the struct pack "B" format

I tried a number of schemes to convert my command string of hex characters into bytes which were recognized as hex on the far end of the UART, before settling on the scheme below. I chose to send the string one byte at a time, because the far end could not process more than four bytes at a time, so I could not send the entire string. The formatting of the individual hex_byte is similar to an sprintf in C.

import time
import serial
import sys
import binascii
import inspect


# This function takes a command string and sends individual bytes.
# It also reports the response.
def send_command(cmd_name, cmd_string):
    print ("\ncmd_name:", cmd_name)
    print ("cmd_string:", cmd_string)
    cmd_bytes = bytearray.fromhex(cmd_string)
    for cmd_byte in cmd_bytes:
        hex_byte = ("{0:02x}".format(cmd_byte))
        #print (hex_byte)
        ser.write(bytearray.fromhex(hex_byte))
        time.sleep(.100)

    # wait an extra 3 seconds for DISP_ON_CMD
    if cmd_name == "DISP_ON_CMD":
        time.sleep(5.0)
    response = ser.read(32)
    print ("response:", binascii.hexlify(bytearray(response)))
    return

# Code to put processor into factory mode.
comm_init='c4c4'
# Here's the list of command strings, captured as tuples.
# The 16-bit Data and Msg CRCs are calculated via gen_crc.exe.
heart_beat_cmd=      ("HEART_BEAT_CMD",      'a5a50a00010000003e1b')

Here is what I see displayed in my Python 3.4.5 window.

cmd_name: HEART_BEAT_CMD

cmd_string: a5a50a00010000003e1b

response: b'a5a51a002a0054373031763200000c08201e0a040000e389f86b'

I never got around to parsing the output into bytes, but that would be a nice addition.

Disclaimer: I am very new to both python and serial port programming. I know this is a very old question and might have been solved already, but I'd like to do my part(I guess) and maybe learn in the process! I know the answer to the second part of the question about the KeyboardInterrupt(or at least I think I do.) So here goes

KeyboardInterrupt is basically when the user hits "Control+C" while the program is still running. This makes the program stop at right whichever line it was executing at then moment. Hence the

File "./sollar.py", line 30, in <module>
    s = ser.readline().decode('utf-8')
File "/usr/local/lib/python3.2/dist-packages/serial/serialposix.py", line 446, in read
    ready,_,_ = select.select([self.fd],[],[], self._timeout)

Because that's where it stopped(hopefully I'm right.) To avoid this, you could use "try.. except"

while True:
    try:
        # your main code here
        break
    except KeyboardInterrupt:
        print("User interrupt encountered. Exiting...")
        time.sleep(3)
        exit()
    except:
        # for all other kinds of error, but not specifying which one
        print("Unknown error...")
        time.sleep(3)
        exit()

This basically helps you exit your program in a rather "clean way." Hope that helps.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!