三菱机器人MelfaRxM.OCX控件的python用法

1.   安装控件  \MelfaRXM\MelfaRXM_Dev\Redist\Installer

2. 在WINDOWS/System32里找到MelfaRxM.ocx

3.把OCX控件转成C#的DLL

a.打vs的开发人员命令行

三菱机器人MelfaRxM.OCX控件的python用法

b.把刚刚的OCX放到命令行显示的目录

c.在命令输入 :   aximp MelfaRxM.ocx

三菱机器人MelfaRxM.OCX控件的python用法

生成的DLL就是pythonnet可用调用的DLL的

4.pythonnet的调用

# -*- coding: utf-8 -*-
"""
Created on Wed May  6 23:24:19 2020

@author: Martin
"""
import time
import clr
import sys
sys.path.append(r"./MISB_ReferencedAssemblies")
clr.AddReference('AxMELFARXMLib')
clr.AddReference('MELFARXMLib')
import AxMELFARXMLib
import System

class robot(System.Windows.Forms.Form):
    #---初始化
    def __init__(self,robot):
        self.robot = robot
        print(self.robot)
        self.InitializeComponent()
        
    def InitializeComponent(self):  
        self.ctrMelfaRxM=AxMELFARXMLib.AxMelfaRxM() 
        ((System.ComponentModel.ISupportInitialize)(self.ctrMelfaRxM)).BeginInit()
        self.Controls.Add(self.ctrMelfaRxM)
        ((System.ComponentModel.ISupportInitialize)(self.ctrMelfaRxM)).EndInit()     
        if self.ctrMelfaRxM.ServerLive()==False:
            self.ctrMelfaRxM.ServerStart()  
                    
    def sv_off(self):
        sSendDataOFF = "1" + "\n" +"0"
        lStatus = self.ctrMelfaRxM.RequestServiceM(self.robot, 403, len(sSendDataOFF), sSendDataOFF, 0, 0, 0)  
        return lStatus    
        
    def sv_on(self):
        sSendDataON = "1" + "\n" +"1"
        lStatus = self.ctrMelfaRxM.RequestServiceM(self.robot, 403, len(sSendDataON), sSendDataON, 0, 0, 0)
        return lStatus
           
    def read_program_list(self):
        lStatus = self.ctrMelfaRxM.RequestServiceM(self.robot, 106, 0, "", 0, 0, 0)        
        return lStatus
        
def runApp():     
    global robot1
    thread=System.Threading.Thread(System.Threading.ThreadStart(runthread))
    thread.SetApartmentState(0)
    thread.Start()
    time.sleep(1)
#    robot1.Hide()
    
def runthread():
    global robot1  
    robot1 = robot(1)   
    System.Windows.Forms.Application.Run(robot1)             
    pass  

global robot1              
if __name__ == "__main__":   
    runApp()    
    pass