Python 使用pySerial进行多处理的好例子

Python 使用pySerial进行多处理的好例子,python,multiprocessing,arduino,pyserial,Python,Multiprocessing,Arduino,Pyserial,有没有什么地方可以让我看看在Python的多处理环境中执行操作的示例 ==更新上述问题=== Arduino的代码: //Initialize the pins void setup() { //Start serial communication } void loop() { //Keep polling to see if any input is present at the serial PORT //If present perform the actio

有没有什么地方可以让我看看在Python的多处理环境中执行操作的示例

==更新上述问题===

Arduino的代码:

//Initialize the pins

void setup()
{
    //Start serial communication
}

void loop()
{
    //Keep polling to see if any input is present at the serial PORT
    //If present perform the action specified.
    //In my case : TURN ON the led or TURN OFF.
}
类似于Python前端的代码:

对于我使用的基本参考,PDF,3.0MB

#import the different modules like time,multiprocessing
#Define the two parallel processes:
def f1(sequence):
    #open the serial port and perform the task of controlling the led's
    #As mentioned in the answer to the above question : Turn ON or OFF as required
    #The 10 seconds ON, then the 10 seconds OFF,finally the 10 seconds ON.

def f2(sequence):
    #Perform the task of turning the LED's off every 2 seconds as mentioned.
    #To do this copy the present conditions of the led.
    #Turn off the led.
    #Turn it back to the saved condition.

def main():
    print "Starting main program"

    hilo1 = multiprocessing.Process(target=f1, args=(sequence))
    hilo2 = multiprocessing.Process(target=f2, args=(sequence))

    print "Launching threads"
    hilo1.start()
    hilo2.start()
    hilo1.join()
    hilo2.join()
    print "Done"

if ____name____ == '____main____':
    main()
在执行上述操作时,我面临几个问题:

流程f1根据需要执行任务。即打开LED 10秒钟,关闭LED 10秒钟,最后打开LED 10秒钟。从外观上看,似乎没有执行进程f2,也就是说,即使程序成功结束,LED也不会每两秒钟关闭一次。这里会发生什么

如果我在进程中使用print打印某些内容,则它不会显示在屏幕上。我很想知道提到这些例子的人是如何能够显示这些过程的打印输出的


好的,您在GUI应用程序PyQt中有一个PySerial监控的代码示例,在单独的线程中运行。

为什么使用join?join阻塞调用线程,直到调用其join方法的进程终止或出现可选超时。 这就是为什么f2没有启动,因为f1正在运行

改为在main中尝试此代码

procs = []
procs.append(Process(target=f1, args=(sequence))
procs.append(Process(target=f2, args=(sequence))
map(lambda x: x.start(), procs)
map(lambda x: x.join(), procs)

pyserial有自己的内部阻塞调用可供选择。我不使用它的主要原因之一。很难将它集成到更大的软件系统中。@Keith:那么你用什么软件包来进行串行连接调用呢?我用,还有那里的模块。它简单但有效,可以与模块结合,在反应堆模型中多路输入。我实际上是在看控制一组连接到Arduino板的LED。我希望我的大部分处理都在python端完成。我必须同时执行两项任务。流程1:根据特定的输入模式控制一组led。假设模式为::打开led 10秒钟,然后关闭10秒钟。然后再次打开10秒b过程2:在特定时间(比如每2秒)关闭led。用外行的话说,整个过程将执行类似于灯光采样的操作。多重处理是我所看到的正确方向吗?阿披舍克:好吧,你现在有个例子了。祝你的任务顺利——如果你遇到了具体问题,请随时询问其他问题questions@Abhishek:请注意,只有操作系统意义上的单个进程可以同时打开给定的串行端口。在时间的帮助下,您在这里指定的内容可以在单个线程内轻松完成keeping@Abhishek听起来您想进行事件驱动编程,例如,在10秒内切换LED 2。这对我来说很容易