如何同时从python向arduino发送和接收数据
我现在有一个arduino代码,它连接到三个传感器:温度、压力和湿度。我想用python编写一个顺序代码(通过int或strg),它发送我想要读取的传感器类型,例如:如果我通过键盘1输入,它会不断向我发送温度数据;如果收入为2,请向我发送压力数据;因此可以在任何时间输入任何数字。 对不起,我的英语不好,我不知道能否解释我的问题 我在arduino和switch case中有一个类似的代码,它可以完美地工作。但是我不能在python中使用它,因为当我放置raw_input()时,程序停止等待输入并停止读取传感器数据 python如何同时从python向arduino发送和接收数据,python,arduino,user-input,raspberry-pi3,pyserial,Python,Arduino,User Input,Raspberry Pi3,Pyserial,我现在有一个arduino代码,它连接到三个传感器:温度、压力和湿度。我想用python编写一个顺序代码(通过int或strg),它发送我想要读取的传感器类型,例如:如果我通过键盘1输入,它会不断向我发送温度数据;如果收入为2,请向我发送压力数据;因此可以在任何时间输入任何数字。 对不起,我的英语不好,我不知道能否解释我的问题 我在arduino和switch case中有一个类似的代码,它可以完美地工作。但是我不能在python中使用它,因为当我放置raw_input()时,程序停止等待输入并
这只适用于从默认指定的一种传感器类型读取数据如果您有需要并行运行的任务,您可以使用线程。一个线程获取传感器数据,另一个线程等待输入 Python有一个非常易于使用的内置线程模块
- 官方python文档:
- Tutoialspoint:
import threading
def wait_input():
while True:
user_input = input()
# do something with user_input
def get_sonsordata()
while True:
med=a.readline()
print(med)
input_thread = threading.Thread(target=wait_input)
input_thread.start()
sensor_thread = threading.Thread(target=get_sonsordata)
sensor_thread.start()
import threading
def wait_input():
while True:
user_input = input()
# do something with user_input
def get_sonsordata()
while True:
med=a.readline()
print(med)
input_thread = threading.Thread(target=wait_input)
input_thread.start()
sensor_thread = threading.Thread(target=get_sonsordata)
sensor_thread.start()