当前位置:   article > 正文

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

robot interface

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

1. 链接发那科机器人

  1. /// 连接发那科机器人
  2. /// </summary>
  3. /// <param name="IPAddress">机器人IP地址</param>
  4. /// <param name="DataPosRegStartIndex">PR寄存器开始地址</param>
  5. /// <param name="DataPosRegEndIndex">PR寄存器结束地址</param>
  6. /// <returns></returns>
  7. public static bool ConnectRobot(string IPAddress, int DataPosRegStartIndex, int DataPosRegEndIndex)
  8. {
  9. lock (Lock_FNCRobot)
  10. {
  11. try
  12. {
  13. mobjCore = new Core();
  14. mobjCore.set_TimeOutValue(1000);
  15. mobjDataTable = mobjCore.get_DataTable();
  16. mobjDataNumReg_INT = mobjDataTable.AddNumReg(FRIF_DATA_TYPE.NUMREG_INT, 1, 200);
  17. mobjDataNumReg_REAL = mobjDataTable.AddNumReg(FRIF_DATA_TYPE.NUMREG_REAL, 1, 200);
  18. mobjDataString_NUMREG_COMMENT = mobjDataTable.AddString(FRIF_DATA_TYPE.NUMREG_COMMENT, 1, 10);
  19. mobjDataPosReg = mobjDataTable.AddPosReg(FRIF_DATA_TYPE.POSREG, 1, DataPosRegStartIndex, DataPosRegEndIndex);
  20. mobjCurPos = mobjDataTable.AddCurPos(FRRJIf.FRIF_DATA_TYPE.CURPOS, 1);
  21. if (mobjCore.Connect(IPAddress.Trim()))
  22. {
  23. return true;
  24. }
  25. else
  26. {
  27. return false;
  28. }
  29. }
  30. catch (Exception e1)
  31. {
  32. MessageBox.Show("异常信息:\r\n" + e1.Message.ToString(), "Exclamation", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  33. return false;
  34. }
  35. }
  36. }

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

  1. /// <summary>
  2. /// 断开和发那科机器人的通讯
  3. /// </summary>
  4. public static void DisConnectRobot()
  5. {
  6. lock (Lock_FNCRobot)
  7. {
  8. try
  9. {
  10. mobjCore.Disconnect();
  11. mobjCore = null;
  12. mobjDataTable = null;
  13. mobjDataNumReg_INT = null;
  14. mobjDataNumReg_REAL = null;
  15. mobjDataString_NUMREG_COMMENT = null;
  16. mobjDataPosReg = null;
  17. mobjCurPos = null;
  18. //MessageBox.Show("连接已终止!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
  19. }
  20. catch (Exception e1)
  21. {
  22. MessageBox.Show("异常信息:\r\n" + e1.Message.ToString(), "Exclamation", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  23. }
  24. }
  25. }

3 读取发那科机器人DO值

  1. /// <summary>
  2. /// 读发那科机器人DO值
  3. /// </summary>
  4. /// <param name="DO">DO号</param>
  5. /// <param name="Result">DO值</param>
  6. /// /// <param name="Count">位数</param>
  7. /// <returns>返回true或false</returns>
  8. public static bool ReadRobotDO(int DO, ref Array Result, int Count)
  9. {
  10. lock (Lock_FNCRobot)
  11. {
  12. try
  13. {
  14. if (mobjCore.ReadSDO(DO, ref Result, Count))
  15. {
  16. return true;
  17. }
  18. else
  19. {
  20. return false;
  21. }
  22. }
  23. catch (Exception e1)
  24. {
  25. MessageBox.Show("异常信息:\r\n" + e1.Message.ToString(), "Exclamation", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  26. return false;
  27. }
  28. }
  29. }

4 写发那科机器人DO值

  1. /// <summary>
  2. /// 写发那科机器人DO值
  3. /// </summary>
  4. /// <param name="DO">DO</param>
  5. /// <param name="Value">DO值:0或1</param>
  6. /// <returns>返回true或false</returns>
  7. public static bool WriteRobotDO(int DO, int Value)
  8. {
  9. lock (Lock_FNCRobot)
  10. {
  11. try
  12. {
  13. Array buf = new short[1];
  14. buf.SetValue((short)Value, 0);
  15. if (mobjCore.WriteSDO(DO, ref buf, 1))
  16. {
  17. return true;
  18. }
  19. else
  20. {
  21. return false;
  22. }
  23. }
  24. catch (Exception e1)
  25. {
  26. MessageBox.Show("异常信息:\r\n" + e1.Message.ToString(), "Exclamation", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  27. return false;
  28. }
  29. }
  30. }

5  读发那科机器人DI值

  1. public static bool ReadRobotDI(int DI, ref Array Result)
  2. {
  3. lock (Lock_FNCRobot)
  4. {
  5. try
  6. {
  7. if (mobjCore.ReadSDI(DI, ref Result, 1))
  8. {
  9. return true;
  10. }
  11. else
  12. {
  13. return false;
  14. }
  15. }
  16. catch (Exception e1)
  17. {
  18. MessageBox.Show("异常信息:\r\n" + e1.Message.ToString(), "Exclamation", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  19. return false;
  20. }
  21. }
  22. }

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

  1. public static bool ReadRobotR(int R, ref object Result)
  2. {
  3. lock (Lock_FNCRobot)
  4. {
  5. try
  6. {
  7. mobjDataTable.Refresh();
  8. if (mobjDataNumReg_INT.GetValue(R, ref Result))
  9. {
  10. return true;
  11. }
  12. else
  13. {
  14. return false;
  15. }
  16. }
  17. catch (Exception e1)
  18. {
  19. MessageBox.Show("异常信息:\r\n" + e1.Message.ToString(), "Exclamation", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  20. return false;
  21. }
  22. }
  23. }

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

  1. public static bool WriteRobotR(int R, int Value, int Count)
  2. {
  3. lock (Lock_FNCRobot)
  4. {
  5. try
  6. {
  7. if (mobjDataNumReg_INT.SetValuesInt(R, ref Value, Count))
  8. {
  9. return true;
  10. }
  11. else
  12. {
  13. return false;
  14. }
  15. }
  16. catch (Exception e1)
  17. {
  18. MessageBox.Show("异常信息:\r\n" + e1.Message.ToString(), "Exclamation", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  19. return false;
  20. }
  21. }
  22. }

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

  1. public static bool WriteRobotR(int R, float Value, int Count)
  2. {
  3. lock (Lock_FNCRobot)
  4. {
  5. try
  6. {
  7. if (mobjDataNumReg_REAL.SetValuesReal(R, ref Value, Count))
  8. {
  9. return true;
  10. }
  11. else
  12. {
  13. return false;
  14. }
  15. }
  16. catch (Exception e1)
  17. {
  18. MessageBox.Show("异常信息:\r\n" + e1.Message.ToString(), "Exclamation", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  19. return false;
  20. }
  21. }
  22. }

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

  1. public static bool WriteRobotR_COMMENT(int R, string Value)
  2. {
  3. lock (Lock_FNCRobot)
  4. {
  5. try
  6. {
  7. if (mobjDataString_NUMREG_COMMENT.SetValue(R, Value))
  8. {
  9. mobjDataString_NUMREG_COMMENT.Update();
  10. return true;
  11. }
  12. else
  13. {
  14. return false;
  15. }
  16. }
  17. catch (Exception e1)
  18. {
  19. MessageBox.Show("异常信息:\r\n" + e1.Message.ToString(), "Exclamation", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  20. return false;
  21. }
  22. }
  23. }

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

  1. public static bool ReadRobotPRJoint(int PR, ref float[] Result)
  2. {
  3. lock (Lock_FNCRobot)
  4. {
  5. try
  6. {
  7. short ut = 0, validJ = 0;
  8. mobjDataTable.Refresh();
  9. 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))
  10. {
  11. return true;
  12. }
  13. else
  14. {
  15. return false;
  16. }
  17. }
  18. catch (Exception e1)
  19. {
  20. MessageBox.Show("异常信息:\r\n" + e1.Message.ToString(), "Exclamation", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  21. return false;
  22. }
  23. }
  24. }

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

  1. public static bool ReadRobotPRXyzwpr(int PR, ref float[] Result)
  2. {
  3. lock (Lock_FNCRobot)
  4. {
  5. try
  6. {
  7. float X = 0, Y = 0, Z = 0, W = 0, P = 0, R = 0, E1 = 0, E2 = 0, E3 = 0;
  8. short C1 = 0, C2 = 0, C3 = 0, C4 = 0, C5 = 0, C6 = 0, C7 = 0;
  9. short ut = 0, validc = 0, uf = 0;
  10. mobjDataTable.Refresh();
  11. 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,
  12. ref C5, ref C6, ref C7, ref uf, ref ut, ref validc))
  13. {
  14. Result[0] = X;
  15. Result[1] = Y;
  16. Result[2] = Z;
  17. Result[3] = W;
  18. Result[4] = P;
  19. Result[5] = R;
  20. return true;
  21. }
  22. else
  23. {
  24. return false;
  25. }
  26. }
  27. catch (Exception e1)
  28. {
  29. MessageBox.Show("异常信息:\r\n" + e1.Message.ToString(), "Exclamation", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  30. return false;
  31. }
  32. }
  33. }

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

  1. public static bool WriteRobotPRJoint(int PR, Array Value, short UF, short UT)
  2. {
  3. lock (Lock_FNCRobot)
  4. {
  5. try
  6. {
  7. if (mobjDataPosReg.SetValueJoint(PR, ref Value, UF, UT))
  8. {
  9. return true;
  10. }
  11. else
  12. {
  13. return false;
  14. }
  15. }
  16. catch (Exception e1)
  17. {
  18. MessageBox.Show("异常信息:\r\n" + e1.Message.ToString(), "Exclamation", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  19. return false;
  20. }
  21. }
  22. }

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

  1. public static bool WriteRobotPRXyzwpr(int PR, float X, float Y, float Z, float W, float P, float R, float E1, float E2, float E3,
  2. short C1, short C2, short C3, short C4, short C5, short C6, short C7, short UF, short UT)
  3. {
  4. lock (Lock_FNCRobot)
  5. {
  6. try
  7. {
  8. if (mobjDataPosReg.SetValueXyzwpr2(PR, X, Y, Z, W, P, R, E1, E2, E3, C1, C2, C3, C4, C5, C6, C7, UF, UT))
  9. {
  10. return true;
  11. }
  12. else
  13. {
  14. return false;
  15. }
  16. }
  17. catch (Exception e1)
  18. {
  19. MessageBox.Show("异常信息:\r\n" + e1.Message.ToString(), "Exclamation", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  20. return false;
  21. }
  22. }
  23. }

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

  1. public static bool ReadCurrRobotXyzwpr(ref Array xyzwpr)
  2. {
  3. lock (Lock_FNCRobot)
  4. {
  5. try
  6. {
  7. Array config = new short[7];
  8. Array joint = new float[9];
  9. short intUF = 0;
  10. short intUT = 0;
  11. short intValidC = 0;
  12. short intValidJ = 0;
  13. mobjDataTable.Refresh();
  14. mobjCurPos.GetValue(ref xyzwpr, ref config, ref joint, ref intUF, ref intUT, ref intValidC, ref intValidJ);
  15. return true;
  16. }
  17. catch (Exception e1)
  18. {
  19. MessageBox.Show("异常信息:\r\n" + e1.Message.ToString(), "Exclamation", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  20. return false;
  21. }
  22. }
  23. }

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号