JohnBrodie / pyjector

Library to help control your projector over a serial connection
MIT License
50 stars 19 forks source link

My pyserial doesn’t handle unicode strings #9

Closed towolf closed 7 years ago

towolf commented 8 years ago
./pyjector_controller -d benq "/dev/ttyAMA0" power on
1452431094.313401 INFO send: '*pow=on#\r'
1452431094.315722 DEBUG _send: u'\r'
Traceback (most recent call last):
  File "./pyjector_controller", line 21, in <module>
    command(args.action)
  File "/root/pyjector/pyjector/pyjector.py", line 298, in handler
    return self._command_handler(command, action)
  File "/root/pyjector/pyjector/pyjector.py", line 252, in _command_handler
    self._do_handshake()
  File "/root/pyjector/pyjector/pyjector.py", line 227, in _do_handshake
    self._send(h['send'])
  File "/root/pyjector/pyjector/pyjector.py", line 215, in _send
    self.serial.write(data)
  File "/usr/lib/python2.7/site-packages/serial/serialposix.py", line 491, in write
    d = to_bytes(data)
  File "/usr/lib/python2.7/site-packages/serial/serialutil.py", line 76, in to_bytes
    b.append(item)  # this one handles int and str for our emulation and ints for Python 3.x
TypeError: an integer or string of size 1 is required

When I inserted .encode('ascii') it started working.

leakim commented 8 years ago

looks like "send": "\r", from benq.json is not parsed correctly? does json really support escaped ascii?

towolf commented 8 years ago

I converted the Unicode coming from json.loads to string. Then it works.

JohnBrodie commented 8 years ago

If someone wants to contribute a fix for this, it looks like we'll need to deal with Python 2 and 3 support... I just moved so I won't be able to get to it for a while.