Exemplo n.º 1
0
        /// <summary>
        /// 关闭设备
        /// </summary>
        public int CloseDevice()
        {
            lock (asyncLocker)
            {
                if (!IsDeviceOpen)
                {
                    return((int)ErrorDef.Success);
                }
                int errCode = opt.TurnOffChannel(0);//关闭所有通道
                if (OPTControllerAPI.OPT_SUCCEED != errCode)
                {
                    throw new Exception("Close all channel failed when close device");
                }
                for (int i = 0; i < LightChannelCount; i++)
                {
                    chnEnables[i] = false;
                }
                if (linkedMode == linkModeParamRange[0] as string) //Linked by serialport
                {
                    errCode = opt.ReleaseSerialPort();
                }
                else //if (linkedMode == 1 || linkedMode == 2) //Linked by IP or SN
                {
                    errCode = opt.DestoryEtheConnect();
                }
                if (errCode != OPTControllerAPI.OPT_SUCCEED)
                {
                    return((int)ErrorDef.InvokeFailed);
                }

                IsDeviceOpen = false;
                opt          = null;
                return((int)ErrorDef.Success);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 打开设备
        /// </summary>
        public int OpenDevice()
        {
            if (!IsInitOK)
            {
                return((int)ErrorDef.InitFailedWhenOpen);
            }
            lock (asyncLocker)
            {
                if (IsDeviceOpen)
                {
                    if (linkedMode == linkMode && linkedParam == linkParam)
                    {
                        return((int)ErrorDef.Success);
                    }
                    CloseDevice();
                }
                opt = new OPTControllerAPI();
                int errCode = 0;
                if (linkMode == linkModeParamRange[0] as string)
                {
                    errCode = opt.InitSerialPort(linkedParam);
                }
                else if (linkMode == linkModeParamRange[1] as string)
                {
                    errCode = opt.CreateEtheConnectionByIP(linkParam);
                }
                else// if(linkMode == linkModeParamRange[2] as string)
                {
                    errCode = opt.CreateEtheConnectionBySN(linkParam);
                }

                if (errCode != OPTControllerAPI.OPT_SUCCEED)
                {
                    //关闭打开的设备
                    if (linkMode == linkModeParamRange[0] as string)
                    {
                        opt.ReleaseSerialPort();
                    }
                    else// if (linkMode == linkModeParamRange[1] as string)
                    {
                        opt.DestoryEtheConnect();
                    }
                    //else// if(linkMode == linkModeParamRange[2] as string)
                    //    errCode = opt.CreateEtheConnectionBySN(linkParam);
                    return((int)ErrorDef.InvokeFailed);
                }
                errCode = opt.TurnOffChannel(0);//关闭所有通道
                if (OPTControllerAPI.OPT_SUCCEED != errCode)
                {
                    if (linkMode == linkModeParamRange[0] as string)
                    {
                        opt.ReleaseSerialPort();
                    }
                    else// if (linkMode == linkModeParamRange[1] as string)
                    {
                        opt.DestoryEtheConnect();
                    }
                    //else// if(linkMode == linkModeParamRange[2] as string)
                    //    errCode = opt.CreateEtheConnectionBySN(linkParam);
                    return((int)ErrorDef.InvokeFailed);
                }
                for (int i = 0; i < LightChannelCount; i++)
                {
                    chnEnables[i] = false;
                }

                //errCode = SetWorkMode(0);//将控制器设为开关模式
                errCode = opt.SetWorkMode(0);
                if (errCode != OPTControllerAPI.OPT_SUCCEED)
                {
                    if (linkMode == linkModeParamRange[0] as string)
                    {
                        opt.ReleaseSerialPort();
                    }
                    else// if (linkMode == linkModeParamRange[1] as string)
                    {
                        opt.DestoryEtheConnect();
                    }
                    //else// if(linkMode == linkModeParamRange[2] as string)
                    //    errCode = opt.CreateEtheConnectionBySN(linkParam);
                    return((int)ErrorDef.InvokeFailed);
                }
                linkedMode   = linkMode;
                linkedParam  = linkParam;
                IsDeviceOpen = true;
                return((int)ErrorDef.Success);
            }
        }