Loading a RAPID module to a 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 on the controller file system. 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);
Upload module to the controller file system.
If the module file is not already on the controller, you must upload it from the PC file system to the controller file system.
Use the following code sample to upload the module file. 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 = @"C:\Temp\MyModule.mod"; string controllerFilePath = Path.Combine(controller.GetEnvironmentVariable("HOME"), "MyModule.mod"); controller.FileSystem.PutFile(filePath, controllerFilePath);
Load 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(controllerFilePath, 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: Upload module from PC
string filePath = @"C:\Temp\MyModule.mod";
string controllerFilePath = Path.Combine(controller.GetEnvironmentVariable("HOME"), "MyModule.mod");
controller.FileSystem.PutFile(@"C:\Temp\MyModule.mod", controllerFilePath);
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(controllerFilePath, 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);
}
}