Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何同时从python向arduino发送和接收数据_Python_Arduino_User Input_Raspberry Pi3_Pyserial - Fatal编程技术网

如何同时从python向arduino发送和接收数据

如何同时从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()时,程序停止等待输入并

我现在有一个arduino代码,它连接到三个传感器:温度、压力和湿度。我想用python编写一个顺序代码(通过int或strg),它发送我想要读取的传感器类型,例如:如果我通过键盘1输入,它会不断向我发送温度数据;如果收入为2,请向我发送压力数据;因此可以在任何时间输入任何数字。 对不起,我的英语不好,我不知道能否解释我的问题

我在arduino和switch case中有一个类似的代码,它可以完美地工作。但是我不能在python中使用它,因为当我放置raw_input()时,程序停止等待输入并停止读取传感器数据

python
这只适用于从默认指定的一种传感器类型读取数据

如果您有需要并行运行的任务,您可以使用线程。一个线程获取传感器数据,另一个线程等待输入

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()