RP2040 磁卡复制机程序 - 使用 MicroPython
以下是一个使用 MicroPython 编写的磁卡复制机程序示例,可以将源 SD 卡中的所有文件复制到目标 SD 卡中:
import machine
import uos
import os
import time
def copy_sd_card():
# 初始化 SD 卡
sd = machine.SDCard(slot=2, width=1, sck=machine.Pin(18), miso=machine.Pin(19), mosi=machine.Pin(20), cs=machine.Pin(21))
uos.mount(sd, "/sd")
# 检查源 SD 卡是否存在
if not 'sd' in uos.listdir("/"):
print("源 SD 卡不存在")
return
# 初始化目标 SD 卡
target_sd = machine.SDCard(slot=1, width=1, sck=machine.Pin(2), miso=machine.Pin(4), mosi=machine.Pin(3), cs=machine.Pin(5))
uos.mount(target_sd, "/target")
# 检查目标 SD 卡是否存在
if not 'target' in uos.listdir("/"):
print("目标 SD 卡不存在")
return
# 复制文件
files = uos.listdir("/sd")
for file in files:
source_path = "/sd/" + file
target_path = "/target/" + file
with open(source_path, "rb") as source_file:
with open(target_path, "wb") as target_file:
while True:
data = source_file.read(512)
if len(data) == 0:
break
target_file.write(data)
print("复制完成")
# 执行复制操作
copy_sd_card()
请确保将源 SD 卡插入到 RP2040 的第 2 个 SD 卡插槽中,并将目标 SD 卡插入到第 1 个 SD 卡插槽中。然后,将上述代码保存为 .py 文件并上传到 RP2040 上运行。程序将会复制源 SD 卡中的所有文件到目标 SD 卡中。
原文地址: https://www.cveoy.top/t/topic/qAuR 著作权归作者所有。请勿转载和采集!