使用Python将数据写入串行端口时发生错误

时间:2018-12-10 17:41:32

标签: python-3.x

我想在串行端口上写数据。我的环境是科学的Linux 7.5和python 3.6。我无法在串行端口上写数据。代码为:

import serial  
ser=serial.Serial(   
    port='/dev/tnt4',
    baudrate=115200,
    parity=serial.PARITY_ODD,
    stopbits=serial.STOPBITS_ONE,
    bytesize=serial.EIGHTBITS
)
ser.open()
ser.write(b'1')
ser.close()

上面的代码产生以下错误:

Traceback (most recent call last):
  File "/opt/ActivePython-3.6/lib/python3.6/site-packages/serial/serialposix.py", line 537, in write
    n = os.write(self.fd, d)
OSError: [Errno 22] Invalid argument

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/shakeel/Python3.6 Projects/SerialPortReadWrite/SerialPortReadWrite.py", line 13, in <module>
    ser.write(b'1')
  File "/opt/ActivePython-3.6/lib/python3.6/site-packages/serial/serialposix.py", line 571, in write
    raise SerialException('write failed: {}'.format(e))
serial.serialutil.SerialException: write failed: [Errno 22] Invalid argument

0 个答案:

没有答案