Concatenating byte of type b'*' to byte string of type b'\x00\x00\x00'

24 Views Asked by At

this is my first time posting on SO. I am creating a python rs422 interpreter for an FPGA im developing.

The FPGA uses a crc8 protocol and i wanted to have an input of 7 frames, 1byte ea + 1 frame of the crc8 check of those 7 frames. (all frames are in hex) I was able to create the crc frame using the crc8 library but: for some of the crc frames that are not in the hex representable range E.G: (2A in Hex gives * in ASCII) when i concatenate with the rest of my bytestring, it does not add the SOH character like the rest of the byte string has.

portnum = "COM4"                                                                #Serial Port initialisation
cmdInput =   b'\x1f\x00\x00\x00\x00\x00\x00'                                  #CMD to Artix7
responseCMD= b''                                                          #Initialising Response bytearray
isBytessent = 0                                                                 #Initialising Bytes sent check counter
exitflag = 0 
errorFlag = 0                                                                   #Initialising exitflag
hash
crc8Frame = ''
HashedInput = b''

port = serial.Serial(port = portnum,                                            #Serial Port open
                     baudrate=115200,
                     bytesize = serial.EIGHTBITS,
                     parity = serial.PARITY_ODD,
                     stopbits = serial.STOPBITS_ONE,
                     timeout= 0.5)
port.flushOutput()                                                              #Port Flushing to clear port
port.flushInput()


def crc8Calculate(cmdInput) :
        hash = crc8.crc8()
        print(cmdInput, type(cmdInput))
        hash.update(cmdInput)
        crc8Frame = hash.digest()        
        print("CRC is {} ({})".format(crc8Frame, type(crc8Frame)) )
        return crc8Frame 
#------------------------Main Function-----------------------------#
while errorFlag == 0:
        if port.open :                                                          #Check that the port is active
                print(" Port open and connected to: " + port.portstr)           #Print Connection Message
                # if not int(crc8Calculate(cmdInput)) in range(0,255) :
                #         errorFlag =1
                #cnvrtInput = bytearray(cmdInput)
                HashedInput = cmdInput + crc8Calculate(cmdInput)
                print(HashedInput , type(HashedInput))
                isBytessent = port.write(HashedInput)                              #Initialise Transmission and Transmission Counter Incrementation
                print("Attempting to Write to Port")
                print("Command type : ", type(HashedInput))                        #Check the format and type of the bytearray being sent
                print("Sent" , isBytessent, "bytes in the form :", HashedInput.hex())
                responseCMD = port.read(42)                                #Read the Response from the Artix7
                print("\n", responseCMD.hex())                             #Output the Response from the Artix 7 to the user
                exitflag = 1                                                    #Raise program end exit flag

                if exitflag == 1:                                               #Exit Program
                        exit()
        else:
                print("Port", port.portstr, "Failed to Open")                   #Port Connection error
                port.close()
                exit()

I tried a few methods. Splitting my string into a list of integers and converting my crc8frame to integer as well, and appending it to the list and then converting to bytes but the result is always the same: #b'\x1f\x00\x00\x00\x00\x00\x00*'
or
#b'\x1f\x00\x00\x00\x00\x00\x002a'

the option to have the packet being sent without the soh is not available as my FPGA wont understand correctly the input command 1f000000000000002a

0

There are 0 best solutions below