Circumstances
I am running my Raspberry Pi 3 Model B+ with Raspberry Pi OS.
I use a FTDI chip serial cable adapter USB to RS232.
https://www.amazon.com.mx/gp/product/B081PP3ZM4/ref=ppx_yo_dt_b_asin_title_o09_s00?ie=UTF8&psc=1
I followed the instructions on following link:
https://notebook.chaopricha.com/?p=3
The following Python script showed that the FTDI serial adapter is found on/dev/ttyUSB0
Here the script to detect the ports:
ports = glob.glob('/dev/tty[A-Za-z]*')
for port in ports:
print(port)
- Target
Turn on and off a BenQ Projector that should work with following commands: Function Type Operation ASCII
Write: Power On<CR>*pow=on#<CR>
Write: Power Off<CR>*pow=off#<CR>
Read: Power Status<CR>*pow=?#<CR>
All BenQ ASCII commands can be found on:
https://business-display.benq.com/content/dam/bb/en/product/projector/corporate/lh770/quick-start-guide/lh770-rs232-control-guide-0-windows7-windows8-winxp.pdf
I used another small Python script to run a test (turning on projector):
import serial
ser = serial.Serial("/dev/ttyUSB0")
ser.baudrate = 9600
ser.write('<CR>*pow=off#<CR>\r')
ser.read(ser.inWaiting())
# You could also use ser.readline() to read but need to know # lines.
ser.close()
- Problem
ser.write('<CR>*pow=off#<CR>\r')shows following error:
TypeError: unicode strings are not supported, please encode to bytes: '<CR>*pow=off#<CR>\r'
After some internet and Stackoverflow research (see links below disclaimer) I tried following commands without success:
ser.write(b'*pow=off#\r')shows no error but also no reaction, as well as:
ser.write(str(b).encode("<CR>*pow=off#<CR>"))
Could anybody help me to understand how the strings <CR>*pow=on#<CR> and <CR>*pow=off#<CR> must be written, in order to help the python serial module’s command ser.write to interpret them correctly?
- Disclaimer
I have to admit I barley know anything about Unicode and ASCII commands, please have pactions with me.
https://stackoverflow.com/questions/36613079/pyserial-sending-unicode-string-as-a-byte-array
https://stackoverflow.com/questions/10268518/python-string-to-unicode
Connecting a Pi to a Projector via RS232
Unfortunately following error appliers: File "/usr/lib/python3/dist-packages/serial/serialutil.py", line 63, in to_bytes raise TypeError('unicode strings are not supported, please encode to bytes: {!r}'.format(seq)) TypeError: unicode strings are not supported, please encode to bytes: '*pow=on#\r'
– Larry Add Jun 10 '21 at 05:19