C# 机器视觉工控通讯——发那科机器人上位机通讯

C#  采用发那科Robot Interface 中间库和机器人控制柜通过网络通讯,主要功能如下:

1. 链接发那科机器人

      /// 连接发那科机器人
        /// </summary>
        /// <param name="IPAddress">机器人IP地址</param>
        /// <param name="DataPosRegStartIndex">PR寄存器开始地址</param>
        /// <param name="DataPosRegEndIndex">PR寄存器结束地址</param>
        /// <returns></returns>
        public static bool ConnectRobot(string IPAddress, int DataPosRegStartIndex, int DataPosRegEndIndex)
        {
            lock (Lock_FNCRobot)
            {
                try
                {
                    mobjCore = new Core();
                    mobjCore.set_TimeOutValue(1000);
                    mobjDataTable = mobjCore.get_DataTable();
                    mobjDataNumReg_INT = mobjDataTable.AddNumReg(FRIF_DATA_TYPE.NUMREG_INT, 1, 200);
                    mobjDataNumReg_REAL = mobjDataTable.AddNumReg(FRIF_DATA_TYPE.NUMREG_REAL, 1, 200);
                    mobjDataString_NUMREG_COMMENT = mobjDataTable.AddString(FRIF_DATA_TYPE.NUMREG_COMMENT, 1, 10);
                    mobjDataPosReg = mobjDataTable.AddPosReg(FRIF_DATA_TYPE.POSREG, 1, DataPosRegStartIndex, DataPosRegEndIndex);
                    mobjCurPos = mobjDataTable.AddCurPos(FRRJIf.FRIF_DATA_TYPE.CURPOS, 1);

                    if (mobjCore.Connect(IPAddress.Trim()))
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                catch (Exception e1)
                {
                    MessageBox.Show("异常信息:\r\n" + e1.Message.ToString(), "Exclamation", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return false;
                }
            }
        }

2. 断开发那科机器人通讯

      /// <summary>
        /// 断开和发那科机器人的通讯
        /// </summary>
        public static void DisConnectRobot()
        {
            lock (Lock_FNCRobot)
            {
                try
                {
                    mobjCore.Disconnect();
                    mobjCore = null;
                    mobjDataTable = null;
                    mobjDataNumReg_INT = null;
                    mobjDataNumReg_REAL = null;
                    mobjDataString_NUMREG_COMMENT = null;
                    mobjDataPosReg = null;
                    mobjCurPos = null;
                    //MessageBox.Show("连接已终止!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch (Exception e1)
                {
                    MessageBox.Show("异常信息:\r\n" + e1.Message.ToString(), "Exclamation", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
        }

3 读取发那科机器人DO值

/// <summary>
        /// 读发那科机器人DO值
        /// </summary>
        /// <param name="DO">DO号</param>
        /// <param name="Result">DO值</param>
        /// /// <param name="Count">位数</param>
        /// <returns>返回true或false</returns>
        public static bool ReadRobotDO(int DO, ref Array Result, int Count)
        {
            lock (Lock_FNCRobot)
            {
                try
                {
                    if (mobjCore.ReadSDO(DO, ref Result, Count))
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                catch (Exception e1)
                {
                    MessageBox.Show("异常信息:\r\n" + e1.Message.ToString(), "Exclamation", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return false;
                }
            }
        }

4 写发那科机器人DO值

        /// <summary>
        /// 写发那科机器人DO值
        /// </summary>
        /// <param name="DO">DO</param>
        /// <param name="Value">DO值:0或1</param>
        /// <returns>返回true或false</returns>
        public static bool WriteRobotDO(int DO, int Value)
        {
            lock (Lock_FNCRobot)
            {
                try
                {
                    Array buf = new short[1];
                    buf.SetValue((short)Value, 0);
                    if (mobjCore.WriteSDO(DO, ref buf, 1))
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                catch (Exception e1)
                {
                    MessageBox.Show("异常信息:\r\n" + e1.Message.ToString(), "Exclamation", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return false;
                }
            }
        }

5  读发那科机器人DI值

    public static bool ReadRobotDI(int DI, ref Array Result)
        {
            lock (Lock_FNCRobot)
            {
                try
                {
                    if (mobjCore.ReadSDI(DI, ref Result, 1))
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                catch (Exception e1)
                {
                    MessageBox.Show("异常信息:\r\n" + e1.Message.ToString(), "Exclamation", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return false;
                }
            }
        }

6 读发那科机器人R寄存器

   public static bool ReadRobotR(int R, ref object Result)
        {
            lock (Lock_FNCRobot)
            {
                try
                {
                    mobjDataTable.Refresh();
                    if (mobjDataNumReg_INT.GetValue(R, ref Result))
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                catch (Exception e1)
                {
                    MessageBox.Show("异常信息:\r\n" + e1.Message.ToString(), "Exclamation", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return false;
                }
            }
        }

7   写发那科机器人R数值寄存器(整型)

   public static bool WriteRobotR(int R, int Value, int Count)
        {
            lock (Lock_FNCRobot)
            {
                try
                {
                    if (mobjDataNumReg_INT.SetValuesInt(R, ref Value, Count))
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                catch (Exception e1)
                {
                    MessageBox.Show("异常信息:\r\n" + e1.Message.ToString(), "Exclamation", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return false;
                }
            }
        }

 8  写发那科机器人R数值寄存器(小数)

  public static bool WriteRobotR(int R, float Value, int Count)
        {
            lock (Lock_FNCRobot)
            {
                try
                {
                    if (mobjDataNumReg_REAL.SetValuesReal(R, ref Value, Count))
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }

                }
                catch (Exception e1)
                {
                    MessageBox.Show("异常信息:\r\n" + e1.Message.ToString(), "Exclamation", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return false;
                }
            }
        }

9 写发那科机器人R数值寄存器的注释

  public static bool WriteRobotR_COMMENT(int R, string Value)
        {
            lock (Lock_FNCRobot)
            {
                try
                {
                    if (mobjDataString_NUMREG_COMMENT.SetValue(R, Value))
                    {
                        mobjDataString_NUMREG_COMMENT.Update();
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                catch (Exception e1)
                {
                    MessageBox.Show("异常信息:\r\n" + e1.Message.ToString(), "Exclamation", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return false;
                }
            }
        }

10   读发那科机器人PR位置寄存器(关节坐标)

  public static bool ReadRobotPRJoint(int PR, ref float[] Result)
        {
            lock (Lock_FNCRobot)
            {
                try
                {
                    short ut = 0, validJ = 0;
                    mobjDataTable.Refresh();
                    if (mobjDataPosReg.GetValueJoint(PR, ref Result[0], ref Result[1], ref Result[2], ref Result[3], ref Result[4], ref Result[5], ref Result[6], ref Result[7], ref Result[8], ref ut, ref validJ))
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                catch (Exception e1)
                {
                    MessageBox.Show("异常信息:\r\n" + e1.Message.ToString(), "Exclamation", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return false;
                }
            }
        }

11   读发那科机器人PR位置寄存器(直角坐标)

     public static bool ReadRobotPRXyzwpr(int PR, ref float[] Result)
        {
            lock (Lock_FNCRobot)
            {
                try
                {
                    float X = 0, Y = 0, Z = 0, W = 0, P = 0, R = 0, E1 = 0, E2 = 0, E3 = 0;
                    short C1 = 0, C2 = 0, C3 = 0, C4 = 0, C5 = 0, C6 = 0, C7 = 0;
                    short ut = 0, validc = 0, uf = 0;
                    mobjDataTable.Refresh();
                    if (mobjDataPosReg.GetValueXyzwpr(PR, ref X, ref Y, ref Z, ref W, ref P, ref R, ref E1, ref E2, ref E3, ref C1, ref C2, ref C3, ref C4,
                        ref C5, ref C6, ref C7, ref uf, ref ut, ref validc))
                    {
                        Result[0] = X;
                        Result[1] = Y;
                        Result[2] = Z;
                        Result[3] = W;
                        Result[4] = P;
                        Result[5] = R;
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                catch (Exception e1)
                {
                    MessageBox.Show("异常信息:\r\n" + e1.Message.ToString(), "Exclamation", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return false;
                }
            }
        }

12 写发那科机器人PR位置寄存器(关节坐标)

      public static bool WriteRobotPRJoint(int PR, Array Value, short UF, short UT)
        {
            lock (Lock_FNCRobot)
            {
                try
                {
                    if (mobjDataPosReg.SetValueJoint(PR, ref Value, UF, UT))
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                catch (Exception e1)
                {
                    MessageBox.Show("异常信息:\r\n" + e1.Message.ToString(), "Exclamation", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return false;
                }
            }
        }

13  写发那科机器人PR位置寄存器(直角坐标)

   public static bool WriteRobotPRXyzwpr(int PR, float X, float Y, float Z, float W, float P, float R, float E1, float E2, float E3,
            short C1, short C2, short C3, short C4, short C5, short C6, short C7, short UF, short UT)
        {
            lock (Lock_FNCRobot)
            {
                try
                {
                    if (mobjDataPosReg.SetValueXyzwpr2(PR, X, Y, Z, W, P, R, E1, E2, E3, C1, C2, C3, C4, C5, C6, C7, UF, UT))
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                catch (Exception e1)
                {
                    MessageBox.Show("异常信息:\r\n" + e1.Message.ToString(), "Exclamation", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return false;
                }
            }
        }

14 读发那科机器人当前位置(直角坐标)

      public static bool ReadCurrRobotXyzwpr(ref Array xyzwpr)
        {
            lock (Lock_FNCRobot)
            {
                try
                {
                    Array config = new short[7];
                    Array joint = new float[9];
                    short intUF = 0;
                    short intUT = 0;
                    short intValidC = 0;
                    short intValidJ = 0;

                    mobjDataTable.Refresh();
                    mobjCurPos.GetValue(ref xyzwpr, ref config, ref joint, ref intUF, ref intUT, ref intValidC, ref intValidJ);
                    return true;
                }
                catch (Exception e1)
                {
                    MessageBox.Show("异常信息:\r\n" + e1.Message.ToString(), "Exclamation", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return false;
                }
            }
        }

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
社会演员多的头像社会演员多普通用户
上一篇 2023年7月15日
下一篇 2023年7月15日

相关推荐