RobotStudio event

How to log on to multiple controllers in RS from PC SDK ?

vikram6892
edited January 2019 in Developer Tools
I would like to log on to both of these Robot controllers in RS via PC SDK application at the same time. Is it possible?



Best Answer

  • scottMHA
    scottMHA
    Answer ✓
    Hello Scott,
    Thank you very much for your answer.
    From your above code, I can understand that the LogOn (to controller) operation takes places as it loops through each (controller) item in the for loop.
    for(int i = 0; i < _storedController.Count(); i++)
    Let's say if we have 3 controllers, after the execution of the for loop, the pc application will be logged on to the 3rd controller. So I can only control or take access over the task from 3rd controller alone. Is it right ? Correct me if I am wrong.
    I would like to do some actions viz., start a rapid task, set program pointer to a routine, etc. in all the 3 controllers simultaneously. Will that be possible ?

    So long as the controllers stored in a list and you do not dispose of the instances the user will remain logged on to all controllers and you can do as you wish. Until you call the
    Controller.LogOff()
    method the controllers will remain logged on. You just need to index through your list to get the controller object. Example below to do an action on all controllers;

    class Class1
        {
            NetworkScanner _networkScan = new NetworkScanner();
            List<Controller> _storedController = new List<Controller>();
            Controller _controller;
            ControllerInfo[] _controllerCollection;
    
            private void ControllerLogon()
            {
                // Network scan for running robots (online and virtual)
                _networkScan.Scan();
                // add running robots to a collection
                _controllerCollection = _networkScan.GetControllers();
    
                // index through found robots
                for (int i = 0; i < _controllerCollection.Count(); i++)
                {
                    if (_controllerCollection[i].IsVirtual)
                    {
                        // if controller is virtual create new Virtual Controller
                        _controller = new Controller(_controllerCollection[i]);
                        // add _controller to list for future use.
                        _storedController.Add(_controller);
                    }
                }
    
                for (int i = 0; i < _storedController.Count(); i++)
                {
                    // Logs into each controller as default user.
                    _storedController[i].Logon(UserInfo.DefaultUser);
                }
            }
    
            private void ControllerActions()
            {
                // Index through controllers that has default user logged on
                for (int i = 0; i < _storedController.Count(); i++)
                {
                    // Check controller is virtual that the user is logged in
                    if (_storedController[i].IsVirtual && _storedController[i].CurrentUser == UserInfo.DefaultUser)
                    {
                        // Execute your code here
                    }
                }
            }
        }
    It's probably better to do it asynchronously so you can access all controllers at the same time and not slow down the main thread but the above will do the job for you if you are not familiar with threading and/or not worried about losing microseconds.


Answers

  • scottMHA
    scottMHA
    edited February 2019
    I'm assuming by the IP address they're virtual robots? If so you create a new controller object for each of them then logon.

    private void ControllerLogon()
            {
                NetworkScanner _networkScan = new NetworkScanner();
                List<Controller> _storedController = new List<Controller>();
                Controller _controller;
    
                // Network scan for running robots (online and virtual)
                _networkScan.Scan();
                // add running robots to a collection
                ControllerInfo[] _controllerCollection = _networkScan.GetControllers();
    
                // index through found robots
                for (int i = 0; i < aCollection.Count(); i++)
                {
                    if (aCollection[i].IsVirtual)
                    {
                        // if controller is virtual create new Virtual Controller
                        _controller = new Controller(aCollection[i]);
                        // add _controller to list for future use.
                        _storedController.Add(_controller);
                    }
                }
    
                for(int i = 0; i < _storedController.Count(); i++)
                {
                    // Logs into each controller as default user.
                    _storedController[i].Logon(UserInfo.DefaultUser);
                }
            }
    Alternatively you could use a new thread to logon to each of them concurrently and do whatever you want to do but you may experience cross threading issues if you then try and use the active station.
  • Hello Scott,
    Thank you very much for your answer.
    From your above code, I can understand that the LogOn (to controller) operation takes places as it loops through each (controller) item in the for loop.
    for(int i = 0; i < _storedController.Count(); i++)
    Let's say if we have 3 controllers, after the execution of the for loop, the pc application will be logged on to the 3rd controller. So I can only control or take access over the task from 3rd controller alone. Is it right ? Correct me if I am wrong.
    I would like to do some actions viz., start a rapid task, set program pointer to a routine, etc. in all the 3 controllers simultaneously. Will that be possible ?

  • Hello @scottMHA,
    Finally I got the answer. Thank you very much.
  • scottMHA
    scottMHA
    edited February 2019
    Hello Scott,
    Thank you very much for your answer.
    From your above code, I can understand that the LogOn (to controller) operation takes places as it loops through each (controller) item in the for loop.
    for(int i = 0; i < _storedController.Count(); i++)
    Let's say if we have 3 controllers, after the execution of the for loop, the pc application will be logged on to the 3rd controller. So I can only control or take access over the task from 3rd controller alone. Is it right ? Correct me if I am wrong.
    I would like to do some actions viz., start a rapid task, set program pointer to a routine, etc. in all the 3 controllers simultaneously. Will that be possible ?


    The below should set the program pointer to whatever module and routine you wish.

    private void ControllerActions()
            {
                // Index through controllers that has default user logged on
                for (int i = 0; i < _storedController.Count(); i++)
                {
                    // Check controller is virtual that the user is logged in
                    if (_storedController[i].IsVirtual && _storedController[i].CurrentUser == UserInfo.DefaultUser)
                    {
                        // If controller execution has stopped set program pointer.
                        if (_storedController[i].Rapid.ExecutionStatus == ExecutionStatus.Stopped)
                        {
                            Task[] _rapidTasks = _storedController[i].Rapid.GetTasks();
    
                            for (int j = 0; j < _rapidTasks.Count(); j++)
                            {
                                if (_rapidTasks[i].Name == "T_ROB1")
                                {
                                    _rapidTasks[i].SetProgramPointer("YourModule", "YourRoutine");
                                }
                            }
                        }
                    }
                }
            }