Mar-16-2020, 07:11 PM
Hello. I have developed a program in Python 3.8 that uses socket to communicate. It works great with 3.8. It will return data back in hex like it should. See below.
I have found python 3.8 socket module returns in bytes where python 2.7 return a string.
I am stuck using python 2.7 because the software I am using on the project requires it. I have tried to convert it many ways and I have had no luck. Any help will be appreciated.
result from Python 3.8 (Correct)
############################################################################
sending "43 41 4E 6F 65 46 44 58 01 01 01 00 00 80 00 00 06 00 06 00 10 A0"
waiting to receive
received "b'CANoeFDX\x01\x02\x02\x00\x00\x80\x00\x00\x10\x00\x04\x00\x03\x00\x00\x00\xe3\x1e\xc4\xdb\xd9\xc1\x03\x00\t\x00\x05\x00\x10\xa0\x01\x00\x03'"
closing socket
################################################################################
result from Python 2.7 (Not Correct Response)
#############################################################################
sending "43 41 4E 6F 65 46 44 58 01 01 01 00 00 80 00 00 06 00 06 00 10 A0"
waiting to receive
received "CANoeFDX Ç â£ÿI┬ á "
closing socket
##############################################################################
I have found python 3.8 socket module returns in bytes where python 2.7 return a string.
I am stuck using python 2.7 because the software I am using on the project requires it. I have tried to convert it many ways and I have had no luck. Any help will be appreciated.
result from Python 3.8 (Correct)
############################################################################
sending "43 41 4E 6F 65 46 44 58 01 01 01 00 00 80 00 00 06 00 06 00 10 A0"
waiting to receive
received "b'CANoeFDX\x01\x02\x02\x00\x00\x80\x00\x00\x10\x00\x04\x00\x03\x00\x00\x00\xe3\x1e\xc4\xdb\xd9\xc1\x03\x00\t\x00\x05\x00\x10\xa0\x01\x00\x03'"
closing socket
################################################################################
result from Python 2.7 (Not Correct Response)
#############################################################################
sending "43 41 4E 6F 65 46 44 58 01 01 01 00 00 80 00 00 06 00 06 00 10 A0"
waiting to receive
received "CANoeFDX Ç â£ÿI┬ á "
closing socket
##############################################################################
#Python 3.8 def Program1():#A010 Shifter Position def readResponse(): try: # Send data print ('sending "%s"' % readVector) sent = sock.sendto(y, ServerAddress) # Receive response print ('waiting to receive') # data, server = sock.recvfrom(1024) data, server = sock.recvfrom(1024) print ('received "%s"' % data) finally: print ('closing socket') # vectorResponse = binascii.hexlify(bytearray(data)) # spits data same as 2.7 but not in byte form vectorResponse = data # vectorResponse = data # Returns the correct bytes string in hex form in 3.8 sock.close() # responsToPLC = vectorResponse[40:41] responsToPLC = vectorResponse print (responsToPLC) return responsToPLC ####---------Read from Vector Box----------#### readVector = '43 41 4E 6F 65 46 44 58 01 01 01 00 00 80 00 00 06 00 06 00 10 A0' y = bytearray().fromhex(readVector) ####----Below is logic for OUTPUT from Vector Box formatted----#### readResponse()