我的 python 程序有问题。我编写了程序来将数据(温度)从 arduino 获取到我的树莓派 sqlite 数据库。但它在第 4 行(导入串行)处给了我一个错误,提示“ImportError:没有名为串行的模块”。我使用 python3 并且已经更新了 pyserial。我是Python新手,所以我犯了一些错误......
#!/ussr/bin/python
# -*- coding: utf-8 -*-
import serial
import datetime
import sqlite3 as lite
import sys
import time
ser = serial.Serial('/dev/ttyACM1', 9600, timeout=1)
ser.open()
count = 0
con = lite.connect('realtime_data.db')
try:
while 1:
indata = ser.readline()
current_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
count = count + 1
print (count)
with con:
cur = con.cursor()
cur.execute("INSERT INTO Temperatures VALUES( ?, ?, ? )", (count, current_time, indata))
if count > 100:
cur.execute("DELETE FROM Temperatures")
count = 0
# time.sleep(3) #upload to database every 5 seconds
except KeyboardInterrupt:
ser.close()
这是一个关于如何使用 Python 3 安装 pip? https://stackoverflow.com/questions/6587507/how-to-install-pip-with-python-3。之后,您可以使用pip
安装pyserial
与 python-3.x 兼容,如下所示:
$ sudo pip3 install pyserial
这里有一个doc http://pyserial.sourceforge.net/pyserial.html#from-source-tar-gz-or-checkout关于如何安装pyserial
使用与 python-3.x 兼容的源代码
P.S.: 如果你的平台上同时存在 python-2.x 和 python-3.x,比如 Arch Linux,当你要安装一些软件包时,你应该小心选择该软件包应该兼容哪个 python 版本,然后使用pip2
or pip3
获取并安装这些软件包。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)