栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python开发树莓派pico串口通讯

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python开发树莓派pico串口通讯

树莓派pico

调试器python版本 3.10.0(64 bit)
调试器thonny版本 3.3.14
板子型号 Raspberry Pi Pico with RP2040
开发库 MicroPython v1.13

串口代码
import _thread

from machine import UART, Pin, Timer, ADC
import utime
import binascii
import select
import array
import random

u1 = UART(1, baudrate=9600, tx=Pin(4), rx=Pin(5), bits=8, parity=None, stop=0)  # 设置波特率和串口号
# u1.write(b'AT+ECICCID rn')
# bin_data = u1.readline(u1.write(b' '))


send_count = 1
send_msg = b'AT+ECICCID rn'


def send():
    global send_count
    global send_msg

    while True:
        print('nn===============send {}==============='.format(send_count))
        # 等待1s钟
        utime.sleep_ms(1000)
        if u1.any() == 0:
            # 发送一条消息
            u1.write(send_msg)
            # 将手到的信息打印在终端
            print('Send: {}'.format(send_msg).format(' {}n'.format(send_count)))
            # 计数器+1
        send_count += 1
        print('---------------------------------------')


receive_count = 1
receive_msg = ''


def receive():
    global receive_count
    global receive_msg
    while True:
        # print('===============receive_count {}==============='.format(receive_count))
        # 有返回值的情况
        if u1.any() > 0:
            # 接收数据并解码 用空串占位符u1.write(b' '),有多少个空格就可以接收多少数据
            bin_data = u1.readline(u1.write(b' ')).decode()
            # 将接收到的数据转为字符串并存储
            receive_msg += '{}'.format(bin_data)
        # 无返回值的情况
        if u1.any() == 0:
            # 存储的数据是否有值
            if len(receive_msg.strip()) > 0:
                # 输出接收到的值
                print(receive_msg)
                # 休眠0.5秒
                utime.sleep(0.5)
                break
        # 计数器+1
        # receive_count += 1
        # print('---------------------------------------')


try:
    # 创建两个线程 此板子最多两个线程并发 一个主线程,一个次线程
    # _thread.start_new_thread(receive, ())
    # 发送AT指令  b可以不写  需要有rn,串口才能识别
    u1.write(b'AT+ECICCIDrn')
    # 接收数据
    receive()

except:
    print("Error: 无法启动线程")


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/942238.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号