调试器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 \r\n')
# bin_data = u1.readline(u1.write(b' '))
send_count = 1
send_msg = b'AT+ECICCID \r\n'
def send():
global send_count
global send_msg
while True:
print('\n\n===============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可以不写 需要有\r\n,串口才能识别
u1.write(b'AT+ECICCID\r\n')
# 接收数据
receive()
except:
print("Error: 无法启动线程")