Okay I wrestled with this one for a while and finally created the following functions because just sending/receiving "byte" data via a serial port did not work regardless of how I formatted/deformatted that data. I hope this helps you get to where you need to go with this.
# *********************************************************************************
# Basic Serial Port Communication Methods
# *********************************************************************************
def SendCommand(self, Packet):
try:
if self.LogDiagnostics:
self.DiaLog(Packet, 1) # Send Packet
# We decided to work with a straight list of numbers to represent our
# data as such a Packet is [255, 255, ...] and we now convert that
# into a byte string plus CheckSum that the write command needs
HexString = ''
ChkSum = (sum(Packet) & 255)
ChckPacket = Packet + [ChkSum]
for digt in ChckPacket:
HexVal = hex(digt)[2:]
if len(HexVal) == 1:
HexString += '0' + HexVal
else:
HexString += HexVal
# Clear both buffers before sending a command just in case there are any
# stray values still remaining from previous communiques
self.serPort.reset_input_buffer()
self.serPort.reset_output_buffer()
# Write the Packet to the Serial Device -- note when attempting to send
# a binary hex value we had to end up using unhexlify( ) because
# everything we sent to the write( ) method was viewed as single values
# b'f', b'f' instead of a single byte b'ff'
self.serPort.write(unhexlify(HexString))
except Exception as err:
#DJ - Need to log this Data
print('Send Error :',err)
print('Packet :',Packet,' : ',HexString)
def GetResponse(self, Bytes):
# It was unclear what we got back from this Serial Device Read as
# maniuplating the raw data did not do what was expected however if we
# hexlify( ) it the results worked fine but then we decided it was just
# better to work with a straight list of numbers as our response so the
# conversion also takes place here
try:
Packet = []
Bytes += 1 # Add 1 for the CheckSum Value
BiHexStr = self.serPort.read(Bytes)
DecList = self.BiHex2DecLst(BiHexStr)
# Separate the Value(s) from the Check Sum
DecValues,ChkSum = DecList[:-1],DecList[-1:]
if ChkSum[0] == (sum(DecValues) & 255):
Packet = DecValues
if self.LogDiagnostics:
self.DiaLog(DecList, 0) # Receive Packet
except Exception as err:
print('Receive Error :',err)
return Packet
def GetStream(self, Bytes):
# GetStream is a version of GetResponse that does not do a CheckSum because
# the stream of data it is receiving is incomplete and thus does not have a
# checksum until it receives the final chunk from the controller
try:
Packet = []
BiHexStr = self.serPort.read(Bytes)
DecList = self.BiHex2DecLst(BiHexStr)
Packet = DecList
if self.LogDiagnostics:
self.DiaLog(DecList, -1) # Receive Partial Packet
except Exception as err:
print('Receive Error :',err)
return Packet
@staticmethod
def BiHex2DecLst(BiHex):
# Convert the Raw Data into a Python Binary Hex String b'ff....'
FullHexVal = hexlify(BiHex)
# Convert the Hex Values String into a Decimal List 'ffff' > [255, 255]
DecList = [int(FullHexVal[idx:idx+2], 16) for idx in range (0, len(FullHexVal), 2)]
return DecList