How to read IOs with C#.

Hi, everyone.
I'm wondering - How can I read IO from IRC5 controller with C#? Example - when di1 is changed I would like to make some actions. This is my pseudo code:

"
    // Cn = new Controller;
   Cn.?? += SignalChangedEven;
   private void SignalChangedEven(Object sender, SignalChangedEventArgs e)
        {
            if (cn.? == true)
            {

            }

            if (true)
            {
                 do something;
            }
        }
"
Thank you.

Answers

  • MickyMicky Germany ✭✭✭
    Hello,

    you can find some info on the following web site:
    http://developercenter.robotstudio.com/blobproxy/devcenter/RobotCommunication/html/455b4712-8d1d-499b-90ef-72a2e4209563.htm

    below you will find an example for the FlexPendant SDK but it will also work for the PC-SDK.
    In this example are 4 signals in an array is connected to the event.
    But you can also use a single declaration for a signal and is it also possible to connect several signal to the same event routine.

    private DigitalSignal[] _sigActPosAdv;
    
    //create the signal 
    _sigActPosRet = new DigitalSignal[4];
    
    for (int i = 0; i < 4; i++)
        _sigActPosRet[i] = (DigitalSignal) _ctrl.GetSignal(string.Format("doAct{0}_{1}_PosRet", _iActuatorNo.ToString("00"), i + 1));
    


    The event is activated in the Activate method of the form:
    public void Activate()
            {
                try
                {
                    for (int i = 0; i < 4; i++)
                    {
                        if (_sigActPosRet != null && _sigActPosRet[i] != null)
                            _sigActPosRet[i].Changed += sigActPosRet_Changed;
    
                        if (_sigActPosAdv != null && _sigActPosAdv[i] != null)
                            _sigActPosAdv[i].Changed += _sigActPosAdv_Changed;
                    }
    
                }
                catch (Exception ex)
                {
                    ExceptionHandler.HandleException(this, "Activate", ex);
                }
            }
    


    In the event routine, it will be check if an invoke is required. If yes, the same routine is called again with the Invoke method.
    If you want to know, which signal has triggered the event routine, you can cast the sender object to the uses signal type. Afterwards, you can use the signal name to compare which action should be executed.

    In case you create for each signal a separate event routine, the sender object is not required because the current value of the signal is provided by the parameter "e.NewSignalState"
    void sigActPosRet_Changed(object sender, SignalChangedEventArgs e)
            {
                try
                {
                    // check if invoke is required
                    if (InvokeRequired)
                    {
                        Invoke(new SignalChangedEventHandler(sigActPosRet_Changed), sender, e);
                        return;
                    }
    
                    var signal = (DigitalSignal)sender;
                    if (signal == null) return;
    
                    Debug.WriteLine("Signal: " + signal.Name + " Value:" + e.NewSignalState.Value);
    
                    if (_sigActPosRet[0] != null && signal.Name == _sigActPosRet[0].Name)
                        _picLedActPosRet[0].Image = _imlLeds.Images[(int)e.NewSignalState.Value];
    
                    else if (_sigActPosRet[1] != null && signal.Name == _sigActPosRet[1].Name)
                        _picLedActPosRet[1].Image = _imlLeds.Images[(int)e.NewSignalState.Value];
    
                    else if (_sigActPosRet[2] != null && signal.Name == _sigActPosRet[2].Name)
                        _picLedActPosRet[2].Image = _imlLeds.Images[(int)e.NewSignalState.Value];
    
                    else if (_sigActPosRet[3] != null && signal.Name == _sigActPosRet[3].Name)
                        _picLedActPosRet[3].Image = _imlLeds.Images[(int)e.NewSignalState.Value];
    
                }
                catch (Exception ex)
                {
                    ExceptionHandler.HandleException(this, "sigActPosRet_Changed", ex);
                }
            }

    I hope this will help you.

    Best regards
    Micky
Sign In or Register to comment.