Ejemplo n.º 1
0
Archivo: DDI_TEST.cs Proyecto: x893/WDS
 public int flushRead(DeviceHandle handle, DataBuffer data)
 {
     if (handle.isInvalid())
     {
         GlobalServices.ErrMsg("DDI_TEST.flushRead()", "called with INVALID handle");
         return -1;
     }
     data.copy(handle.TestDevHnd.readBuff);
     handle.TestDevHnd.readBuff.NumItems = 0;
     handle.TestDevHnd.readBuffIdx = 0;
     return data.NumItems;
 }
Ejemplo n.º 2
0
        private FDResult WriteFirmwareToDevice(ArrayList firmware, UpdateFirmwareCallback callback)
        {
            FDResult oK = FDResult.OK;
            try
            {
                if (_adtRecord.PortAddress.comAddr.COMPortId <= 0)
                {
                    callback(0, true, "The com port: " + _adtRecord.PortAddress.comAddr.COMPortId.ToString() + " is not valid.", MessageBoxButtons.OK);
                    return FDResult.FatalError;
                }
                _ddi.enablePollerRun(false);
                Thread.Sleep(300);
                bool flag = false;

                do
                {
                    flag = false;
                    try
                    {
                        int num;
                        _log.Info("==== WriteFirmwareToDevice ==== ");
                        if (_adtRecord.DeviceInfo.DeviceType.TypeID != DeviceTypeID.SRW002)
                        {
                            num = _ddi.close(_adtRecord.PortAddress.handle);
                            _adtRecord.PortAddress.comAddr.baudRate = 19200;
                            _adtRecord.PortAddress.handle = _ddi.open(_adtRecord, 'E');
                        }
                        if (!SendRequest("bl\r"))
                        {
                            oK = FDResult.FatalError;
                            throw new Exception("Error while writing command to change to boot loader mode of the device.");
                        }
                        Thread.Sleep(100);
                        num = _ddi.close(_adtRecord.PortAddress.handle);
                        _adtRecord.PortAddress.handle = null;
                        if (num != 0)
                        {
                            oK = FDResult.Error;
                            throw new Exception("Error while closing serial port.");
                        }
                        _adtRecord.PortAddress.comAddr.baudRate = 115200;
                        _adtRecord.PortAddress.handle = _ddi.open(_adtRecord, 'E');
                        if (_adtRecord.PortAddress.handle == null)
                        {
                            oK = FDResult.FatalError;
                            throw new Exception("The serial port could not be opened in high speed mode.");
                        }
                        Thread.Sleep(100);
                        _log.Info("in bl mode: J cmd");
                        if (!SendRequest("J\r"))
                        {
                            oK = FDResult.FatalError;
                            throw new Exception("Error while writing command to change the communication speed on the device.");
                        }
                        if (WaitForResponse("BLD", "V1.10W"))
                        {
                            oK = FDResult.FatalError;
                            throw new Exception("Error while waiting for response to Get Bootloader revision.");
                        }
                        string response = respBuff.objectInfo(false);
                        _log.Info("in bl mode: response of 'J':" + response);
                        if (response.Contains("SRW"))
                        {
                            string[] strArray = response.Split(new char[] { ',' });
                            string text1 = strArray[0];
                            if (strArray[3].Substring(0, 3) != "BLD")
                            {
                                oK = FDResult.FatalError;
                                throw new Exception("HW is not in Bootloader mode");
                            }
                            if (strArray[2] != "01.2r")
                            {
                                oK = FDResult.FatalError;
                                throw new Exception("Bootloader revision mismatch.");
                            }
                        }
                        response = "";
                        _log.Info("in bl mode: Na5f1H cmd");
                        if (!SendRequest("Na5f1H"))
                        {
                            oK = FDResult.Error;
                            throw new Exception("Error while writing command to Set Key Sequence on the device.");
                        }
                        if (WaitForResponse("E09", null))
                        {
                            oK = FDResult.FatalError;
                            throw new Exception("Error while waiting for response to Set Key Sequence on the device.");
                        }
                        _log.Info("in bl mode: K cmd");
                        if (!SendRequest("K"))
                        {
                            oK = FDResult.Error;
                            throw new Exception("Error while writing command to Set Key Sequence on the device.");
                        }
                        if (WaitForResponse("EA\r\nOK\r\n>", null, 50))
                        {
                            oK = FDResult.FatalError;
                            throw new Exception("Error while waiting for response to Set Key Sequence on the device.");
                        }
                        _log.Info("in bl mode: download loop");
                        for (int i = 0; i < firmware.Count; i++)
                        {
                            string fw_cmd = (string)firmware[i];
                            DataBuffer data = new DataBuffer(fw_cmd.Length);
                            data.copy(fw_cmd);
                            Thread.Sleep(1);
                            if (_ddi.write(_adtRecord.PortAddress.handle, data) != 0)
                            {
                                oK = FDResult.FatalError;
                                throw new Exception("Error while writing firmware command.");
                            }
                            callback((int)Math.Round((double)((((double)(i + 1)) / ((double)firmware.Count)) * 100.0), 0), false, "", MessageBoxButtons.OK);
                        }
                        _log.Info("in bl mode: end of download loop");
                        if (WaitForResponse("E09", null))
                        {
                            oK = FDResult.FatalError;
                            throw new Exception("Error while waiting for response to Hex download.");
                        }
                        _log.Info("in bl mode: L mcd");
                        if (!SendRequest("L"))
                        {
                            oK = FDResult.FatalError;
                            throw new Exception("Error while checking image integrity.");
                        }
                        if (WaitForResponse("00000000", null))
                        {
                            oK = FDResult.FatalError;
                            throw new Exception("Error while waiting for response to check image integrity.");
                        }
                        _log.Info("in bl mode: M cmd");
                        if (!SendRequest("M"))
                        {
                            oK = FDResult.Error;
                            throw new Exception("Error while starting application.");
                        }
                        _log.Info("==== WriteFirmwareToDevice has finished ====");
                        Thread.Sleep(1000);
                    }
                    catch (Exception exception)
                    {
                        _log.Info("in bl mode: error occured:" + exception.Message);
                        flag = false;
                        switch (callback(0, true, "There was an error while writing the firmware to the device.\r\n" + exception.Message, MessageBoxButtons.RetryCancel))
                        {
                            case DialogResult.Cancel:
                                flag = false;
                                break;
                            case DialogResult.Retry:
                                flag = true;
                                break;
                            default:
                                flag = false;
                                break;
                        }
                    }
                } while (flag);
            }
            catch (Exception exception2)
            {
                callback(0, true, "There was a fatal error while writing the firmware to the device.\r\n" + exception2.Message, MessageBoxButtons.OK);
            }
            finally
            {
                _ddi.close(_adtRecord.PortAddress.handle);
                _adtRecord.PortAddress.comAddr.baudRate = 19200;
                _adtRecord.PortAddress.handle = _ddi.open(_adtRecord, 'D');
                _ddi.enablePollerRun(true);
            }
            return oK;
        }
Ejemplo n.º 3
0
 private bool SendRequest(string request, int sliptime)
 {
     DataBuffer data = new DataBuffer(1);
     byte[] buf = new byte[2];
     Thread.Sleep(sliptime);
     for (int i = 0; i < request.Length; i++)
     {
         buf[0] = (byte) request[i];
         data.copy(buf, 1);
         Thread.Sleep(sliptime);
         if (_ddi.write(_adtRecord.PortAddress.handle, data) != 0)
             return false;
     }
     return true;
 }
Ejemplo n.º 4
0
        private void execReceiveCommandFromDevice()
        {
            int initialTimeout = int.Parse(ConfigurationManager.AppSettings["DCP2IndicationTimeout"]);
            Interlocked.Increment(ref _adtRec.IsDeviceBusy);
            lock (_adtRec.DevSyncObj)
            {
                DCPRetCodes codes = _dcpProtocolEngine.doRxDCP(initialTimeout, _DCPRXMsg);
                if ((codes != DCPRetCodes.DCP_ERR_TIMEOUT) || (codes != DCPRetCodes.DCP_TERMINATED))
                {
                    TelegramType deviceFailure;
                    switch (codes)
                    {
                        case DCPRetCodes.DCP_OK:
                        {
                            deviceFailure = TelegramType.PacketRecERROR;
                            if (_DCPRXMsg.MsgType == 0)
                            {
                                deviceFailure = TelegramType.PacketRecOK;
                            }
                            _dcpProtocolEngine.AnswerToDCPMessage(new DataBuffer(), MessageTypes.ACK, (CommandSet)_teleFromWin.DCPCmdSet, _DCPTXBuf, _DCPRXMsg);
                            sendTelegramToMainWin(deviceFailure, null, _DCPRXMsg);
                            DataBuffer buffer = new DataBuffer(4);
                            buffer.copy(new byte[2]);
                            _dcpProtocolEngine.exchangeDCPMessage(buffer, MessageTypes.SynchReqOrIndication, (CommandSet)_teleFromWin.DCPCmdSet, _DCPTXMsg, _DCPAnswerMsg);
                            goto Label_014F;
                        }
                        case DCPRetCodes.DCP_FATAL:
                            deviceFailure = TelegramType.DeviceFailure;
                            sendTelegramToMainWin(deviceFailure, null);
                            goto Label_014F;

                        case DCPRetCodes.DCP_ERR_TIMEOUT:
                            goto Label_014F;
                    }
                    deviceFailure = TelegramType.DeviceFailure;
                    sendTelegramToMainWin(deviceFailure, null);
                }
            Label_014F:;
            }
            Interlocked.Decrement(ref _adtRec.IsDeviceBusy);
        }
Ejemplo n.º 5
0
        private DeviceDescriptor queryDeviceInfo(DeviceHandle handle, DeviceDescriptor devInfo)
        {
            DataBuffer data = new DataBuffer(128);
            int writeRetryCnt = int.Parse(ConfigurationManager.AppSettings["WriteRetryCnt"]);
            int pollDeviceTypeProcDelay = int.Parse(ConfigurationManager.AppSettings["PollDeviceTypeProcDelay"]);
            int readRetryCnt = int.Parse(ConfigurationManager.AppSettings["ReadRetryCnt"]);
            if (handle.isInvalid())
            {
                _log.Error("QueryDeviceInfo() - Called with INVALID argument");
                return devInfo;
            }
            data.copy("\r");
            write(handle, data);
            Thread.Sleep(pollDeviceTypeProcDelay);
            flushRead(handle, data);
            while (writeRetryCnt > 0)
            {
                if (devInfo.BootloaderMode)
                {
                    data.copy("J");
                    _log.Debug("queryDeviceInfo():: 'J'");
                }
                else
                {
                    data.copy("I\r");
                    _log.Debug("queryDeviceInfo():: 'I\r'");
                }
                if (write(handle, data) < 0)
                {
                    _log.Warn("Device was probably disconnected. write() returned with -1");
                    return devInfo;
                }

                Thread.Sleep(pollDeviceTypeProcDelay);
                data.NumItems = 0;
                for (int i = readRetryCnt; i > 0; i--)
                {
                    if (readAppend(handle, data, data.Capacity - data.NumItems) < 0)
                    {
                        _log.Warn("Device was probably disconnected.  readAppend() returned with -1");
                        return devInfo;
                    }
                    if (data.endsWith("OK\n\r>"))
                        break;
                    Thread.Sleep(20);
                }

                string response = data.BufToStr;
                _log.Info("queryDeviceInfo():: response:" + response);
                try
                {
                    if (response.Length < 10)
                        return devInfo;

                    string strTmp = response.Substring(0, 10);
                    if (response.Contains("SRW"))
                    {
                        #region SRW...

                        string[] strArray = response.Remove(0, response.IndexOf("SRW")).Split(new char[] { ',' });
                        string subType = strArray[3].Substring(0, 3).Trim();
                        switch (strArray[0].Trim())
                        {
                            case "SRW001":
                                devInfo.DeviceType = new DeviceType(DeviceTypeID.SDBC_8);
                                switch (subType)
                                {
                                    case "NW2":
                                        devInfo.FirmwareType = new FirmwareType("ENW_Master");
                                        break;

                                    case "BLD":
                                        devInfo.FirmwareType = new FirmwareType("Bootloader");
                                        break;

                                    case "PED":
                                        devInfo.FirmwareType = new FirmwareType("PER_Demo");
                                        break;
                                }
                                devInfo.DevicePhoto = SDBC.SDBC_8;
                                devInfo.MarketingName = SDBC.MarketingName;
                                devInfo.UnAssignedDeviceText = SDBC.UnAssignedDeviceText;
                                devInfo.SwitchPhoto = SDBC.SDBC_switch;
                                devInfo.WarningText = SDBC.WarningText;
                                devInfo.PartNumber = SDBC.PartNumber;
                                break;

                            case "SRW002":
                                devInfo.DeviceType = new DeviceType(DeviceTypeID.SRW002);
                                if (subType != null && subType == "LBF")
                                    devInfo.FirmwareType = new FirmwareType("LBF");
                                devInfo.DevicePhoto = SRW002.SRW002_400;
                                devInfo.MarketingName = SRW002.MarketingName;
                                devInfo.UnAssignedDeviceText = SRW002.UnAssignedDeviceText;
                                devInfo.SwitchPhoto = SRW002.SRW002_400;
                                devInfo.WarningText = SRW002.WarningText;
                                devInfo.PartNumber = SRW002.PartNumber;
                                break;

                            case "SRW003":
                                devInfo.DeviceType = new DeviceType(DeviceTypeID.EZLink);
                                switch (subType)
                                {
                                    case "NW1":
                                        devInfo.FirmwareType = new FirmwareType("NWD_Slave");
                                        break;

                                    case "NW2":
                                        devInfo.FirmwareType = new FirmwareType("ENW_Slave");
                                        break;

                                    case "BLD":
                                        devInfo.FirmwareType = new FirmwareType("Bootloader");
                                        break;

                                    case "ED":
                                        devInfo.FirmwareType = new FirmwareType("EZLink");
                                        break;
                                }
                                devInfo.DevicePhoto = EzLink.EZLink_D_EP;
                                devInfo.MarketingName = EzLink.MarketingName;
                                devInfo.UnAssignedDeviceText = EzLink.UnAssignedDeviceText;
                                devInfo.SwitchPhoto = EzLink.EZLink_switch;
                                devInfo.WarningText = EzLink.WarningText;
                                devInfo.PartNumber = EzLink.PartNumber;
                                break;

                            case "SRW004":
                                devInfo.DeviceType = new DeviceType(DeviceTypeID.SRW004);
                                switch (subType)
                                {
                                    case "LBL":
                                        devInfo.FirmwareType = new FirmwareType("LoadBoard_Lite");
                                        break;

                                    case "BLD":
                                        devInfo.FirmwareType = new FirmwareType("Bootloader");
                                        break;
                                }
                                devInfo.DevicePhoto = SRW004.Si1000DK;
                                devInfo.MarketingName = SRW004.MarketingName;
                                devInfo.UnAssignedDeviceText = SRW004.UnAssignedDeviceText;
                                devInfo.WarningText = SRW004.WarningText;
                                devInfo.PartNumber = SRW004.PartNumber;
                                break;

                            case "SRW005":
                                devInfo.DeviceType = new DeviceType(DeviceTypeID.EZLink);
                                switch (subType)
                                {
                                    case "NW1":
                                        devInfo.FirmwareType = new FirmwareType("NWD_Slave");
                                        break;

                                    case "NW2":
                                        devInfo.FirmwareType = new FirmwareType("ENW_Slave");
                                        break;

                                    case "BLD":
                                        devInfo.FirmwareType = new FirmwareType("Bootloader");
                                        break;

                                    case "ED":
                                        devInfo.FirmwareType = new FirmwareType("EZLink");
                                        break;
                                }
                                devInfo.DevicePhoto = EzLink.EZLink_D_EP;
                                devInfo.MarketingName = EzLink.MarketingName;
                                devInfo.UnAssignedDeviceText = EzLink.UnAssignedDeviceText;
                                devInfo.SwitchPhoto = EzLink.EZLink_switch;
                                devInfo.WarningText = EzLink.WarningText;
                                devInfo.PartNumber = EzLink.PartNumber;
                                break;

                            case "SRW007":
                                devInfo.DeviceType = new DeviceType(DeviceTypeID.SRW007);
                                switch (subType)
                                {
                                    case "RKD":
                                        devInfo.FirmwareType = new FirmwareType("RKE_Master");
                                        break;

                                    case "BLD":
                                        devInfo.FirmwareType = new FirmwareType("Bootloader");
                                        break;
                                }
                                devInfo.DevicePhoto = SRW007.SRW007_100x84;
                                devInfo.MarketingName = SRW007.MarketingName;
                                devInfo.UnAssignedDeviceText = SRW007.UnAssignedDeviceText;
                                devInfo.SwitchPhoto = SRW007.SRW007_switch;
                                devInfo.WarningText = SRW007.WarningText;
                                devInfo.PartNumber = SRW007.PartNumber;
                                break;

                            case "SRW008":
                                devInfo.DeviceType = new DeviceType(DeviceTypeID.SRW008);
                                switch (subType)
                                {
                                    case "RKD":
                                        devInfo.FirmwareType = new FirmwareType("RKE_Master");
                                        break;

                                    case "BLD":
                                        devInfo.FirmwareType = new FirmwareType("Bootloader");
                                        break;
                                }
                                devInfo.DevicePhoto = SRW008.SRW008_100x84;
                                devInfo.MarketingName = SRW008.MarketingName;
                                devInfo.UnAssignedDeviceText = SRW008.UnAssignedDeviceText;
                                devInfo.SwitchPhoto = SRW008.SRW008_switch;
                                devInfo.WarningText = SRW008.WarningText;
                                devInfo.PartNumber = SRW008.PartNumber;
                                break;

                            case "SRW009":
                                devInfo.DeviceType = new DeviceType(DeviceTypeID.SRW009);
                                switch (subType)
                                {
                                    case "RKD":
                                        devInfo.FirmwareType = new FirmwareType("RKE_Master");
                                        break;

                                    case "BLD":
                                        devInfo.FirmwareType = new FirmwareType("Bootloader");
                                        break;
                                }
                                devInfo.DevicePhoto = SRW009.SRW009_100x84;
                                devInfo.MarketingName = SRW009.MarketingName;
                                devInfo.UnAssignedDeviceText = SRW009.UnAssignedDeviceText;
                                devInfo.SwitchPhoto = SRW009.SRW009_switch;
                                devInfo.WarningText = SRW009.WarningText;
                                devInfo.PartNumber = SRW009.PartNumber;
                                break;

                            default:
                                break;
                        }
                        devInfo.DeviceVersion = new DeviceVersion(strArray[1]);
                        devInfo.FirmwareVersion = new FirmwareVersion(strArray[2]);
                        return devInfo;

                        #endregion
                    }

                    if (strTmp.Contains("SDBC-8"))
                    {
                        #region SDBC-8

                        int idx = strTmp.IndexOf("SDBC-8");
                        //	SDBC-8......VVVVV......FFFFF..PED
                        //	SDBC-8......VVVVV......FFFFF..NWD
                        //            dev.version  fw.version

                        devInfo.DeviceType = new DeviceType(DeviceTypeID.SDBC_8);
                        strTmp = response.Substring(idx + 30, 3);
                        if (strTmp == "NWD")
                        {
                            devInfo.DeviceVersion = new DeviceVersion(response.Substring(idx + 12, 5));
                            devInfo.FirmwareType = new FirmwareType("NWD_Master");
                            devInfo.FirmwareVersion = new FirmwareVersion(response.Substring(idx + 23, 5));
                            devInfo.DevicePhoto = SDBC.SDBC_8;
                            devInfo.SwitchPhoto = SDBC.SDBC_switch;
                            devInfo.WarningText = SDBC.WarningText;
                            return devInfo;
                        }
                        if (strTmp == "PED")
                        {
                            devInfo.DeviceVersion = new DeviceVersion(response.Substring(idx + 12, 5));
                            devInfo.FirmwareType = new FirmwareType("PER_Demo");
                            devInfo.FirmwareVersion = new FirmwareVersion(response.Substring(idx + 23, 5));
                            devInfo.DevicePhoto = SDBC.SDBC_8;
                            devInfo.SwitchPhoto = SDBC.SDBC_switch;
                            devInfo.WarningText = SDBC.WarningText;
                            return devInfo;
                        }

                        #endregion
                    }
                    else if (strTmp.Contains("D-EP"))
                    {
                        #region EZLink
                        int idx = strTmp.IndexOf("D-EP");
                        devInfo.DeviceType = new DeviceType(DeviceTypeID.EZLink);
                        if (response.Substring(idx + 27, 3) == "NWD")
                        {
                            strTmp = response.Substring(idx + 9, 5);
                            devInfo.DeviceVersion = new DeviceVersion(strTmp);
                            devInfo.FirmwareType = new FirmwareType("NWD_Slave");
                            strTmp = response.Substring(idx + 20, 5);
                            devInfo.FirmwareVersion = new FirmwareVersion(strTmp);
                            return devInfo;
                        }
                        #endregion
                    }
                    else
                    {
                        if (response.Contains("V1.10W"))
                        {
                            devInfo.DeviceType = new DeviceType(DeviceTypeID.SDBC_8);
                            devInfo.DeviceVersion = new DeviceVersion("01.1r");
                            devInfo.FirmwareType = new FirmwareType("Bootloader");
                            devInfo.FirmwareVersion = new FirmwareVersion("V1.10W");
                            devInfo.DevicePhoto = SDBC.SDBC_8;
                            devInfo.SwitchPhoto = SDBC.SDBC_switch;
                            devInfo.WarningText = SDBC.WarningText;
                            return devInfo;
                        }
                        if (response.Length >= 26)
                        {
                            #region LoadBoard
                            if (response.Substring(4, 26) == "Wireless Development Suite")
                            {
                                devInfo.DeviceType = new DeviceType(DeviceTypeID.LoadBoard);
                                devInfo.DeviceVersion = new DeviceVersion("02.20");
                                devInfo.FirmwareType = new FirmwareType("LoadBoard");
                                devInfo.FirmwareVersion = new FirmwareVersion(response.Substring(0x23, 4));
                                devInfo.DevicePhoto = LoadBoard.HWBoard_LoadBoard;
                                return devInfo;
                            }
                            if (response.Substring(4, 16) == "LoadBoard Loader")
                            {
                                devInfo.DeviceType = new DeviceType(DeviceTypeID.LoadBoard);
                                devInfo.DeviceVersion = new DeviceVersion("02.20");
                                devInfo.FirmwareType = new FirmwareType("BootLoader");
                                devInfo.FirmwareVersion = new FirmwareVersion(response.Substring(0x17, 4));
                                return devInfo;
                            }
                            #endregion
                        }
                    }
                }
                catch (Exception exception)
                {
                    _log.Error("QueryDeviceInfo() - " + exception.Message);
                    return devInfo;
                }
                writeRetryCnt--;
            }
            return devInfo;
        }
Ejemplo n.º 6
0
        private bool isDeviceAlive(IOPortAddress iopaddr, out bool bootLoaderMode, out DeviceTypeID deviceType)
        {
            string command_CR = "\r";
            DataBuffer data = new DataBuffer(100);
            bootLoaderMode = false;
            deviceType = DeviceTypeID.Unknown;

            if (iopaddr.handle == null)
                return false;
            if (iopaddr.handle.isInvalid())
            {
                _log.Warn("Called with INVALID handle");
                return false;
            }

            int writeRetryCnt = int.Parse(ConfigurationManager.AppSettings["WriteRetryCnt"]);
            int readRetryCnt = int.Parse(ConfigurationManager.AppSettings["ReadRetryCnt"]);

            while (writeRetryCnt > 0)
            {
                data.copy(command_CR);
                if (write(iopaddr.handle, data) < 0)
                {
                    Thread.Sleep(10);
                    writeRetryCnt--;
                }
                else
                {
                    _log.Debug("[CR] was sent");
                    data.NumItems = 0;
                    int readCount = readRetryCnt;
                    while (readCount > 0)
                    {
                        if (readAppend(iopaddr.handle, data, data.Capacity - data.NumItems) < 0)
                            readCount--;
                        else
                        {
                            if (data.endsWith("OK\n\r>"))
                            {
                                _log.Debug("Device looks to be alive");
                                return true;
                            }
                            Thread.Sleep(20);
                            readCount--;
                        }
                    }
                    Thread.Sleep(20);
                    writeRetryCnt--;
                }
            }
            _log.Debug("Device to be checked with RTS control enabled");
            close(iopaddr.handle);

            iopaddr.handle = open(iopaddr, 19200, 'E');
            _log.Debug("Port opened by isDeviceAlive()@19200:" + iopaddr.comAddr.COMPortId + " DTR Enabled");
            writeRetryCnt = int.Parse(ConfigurationManager.AppSettings["WriteRetryCnt"]);
            readRetryCnt = int.Parse(ConfigurationManager.AppSettings["ReadRetryCnt"]);
            while (writeRetryCnt > 0)
            {
                data.copy(command_CR);
                if (write(iopaddr.handle, data) < 0)
                {
                    Thread.Sleep(10);
                    writeRetryCnt--;
                }
                else
                {
                    _log.Debug("isDeviceAlive():: '\r' sent");
                    data.NumItems = 0;
                    int num4 = readRetryCnt;
                    while (num4 > 0)
                    {
                        if (readAppend(iopaddr.handle, data, data.Capacity - data.NumItems) < 0)
                        {
                            Thread.Sleep(10);
                            num4--;
                        }
                        else
                        {
                            _log.Debug("isDeviceAlive():: response to '\r':" + data.BufToStr);
                            if (data.BufToStr.Contains("OK\n\r>"))
                                return true;
                            Thread.Sleep(20);
                            num4--;
                        }
                    }
                    Thread.Sleep(20);
                    writeRetryCnt--;
                }
            }
            close(iopaddr.handle);
            _log.Debug("Port closed by isDeviceAlive()@19200:" + iopaddr.comAddr.COMPortId);

            iopaddr.handle = open(iopaddr, 115200, 'D');
            _log.Debug("Port opened by isDeviceAlive()@115200:" + iopaddr.comAddr.COMPortId);
            for (int i = 0; i < 4; i++)
            {
                data.copy("J");
                if (write(iopaddr.handle, data) < 0)
                    Thread.Sleep(10);
                Thread.Sleep(50);
                _log.Debug("isDeviceAlive():: 'J' sent");
                data.NumItems = 0;
                int readCnt = readRetryCnt;
                while (readCnt > 0)
                {
                    if (readAppend(iopaddr.handle, data, data.Capacity - data.NumItems) < 0)
                    {
                        Thread.Sleep(10);
                        readCnt--;
                    }
                    else
                    {
                        _log.Debug("isDeviceAlive():: response to 'J':" + data.BufToStr);
                        if (data.endsWith("OK\r\n>\r\n"))
                        {
                            deviceType = DeviceTypeID.SDBC_8;
                            bootLoaderMode = true;
                            return true;
                        }
                        Thread.Sleep(20);
                        readCnt--;
                    }
                }
            }

            writeRetryCnt = int.Parse(ConfigurationManager.AppSettings["WriteRetryCnt"]);
            readRetryCnt = int.Parse(ConfigurationManager.AppSettings["ReadRetryCnt"]);
            while (writeRetryCnt > 0)
            {
                data.copy("OK\n\r>");
                if (write(iopaddr.handle, data) < 0)
                {
                    Thread.Sleep(10);
                    writeRetryCnt--;
                }
                else
                {
                    _log.Debug("[CRLF] sent");
                    data.NumItems = 0;
                    int num7 = readRetryCnt;
                    while (num7 > 0)
                    {
                        if (readAppend(iopaddr.handle, data, data.Capacity - data.NumItems) < 0)
                        {
                            Thread.Sleep(10);
                            num7--;
                        }
                        else
                        {
                            _log.Debug("Response to [CRLF]:" + data.BufToStr);
                            if (data.endsWith("OK\n\r>"))
                                return true;
                            Thread.Sleep(20);
                            num7--;
                        }
                    }
                    Thread.Sleep(20);
                    writeRetryCnt--;
                }
            }
            return false;
        }
Ejemplo n.º 7
0
 private bool ExchCmdWithDev(DeviceHandle a_Handle, DataBuffer a_Data)
 {
     int writeRetryCnt = int.Parse(ConfigurationManager.AppSettings["WriteRetryCnt"]);
     DataBuffer data = new DataBuffer(512)
     {
         DType = Data_Type.ASCII
     };
     while (writeRetryCnt > 0)
     {
         data.copy(a_Data);
         int readRetryCnt = int.Parse(ConfigurationManager.AppSettings["ReadRetryCnt"]);
         if (write(a_Handle, data) < 0)
         {
             _log.Warn("Device was probably disconnected.  write() returned with -1");
             return false;
         }
         Thread.Sleep(100);
         data.NumItems = 0;
         while (readRetryCnt > 0)
         {
             if (readAppend(a_Handle, data, data.Capacity - data.NumItems) < 0)
             {
                 _log.Warn("Device was probably disconnected.  readAppend() returned with -1");
                 Thread.Sleep(50);
                 readRetryCnt--;
             }
             else
             {
                 if (data.endsWith("OK\n\r>"))
                 {
                     a_Data.copy(data);
                     return true;
                 }
                 Thread.Sleep(100);
                 readRetryCnt--;
             }
         }
         writeRetryCnt--;
     }
     a_Data.NumItems = 0;
     return false;
 }
Ejemplo n.º 8
0
 public void StopIdentifyDevice(DeviceHandle handle, DeviceDescriptor a_dev)
 {
     DataBuffer data = new DataBuffer(128);
     int pollDeviceTypeProcDelay = int.Parse(ConfigurationManager.AppSettings["PollDeviceTypeProcDelay"]);
     if ((a_dev.DeviceType != null) && (a_dev.DeviceType.TypeID == DeviceTypeID.LoadBoard))
     {
         data.copy("d0\r");
         write(handle, data);
         Thread.Sleep(pollDeviceTypeProcDelay);
     }
 }
Ejemplo n.º 9
0
 public void IdentifyDevice(DeviceHandle handle, DeviceDescriptor a_dev)
 {
     if (a_dev.DeviceType != null && a_dev.DeviceType.TypeID == DeviceTypeID.LoadBoard)
     {
         DataBuffer data = new DataBuffer(128);
         data.copy("d3\r");
         write(handle, data);
         Thread.Sleep(int.Parse(ConfigurationManager.AppSettings["PollDeviceTypeProcDelay"]));
     }
 }
Ejemplo n.º 10
0
        private void queryEBID(DeviceHandle handle, DeviceDescriptor a_dev)
        {
            DataBuffer data = new DataBuffer(512)
            {
                DType = Data_Type.ASCII
            };
            string bufToStr = "";
            if (handle.isInvalid() || a_dev.DeviceType == null)
                _log.Error("queryEBID() - Called with INVALID argument");
            else
            {
                flushRead(handle, data);
                if (a_dev.FirmwareType != null &&
                    (a_dev.FirmwareType.Name == "LoadBoard" || a_dev.FirmwareType.Name == "LoadBoard_Lite")
                    )
                {
                    switch (a_dev.DeviceType.TypeID)
                    {
                        case DeviceTypeID.LoadBoard:
                        case DeviceTypeID.SDBC:
                            data.copy("L6\r");
                            write(handle, data);
                            return;

                        case DeviceTypeID.EZLink:
                        case DeviceTypeID.SRW002:
                        case DeviceTypeID.Unknown:
                            return;

                        case DeviceTypeID.SRW004:
                            data.copy("ged\r");
                            if (ExchCmdWithDev(handle, data) && data.BufToStr.Contains("0x40"))
                            {
                                data.copy("gwd\r");
                                if (ExchCmdWithDev(handle, data))
                                {
                                    bufToStr = data.BufToStr;
                                    try
                                    {
                                        string[] strArray = bufToStr.Split(new char[] { ' ' });
                                        int index = strArray[0].IndexOf("x");
                                        strArray[0] = strArray[0].Remove(0, index + 1);
                                        for (int i = 1; i < strArray.Length; i++)
                                            strArray[i] = strArray[i].Replace("0x", "");

                                        byte[] bytes = new byte[strArray.Length];
                                        for (int j = 0; j < (strArray.Length - 1); j++)
                                            bytes[j] = Convert.ToByte(strArray[j], 16);

                                        string str3 = Encoding.Default.GetString(bytes);
                                        if (str3.Length > 6)
                                        {
                                            switch(str3.Substring(0, 6))
                                            {
                                                case "SI1000":
                                                    a_dev.PnPEBID = new CDTRecord();
                                                    a_dev.PnPEBID.ChipType = "Si1000";
                                                    a_dev.PnPEBID.ChipClass = "Transceiver";
                                                    a_dev.PnPEBID.ChipRevision = "B1";
                                                    break;
                                                case "SI1002":
                                                    a_dev.PnPEBID = new CDTRecord();
                                                    a_dev.PnPEBID.ChipType = "Si1002";
                                                    a_dev.PnPEBID.ChipClass = "Transceiver";
                                                    a_dev.PnPEBID.ChipRevision = "B1";
                                                    break;
                                                case "SI1004":
                                                    a_dev.PnPEBID = new CDTRecord();
                                                    a_dev.PnPEBID.ChipType = "Si1004";
                                                    a_dev.PnPEBID.ChipClass = "Transceiver";
                                                    a_dev.PnPEBID.ChipRevision = "B1";
                                                    break;
                                                case "SI1010":
                                                    a_dev.PnPEBID = new CDTRecord();
                                                    a_dev.PnPEBID.ChipType = "Si1010";
                                                    a_dev.PnPEBID.ChipClass = "Transceiver";
                                                    a_dev.PnPEBID.ChipRevision = "B1";
                                                    break;
                                                case "SI1012":
                                                    a_dev.PnPEBID = new CDTRecord();
                                                    a_dev.PnPEBID.ChipType = "Si1012";
                                                    a_dev.PnPEBID.ChipClass = "Transceiver";
                                                    a_dev.PnPEBID.ChipRevision = "B1";
                                                    break;
                                                case "SI1014":
                                                    a_dev.PnPEBID = new CDTRecord();
                                                    a_dev.PnPEBID.ChipType = "Si1014";
                                                    a_dev.PnPEBID.ChipClass = "Transceiver";
                                                    a_dev.PnPEBID.ChipRevision = "B1";
                                                    break;
                                            }
                                        }
                                    }
                                    catch (FormatException exception)
                                    {
                                        _log.Error("queryEBID()" + exception.Message);
                                    }
                                }
                            }
                            return;
                    }
                }
            }
        }
Ejemplo n.º 11
0
Archivo: DDI_TEST.cs Proyecto: x893/WDS
 public bool isDeviceAlive(DeviceHandle handle)
 {
     DataBuffer data = new DataBuffer("\r".Length + "\n\rOK\n\r>".Length);
     if (handle.isInvalid())
     {
         GlobalServices.ErrMsg("DDI_TEST.isDeviceAlive()", "called with INVALID handle");
         return false;
     }
     data.copy("\r");
     if (this.write(handle, data) < 0)
     {
         return false;
     }
     if (this.read(handle, data, data.Capacity) < 0)
     {
         return false;
     }
     return data.endsWith("\n\rOK\n\r>");
 }
Ejemplo n.º 12
0
Archivo: DDI_HID.cs Proyecto: x893/WDS
 private int readICommand(out string[] replyFields, DeviceHandle handle)
 {
     try
     {
         replyFields = null;
         byte[] buf = new byte[] { 0x69 };
         DataBuffer data = new DataBuffer(0x80);
         data.copy(buf);
         if (this.write(handle, data) != 0)
         {
             throw new Exception("Could not send the command.");
         }
         DataBuffer buffer3 = new DataBuffer(0x80);
         int num = this.readDevice(handle, buffer3);
         if (num <= 0)
         {
             throw new Exception("Could not read the answer.");
         }
         string[] strArray = buffer3.BufToStr.Trim().Split(new char[] { ',' });
         if (strArray.Length < 4)
         {
             throw new Exception("The reply for 'i' request is having too few fields. " + buffer3.BufToStr);
         }
         if ((strArray.Length > 0) && (strArray[0] == "UDP001"))
         {
             if ((strArray.Length > 1) && (strArray[1] == "00.2b"))
             {
                 strArray[1] = "00.2r";
             }
             if ((strArray.Length > 2) && (strArray[2] == "??./b"))
             {
                 strArray[2] = "00.0b";
             }
         }
         replyFields = strArray;
         return num;
     }
     catch (Exception exception)
     {
         _log.Error("Executing 'i' command failed! " + exception.Message);
         replyFields = null;
         return 0;
     }
 }
Ejemplo n.º 13
0
Archivo: DDI_HID.cs Proyecto: x893/WDS
 public int GetFeatureReport(DeviceHandle handle, byte reportID, DataBuffer data)
 {
     if ((handle == null) || handle.isInvalid())
     {
         _log.Warn("Called with INVALID handle");
         return -1;
     }
     try
     {
         string str = "";
         byte[] buffer = new byte[handle.HIDhnd.hnd.GetFeatureReportBufferLength()];
         if ((buffer == null) || (buffer.Length == 0))
         {
             handle.HIDhnd.hnd.IsOpened();
         }
         buffer[0] = reportID;
         _log.Debug("HID feature read: " + (str + buffer[0].ToString("x") + ", "));
         if (handle.HIDhnd.hnd.GetFeatureReport_Control(buffer, (uint) buffer.Length) != 0)
         {
             throw new Exception("HID GetFeatureReport failed, ErrorCode = " + Marshal.GetLastWin32Error().ToString());
         }
         data.NumItems = buffer.Length;
         data.copy(buffer);
     }
     catch (Exception exception)
     {
         _log.Error(exception.Message);
         return -1;
     }
     return 0;
 }
Ejemplo n.º 14
0
        private FDResult WriteFirmwareToDevice(ArrayList firmware, UpdateFirmwareCallback callback)
        {
            FDResult oK = FDResult.OK;
            try
            {
                if (this._adtRecord.PortAddress.comAddr.COMPortId <= 0)
                {
                    callback(0, true, "The com port: " + this._adtRecord.PortAddress.comAddr.COMPortId.ToString() + " is not valid.", MessageBoxButtons.OK);
                    return FDResult.FatalError;
                }
                this._ddi.enablePollerRun(false);
                bool flag = false;
            Label_0065:
                flag = false;
                try
                {
                    string str = "C\r";
                    DataBuffer data = new DataBuffer(str.Length);
                    data.copy(str);
                    if (this._ddi.write(this._adtRecord.PortAddress.handle, data) != 0)
                    {
                        oK = FDResult.Error;
                        throw new Exception("Error while writing command to change the communication speed on the device.");
                    }
                    int num = this._ddi.close(this._adtRecord.PortAddress.handle);
                    this._adtRecord.PortAddress.handle = null;
                    if (num != 0)
                    {
                        oK = FDResult.Error;
                        throw new Exception("Error while closing serial port.");
                    }
                    this._adtRecord.PortAddress.comAddr.baudRate = 0xe100;
                    this._adtRecord.PortAddress.handle = this._ddi.open(this._adtRecord, 'D');
                    if (this._adtRecord.PortAddress.handle == null)
                    {
                        oK = FDResult.FatalError;
                        throw new Exception("The serial port could not be opened in high speed mode.");
                    }
                    this.inputBuffer.Clear();
                    bool flag2 = true;
                    for (int i = 0; i < firmware.Count; i++)
                    {
                        string str2 = ((string) firmware[i]) + "\r\n";
                        if (flag2)
                        {
                            str2 = "L" + str2;
                            flag2 = false;
                        }
                        data = new DataBuffer(str.Length);
                        data.copy(str2);
                        if (this._ddi.write(this._adtRecord.PortAddress.handle, data) != 0)
                        {
                            oK = FDResult.FatalError;
                            throw new Exception("Error while writing firmware command.");
                        }
                        if ((i + 1) < firmware.Count)
                        {
                            lock (this)
                            {
                                this.inputBuffer.Clear();
                            }
                            if (this.WaitForResult(ResultType.XOFF))
                            {
                                oK = FDResult.FatalError;
                                throw new Exception("Error while waiting for XOFF while writing the file.");
                            }
                            if (this.WaitForResult(ResultType.XON))
                            {
                                oK = FDResult.FatalError;
                                throw new Exception("Error while waiting for XON while writing the file.");
                            }
                        }
                        callback((int) Math.Round((double) ((((double) (i + 1)) / ((double) firmware.Count)) * 100.0), 0), false, "", MessageBoxButtons.OK);
                    }
                    if (this._adtRecord.PortAddress.handle != null)
                    {
                        this._ddi.close(this._adtRecord.PortAddress.handle);
                        this._adtRecord.PortAddress.handle = null;
                        this._adtRecord.PortAddress.comAddr.baudRate = 0x4b00;
                        this._adtRecord.PortAddress.handle = this._ddi.open(this._adtRecord, 'D');
                    }
                }
                catch (Exception exception)
                {
                    switch (callback(0, true, "There was an error while writing the firmware to the device.\r\n" + exception.Message, MessageBoxButtons.RetryCancel))
                    {
                        case DialogResult.Cancel:
                            flag = false;
                            goto Label_0339;

                        case DialogResult.Retry:
                            flag = true;
                            goto Label_0339;
                    }
                    flag = false;
                }
            Label_0339:
                if (flag)
                {
                    goto Label_0065;
                }
            }
            catch (Exception exception2)
            {
                callback(0, true, "There was a fatal error while writing the firmware to the device.\r\n" + exception2.Message, MessageBoxButtons.OK);
            }
            finally
            {
                this._ddi.enablePollerRun(true);
            }
            return oK;
        }