How to: Use from Python |
Python users use the RMCLink COM Component interface. Specifics on using this component from Python are described below, including a complete example script. The example script is also included as a .py file in the RMCLink example projects.
The ability to create and use COM objects requires an extension. These instructions use pywin32, Python Extensions for Microsoft Windows, available from SourceForge. The pywin32 also requires the Visual C++ Redistributable for Visual Studio, available for download from Microsoft.
After installing pywin32, and making sure the Visual C++ Redistributable for Visual Studio is also installed, the pywin32 postinstall script may need to be run manually if it failed to install. Do this from the command prompt with a command such as:
py C:\Users\[name]\AppData\Local\Programs\Python\Python35\Scripts\pywin32_postinstall.py -install
The code must first import the COM extension library:
import win32com.client
As is common for all languages, the first step is to create an instance of the RMCLink class. This is a two step process: first create an RMCLinkServer object, and then use that object to create the appropriate type of RMCLink object.
To create the RMCLinkServer object, use the following line in your Python file:
srv = win32com.client.Dispatch("RMCLink.RMCLinkServer")
You can now use the newly created srv variable to create the RMCLink object using one of RMCLinkServer's CreateEthernetLink, CreateSerialLink or CreateUSBLink methods. The following line creates a link for communicating with an RMC70 at IP address 192.168.0.22:
rmc = srv.CreateEthernetLink(2, "192.168.0.22");
Notice that as described below, Python cannot use enumerations, and therefore, although the CreateEthernetLink, CreateSerialLink and CreateUSBLink methods take a DeviceType parameter, the literal value for the desired enumeration must be used.
Python cannot use enumerations. Therefore, although the CreateEthernetLink, CreateSerialLink and CreateUSBLink methods are specified to have the devType parameter be of type DeviceType, you must enter the literal value of the enumeration member you want to use. Similarly, for the IsConnected method's ping parameter, you will have to use the literal values in place of the PingType enumeration members. Each enumeration topic lists the literal values next to the named values (e.g. ptDoNotPing = 0).
To read data from the controller, use the ReadFFile or ReadLFile methods. It is not necessary to use the ReadFFile_Script or ReadLFile_Script methods.
data = rmc.ReadFFile(8,8,2);
Notice that the ReadBit and ReadBitField methods can be used as well.
Use the WriteFFile and WriteLFile methods. They need to be passed a Python array.
cmd = [ 20.0, 12.5, 5, 100.0, 100.0, 0.0, 0.0, 0.0, 0.0, 0.0]
rmc.WriteFFile(40, 0, 10, cmd)
The following code is an example of a Python program. This example script is also included as a .py file in the RMCLink example projects.
Python (Sample.py) |
# Import the COM extension import win32com.client # First we need to create the RMCLinkServer COM object. srv = win32com.client.Dispatch("RMCLink.RMCLinkServer") # Use our RMCLinkServer to create the correct type of RMCLink object. # NOTE: Python does not support enumerations from the COM object, so we use the literal value (dtRMC150=3). rmc = srv.CreateEthernetLink(3,"192.168.0.196") # Establish the connection. rmc.Connect() # Read F8:8 and F8:9 (Axis 0 Actual Position and Velocity) data = rmc.ReadFFile(8,8,2) print('Actual Postion: '+str(data)) # Close the connection. rmc.Disconnect() |
See Also
RMCLink COM Component | How Do I Overview | RMCLink Component
Copyright (c) 2024 Delta Computer Systems, Inc. dba Delta Motion