/// <summary> /// 关闭板卡。先关从卡再关主卡。 /// </summary> /// <returns></returns> public EM_RES Close() { if (!isInit) { return(EM_RES.OK); } int ret; #if LEADSHINE if (brand == BRAND.LEADSHINE) { if (type == TYPE.MOTION) { //axis dec stop for (int n = 0; n < ax_num; n++) { LTDMC.dmc_stop((ushort)card_id, (ushort)n, 0); Thread.Sleep(10); } ret = LTDMC.dmc_board_close(); if (ret != 0) { VAR.msg.AddMsg(Msg.EM_MSGTYPE.SYS, string.Format("{0}关闭失败,Err:0x{1:X8}", disc, ret)); return(EM_RES.ERR); } VAR.msg.AddMsg(Msg.EM_MSGTYPE.SYS, string.Format("{0}关闭成功", disc)); isInit = false; return(EM_RES.OK); } else if (type == TYPE.CAN_IO) { //连接IO卡 ret = LTDMC.dmc_set_can_state((ushort)maincard_id, (ushort)card_id, 0, 0); if (ret != 0) { VAR.msg.AddMsg(Msg.EM_MSGTYPE.ERR, string.Format("{0}断开失败,Err:0x{1:X8}", disc, ret)); return(EM_RES.ERR); } VAR.msg.AddMsg(Msg.EM_MSGTYPE.SYS, string.Format("{0}断开成功", disc)); isInit = false; return(EM_RES.OK); } #if LEADSHINE_IO else if (type == TYPE.IO) { IOC0640.ioc_board_close(); isInit = false; VAR.msg.AddMsg(Msg.EM_MSGTYPE.ERR, string.Format("{0}关闭成功", disc)); return(EM_RES.OK); } #endif } #endif #if ZMOTION if (brand == BRAND.ZMOTION) { if (handle != (IntPtr)0) { ret = zmcaux.ZAux_Close(handle); if (ret != 0) { VAR.msg.AddMsg(Msg.EM_MSGTYPE.SYS, string.Format("{0}关闭失败,Err:0x{1:X8}", disc, ret)); return(EM_RES.ERR); } handle = (IntPtr)0; isInit = false; VAR.msg.AddMsg(Msg.EM_MSGTYPE.ERR, string.Format("{0}关闭成功", disc)); return(EM_RES.OK); } return(EM_RES.OK); } #endif #if ADVANTTECK if (brand == BRAND.ADVANTTECH) { isInit = false; VAR.msg.AddMsg(Msg.EM_MSGTYPE.ERR, "研华库未添加!"); return(EM_RES.ERR); } #endif #if ORIENTALMOTOR if (brand == BRAND.ORIENTALMOTOR) { if (AZD.Motor.Close()) { isInit = false; return(EM_RES.OK); } else { return(EM_RES.ERR); } } #endif VAR.msg.AddMsg(Msg.EM_MSGTYPE.ERR, string.Format("未定义异常,BRAND={0},TYPE={1}", brand, type)); return(EM_RES.ERR); }
/// <summary> /// 初始化板卡。 /// 如有主次卡之分,请先初始化主卡。关闭则相反,先关从卡再关主卡。 /// </summary> /// <param name="id">卡ID</param> /// <param name="maincard_id">主卡ID</param> /// <param name="ip">卡IP</param> /// <param name="filename">配置文件路径</param> /// <returns></returns> public EM_RES Init(int card_id = -1, int maincard_id = -1, string ip = "", string filename = "") { if (isReady) { return(EM_RES.OK); } int ret = 0; EM_RES res = EM_RES.OK; //update id if (card_id >= 0) { this.card_id = card_id; } else { card_id = this.card_id; } //update ip if (ip.Length > 7) { this.ip = ip; } else { ip = this.ip; } //update maincard_id if (maincard_id >= 0) { this.maincard_id = maincard_id; } else { maincard_id = this.maincard_id; } #if LEADSHINE if (brand == BRAND.LEADSHINE) { if (type == TYPE.MOTION) { //init card ret = LTDMC.dmc_board_init(); if (ret < 0) { VAR.msg.AddMsg(Msg.EM_MSGTYPE.ERR, string.Format("{0}ID有重复,请检查确认!0x{1:X8}", disc, ret)); return(EM_RES.ERR); } //get card list ushort cardnum = 0; ushort[] cardid = new ushort[64]; uint[] cardtype = new uint[64]; ret = LTDMC.dmc_get_CardInfList(ref cardnum, cardtype, cardid); if (ret != 0) { VAR.msg.AddMsg(Msg.EM_MSGTYPE.ERR, string.Format("获取{0}列表错误,0x{1:X8}", disc, ret)); return(EM_RES.ERR); } //search card list bool bfound = false; for (int n = 0; n < cardnum; n++) { if (card_id == cardid[n]) { bfound = true; break; } } if (!bfound) { isInit = false; VAR.msg.AddMsg(Msg.EM_MSGTYPE.ERR, string.Format("{0}列表找不到ID为{1}的板卡!", disc, card_id)); return(EM_RES.ERR); } //download config res = DownLoadFile(filename); if (res != EM_RES.OK) { return(res); } isInit = true; handle = (IntPtr)1; VAR.msg.AddMsg(Msg.EM_MSGTYPE.SYS, string.Format("{0}初始化成功!", disc)); return(EM_RES.OK); } else if (type == TYPE.CAN_IO) { //连接IO卡 ret = LTDMC.dmc_set_can_state((ushort)maincard_id, (ushort)card_id, 1, 0); if (ret != 0) { VAR.msg.AddMsg(Msg.EM_MSGTYPE.ERR, string.Format("{0}连接失败,Err:0x{1:X8},重新连接...", disc, ret)); ret = LTDMC.dmc_set_can_state((ushort)maincard_id, (ushort)card_id, 1, 0); if (ret != 0) { VAR.msg.AddMsg(Msg.EM_MSGTYPE.ERR, string.Format("{0}重新连接失败,请确认卡连接是否正确!Err:0x{1:X8}", disc, ret)); return(EM_RES.ERR); } } isInit = true; handle = (IntPtr)1; VAR.msg.AddMsg(Msg.EM_MSGTYPE.SYS, string.Format("{0}初始化成功!", disc)); return(EM_RES.OK); } #if LEADSHINE_IO else if (type == TYPE.IO) { ret = IOC0640.ioc_board_init(); if (ret > 0) { isInit = true; handle = (IntPtr)1; VAR.msg.AddMsg(Msg.EM_MSGTYPE.SYS, string.Format("{0}初始化成功!", disc)); return(EM_RES.OK); } else { IOC0640.ioc_board_close(); isInit = false; handle = (IntPtr)0; VAR.msg.AddMsg(Msg.EM_MSGTYPE.ERR, string.Format("{0}初始化失败!Err:0x{1:X8}", disc, ret)); return(EM_RES.ERR); } } #endif } #endif #if ZMOTION if (brand == BRAND.ZMOTION) { //check ip if (ip.Length < 7) { isInit = false; handle = (IntPtr)0; VAR.msg.AddMsg(Msg.EM_MSGTYPE.SYS, string.Format("{0} IP异常,{1}", disc, ip)); return(EM_RES.CAM_PARA_ERR); } VAR.msg.AddMsg(Msg.EM_MSGTYPE.SYS, string.Format("{0} 连接...", disc)); ////search and open,多网卡时,搜索异常 //for (int n = 0; n < 10; n++) //{ // ret = zmcaux.ZAux_SearchEth(ip, 100); // if (ret == 0) break; // Application.DoEvents(); //} ret = 0; if (ret == 0) { ret = zmcaux.ZAux_OpenEth(ip, out handle); if (ret != 0) { isInit = false; handle = (IntPtr)0; VAR.msg.AddMsg(Msg.EM_MSGTYPE.ERR, string.Format("{0}初始化失败,IP={1},Err:{2}", disc, ip, ret)); return(EM_RES.ERR); } else { //check param int ftemp = 0; bool bdownload = false; foreach (AXIS ax in AxList) { zmcaux.ZAux_Direct_GetMaxSpeed(handle, ax.num, ref ftemp); if (Math.Abs((ax.max_spd * ax.pul_per_mm) - ftemp) > 100) { bdownload = true; break; } } //download config if (bdownload) { res = DownLoadFile(filename); if (res != EM_RES.OK) { return(res); } } isInit = true; VAR.msg.AddMsg(Msg.EM_MSGTYPE.SYS, string.Format("{0}初始化成功!", disc)); foreach (AXIS ax in AxList) { ax.Init(); } return(EM_RES.OK); } } else { isInit = false; handle = (IntPtr)0; VAR.msg.AddMsg(Msg.EM_MSGTYPE.ERR, string.Format("{0}找不到,IP={1}", disc, ip)); return(EM_RES.ERR); } } #endif #if ADVANTTECH if (brand == BRAND.ADVANTTECH) { isInit = false; VAR.msg.AddMsg(Msg.EM_MSGTYPE.ERR, "研华库未添加!"); return(EM_RES.ERR); } #endif #if ORIENTALMOTOR if (brand == BRAND.ORIENTALMOTOR) { //get config if (filename.Length < 3) { filename = Path.GetFullPath("..") + "\\syscfg\\ORIENTALMOTOR.ini"; } if (File.Exists(filename)) { IniFile inf = new IniFile(filename); int temp = inf.ReadInteger("COM", "PORT", -1); if (temp >= 0 && temp < 100) { PortName = string.Format("COM{0}", temp); } temp = inf.ReadInteger("COM", "BAUD", -1); if (temp > 0) { Baudrate = temp; } } if (0 == AZD.Motor.InitRs485(PortName, Baudrate)) { isInit = true; handle = (IntPtr)1; VAR.msg.AddMsg(Msg.EM_MSGTYPE.SYS, string.Format("{0}初始化成功!", disc)); foreach (AXIS ax in AxList) { ax.Init(); } return(EM_RES.OK); } else { VAR.msg.AddMsg(Msg.EM_MSGTYPE.ERR, string.Format("InitRs485异常,BRAND={0},TYPE={1},Port={2}/{3}", brand, type, PortName, Baudrate)); return(EM_RES.ERR); } } #endif VAR.msg.AddMsg(Msg.EM_MSGTYPE.ERR, string.Format("未定义异常,BRAND={0},TYPE={1}", brand, type)); return(EM_RES.ERR); }