123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231 |
- using EtherCAT_DLL;
- using EtherCAT_DLL_Err;
- using LogClass;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Xml;
- namespace CCDCount.DLL
- {
- public class MechanicalControlClass
- {
- ushort g_uRet = 0;
- // 当前选择卡的卡号
- ushort g_uESCCardNo = 99;
- ushort g_uESCNodeID = 0;
- ushort g_uESCSlotID;
- // 卡片初始化状态
- bool g_bInitialFlag = false;
- //全部卡号
- ushort[] g_uESCCardNoList = new ushort[32];
- List<ushort> LsNodeID = new List<ushort>();
- List<short> LsSlotID = new List<short>();
- ushort g_nESCExistCards = 0;
- bool IsCanInit = false;
- public MechanicalControlClass()
- {
- OpenTaidaCard();
- InitControl();
- FindSlave();
- }
- /// <summary>
- /// 初始化
- /// </summary>
- public void InitControl()
- {
- ushort nSeq = 0, nCardNo = 0;
- g_uRet = 0;
- for (nSeq = 0; nSeq < 32; nSeq++)
- {
- // 清空卡号记录
- g_uESCCardNoList[nSeq] = 0;
- }
- g_bInitialFlag = false;
- for (nSeq = 0; nSeq < g_nESCExistCards; nSeq++)
- {
- // 获取卡号
- g_uRet = CEtherCAT_DLL.CS_ECAT_Master_Get_CardSeq(nSeq, ref nCardNo);
- // 初始化控制卡
- g_uRet = CEtherCAT_DLL.CS_ECAT_Master_Initial(nCardNo);
- if (g_uRet != CEtherCAT_DLL_Err.ERR_ECAT_NO_ERROR)
- {
- LOG.error("_ECAT_Master_Initial, ErrorCode = " + g_uRet.ToString());
- g_uESCCardNoList[nSeq] = 99;
- }
- else
- {
- //记录全部的卡号
- g_uESCCardNoList[nSeq] = nCardNo;
- //标记卡是否已经初始化
- g_bInitialFlag = true;
- }
- }
- g_uESCCardNo = g_uESCCardNoList[0];
- }
- /// <summary>
- /// 释放资源
- /// </summary>
- public void Dispose()
- {
-
- }
- /// <summary>
- /// 电机旋转
- /// </summary>
- public void MotorRotation(float Angle)
- {
- //开启电机
- g_uRet = CEtherCAT_DLL.CS_ECAT_Slave_Motion_Set_Svon(g_uESCCardNo, g_uESCNodeID, g_uESCSlotID, 1);
- if (g_uRet != CEtherCAT_DLL_Err.ERR_ECAT_NO_ERROR)
- {
- LOG.error("_ECAT_Slave_Motion_Set_Svon, Error Code = " + g_uRet.ToString());
- }
- }
- /// <summary>
- /// 推出气动阀
- /// </summary>
- public void OpenAirValve(int ChannelNO)
- {
- Console.WriteLine("{0}气缸阀门关闭", ChannelNO);
- }
- /// <summary>
- /// 收回气动阀
- /// </summary>
- public void CloseAirValve(int ChannelNO)
- {
- }
- /// <summary>
- /// 收回所有气动阀
- /// </summary>
- public void CloseAllAirValve()
- {
- Console.WriteLine("所有阀门打开");
- }
- // <summary>
- /// 启动震动盘
- /// </summary>
- public void OpenShockPlate()
- {
- }
- /// <summary>
- /// 修改震动盘速度
- /// </summary>
- public void ChangeShokSpeed()
- {
- Console.WriteLine("震盘速度修改");
- }
- // <summary>
- /// 停止震动盘
- /// </summary>
- public void CloseShockPlate()
- {
- Console.WriteLine("震盘停止");
- }
- private void OpenTaidaCard()
- {
- g_uRet = 0;
- g_bInitialFlag = false;
- // 获取轴卡数量
- g_uRet = CEtherCAT_DLL.CS_ECAT_Master_Open(ref g_nESCExistCards);
- if (g_uRet != CEtherCAT_DLL_Err.ERR_ECAT_NO_ERROR)
- {
- LOG.error("CS_ECAT_Master_Open, rt =" + g_uRet.ToString());
- }
- else
- {
- if (g_nESCExistCards == 0)
- {
- LOG.error("No EtherCat can be found!");
- }
- else
- {
- IsCanInit = true;
- }
- }
- }
- // 查找全部从站
- private void FindSlave()
- {
- short nSID = 0, Cnt = 0;
- ushort uNID = 0, uSlaveNum = 0, uReMapNodeID = 0;
- uint uVendorID = 0, uProductCode = 0, uRevisionNo = 0, uSlaveDCTime = 0;
- g_uRet = CEtherCAT_DLL.CS_ECAT_Master_Get_SlaveNum(g_uESCCardNo, ref uSlaveNum);
- if (g_uRet != CEtherCAT_DLL_Err.ERR_ECAT_NO_ERROR)
- {
- LOG.error("_ECAT_Master_Get_SlaveNum, ErrorCode = " + g_uRet.ToString());
- }
- else
- {
- if (uSlaveNum == 0)
- {
- LOG.error("Card NO: " + g_uESCCardNo.ToString() + " No slave found!");
- }
- else
- {
- for (uNID = 0; uNID < uSlaveNum; uNID++)
- {
- g_uRet = CEtherCAT_DLL.CS_ECAT_Master_Get_Slave_Info(g_uESCCardNo, uNID, ref uReMapNodeID, ref uVendorID, ref uProductCode, ref uRevisionNo, ref uSlaveDCTime);
- if (uVendorID == 0x1DD && uProductCode == 0x10305070) // A2E
- {
- nSID = 0;
- LsNodeID.Add(uNID);
- LsSlotID.Add(nSID);
- //CmbNode.Items.Add("NodeID:" + uNID + " - SlotID:" + nSID + "-A2E");
- Cnt++;
- }
- else if (uVendorID == 0x539 && uProductCode == 0x2200001) //Yaskawa
- {
- nSID = 0;
- LsNodeID.Add(uNID);
- LsSlotID.Add(nSID);
- //CmbNode.Items.Add("NodeID:" + uNID + " - SlotID:" + nSID + "-Yaskawa");
- Cnt++;
- }
- else if ((uVendorID == 0x1A05 || uVendorID == 0x1DD) && uProductCode == 0x0624) //Ec4Axis
- {
- for (nSID = 0; nSID < 4; nSID++)
- {
- LsNodeID.Add(uNID);
- LsSlotID.Add(nSID);
- //CmbNode.Items.Add("NodeID:" + uNID + " - SlotID:" + nSID + "-Ec4Axis");
- Cnt++;
- }
- }
- else if ((uVendorID == 0x1A05 || uVendorID == 0x1DD) && uProductCode == 0x5621) //EcAxis
- {
- nSID = 0;
- LsNodeID.Add(uNID);
- LsSlotID.Add(nSID);
- // CmbNode.Items.Add("NodeID:" + uNID + " - SlotID:" + nSID + "EcAxis");
- Cnt++;
- }
- }
- }
- }
- }
- }
- }
|