2017-05-26 6 views
0

私はディスプレースメント測定をしています。私はそのPythonに値を与えてもらいたいですが、わかりません。どのようにデータを受け取りますか?このコードを使用して、私の次のエラー - TypeError:バイトをstrに連結できません。RS232からのデータをPythonで読み取る

`import serial 

port = "COM3" 
baud = 115200 

ser = serial.Serial(port, baud, timeout=1) 

    # open the serial port 
if ser.isOpen(): 
    print(ser.name + ' is open...') 

while True: 
    cmd = input("Enter command or 'exit':") 
     # for Python 2 
    # cmd = input("Enter command or 'exit':") 
     # for Python 3 
    if cmd == 'exit': 
     ser.close() 
     exit() 
    else: 
     ser.write(cmd.encode('ascii','strict')+'\r\n') 
     out = ser.read() 
     print('Receiving...'+out) 

` 

答えて

0

write(...は、binaryのデータのみを受け入れます。

Documentation pySerial API
write(data)
Write the bytes data to the port. This should be of type bytes (or compatible such as bytearray or memoryview). Unicode strings must be encoded (e.g. 'hello'.encode('utf-8') .

関連する問題