示例#1
0
        private void btnInit_Click(object sender, EventArgs e)
        {
            //--------------------------------------------------------------------------
            ret = MXIO_CS.MXIO_GetDllVersion();
            tbCommunication.AppendText(string.Format("MXIO_GetDllVersion:{0}.{1}.{2}.{3}{4}", (ret >> 12) & 0xF, (ret >> 8) & 0xF, (ret >> 4) & 0xF, (ret) & 0xF, "\r\n"));

            ret = MXIO_CS.MXIO_GetDllBuildDate();
            tbCommunication.AppendText(string.Format("MXIO_GetDllBuildDate:{0:x}/{1:x}/{2:x}{3}", (ret >> 16), (ret >> 8) & 0xFF, (ret) & 0xFF, "\r\n"));
            //--------------------------------------------------------------------------
            ret = MXIO_CS.MXEIO_Init();
            tbCommunication.AppendText(string.Format("MXEIO_Init return {0}{1}", ret, "\r\n"));
            //--------------------------------------------------------------------------
            //Connect to ioLogik device
            tbCommunication.AppendText(string.Format("MXEIO_E1K_Connect IP={0}, Port={1}, Timeout={2}, Password={3}{4}", ioLogicIP, ioLogicPortNo, ioLogicTimeout, Password, "\r\n"));
            //Create Connection
            CreateIoHandle();
            //Check Connection
            CheckConnection(true);
            //Check the if the webservice for Navision is running
            if (cbService.Checked)
            {
                CheckService(NavServiceServer, NavServiceUserName, NavServicePassword, NavServiceServiceName, true);
            }
            //Get firmware Version
            byte[] bytRevision = new byte[4];
            ret = MXIO_CS.MXIO_ReadFirmwareRevision(hConnection[0], bytRevision);
            MXEIO_Error.CheckErr(ret, "MXIO_ReadFirmwareRevision");
            if (ret == MXIO_CS.MXIO_OK)
            {
                tbCommunication.AppendText(string.Format("MXIO_ReadFirmwareRevision:V{0}.{1}, Release:{2}, build:{3}{4}", bytRevision[0], bytRevision[1], bytRevision[2], bytRevision[3], "\r\n"));
            }
            //--------------------------------------------------------------------------
            //Get firmware Release Date
            UInt16[] wGetFirmwareDate = new UInt16[2];
            ret = MXIO_CS.MXIO_ReadFirmwareDate(hConnection[0], wGetFirmwareDate);
            MXEIO_Error.CheckErr(ret, "MXIO_ReadFirmwareDate");
            if (ret == MXIO_CS.MXIO_OK)
            {
                tbCommunication.AppendText(string.Format("MXIO_ReadFirmwareDate:{0:x}/{1:x}/{2:x}{3}", wGetFirmwareDate[1], (wGetFirmwareDate[0] >> 8) & 0xFF, (wGetFirmwareDate[0]) & 0xFF, "\r\n"));
            }
            //--------------------------------------------------------------------------
            //Get Module Type
            UInt16[] wModuleType = new UInt16[1];
            ret = MXIO_CS.MXIO_GetModuleType(hConnection[0], 0, wModuleType);
            MXEIO_Error.CheckErr(ret, "MXIO_GetModuleType");
            if (ret == MXIO_CS.MXIO_OK)
            {
                tbCommunication.AppendText(string.Format("MXIO_GetModuleType: Module Type = {0:x}{1}", wModuleType[0], "\r\n"));
            }
            //--------------------------------------------------------------------------

            byte safebytCount        = 8;
            byte safebytStartChannel = 0;

            //Set Power On value = OFF
            UInt32 dwSetDOPowerOnValue = 0;

            ret = MXIO_CS.E1K_DO_SetPowerOnValues(hConnection[0], safebytStartChannel, safebytCount, dwSetDOPowerOnValue);
            MXEIO_Error.CheckErr(ret, "E1K_DO_GetPowerOnValues");
            if (ret == MXIO_CS.MXIO_OK)
            {
                tbCommunication.AppendText(string.Format("E1K_DO_SetPowerOnValues Set Ch{0}~ch{1} DO Direction DO Mode Power On value = OFF success.{2}", bytStartChannel, safebytCount + bytStartChannel - 1, "\r\n"));
            }

            //Get Ch{0}~ch{1} DO Direction DO Mode value
            UInt32[] dwGetDOValue = new UInt32[1];
            ret = MXIO_CS.E1K_DO_Reads(hConnection[0], safebytStartChannel, safebytCount, dwGetDOValue);
            MXEIO_Error.CheckErr(ret, "E1K_DO_Reads");
            if (ret == MXIO_CS.MXIO_OK)
            {
                tbCommunication.AppendText(string.Format("E1K_DO_Reads Get Ch{0}~ch{1} DO Direction DO Mode value success.{2}", bytStartChannel, bytCount + bytStartChannel - 1, "\r\n"));
                for (int i = 0, dwShiftValue = 0; i < safebytCount; i++, dwShiftValue++)
                {
                    tbCommunication.AppendText(string.Format("DO value: ch[{0}] = {1}{2}", i + safebytStartChannel, ((dwGetDOValue[0] & (1 << dwShiftValue)) == 0) ? "OFF" : "ON", "\r\n"));
                }
            }

            UInt16[] wDoSafeValue = new UInt16[8] {
                0, 0, 0, 0, 0, 0, 0, 0
            };

            ret = MXIO_CS.E1K_DO_SetSafeValues_W(hConnection[0], safebytStartChannel, safebytCount, wDoSafeValue);
            MXEIO_Error.CheckErr(ret, "E1K_DO_SetSafeValues_W");
            if (ret == MXIO_CS.MXIO_OK)
            {
                tbCommunication.AppendText(string.Format("E1K_DO_SetSafeValues_W ch{0}~ch{1} success.{2}", safebytStartChannel, safebytStartChannel + safebytCount - 1, "\r\n"));
            }
            //Get Ch XX DO Direction DO Mode safe values
            UInt16[] wGetDOSafeValue = new UInt16[8];
            ret = MXIO_CS.E1K_DO_GetSafeValues_W(hConnection[0], safebytStartChannel, safebytCount, wGetDOSafeValue);
            MXEIO_Error.CheckErr(ret, "E1K_DO_GetSafeValues_W");
            if (ret == MXIO_CS.MXIO_OK)
            {
                tbCommunication.AppendText(string.Format("E1K_DO_GetSafeValues_W Get Ch{0}~ch{1} DO Direction DO Mode DO Safe Value success.", safebytStartChannel, safebytStartChannel + safebytCount - 1, "\r\n"));
                for (int i = 0; i < safebytCount; i++)
                {
                    switch (wGetDOSafeValue[i])
                    {
                    case 0:
                        tbCommunication.AppendText(string.Format("DO Safe value: ch[{0}] = {1}{2}", i, "OFF", "\r\n"));
                        break;

                    case 1:
                        tbCommunication.AppendText(string.Format("DO Safe value: ch[{0}] = {1}{2}", i, "ON", "\r\n"));
                        break;

                    case 2:
                        tbCommunication.AppendText(string.Format("DO Safe value: ch[{0}] = {1}{2}", i, "Hold Last", "\r\n"));
                        break;
                    }
                }
            }
            //  ---------------------------------------  DI setup -------------------------------------------------

            //Set Ch0~ch3 DI Direction DI Mode
            for (int i = 0; i < bytCount; i++)
            {
                wSetDI_DIMode[i] = DI_DIRECTION_DI_MODE;
            }

            ret = MXIO_CS.E1K_DI_SetModes(hConnection[0], bytStartChannel, bytCount, wSetDI_DIMode);
            MXEIO_Error.CheckErr(ret, "E1K_DI_SetModes");
            if (ret == MXIO_CS.MXIO_OK)
            {
                tbCommunication.AppendText(string.Format("E1K_DI_SetModes Set Ch{0} ~ Ch{1} DI Direction DI Mode Succcess.{2}", bytStartChannel, bytCount - 1, "\r\n"));
            }
            //Get Ch0~ch3 DI Direction Mode
            ret = MXIO_CS.E1K_DI_GetModes(hConnection[0], bytStartChannel, bytCount, wGetDIMode);
            MXEIO_Error.CheckErr(ret, "E1K_DI_GetModes");
            if (ret == MXIO_CS.MXIO_OK)
            {
                tbCommunication.AppendText(string.Format("E1K_DI_GetModes Get Ch{0}~ch{1} DI Direction Mode success.", bytStartChannel, bytCount + bytStartChannel - 1));
                tbCommunication.AppendText("\r\n");
                for (int i = 0; i < bytCount; i++)
                {
                    tbCommunication.AppendText(string.Format("ch{0}={1}", i + bytStartChannel, (wGetDIMode[i] == wDI_DI_MODE) ? "DI_MODE" : "COUNT_MODE"));
                    tbCommunication.AppendText("\r\n");
                }
            }
            //*******************
            // Set/Get DI filter
            //*******************
            //Set Ch0~ch3 DI Direction Filter
            UInt16[] wFilter = new UInt16[8];
            for (int i = 0; i < safebytCount; i++)
            {
                wFilter[i] = (UInt16)(DI_SignalFilter);
            }
            ret = MXIO_CS.E1K_DI_SetFilters(hConnection[0], safebytStartChannel, safebytCount, wFilter);
            MXEIO_Error.CheckErr(ret, "E1K_DI_SetFilters");
            if (ret == MXIO_CS.MXIO_OK)
            {
                tbCommunication.AppendText(string.Format("E1K_DI_SetFilters Set Ch{0}~ch{1} DI Direction Filter to {2} return {3}{4}", safebytStartChannel, safebytCount + safebytStartChannel - 1, DI_SignalFilter, ret, "\r\n"));
            }

            //Get Ch0~ch3 DI Direction Filter
            ret = MXIO_CS.E1K_DI_GetFilters(hConnection[0], safebytStartChannel, safebytCount, wFilter);
            MXEIO_Error.CheckErr(ret, "E1K_DI_GetFilters");
            if (ret == MXIO_CS.MXIO_OK)
            {
                tbCommunication.AppendText(string.Format("E1K_DI_GetFilters Get Ch{0}~ch{1} DI Direction Filter return {2}", bytStartChannel, bytCount + bytStartChannel - 1, ret));
                tbCommunication.AppendText("\r\n");
                for (int i = 0; i < safebytCount; i++)
                {
                    tbCommunication.AppendText(string.Format("DI Filter Value: ch[{0}] = {1}{2}", i, wFilter[i], "\r\n"));
                }
            }
        }