1
  1. 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)
  1. 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()
  1. 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?

  1. 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

5
  • This is a simple Python programming problem, not Pi specific. You really should do some basic python courses. You are over complicating the issue. Just use normal strings with \n or \r.
    – Milliways
    Jun 8, 2021 at 22:38
  • To be honest, this is not a quiet helpful comment. Actually, I wrote a "normal" string (whatever that means) with \r - ser.write('<CR>*pow=off#<CR>\r') but this is showing no reaction (Power on does neither). If your intention is to help please help me with the code line which you think should work.
    – Larry Add
    Jun 9, 2021 at 2:44
  • Forget <CR> and use normal return characters \r. All the stuff about Unicode and byte strings is irrelevant. Also as you appear to be a beginner forget Python2 (which is obsolete) and use python3 which supports Unicode by default .
    – Milliways
    Jun 9, 2021 at 3:52
  • Thank you for the comment. I wrote following lines and ran them with Python3: import serial ser = serial.Serial("/dev/ttyUSB0") # Open named port ser.baudrate = 9600 # Set baud rate to 9600 ser.write('*pow=on#\r') # send command string ser.close() 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, 2021 at 5:19
  • I also tried ser.write('\rpow=on#\r') and substituded all <CR> but return is also: Traceback (): File "serial_interf_config.py", line 4, in <module> ser.write('\rpow=on#\r') File "/usr/lib/python3/dist-packages/serial/serialposix.py", line 532, in write d = to_bytes(data) 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: '\r*pow=on#\r'
    – Larry Add
    Jun 10, 2021 at 12:22

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.