binaere Werte an /dev/ttyUSB1

Gast #6628010
Lesenswert?

Ich muss einen Sensor über eine ser. Schnittstelle /dev/ttyUSB1 
ansteueren, die Codefolge ist binaer 
(\xFF\x01\x86\x00\x00\x00\x00\x00\x79). In Python3 habe ich das so 
gemacht:
1
#!/usr/bin/python3
2
# -*- coding: utf-8 -*-
3

4
import time, serial
5

6
geraet  = '/dev/ttyUSB1'
7

8
ser = serial.Serial(port = geraet, baudrate = 9600, bytesize = 8, parity = 'N', stopbits = 1, timeout = 10, xonxoff = 0, rtscts = 0, dsrdtr = 0)
9
ser.flushOutput()
10
ser.flushInput()
11

12
ser.write('\xFF\x01\x86\x00\x00\x00\x00\x00\x79')
13

14
data = ser.read(9)
15
ser.close()
16

17
wert = data[2] * 256 + data[3]
18
print(data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7], data[8], wert)

Prompt meckert Python 'unicode strings are not supported':
1
Traceback (most recent call last):
2
  File "./mh.py", line 12, in <module>
3
    ser.write('\xFF\x01\x86\x00\x00\x00\x00\x00\x79')
4
  File "/usr/lib/python3/dist-packages/serial/serialposix.py", line 532, in write
5
    d = to_bytes(data)
6
  File "/usr/lib/python3/dist-packages/serial/serialutil.py", line 63, in to_bytes
7
    raise TypeError('unicode strings are not supported, please encode to bytes: {!r}'.format(seq))
8
TypeError: unicode strings are not supported, please encode to bytes: 'ÿ\x01\x86\x00\x00\x00\x00\x00y'

Wie mache ich das denn?
Gast #6628216
Lesenswert?

Leider nicht zielführend
1
Traceback (most recent call last):
2
  File "./mh.py", line 12, in <module>
3
    ser.write('\xFF\x01\x86\x00\x00\x00\x00\x00\x79'.encode('ascii'))
4
UnicodeEncodeError: 'ascii' codec can't encode character '\xff' in position 0: ordinal not in range(128)

Antwort schreiben

Bitte melde dich an, um einen Beitrag zu schreiben.

oder

Mit Google-Account einloggen

Die Registrierung ist kostenlos und dauert nur eine Minute.

Jetzt registrieren