The test modifies main.py in micro python

Zhuo Qing 2021-11-25 10:58:10
test modifies main.py main py

Jane Medium : Write the python Script program , It can be automatically generated MicroPython Internal execution procedures , With the help of STM32-BOOTLOADER adopt REPL send out MicroPython, from MicroPython Finish right SD In the card main.py Empty or update files . This requires the original main.py Can return to REPL, And STM32-BOOTLOADER Cooperate with the download program . Otherwise it needs to pass USB SD The card reader / writer is complete for SD In the card main.py Modification of .

key word MicroPython,MM32F3277,mainpy

 

 

§01 MicroPython perform main.py


One 、MicroPython The boot process

stay Design with SD Card MM32F3277 MicroPython Experiment board Tested with SD Card MM32F3277 MicroPython Experiment board . The circuit is portable MicroPython From MindMotion Su Yong (2021-11-5 edition ), Confirm after power on SDIO Whether the port is connected SD card ( Now this version needs SD The capacity of the card is greater than 4Gb), And detect whether there is main.py file . If you find main.py, Start execution main.py.

about SD card main.py The file of , Often need another SD The card reader has the functions of writing and deleting by the computer . The following tests whether you can use MicroPython Software completion for main.py Modification and stored procedure .

▲ chart 1.1.1 Reading and writing SD card USB Connecting socket

▲ chart 1.1.1 Reading and writing SD card USB Connecting socket

Two 、 Delete main.py

In the current version , There is no direct support for SD Instructions for deleting files in the card . However, with the help of the write function , The original main.py Empty the file .

1、 Test starting conditions

First , Use SD card USB Reader writer , stay SD Write a test... Into the card main.py Program .

from machine import Pin
import utime
for _ in range(10):
print(_)
utime.sleep_ms(100)

The test circuit is after the upper point , Will execute first main.py, Showing 10 After a number , Retreat to REPL state .


[Y] sdcard ready.
[Y] file system on sdcard ready.
[Y] run the main.py on disk ...
0
1
2
3
4
5
6
7
8
9
[Y] done. 1
MicroPython v1.16 on 2021-11-05; MB_F3270 with MM32F3277G7P
>>>

2、 Empty main.py

Write a test program for SD In the card main.py To empty .

(1) Clear the code

from machine import Pin
import utime
with open('main.py', 'w') as f:
f.write('')

(2) Execution process

Reset MicroPython...
Wait for MicroPython coming back...
Download MicroPython : 22 lines/507 characters.
Begin to download program...
-------------------------------------------------------------------------
0
>>>

(3) Check the emptying effect

Power on to execute , Here is the display information . You can see , here MicroPython Although... Was detected main.py The existence of , But because of main.py It's an empty file , So go straight back to REPL .

[Y] sdcard ready.
[Y] file system on sdcard ready.
[Y] run the main.py on disk ...
[Y] done. 1
MicroPython v1.16 on 2021-11-05; MB_F3270 with MM32F3277G7P
>>>

3、 ... and 、 write in main.py

Write files to SD In the card main.py, The program to be written needs to be programmed into an executable program , Well main.py The procedure is MicroPython open main.py Parameters to be written later .

1、 Test to be written main.py

Here's what needs to be done in SD Card write main.py Code .

from machine import Pin
import utime
for _ in range(10):
print(_)
utime.sleep_ms(100)

2、Python command

To write Python Script program pymain.py. Its function :

  • It is determined whether to generate an empty... According to whether there are parameters main.py Implementation procedure of , Or write the current editing window program to SD card main.py;
  • Generate MicroPython Execute code ;
  • copy to clipboard, Go to STM32-Bootloader send out "MPDLD" command , from STM32-BOOTLOADER adopt REPL Deliver to MicroPython, It completes the emptying main.py, Or write the program to main.py.

(1)pymain.py Code

#!/usr/local/bin/python
# -*- coding: gbk -*-
#============================================================
# PYMAIN.PY -- by Dr. ZhuoQing 2021-11-07
#
# Note:
#============================================================
from head import *
from tsmodule.tsstm32 import *
#------------------------------------------------------------
pastestr = ''
if len(sys.argv) == 1:
tspcopyclipboard()
pastestr = clipboard.paste().replace('\r', '').replace('\n', '\\n')
writestr = 'with open("main.py", "w") as f:\n f.write("%s")\n\n'%pastestr
clipboard.copy(writestr)
stm32cmd("MPDLD")
tspfocuswindow("TEASOFT:1")
printf('\a')
#------------------------------------------------------------
# END OF FILE : PYMAIN.PY
#============================================================

(2) A functional test

Use TEASOFT Edit downloaded main.py Program , Do the following pymain Program .

*Ⅰ. eliminate main.py
  • dispatch orders : pymain +
*Ⅱ. write in main.py
  • dispatch orders : pymain ( No parameter )

After testing , The above process meets the design function .

Four 、 Existing problems

Complete the above functions , need MicroPython Can return to REPL Talent . If the original main.py It's a dead cycle , Cannot have external conditions to return to REPL, Then the above procedure cannot complete the work . Only through USB SD The reader / writer completes for main.py The removal of .

 

measuring Try to summarize ※


Ed Yes python Script program , It can be automatically generated MicroPython Internal execution procedures , With the help of STM32-BOOTLOADER adopt REPL send out MicroPython, from MicroPython Finish right SD In the card main.py Empty or update files .

This requires the original main.py Can return to REPL, And STM32-BOOTLOADER Cooperate with the download program . Otherwise it needs to pass USB SD The card reader / writer is complete for SD In the card main.py Modification of .

One 、 complete pymain.py Program

#!/usr/local/bin/python
# -*- coding: gbk -*-
#============================================================
# PYMAIN.PY -- by Dr. ZhuoQing 2021-11-07
#
# Usage:
# pymain * # Clear main.py content
# pymain # Write the current edit file into micropython main.py
#
# Note:
#============================================================
from head import *
from tsmodule.tsstm32 import *
#------------------------------------------------------------
pastestr = ''
if len(sys.argv) == 1:
tspcopyclipboard()
pastestr = clipboard.paste().replace('\r', '').replace('\n', '\\n')
printf("Write the main.py code.")
else:
printf("Clear MicroPython main.py.")
writestr = 'with open("main.py", "w") as f:\n f.write("%s")\n\n'%pastestr
clipboard.copy(writestr)
stm32cmd("MPDLD")
tspfocuswindow("TEASOFT:1")
printf('\a')
#------------------------------------------------------------
# END OF FILE : PYMAIN.PY
#============================================================


■ Links to related literature :

● Related chart Links :

版权声明
本文为[Zhuo Qing]所创,转载请带上原文链接,感谢
https://pythonmana.com/2021/11/20211109010513817i.html

  1. Python basic syntax -- Boolean operation, break, continue, pass
  2. Python basic syntax -- loop
  3. Python first experience - efficient office, data analysis, crawler
  4. Modulenotfounderror: no module named 'Django summernote details
  5. Key points for Django to make personal blog website
  6. Path setting of templates in Django settings
  7. Leetcode 1611. Minimum one bit operations to make integers Zero (Python)
  8. Directory C: \ users \ a \ desktop \ Django_ The blog master is registered as the GIT root, but no git repository details are found there
  9. Django. Core. Exceptions. Improveconfigured: application labels aren't unique, duplicates: admin
  10. How to verify that Django has created the project details correctly
  11. How to create a database when using Django to construct a website
  12. The solution of using Django framework to create project in Windows system
  13. Running Python virtual environment on win10 system to execute ll_ Env \ scripts \ activate: unable to load file elaboration scheme
  14. Detailed explanation of constructing virtual environment with Django in Python 3
  15. Python implementation of affine cipher
  16. RC4 Python implementation
  17. Simple: Python_ Automatic body temperature clock
  18. 用python把两个csv中的日期的列提出年,做出新的一列,再把两个csv表格按照新做出的日期这列和ID号合并为一个表。
  19. python中类实例化后,其对象无法被其他模块调用方法
  20. [JSON] - Python creates JSON file format
  21. Utilisez Python pour proposer l'année de la colonne de date dans les deux CSV, faire une nouvelle colonne, puis combiner les deux tableaux CSV en un seul tableau avec la colonne de date et le numéro d'identification.
  22. 关于#python#的问题,请各位专家解答!
  23. ***
  24. ***
  25. 關於#python#的問題,請各比特專家解答!
  26. S'il vous plaît répondre aux questions de Python!
  27. About the import of Python class
  28. Magic Python property decorator: 1 line of code makes Python methods become properties in seconds
  29. Python 音频调整音量(附代码) | Python工具
  30. Python programming ideas [series of articles]
  31. Python crawler programming idea (67): modify nodes using pyquery
  32. Python crawler programming idea (66): using pyquery to obtain node information
  33. Python crawler programming idea (65): find nodes using pyquery
  34. Python crawler programming idea (64): using CSS selectors in pyquery
  35. Python crawler programming idea (63): basic knowledge of pyquery
  36. Python crawler programming idea (62): project practice: capturing cool dog online red song list
  37. Python crawler programming idea (61): project practice: capturing rental information
  38. Python crawler programming idea (60): get CSS selector code through browser
  39. Python爬虫编程思想(85):在Python中使用非关系型数据库
  40. Volume de réglage audio Python (avec Code) | outils Python
  41. Python crawler programming idea (59): get attribute value and text with beautiful soup CSS selector
  42. Python crawler programming idea (58): nested selection nodes with beautiful soup CSS selectors
  43. Python crawler programming idea (57): basic usage of CSS selector in beautiful soup
  44. Python crawler programming idea (56): find method of beautiful soup method selector
  45. Python crawler programming idea (55): find of beautiful soup method selector_ All method
  46. Python crawler programming idea (54): use beautiful soup to select sibling nodes
  47. Python crawler programming idea (53): use beautiful soup to select the parent node
  48. Django3.0 solves the problem of error reporting in reverse parsing
  49. Precautions for Python crawler
  50. Python 3 crawler series (1) -- climbing blind date websites
  51. Python到底是什么?为什么要学Python?
  52. #yyds干货盘点#Pandas数据清洗实用指南
  53. Python打包exe文件无法运行
  54. Two common ways to save files in Python
  55. #yyds幹貨盤點#Pandas數據清洗實用指南
  56. Yyds Dry Inventory pandas Data Cleaning Practical Guide
  57. PYTHON用LSTM长短期记忆神经网络的参数优化方法预测时间序列洗发水销售数据
  58. Python集成学习:自己编写构建AdaBoost分类模型可视化决策边界及sklearn包调用比较
  59. Python 3 makes a search software
  60. Python 3 simulated microblog login