Raspberry Pi,pyserial不与arduino一起使用

时间:2016-09-10 04:59:21

标签: python arduino raspberry-pi archlinux

我使用sudo python test.py getsudo python test.py led调用以下python脚本会出现以下错误:

终端输出:

[pi@raspberrypi pi]$ sudo python test.py led
led
^CTraceback (most recent call last):
  File "test.py", line 19, in <module>
    send()
  File "test.py", line 10, in send
    ser.readline()
  File "/usr/lib/python3.5/site-packages/serial/serialposix.py", line 450, in read
    ready, _, _ = select.select([self.fd, self.pipe_abort_read_r], [], [], timeout)
KeyboardInterrupt
[pi@raspberrypi pi]$ sudo python test.py get
get
^CTraceback (most recent call last):
  File "test.py", line 22, in <module>
    get()
  File "test.py", line 14, in get
    ser.readline()
  File "/usr/lib/python3.5/site-packages/serial/serialposix.py", line 450, in read
    ready, _, _ = select.select([self.fd, self.pipe_abort_read_r], [], [], timeout)
KeyboardInterrupt

test.py脚本:

#!/bin/python

import sys
import serial

ser = serial.Serial('/dev/ttyACM0', 9600)

def send():
    ser.write(bytes('led', 'ascii'))
    return ser.readline()

def get():
    ser.write(bytes('get', 'ascii'))
    return ser.readline()

if __name__ == '__main__':
    if str(sys.argv[1]) == 'led':
        print('led')
        send()
    elif str(sys.argv[1]) == 'get':
        print('get')
        get()

但是当我直接通过python运行它时:

sudo python

python console:

>>> import serial
>>> ser = serial.Serial('/dev/ttyACM0', 9600)
>>> def send():
...     ser.write(bytes('led', 'ascii'))
...     return ser.readline()
... 
>>> def get():
...     ser.write(bytes('get', 'ascii'))
...     return ser.readline()
... 
>>> print(send())
b'LED on\r\n'
>>> print(get())
b'1023\r\n'
>>> 

直接运行相同的脚本是有效的,我不确定我做错了什么

0 个答案:

没有答案