Loading a RAPID module to the task in the Robot controller
To load a RAPID module into one of the tasks of the robot controller, you need mastership, and a valid path to the module file. Modules can be loaded either from the controller filesystem or the PC filesystem. The default behavior is to not allow loading a module with the same name as the one that already exists. You can override this if you want, and replace the already existing module.
If a RAPID module with syntax errors is loaded then an appropriate message will be displayed in the EventLog .
Solution
Connect to the controller. You can connect to the controller either by using the System ID or by using the IP address of the target controller. Use the following code sample to connect to the controller using the System ID.
Note
You must replace the System ID given in the following code sample with the System ID of the target controller.
Guid systemId = new Guid("32943a02-bb32-4422-9cc6-af8fca1edbab"); Controller controller = new Controller(systemId); controller.Logon(UserInfo.DefaultUser);
Load module from PC or controller file system. You can load the module from either the PC file system or the controller file system.
Use the following code sample to load the module from the PC file system to the target controller. You can use the absolute path of the RAPID module file.
string filePath = @"c:\Temp\MyModule.mod";
Use the following code sample to load the module from the controller file system. The most common location to load a module from is the controller HOME folder. It is recommended to use the GetEnvironmentVariable(String) to retrieve the path to the home folder.
string filePath = Path.Combine(controller.GetEnvironmentVariable("HOME"), "MyModule.mod");
Verify the RAPID module. Load a RAPID module to T_ROB1 task in the robot controller. The method returns True if loading succeeds without any errors, otherwise False. You can view these error messages in the EventLog.
using (Mastership.Request(controller.Rapid)) { // Loads a RAPID module to the task in the robot controller. bLoadSuccess = tRob1.LoadModuleFromFile(filePath, RapidLoadMode.Replace); }
Complete example
The following example gives information on how to load a RAPID module to T_ROB1 task in the robot controller using the LoadModuleFromFile(String, RapidLoadMode) function.
public void LoadModuleFromFile()
{
try
{
// Step 1: Connect to the controller
Guid systemId = new Guid("32943a02-bb32-4422-9cc6-af8fca1edbab");
Controller controller = new Controller(systemId);
controller.Logon(UserInfo.DefaultUser);
// Clears the eventlogs in the controller
controller.EventLog.ClearAll();
// Gets the task with the specified name.
Task tRob1 = controller.Rapid.GetTask("T_ROB1");
// Step 2: Select module from PC
string filePath = @"C:\Temp\MyModule.mod";
bool bLoadSuccess = false;
// Step 3: Load Module
using (Mastership.Request(controller.Rapid))
{
// Loads a RAPID module to the task in the robot controller.
bLoadSuccess = tRob1.LoadModuleFromFile(filePath, RapidLoadMode.Replace);
}
// True if loading succeeds without any errors, otherwise false.
if (!bLoadSuccess)
{
// Gets the available categories of the EventLog.
foreach (EventLogCategory category in controller.EventLog.GetCategories())
{
if (category.Name == "Common")
{
if (category.Messages.Count > 0)
{
foreach (EventLogMessage message in category.Messages)
{
Console.WriteLine("Program [{1}:{2}({0})] {3} {4}",
message.Name, message.SequenceNumber,
message.Timestamp, message.Title, message.Body);
}
}
}
}
}
}
catch (Exception ex)
{
Console.WriteLine("Load Module Error:{0}", ex.Message);
}
}