Micropython——关于_thread(多线程)开发的二次应用

摘要:

上次介绍了_thread(多线程)的基础讲解,这次将结合UART(串口)开进行开发应用讲解。串口在日常的设计中是一个非常常见的东西,所以熟练掌握是基本知识。下面,就来介绍一下关于串口与_thread 结合的一个小程序。下面是所需要准备的东西。

硬件设计:

  • Rasspberry Pi Pico开发板(应用Micropython开发)
  • USB转TTL开发板
  • 软连线

 上面显示了其连线示意图。

演示代码:

思路:当程序运行时,在串口发送字符,其显示温度,若不发送字符时,其显示时间参数,时间参数显示在程序行窗口。

import _thread
import machine
import utime
from machine import UART,Pin,ADC

temp = ADC(4)
uart = UART(0,baudrate = 115200,bits = 8,parity = None,stop = 1,tx = Pin(0),rx = Pin(1))
led = Pin(25,Pin.OUT)
gLock = None

//温度显示
def temperate():
    
    read_temp_voltage = temp.read_u16()*3.3/65535
    temperature = 27-(read_temp_voltage-0.706)/0.001721
    uart.write('Temperature {}\n'.format(temperature))

//显示时间
def data():
    print(utime.localtime())

def main():
    print('---------Start Progarm-----------\n')
    utime.sleep(1)
    print('-----请输入任意字符,以显示温度----\n')
    while True:
        gLock = _thread.allocate_lock()
        gLock.acquire()
        if uart.any() :
             print('Echo String:{}'.format(uart.readline().decode()))
             _thread.start_new_thread(temperate,())
             utime.sleep(2)     
             led.value(1)
        else:
             print('------当前时间为如下所示-------')
             _thread.start_new_thread(data,())
             utime.sleep(2)
        gLock.release()
         
if __name__ == '__main__':
    main()

示例结果:

 我们在串口软件中输入任意字符,都将会引发进程,从而调用温度显示模块,从而将温度显示在串口界面。

 当我们串口不发送字符时,我们会得到以下结果,其只进行线程2的调用,即只显示时间。

 


技术交流
欢迎转载、收藏、有所收获点赞支持一下!

 

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Micropython是一种运行在微控制器上的Python语言实现,它允许我们使用Python语言编写嵌入式应用程序。ESP32是一款双核心的微控制器,它可以利用多线程来实现并发执行代码。下面是使用Micropython实现ESP32模块多线程的步骤: 1. 安装Micropython固件 首先需要在ESP32模块上安装Micropython固件,可以从Micropython官网下载适合ESP32模块的固件,并使用esptool.py工具烧录到ESP32模块。 2. 导入_thread模块 在Micropython中,使用_thread模块实现多线程。可以使用import语句导入该模块。 ```python import _thread ``` 3. 定义线程函数 使用Micropython定义线程函数的方式与Python标准库中的threading模块相似。可以使用def语句定义一个函数,并在函数中编写线程的执行逻辑。 ```python def thread1(): while True: print("Thread 1 is running") def thread2(): while True: print("Thread 2 is running") ``` 上述代码定义了两个线程函数,分别为thread1和thread2,它们会不断地输出线程正在运行的信息。 4. 创建线程 使用_thread模块的start_new_thread()函数创建线程,并将线程函数和参数传入该函数。可以创建多个线程来实现并发执行多个任务。 ```python _thread.start_new_thread(thread1, ()) _thread.start_new_thread(thread2, ()) ``` 上述代码创建了两个线程,并分别将thread1和thread2函数作为线程函数传入。空元组()表示不传递任何参数给线程函数。 5. 主线程等待子线程结束 由于Micropython的主线程会在所有子线程结束后退出,因此需要在主线程中等待子线程结束。可以使用while循环来等待所有子线程结束。 ```python while True: pass ``` 上述代码中的while循环会一直执行,直到所有子线程结束后才会退出。 完整的多线程示例代码如下: ```python import _thread def thread1(): while True: print("Thread 1 is running") def thread2(): while True: print("Thread 2 is running") _thread.start_new_thread(thread1, ()) _thread.start_new_thread(thread2, ()) while True: pass ``` 在ESP32模块上运行上述代码后,可以看到两个线程不断地输出线程正在运行的信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Xa_L

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值