WiFi Module Relay Control using Python 3.6

I am using the Relay Controller 1-Channel General Purpose SPDT + 8 Channel ADC ProXR Lite with a WiFi TCP/IP connection, using the ncd_industrial_relay.py library linked under resources.

I was porting my existing code from Python 2.7 to 3.6, when I found that I wasn’t able to receive anything from the socket using socket.recv(buffersize) (timeout error).
This is not an issue in 2.7, and since I can find no other instances of socket.recv() not working I believe this problem is on the controller side. I tried making a socket connection without using ncd_industrial_control.py but when I try socket.recv() it always times out.

Are there any known compatibility issues with Python 3.6, and is there a way to solve this issue?

Looking into the code further, I found the problem and a solution for it.

Changing the function ‘convert_data’ to (sorry about the tab spacing, I couldn’t figure it out)

def convert_data(self, data):
if type(b’’) == str:
command_string = b’’
for character in data:
command_string += chr(character)
else: # adds Python 3 support
command_string = bytes(data)
return command_string

solves the problem.

The reason why this problem exists is because Python 2.x strings are bytes type and strings in 3.x are unicode. The data which is originally a list of numbers must be converted to a byte string, and to do so in Python 3.x the bytes function can be called. This doesn’t work in 2.x so there needs to be a check to see whether a byte string is considered a regular string or a bytes string.

Hi @karbuckle

Thank you for sharing. Sorry, none of us here develop in Python 3 much so our tech support staff couldn’t be much help. I am sure customers in the future will run across this and will appreciate you sharing your solution.