o88ne 发表于 2023-1-29 11:49

利用Python模拟鼠标自动完成MM32-LINK程序下载

    简 介: 编写了利用Python控制MM32-LINK自动下载程序,这可以减少在开发过程中的操作。

    MM32-LINK在打开程序过程中,对话框的标题出现错误,“Load form file”,应该修改成“Load from file”。

    关键词: MM32-LINK,Python,模拟鼠标,自动程序下载


————————————————
版权声明:本文为CSDN博主「卓晴」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/zhuoqingjoking97298/article/details/121131071

o88ne 发表于 2023-1-29 11:51

MM32-LINK

  在 制作灵动单片机MM32F3277 测试版 描述了通过MM32-LINK完成对于灵动MCU开发过程。特别是设计了基于MM32-LINK的5PIN程序下载接口。但是,如果在整个开发过程,需要MM32-LINK自动完成程序下载,需要通过MM32-LINK界面上的载入程序,并进行编程过程。使用鼠标需要经过多个操作。

  下面将会通过Python程序,模拟鼠标与键盘操作,完成一键下载程序的过程。


▲ 图1.1 MM32-LINK界面

  通过截取MM32-LINK对话框,利用线段绘制出从左上角到打开程序位置的线段,通过显示线段属性(宽,长)从而获得鼠标点击打开程序按钮相对位置。(803,175)

o88ne 发表于 2023-1-29 11:52

一、模拟鼠标操作
1、Python软件包

  根据 Simulate a mouse click python 中的介绍,可以使用pyautogui程序包完成对鼠标模拟操作。

o88ne 发表于 2023-1-29 11:53

测试点击程序


o88ne 发表于 2023-1-29 11:54

from head import *
import pyautogui

MM32_TITLE = 'MM32-LINK'
offset_x = 803
offset_y = 175

rect = tspgetwindowrect(MM32_TITLE)

click_x = rect + offset_x
click_y = rect + offset_y

pyautogui.click(click_x, click_y)

for _ in range(20):

    time.sleep(.1)
    rect = tspgetwindowrect('Load form file')
    if sum(rect) != 0:
      break

printf(rect)

o88ne 发表于 2023-1-29 11:56

错误:MM32-LINK软件在打开的对话框标题出现错误:Load form file

  应该修改为: “Load from file”

o88ne 发表于 2023-1-29 11:57

载入程序

  然后在使用Python模拟快捷键,加成对于程序的整个自动下载流程控制。

o88ne 发表于 2023-1-29 11:59

from head import *
import pyautogui

MM32_TITLE = 'MM32-LINK'
offset_x = 803
offset_y = 175

filename = r'D:\zhuoqing\window\ARM\IAR\MM32\Test\TestF103UART2.hex'

if not os.path.isfile(filename):
    printf("ERROR:Can not find file :%s\a"%filename)
    exit()

rect = tspgetwindowrect(MM32_TITLE)

click_x = rect + offset_x
click_y = rect + offset_y

LOAD_FILE = 'Load form file'

pyautogui.click(click_x, click_y)

for _ in range(20):

    time.sleep(.1)
    rect = tspgetwindowrect(LOAD_FILE)
    if sum(rect) != 0:
      break

if sum(rect) == 0:
    printf("No load file dialog open !\a")
    exit()

clipboard.copy(filename)
tspsendwindowkey(LOAD_FILE, "n", alt=1, noreturn=1)
tspsendwindowkey(LOAD_FILE, "v", control=1, noreturn=1)
tspsendwindowkey(LOAD_FILE, "o", alt=1, noreturn=1)

time.sleep(.5)

tspsendwindowkey(MM32_TITLE, "p", alt=1, noreturn=1)

for _ in range(20):
    time.sleep(.1)
    rect = tspgetwindowrect('Program')
    if sum(rect) != 0: break

if sum(rect) == 0:
    printf("ERROR:No program dialog open.\a")
    exit()

tspsendwindowkey('Program', '\r', noreturn=1)
printf("\a")
time.sleep(2.0)
tspsendwindowkey('Program', 'c', alt=1, noreturn=1)

printf("\a")

o88ne 发表于 2023-1-29 11:59

※ 实验总结 ※

  编写了利用Python控制MM32-LINK自动下载程序,这可以减少在开发过程中的操作。

  MM32-LINK在打开程序过程中,对话框的标题出现错误,“Load form file”,应该修改成“Load from file”。

  下面给出完成的程序。

#!/usr/local/bin/python
# -*- coding: gbk -*-
#============================================================
# MM32PRG.PY                   -- by Dr. ZhuoQing 2021-11-03
#
# Note:
#============================================================

from head import *
import pyautogui


#------------------------------------------------------------
MM32_TITLE = 'MM32-LINK'
offset_x = 803
offset_y = 175

#------------------------------------------------------------
filename = r'D:\zhuoqing\window\ARM\IAR\MM32\Test\SeekFreeMP.Hex'

if len(sys.argv) > 1:
    filename = sys.argv

printf(filename)

#------------------------------------------------------------
if not os.path.isfile(filename):
    printf("ERROR:Can not find file :%s\a"%filename)
    exit()

#------------------------------------------------------------
windowrect = tspgetwindowrect(MM32_TITLE)

click_x = windowrect + offset_x
click_y = windowrect + offset_y

#------------------------------------------------------------
LOAD_FILE = 'Load form file'

pyautogui.click(click_x, click_y)

for _ in range(20):

    time.sleep(.1)
    rect = tspgetwindowrect(LOAD_FILE)
    if sum(rect) != 0:
      break

#------------------------------------------------------------
if sum(rect) == 0:
    printf("No load file dialog open !\a")
    exit()

#------------------------------------------------------------
clipboard.copy(filename)
tspsendwindowkey(LOAD_FILE, "n", alt=1, noreturn=1)
tspsendwindowkey(LOAD_FILE, "v", control=1, noreturn=1)
tspsendwindowkey(LOAD_FILE, "o", alt=1, noreturn=1)

time.sleep(.5)

#------------------------------------------------------------
tspsendwindowkey(MM32_TITLE, "c", alt=1, noreturn=1)
time.sleep(1)

wm_offsetx = 430
wm_offsety = 503

click_x = windowrect + wm_offsetx
click_y = windowrect + wm_offsety
pyautogui.click(click_x, click_y)


#------------------------------------------------------------
'''

tspsendwindowkey(MM32_TITLE, "p", alt=1, noreturn=1)

for _ in range(20):
    time.sleep(.1)
    rect = tspgetwindowrect('Program')
    if sum(rect) != 0: break

if sum(rect) == 0:
    printf("ERROR:No program dialog open.\a")
    exit()

tspsendwindowkey('Program', '\r', noreturn=1)
printf("\a")
time.sleep(2.0)
tspsendwindowkey('Program', 'c', alt=1, noreturn=1)
'''
#------------------------------------------------------------
printf("\a")




#------------------------------------------------------------
#      END OF FILE : MM32PRG.PY
#============================================================

单片小菜 发表于 2023-1-29 16:53

这个工程可以下载吗?

tpgf 发表于 2023-2-6 14:27

看了看大概的功能是自己做了一个自动点击器是吗

guanjiaer 发表于 2023-2-6 14:35

对屏幕位置的准确度高不高 稳定吗

heimaojingzhang 发表于 2023-2-6 14:55

这种上位机代码使用什么语言编程是最方便的呢

keaibukelian 发表于 2023-2-6 15:28

感觉从语法上来说 比c语言更加简单

paotangsan 发表于 2023-2-6 15:40

感觉并不是很实用因为我们自己用鼠标点也点不了几下

renzheshengui 发表于 2023-2-6 16:00

这种模拟鼠标操作的话 是不是需要每次操作前需要校对零点啊

Undshing 发表于 2023-3-9 22:25

不同分辨率屏幕能用吗?

Jacquetry 发表于 2023-3-12 20:42

屏幕位置需要特别注意吗?
页: [1]
查看完整版本: 利用Python模拟鼠标自动完成MM32-LINK程序下载