Question

I am currently doing project which requires communication from a PC to the device, so far I've decided on socket comms. and have written some code. I am also using ZMQ for ipc on the device itself.

My script works by sending data as text across. I was trying to encode my data to utf-8 so that it can be easily read on the device and displays in a frame and performs the tasks as needed. However, I can't seem to get the encoding working right, I've tried searching for examples or tutorials online but can't seem to find any.

I've tried using socket.send (msg.encode("UTF-8")) to encode my data, and message = socket.recv() to recv & print the data on the server. This works but the server would print out the exact text data which is not what I wanted. I'm unsure whether this is the correct way, and hope that someone could point me in the correct direction for encoding & printing the encoded data without decoding back to text.

Was it helpful?

Solution

You are receiving the text as encoded UTF8 data. It is all working correctly.

However, if you are printing the data on the receiving end directly to a terminal that happens to be configured to display UTF-8, you won't see any difference.

Print the representation instead:

print repr(message)

to see the string literal representation including any non-printable, non-ASCII bytes displayed as escape strings.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top