Пример #1
0
        public bool disconnectDevice()
        {
            ReadDone = false;
            try
            {
                if (spt != null)
                {
                    spt.DataRecieved -= new DataRecievedEventHandler(spt_DataRecieved);
                    spt.Dispose();
                    GC.Collect(2);
                }
                spt = null;

                GC.Collect(2);
                return(true);
            }
            catch
            {
                return(false);
            }
        }
Пример #2
0
    public void writeData()
    {
        //byte[] MainForm.eeprom;// no need to allocate this as its read in from file. = new byte[128 * 1024];// whole of the codeplug
        byte[] usbBuf = new byte[160];        // buffer for individual usb transfers

        int             blockLength     = 0;
        int             addr32          = 0;
        int             addr16          = 0;
        int             pageAddr        = 0;
        SpecifiedDevice specifiedDevice = null;

        try
        {
            specifiedDevice = SpecifiedDevice.FindSpecifiedDevice(HID_VID, HID_PID);            //0x152A HID_PID
            if (specifiedDevice == null)
            {
                if (this.OnFirmwareUpdateProgress != null)
                {
                    this.OnFirmwareUpdateProgress(this, new FirmwareUpdateProgressEventArgs(0f, "Device not found", true, true));
                }
            }
            else
            {
                while (true)
                {
                    Array.Clear(usbBuf, 0, usbBuf.Length);
                    specifiedDevice.SendData(CodeplugComms.CMD_PRG);        // Send PROGRA command to initiate comms
                    //Console.WriteLine("Send PRG1");
                    specifiedDevice.ReceiveData(usbBuf);                    // Wait for response
                    if (usbBuf[0] != CodeplugComms.CMD_ACK[0])
                    {
                        break;                                        // Exit if not ack
                    }
                    specifiedDevice.SendData(CodeplugComms.CMD_PRG2); // Send second half of comms init sequence
                    //Console.WriteLine("Send PRG2");
                    Array.Clear(usbBuf, 0, usbBuf.Length);
                    specifiedDevice.ReceiveData(usbBuf);                    // GD77 send back device information
                    byte[] array3 = new byte[8];
                    Buffer.BlockCopy(usbBuf, 0, array3, 0, 8);              // Extract the first 8 bytes from the response
                    {
                        // its the correct model number
                        specifiedDevice.SendData(CodeplugComms.CMD_ACK);            // send ACK
                        Array.Clear(usbBuf, 0, usbBuf.Length);
                        specifiedDevice.ReceiveData(usbBuf);                        // Wait for response (of ACK)

                        if (usbBuf[0] == CodeplugComms.CMD_ACK[0])
                        {
                            //Console.WriteLine("Got ACK");
                            // --------------- removed the password checking
                            blockLength = 32;                            // Max transfer length is 32 bytes
                            int currentPage = 0;
                            int bankSize    = 65536;
                            int numBlocks   = MainForm.transferLength / blockLength;
                            int startBlock  = MainForm.startAddress / blockLength;
                            for (int block = startBlock; block < (startBlock + numBlocks); block++)
                            {
                                //Console.Write("Processing block " + block + " end block " + (startBlock + numBlocks));
                                if (currentPage != (block * blockLength) / bankSize)
                                {
                                    currentPage = (block * blockLength) / bankSize;
                                    addr32      = blockLength * block;
                                    byte[] array4 = new byte[8] {
                                        (byte)'C', (byte)'W', (byte)'B', 4, 0, 0, 0, 0
                                    };
                                    pageAddr  = addr32 >> 16 << 16;
                                    array4[4] = (byte)(pageAddr >> 24);
                                    array4[5] = (byte)(pageAddr >> 16);
                                    array4[6] = (byte)(pageAddr >> 8);
                                    array4[7] = (byte)pageAddr;
                                    //Console.WriteLine("Send address changed to 0x" + pageAddr.ToString("X"));
                                    Array.Clear(usbBuf, 0, usbBuf.Length);
                                    specifiedDevice.SendData(array4, 0, array4.Length);
                                    specifiedDevice.ReceiveData(usbBuf);
                                    if (usbBuf[0] != CodeplugComms.CMD_ACK[0])
                                    {
                                        goto end_IL_02a2;
                                    }
                                }

                                addr16 = (block * blockLength) & 0xffff;
                                // Send request for dcata
                                byte[] data2 = new byte[4 + 32] {
                                    87, (byte)(addr16 >> 8), (byte)addr16, (byte)blockLength,
                                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
                                };

                                Array.Clear(usbBuf, 0, usbBuf.Length);
                                Buffer.BlockCopy(MainForm.eeprom, (block * blockLength), data2, 4, blockLength);
                                specifiedDevice.SendData(data2, 0, 4 + 32);
                                //Console.WriteLine("Send Data to address 0x"+addr16.ToString("X"));

                                specifiedDevice.ReceiveData(usbBuf);
                                if (usbBuf[0] != CodeplugComms.CMD_ACK[0])
                                {
                                    goto end_IL_02a2;
                                }
                                if (this.OnFirmwareUpdateProgress != null)
                                {
                                    this.OnFirmwareUpdateProgress(this, new FirmwareUpdateProgressEventArgs((float)(block + 1 - startBlock) * 100 / (float)numBlocks, "", false, false));
                                }
                            }
                            // SEND END OF WRITE
                            specifiedDevice.SendData(CodeplugComms.CMD_ENDW);
                            specifiedDevice.ReceiveData(usbBuf);
                        }
                        break;
                    }
                    return;

end_IL_02a2:
                    // SEND END OF WRITE
                    specifiedDevice.SendData(CodeplugComms.CMD_ENDW);
                    specifiedDevice.ReceiveData(usbBuf);
                    break;
                }
            }
        }
        catch (TimeoutException ex)
        {
            Console.WriteLine(ex.Message);
            if (this.OnFirmwareUpdateProgress != null)
            {
                this.OnFirmwareUpdateProgress(this, new FirmwareUpdateProgressEventArgs(0f, "Error", false, false));
            }
        }
        finally
        {
            if (specifiedDevice != null)
            {
                specifiedDevice.Dispose();
            }
        }

        if (this.OnFirmwareUpdateProgress != null)
        {
            this.OnFirmwareUpdateProgress(this, new FirmwareUpdateProgressEventArgs(100f, "", false, true));
        }
    }
Пример #3
0
    // Test function added by Roger Clark to read all 1Mb from the external Flash chip on the GD-77.
    // It outputs the data dummp to the file c:\\gd-77_datadump.bin

    // However depending on the mode in which the GD-77 is booted,  addresses 0x000000 - 0x01FFFF  (for codeplug mode)
    // or addresses 0x030000 - 0x03FFFF for DMR-ID mode are returned with anything other than 0x00 in them

    // Its strange that address 0x020000 - 0x02FFFF does not seem to be accessible
    // Note. Valid transfer lengths seem to be 8,16 or 32 bytes.  64 bytes does not work, as it just returns 0x00 in addresses after 32 bytes

    public void readData()
    {
        byte[]          usbBuf          = new byte[160];// buffer for individual transfers
        int             blockLength     = 0;
        int             addr32          = 0;
        int             addr16          = 0;
        int             pageAddr        = 0;
        SpecifiedDevice specifiedDevice = null;

        try
        {
            specifiedDevice = SpecifiedDevice.FindSpecifiedDevice(HID_VID, HID_PID);            //0x152A HID_PID
            if (specifiedDevice == null)
            {
                if (this.OnFirmwareUpdateProgress != null)
                {
                    this.OnFirmwareUpdateProgress(this, new FirmwareUpdateProgressEventArgs(0f, "Device not found", true, true));
                }
            }
            else
            {
                while (true)
                {
                    Array.Clear(usbBuf, 0, usbBuf.Length);
                    specifiedDevice.SendData(CodeplugComms.CMD_PRG);        // Send PROGRA command to initiate comms
                    specifiedDevice.ReceiveData(usbBuf);                    // Wait for response
                    if (usbBuf[0] != CodeplugComms.CMD_ACK[0])
                    {
                        break;                                        // Exit if not ack
                    }
                    specifiedDevice.SendData(CodeplugComms.CMD_PRG2); // Send second half of comms init sequence
                    Array.Clear(usbBuf, 0, usbBuf.Length);
                    specifiedDevice.ReceiveData(usbBuf);              // GD77 send back device information
                    byte[] array3 = new byte[8];
                    Buffer.BlockCopy(usbBuf, 0, array3, 0, 8);        // Extract the first 8 bytes from the response
                    // REMOVED CURRENT MODEL CHECK !!!  if (array3.smethod_4(Settings.CUR_MODEL))
                    {
                        // its the correct model number
                        specifiedDevice.SendData(CodeplugComms.CMD_ACK);            // send ACK
                        Array.Clear(usbBuf, 0, usbBuf.Length);
                        specifiedDevice.ReceiveData(usbBuf);                        // Wait for response (of ACK)


                        if (usbBuf[0] == CodeplugComms.CMD_ACK[0])
                        {
                            // --------------- removed the password checking
                            blockLength = 32;                            // Max transfer length is 32 bytes
                            int currentPage = 0;
                            int bankSize    = 65536;
                            int numBlocks   = MainForm.transferLength / blockLength;
                            int startBlock  = MainForm.startAddress / blockLength;
                            for (int block = startBlock; block < (startBlock + numBlocks); block++)
                            {
                                if (currentPage != (block * blockLength) / bankSize)
                                {
                                    currentPage = (block * blockLength) / bankSize;
                                    addr32      = blockLength * block;
                                    byte[] array4 = new byte[8] {
                                        (byte)'C', (byte)'W', (byte)'B', 4, 0, 0, 0, 0
                                    };
                                    pageAddr  = addr32 >> 16 << 16;
                                    array4[4] = (byte)(pageAddr >> 24);
                                    array4[5] = (byte)(pageAddr >> 16);
                                    array4[6] = (byte)(pageAddr >> 8);
                                    array4[7] = (byte)pageAddr;
                                    Console.WriteLine(SpecifiedDevice.ByteArrayToString(array4));
                                    Array.Clear(usbBuf, 0, usbBuf.Length);
                                    specifiedDevice.SendData(array4, 0, array4.Length);
                                    specifiedDevice.ReceiveData(usbBuf);
                                    if (usbBuf[0] != CodeplugComms.CMD_ACK[0])
                                    {
                                        goto end_IL_02a2;
                                    }
                                }

                                addr16 = (block * blockLength) & 0xffff;
                                // Send request for dcata
                                byte[] data2 = new byte[4] {
                                    82, (byte)(addr16 >> 8), (byte)addr16, (byte)blockLength
                                };

                                Array.Clear(usbBuf, 0, usbBuf.Length);
                                specifiedDevice.SendData(data2, 0, 4);
                                if (!specifiedDevice.ReceiveData(usbBuf))
                                {
                                    goto end_IL_02a2;
                                }

                                Buffer.BlockCopy(usbBuf, 4, MainForm.eeprom, (block * blockLength), blockLength);                                // Extract the first 8 bytes from the response

                                if (this.OnFirmwareUpdateProgress != null)
                                {
                                    this.OnFirmwareUpdateProgress(this, new FirmwareUpdateProgressEventArgs((float)(block + 1 - startBlock) * 100 / (float)numBlocks, "", false, false));
                                }
                            }
                            // SEND END OF READ
                            specifiedDevice.SendData(CodeplugComms.CMD_ENDR);
                            specifiedDevice.ReceiveData(usbBuf);
                        }
                        break;
                    }
                    return;

end_IL_02a2:
                    break;
                }
            }
        }
        catch (TimeoutException ex)
        {
            Console.WriteLine(ex.Message);
            if (this.OnFirmwareUpdateProgress != null)
            {
                this.OnFirmwareUpdateProgress(this, new FirmwareUpdateProgressEventArgs(0f, "Comms error", false, false));
            }
        }
        finally
        {
            if (specifiedDevice != null)
            {
                specifiedDevice.Dispose();
            }
        }

        if (this.OnFirmwareUpdateProgress != null)
        {
            this.OnFirmwareUpdateProgress(this, new FirmwareUpdateProgressEventArgs(100f, "", false, true));
        }
    }
Пример #4
0
        public static int UploadFirmare(string fileName, FirmwareLoaderUI progessForm = null)
        {
            byte[] encodeKey = null;
            _progessForm = progessForm;

            switch (outputType)
            {
            case OutputType.OutputType_GD77:
                encodeKey = new Byte[4] {
                    (0x61 + 0x00), (0x61 + 0x0C), (0x61 + 0x0D), (0x61 + 0x01)
                };
                break;

            case OutputType.OutputType_GD77S:
                encodeKey = new Byte[4] {
                    (0x47), (0x70), (0x6d), (0x4a)
                };
                break;

            case OutputType.OutputType_DM1801:
                encodeKey = new Byte[4] {
                    (0x74), (0x21), (0x44), (0x39)
                };
                break;

            case OutputType.OutputType_UNKOWN:
                return(-99);
            }

            _specifiedDevice = SpecifiedDevice.FindSpecifiedDevice(VENDOR_ID, PRODUCT_ID);
            if (_specifiedDevice == null)
            {
                _progessForm.SetLabel(String.Format("Error. Can't connect to the {0}", getModelName()));
                //Console.WriteLine("Error. Can't connect to the GD-77");
                return(-1);
            }

            byte[] fileBuf = File.ReadAllBytes(fileName);
            if (Path.GetExtension(fileName).ToLower() == ".sgl")
            {
                Dictionary <FirmwareLoader.OutputType, byte> firmwareModelTag = new Dictionary <FirmwareLoader.OutputType, byte>();
                byte headerModel = 0x00;

                firmwareModelTag.Add(OutputType.OutputType_GD77, 0x1B);
                firmwareModelTag.Add(OutputType.OutputType_GD77S, 0x70);
                firmwareModelTag.Add(OutputType.OutputType_DM1801, 0x4F);
                firmwareModelTag.Add(OutputType.OutputType_RD5R, 0x5C);                         // valid value for DM5R firmware v2.1.7

                // Couls be a SGL file !
                fileBuf = checkForSGLAndReturnEncryptedData(fileBuf, encodeKey, ref headerModel);
                if (fileBuf == null)
                {
                    _progessForm.SetLabel("Error. Missing SGL! in .sgl file header");
                    Console.WriteLine("Error. Missing SGL! in .sgl file header.");
                    _specifiedDevice.Dispose();
                    _specifiedDevice = null;
                    return(-5);
                }

                _progessForm.SetLabel("Firmware file confirmed as SGL");

                if (firmwareModelTag[FirmwareLoader.outputType] != headerModel)
                {
                    MessageBox.Show("Error. The firmware doesn't match the transceiver model.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    _specifiedDevice.Dispose();
                    _specifiedDevice = null;
                    return(-10);
                }
            }
            else
            {
                _progessForm.SetLabel("Firmware file is unencrypted binary");
                fileBuf = encrypt(fileBuf);
            }


            if (fileBuf.Length > 0x7b000)
            {
                _progessForm.SetLabel("Error. Firmware file too large.");
                _specifiedDevice.Dispose();
                _specifiedDevice = null;
                return(-2);
            }

            if (sendInitialCommands(encodeKey) == true)
            {
                int respCode = sendFileData(fileBuf);
                if (respCode == 0)
                {
                    _progessForm.SetLabel("Success");
                    MessageBox.Show(String.Format("Firmware update complete. Please reboot the {0}", getModelName()), "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    switch (respCode)
                    {
                    case -1:
                        _progessForm.SetLabel("Error. Firmware file too large.");
                        break;

                    case -2:
                    case -3:
                    case -4:
                    case -5:
                        _progessForm.SetLabel("Error " + respCode + " While sending data file");
                        break;
                    }
                    _specifiedDevice.Dispose();
                    _specifiedDevice = null;
                    return(-3);
                }
            }
            else
            {
                _progessForm.SetLabel(String.Format("Error while sending initial commands. Is the {0} in firmware update mode?", getModelName()));
                _specifiedDevice.Dispose();
                _specifiedDevice = null;
                return(-4);
            }
            _specifiedDevice.Dispose();
            _specifiedDevice = null;
            return(0);
        }
Пример #5
0
        public void usbCommTest()
        {
            byte[]          usbBuf          = new byte[160];// buffer for individual transfers
            SpecifiedDevice specifiedDevice = null;

            try
            {
                specifiedDevice = SpecifiedDevice.FindSpecifiedDevice(HID_VID, HID_PID);
                if (specifiedDevice == null)
                {
                    SetStatusText("USB Test ERROR: Device not found");
                }
                else
                {
                    SetStatusText("USB Test started");

                    for (int cnt = 0; cnt < 4000; cnt++)
                    {
                        specifiedDevice.SendData(GD77_livedisplay.CMD_CMD1);
                        Array.Clear(usbBuf, 0, usbBuf.Length);
                        specifiedDevice.ReceiveData(usbBuf);// Wait for response
                        if (usbBuf[0] != GD77_livedisplay.CMD_ACK[0])
                        {
                            SetStatusText("USB Test ERROR: No ACK");
                            break;
                        }

                        byte[] buffer = new byte[32];
                        for (int i = 0; i < 32; i++)
                        {
                            buffer[i] = (byte)i;
                        }

                        specifiedDevice.SendData(buffer);
                        Array.Clear(usbBuf, 0, usbBuf.Length);
                        specifiedDevice.ReceiveData(usbBuf);

                        bool error = false;
                        for (int i = 0; i < 32; i++)
                        {
                            if (usbBuf[i] != 255 - i)
                            {
                                error = true;
                                break;
                            }
                        }

                        if (error)
                        {
                            SetStatusText("USB Test ERROR: Received data does not match");
                            break;
                        }

                        SetStatusText(String.Format("USB Test: {0:0.0}%", (float)(cnt + 1) * 100 / (float)4000));

                        if (usbCommTestStopTask)
                        {
                            SetStatusText("USB Test canceled");
                            break;
                        }
                    }
                }
            }
            catch (TimeoutException ex)
            {
                Console.WriteLine(ex.Message);
                SetStatusText("USB Test ERROR: Comms error");
            }
            finally
            {
                if (specifiedDevice != null)
                {
                    specifiedDevice.Dispose();
                }
                usbCommTestRunning = false;
            }
        }
Пример #6
0
        static public OutputType probeModel()
        {
            byte[]                commandLetterA = new byte[] { 0x41 };                                                                                                                           // 'A'
            byte[][]              command0       = new byte[][] { new byte[] { 0x44, 0x4f, 0x57, 0x4e, 0x4c, 0x4f, 0x41, 0x44 }, new byte[] { 0x23, 0x55, 0x50, 0x44, 0x41, 0x54, 0x45, 0x3f } }; // DOWNLOAD
            byte[][]              command1       = new byte[][] { commandLetterA, responseOK };
            byte[]                commandDummy   = new byte[] { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
            byte[][][]            commandID      = { command0, command1 };
            StringAndOutputType[] models         = new StringAndOutputType[] {
                new StringAndOutputType {
                    Model = Encoding.ASCII.GetBytes("DV01"), Type = OutputType.OutputType_GD77
                },
                new StringAndOutputType {
                    Model = Encoding.ASCII.GetBytes("DV02"), Type = OutputType.OutputType_GD77S
                },
                new StringAndOutputType {
                    Model = Encoding.ASCII.GetBytes("DV03"), Type = OutputType.OutputType_DM1801
                },
                new StringAndOutputType {
                    Model = Encoding.ASCII.GetBytes("DV02"), Type = OutputType.OutputType_RD5R
                }                                                                                                                             // Also have "DV02"
            };
            int commandNumber = 0;

            byte[] resp;



            _specifiedDevice = SpecifiedDevice.FindSpecifiedDevice(VENDOR_ID, PRODUCT_ID);

            if (_specifiedDevice == null)
            {
                Console.WriteLine("Error. Can't connect the transceiver");
                return(OutputType.OutputType_UNKOWN);
            }

            while (commandNumber < commandID.Length)
            {
                if (sendAndCheckResponse(commandID[commandNumber][0], commandID[commandNumber][1]) == false)
                {
                    Console.WriteLine("Error sending command.");
                    _specifiedDevice.Dispose();
                    _specifiedDevice = null;
                    return(OutputType.OutputType_UNKOWN);
                }

                commandNumber = commandNumber + 1;
            }

            resp = sendAndGetResponse(commandDummy);

            if (resp.Length >= 4)
            {
                foreach (StringAndOutputType model in models)
                {
                    if (model.Model.SequenceEqual(resp.ToList().GetRange(0, 4).ToArray()))
                    {
                        _specifiedDevice.Dispose();
                        _specifiedDevice = null;
                        return(model.Type);
                    }
                }
            }

            _specifiedDevice.Dispose();
            _specifiedDevice = null;
            return(OutputType.OutputType_UNKOWN);
        }
Пример #7
0
        public void usbLivemodus()
        {
            byte[]          usbBuf          = new byte[160];// buffer for individual transfers
            SpecifiedDevice specifiedDevice = null;

            try
            {
                specifiedDevice = SpecifiedDevice.FindSpecifiedDevice(HID_VID, HID_PID);
                if (specifiedDevice == null)
                {
                    SetStatusText("USB Livemodus ERROR: Device not found");
                }
                else
                {
                    SetStatusText("USB Livemodus started");

                    while (true)
                    {
                        if (!checkBoxEnableDisplayTest.Checked)
                        {
                            specifiedDevice.SendData(GD77_livedisplay.CMD_CMD2);
                            Array.Clear(usbBuf, 0, usbBuf.Length);
                            specifiedDevice.ReceiveData(usbBuf);// Wait for response
                            if (usbBuf[0] != GD77_livedisplay.CMD_ACK[0])
                            {
                                SetStatusText("USB Livemodus ERROR: No ACK");
                                break;
                            }

                            byte[] buffer = new byte[5] {
                                0, 0, 0, 0, 0
                            };
                            if (checkBoxLEDgreen.Checked)
                            {
                                buffer[0] |= 0x01;
                            }
                            if (checkBoxLEDred.Checked)
                            {
                                buffer[0] |= 0x02;
                            }
                            if (checkBoxDisplayLight.Checked)
                            {
                                buffer[0] |= 0x04;
                            }

                            specifiedDevice.SendData(buffer);
                            Array.Clear(usbBuf, 0, usbBuf.Length);
                            specifiedDevice.ReceiveData(usbBuf);

                            SetCheckBox(checkBoxPTT, (usbBuf[0] & 0x01) != 0);
                            SetCheckBox(checkBoxSK1, (usbBuf[0] & 0x02) != 0);
                            SetCheckBox(checkBoxSK2, (usbBuf[0] & 0x04) != 0);
                            SetCheckBox(checkBoxOrange, (usbBuf[0] & 0x08) != 0);

                            int keys = (usbBuf[1] << 0) + (usbBuf[2] << 8) + (usbBuf[3] << 16) + (usbBuf[4] << 24);

                            SetCheckBox(checkBoxKey1, (keys & 0x00000001) != 0);
                            SetCheckBox(checkBoxKey2, (keys & 0x00000002) != 0);
                            SetCheckBox(checkBoxKey3, (keys & 0x00000004) != 0);
                            SetCheckBox(checkBoxKeyGreen, (keys & 0x00000008) != 0);

                            SetCheckBox(checkBoxKeyRight, (keys & 0x00000010) != 0);
                            SetCheckBox(checkBoxKey4, (keys & 0x00000020) != 0);
                            SetCheckBox(checkBoxKey5, (keys & 0x00000040) != 0);
                            SetCheckBox(checkBoxKey6, (keys & 0x00000080) != 0);

                            SetCheckBox(checkBoxKeyUp, (keys & 0x00000100) != 0);
                            SetCheckBox(checkBoxKeyLeft, (keys & 0x00000200) != 0);
                            SetCheckBox(checkBoxKey7, (keys & 0x00000400) != 0);
                            SetCheckBox(checkBoxKey8, (keys & 0x00000800) != 0);

                            SetCheckBox(checkBoxKey9, (keys & 0x00001000) != 0);
                            SetCheckBox(checkBoxKeyDown, (keys & 0x00002000) != 0);
                            SetCheckBox(checkBoxKeyStar, (keys & 0x00008000) != 0);

                            SetCheckBox(checkBoxKey0, (keys & 0x00010000) != 0);
                            SetCheckBox(checkBoxKeyHash, (keys & 0x00020000) != 0);
                            SetCheckBox(checkBoxKeyRed, (keys & 0x00040000) != 0);
                        }
                        else
                        {
                            if (checkBoxClearDisplay.Checked)
                            {
                                SetCheckBox(checkBoxClearDisplay, false);

                                if (!display_clear(specifiedDevice))
                                {
                                    break;
                                }
                            }

                            if (checkBoxDisplayTest1.Checked)
                            {
                                SetCheckBox(checkBoxDisplayTest1, false);

                                if (!display_clear(specifiedDevice))
                                {
                                    break;
                                }

                                if (!display_set_cursor(specifiedDevice, 24, 1))
                                {
                                    break;
                                }
                                if (!display_write(specifiedDevice, 'T'))
                                {
                                    break;
                                }
                                if (!display_write(specifiedDevice, 'E'))
                                {
                                    break;
                                }
                                if (!display_write(specifiedDevice, 'S'))
                                {
                                    break;
                                }
                                if (!display_write(specifiedDevice, 'T'))
                                {
                                    break;
                                }
                                if (!display_write(specifiedDevice, '1'))
                                {
                                    break;
                                }

                                if (!display_set_cursor(specifiedDevice, 24, 2))
                                {
                                    break;
                                }
                                if (!display_write(specifiedDevice, 'T'))
                                {
                                    break;
                                }
                                if (!display_write(specifiedDevice, 'E'))
                                {
                                    break;
                                }
                                if (!display_write(specifiedDevice, 'S'))
                                {
                                    break;
                                }
                                if (!display_write(specifiedDevice, 'T'))
                                {
                                    break;
                                }
                                if (!display_write(specifiedDevice, '2'))
                                {
                                    break;
                                }

                                if (!display_set_cursor(specifiedDevice, 24, 4))
                                {
                                    break;
                                }
                                if (!display_print(specifiedDevice, "Testtext!"))
                                {
                                    break;
                                }

                                if (!display_set_cursor(specifiedDevice, 24, 5))
                                {
                                    break;
                                }
                                if (!display_print(specifiedDevice, "Hello World!"))
                                {
                                    break;
                                }
                            }

                            if (checkBoxDisplayTest2.Checked)
                            {
                                SetCheckBox(checkBoxDisplayTest2, false);

                                if (!display_clear(specifiedDevice))
                                {
                                    break;
                                }

                                for (int i = 0; i < 8; i++)
                                {
                                    if (!display_set_cursor(specifiedDevice, 0, (byte)i))
                                    {
                                        break;
                                    }
                                    if (!display_print(specifiedDevice, "012345678901234567890"))
                                    {
                                        break;
                                    }
                                }
                            }
                        }

                        Thread.Sleep(50);

                        if (usbLivemodusStopTask)
                        {
                            SetStatusText("USB Livemodus stopped");
                            break;
                        }
                    }
                }
            }
            catch (TimeoutException ex)
            {
                Console.WriteLine(ex.Message);
                SetStatusText("USB Livemodus ERROR: Comms error");
            }
            finally
            {
                if (specifiedDevice != null)
                {
                    specifiedDevice.Dispose();
                }
                usbLivemodusRunning = false;
            }
        }
Пример #8
0
    public void writeCodeplug()
    {
        int num  = 0;
        int num2 = 0;
        int num3 = 0;
        int num4 = 0;

        byte[]     array  = new byte[160];
        byte[]     array2 = MainForm.DataToByte();
        List <int> list   = new List <int>();
        List <int> list2  = new List <int>();
        int        num5   = 0;
        int        num6   = 0;
        int        i      = 0;
        int        num7   = 0;
        int        num8   = 0;
        int        num9   = 0;
        int        num10  = 0;
        bool       flag   = false;

        byte[] array3 = new byte[6];
        int    year   = DateTime.Now.Year;
        int    month  = DateTime.Now.Month;
        int    day    = DateTime.Now.Day;
        int    hour   = DateTime.Now.Hour;
        int    minute = DateTime.Now.Minute;

        array3[0] = (byte)(year / 1000 << 4 | year / 100 % 10);
        array3[1] = (byte)(year % 100 / 10 << 4 | year % 10);
        array3[2] = (byte)(month / 10 << 4 | month % 10);
        array3[3] = (byte)(day / 10 << 4 | day % 10);
        array3[4] = (byte)(hour / 10 << 4 | hour % 10);
        array3[5] = (byte)(minute / 10 << 4 | minute % 10);
        Array.Copy(array3, 0, array2, Class15.ADDR_DEVICE_INFO + Class15.OFS_LAST_PRG_TIME, 6);
        SpecifiedDevice specifiedDevice = SpecifiedDevice.FindSpecifiedDevice(HID_VID, 0x0073);

        if (specifiedDevice == null)
        {
            if (this.OnFirmwareUpdateProgress != null)
            {
                this.OnFirmwareUpdateProgress(this, new FirmwareUpdateProgressEventArgs(0f, Class15.SZ_DEVICE_NOT_FOUND, true, true));
            }
        }
        else
        {
            try
            {
                while (true)
                {
                    specifiedDevice.SendData(Class19.CMD_PRG);
                    Array.Clear(array, 0, array.Length);
                    specifiedDevice.ReceiveData(array);
                    if (array[0] != Class19.CMD_ACK[0])
                    {
                        break;
                    }
                    specifiedDevice.SendData(Class19.CMD_PRG2);
                    Array.Clear(array, 0, array.Length);
                    specifiedDevice.ReceiveData(array);
                    byte[] array4 = new byte[8];
                    Buffer.BlockCopy(array, 0, array4, 0, 8);
                    if (array4.smethod_4(Class15.CUR_MODEL))
                    {
                        specifiedDevice.SendData(Class19.CMD_ACK);
                        Array.Clear(array, 0, array.Length);
                        specifiedDevice.ReceiveData(array);
                        if (array[0] == Class19.CMD_ACK[0])
                        {
                            if (!flag && Class15.CUR_PWD != "DT8168")
                            {
                                i    = Class15.ADDR_PWD;
                                num5 = 8;
                                byte[] data = new byte[4]
                                {
                                    82,
                                    (byte)(i >> 8),
                                    (byte)i,
                                    8
                                };
                                Array.Clear(array, 0, array.Length);
                                specifiedDevice.SendData(data, 0, 4);
                                specifiedDevice.ReceiveData(array);
                                string text = "";
                                for (num = 0; num < 8; num++)
                                {
                                    char c = Convert.ToChar(array[num + 4]);
                                    if ("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz\b".IndexOf(c) < 0)
                                    {
                                        break;
                                    }
                                    text += c;
                                }
                                if (string.IsNullOrEmpty(text))
                                {
                                    Array.Clear(array, 0, array.Length);
                                    specifiedDevice.SendData(Class19.CMD_ENDW);
                                    specifiedDevice.ReceiveData(array);
                                    if (array[0] != Class19.CMD_ACK[0])
                                    {
                                        break;
                                    }
                                    flag = true;
                                }
                                else
                                {
                                    if (text != Class15.CUR_PWD)
                                    {
                                        Class15.CUR_PWD = "";
                                        PasswordForm passwordForm = new PasswordForm();
                                        if (passwordForm.ShowDialog() == DialogResult.OK)
                                        {
                                            Array.Clear(array, 0, array.Length);
                                            specifiedDevice.SendData(Class19.CMD_ENDW);
                                            specifiedDevice.ReceiveData(array);
                                            if (array[0] != Class19.CMD_ACK[0])
                                            {
                                                break;
                                            }
                                            flag = true;
                                            continue;
                                        }
                                        this.OnFirmwareUpdateProgress(this, new FirmwareUpdateProgressEventArgs(0f, "", true, true));
                                        return;
                                    }
                                    Array.Clear(array, 0, array.Length);
                                    specifiedDevice.SendData(Class19.CMD_ENDW);
                                    specifiedDevice.ReceiveData(array);
                                    if (array[0] != Class19.CMD_ACK[0])
                                    {
                                        break;
                                    }
                                    flag = true;
                                }
                                continue;
                            }
                            byte[] array5 = new byte[16];
                            Array.Copy(array2, Class15.ADDR_CHANNEL, array5, 0, array5.Length);
                            BitArray bitArray = new BitArray(array5);
                            list.Add(Class15.ADDR_CHANNEL);
                            list2.Add(Class15.ADDR_CHANNEL + 16);
                            for (num = 0; num < 128; num++)
                            {
                                if (bitArray[num])
                                {
                                    num9  = Class15.ADDR_CHANNEL + 16 + num * ChannelForm.SPACE_CH;
                                    num10 = num9 + ChannelForm.SPACE_CH;
                                    list.Add(num9);
                                    list2.Add(num10);
                                }
                            }
                            for (num2 = 1; num2 < 8; num2++)
                            {
                                num7 = Class15.ADDR_EX_CH + (num2 - 1) * ChannelForm.SPACE_CH_GROUP;
                                Array.Copy(array2, num7, array5, 0, array5.Length);
                                bitArray = new BitArray(array5);
                                list.Add(num7);
                                list2.Add(num7 + 16);
                                for (num = 0; num < 128; num++)
                                {
                                    if (bitArray[num])
                                    {
                                        num9  = num7 + 16 + num * ChannelForm.SPACE_CH;
                                        num10 = num9 + ChannelForm.SPACE_CH;
                                        list.Add(num9);
                                        list2.Add(num10);
                                    }
                                }
                            }
                            byte[] array6 = new byte[32];
                            num7 = Class15.ADDR_EX_ZONE_LIST;
                            Array.Copy(array2, num7, array6, 0, array6.Length);
                            bitArray = new BitArray(array6);
                            list.Add(num7);
                            list2.Add(num7 + 32);
                            for (num = 0; num < 250; num++)
                            {
                                num7 = Class15.ADDR_EX_ZONE_LIST + 32;
                                if (bitArray[num])
                                {
                                    num9  = num7 + num * ZoneForm.SPACE_ZONE;
                                    num10 = num9 + ZoneForm.SPACE_ZONE;
                                    list.Add(num9);
                                    list2.Add(num10);
                                }
                            }
                            for (num = 0; num < this.START_ADDR.Length; num++)
                            {
                                list.Add(this.START_ADDR[num]);
                                list2.Add(this.END_ADDR[num]);
                            }
                            for (num = 0; num < list.Count; num++)
                            {
                                num9  = list[num];
                                num10 = list2[num];
                                for (i = num9; i < num10; i += num5)
                                {
                                    num6 = i % 32;
                                    num5 = ((i + 32 <= num10) ? (32 - num6) : (num10 - i));
                                    num4++;
                                }
                            }
                            num7 = 0;
                            num  = 0;
                            while (true)
                            {
                                if (num < list.Count)
                                {
                                    num9  = list[num];
                                    num10 = list2[num];
                                    i     = num9;
                                    while (i < num10)
                                    {
                                        if (!this.method_0())
                                        {
                                            if (num7 >> 16 != i >> 16)
                                            {
                                                byte[] array7 = new byte[8]
                                                {
                                                    67,
                                                    87,
                                                    66,
                                                    4,
                                                    0,
                                                    0,
                                                    0,
                                                    0
                                                };
                                                num7      = i >> 16 << 16;
                                                array7[4] = (byte)(num7 >> 24);
                                                array7[5] = (byte)(num7 >> 16);
                                                array7[6] = (byte)(num7 >> 8);
                                                array7[7] = (byte)num7;
                                                Array.Clear(array, 0, array.Length);
                                                specifiedDevice.SendData(array7, 0, array7.Length);
                                                specifiedDevice.ReceiveData(array);
                                                if (array[0] == Class19.CMD_ACK[0])
                                                {
                                                    goto IL_06e2;
                                                }
                                                goto IL_083e;
                                            }
                                            goto IL_06e2;
                                        }
                                        specifiedDevice.SendData(Class19.CMD_ENDR);
                                        specifiedDevice.ReceiveData(array);
                                        return;

IL_06e2:
                                        num6 = i % 32;
                                        num5 = ((i + 32 <= num10) ? (32 - num6) : (num10 - i));
                                        num8 = i - num7;
                                        byte[] array8 = new byte[num5 + 4];
                                        array8[0] = 87;
                                        array8[1] = (byte)(num8 >> 8);
                                        array8[2] = (byte)num8;
                                        array8[3] = (byte)num5;
                                        Array.Clear(array, 0, array.Length);
                                        Array.Copy(array2, i, array8, 4, num5);
                                        specifiedDevice.SendData(array8, 0, 4 + num5);
                                        specifiedDevice.ReceiveData(array);
                                        if (array[0] == Class19.CMD_ACK[0])
                                        {
                                            if (this.OnFirmwareUpdateProgress != null)
                                            {
                                                this.OnFirmwareUpdateProgress(this, new FirmwareUpdateProgressEventArgs((float)(++num3) * 100f / (float)num4, i.ToString(), false, false));
                                            }
                                            i += num5;
                                            continue;
                                        }
                                        goto IL_0857;
                                    }
                                    num++;
                                    continue;
                                }
                                Array.Clear(array, 0, array.Length);
                                specifiedDevice.SendData(Class19.CMD_ENDW);
                                specifiedDevice.ReceiveData(array);
                                if (array[0] != Class19.CMD_ACK[0])
                                {
                                    break;
                                }
                                if (this.OnFirmwareUpdateProgress != null)
                                {
                                    this.OnFirmwareUpdateProgress(this, new FirmwareUpdateProgressEventArgs(100f, "", false, true));
                                }
                                return;

IL_0857:
                                specifiedDevice.SendData(Class19.CMD_ENDW);
                                specifiedDevice.ReceiveData(array);
                                break;
IL_083e:
                                specifiedDevice.SendData(Class19.CMD_ENDW);
                                specifiedDevice.ReceiveData(array);
                                break;
                            }
                        }
                        break;
                    }
                    if (this.OnFirmwareUpdateProgress != null)
                    {
                        this.OnFirmwareUpdateProgress(this, new FirmwareUpdateProgressEventArgs(0f, Class15.SZ_MODEL_NOT_MATCH, true, true));
                    }
                    return;
                }
                if (this.OnFirmwareUpdateProgress != null)
                {
                    this.OnFirmwareUpdateProgress(this, new FirmwareUpdateProgressEventArgs(0f, Class15.SZ_COMM_ERROR, true, true));
                }
            }
            catch (TimeoutException ex)
            {
                Console.WriteLine(ex.Message);
                if (this.OnFirmwareUpdateProgress != null)
                {
                    this.OnFirmwareUpdateProgress(this, new FirmwareUpdateProgressEventArgs(0f, Class15.SZ_COMM_ERROR, false, false));
                }
            }
            finally
            {
                if (specifiedDevice != null)
                {
                    specifiedDevice.Dispose();
                }
            }
        }
    }
Пример #9
0
    public void readCodeplug()
    {
        int num  = 0;
        int num2 = 0;
        int num3 = 0;
        int num4 = 0;

        byte[]          array           = new byte[Class15.EEROM_SPACE];
        byte[]          array2          = new byte[160];
        List <int>      list            = new List <int>();
        List <int>      list2           = new List <int>();
        int             num5            = 0;
        int             num6            = 0;
        int             i               = 0;
        int             num7            = 0;
        int             num8            = 0;
        int             num9            = 0;
        int             num10           = 0;
        bool            flag            = false;
        float           num11           = 0f;
        int             num12           = 0;
        SpecifiedDevice specifiedDevice = null;

        try
        {
            specifiedDevice = SpecifiedDevice.FindSpecifiedDevice(HID_VID, HID_PID);            //0x152A HID_PID
            if (specifiedDevice == null)
            {
                if (this.OnFirmwareUpdateProgress != null)
                {
                    this.OnFirmwareUpdateProgress(this, new FirmwareUpdateProgressEventArgs(0f, Class15.SZ_DEVICE_NOT_FOUND, true, true));
                }
            }
            else
            {
                while (true)
                {
                    Array.Clear(array2, 0, array2.Length);
                    specifiedDevice.SendData(Class19.CMD_PRG);
                    specifiedDevice.ReceiveData(array2);
                    if (array2[0] != Class19.CMD_ACK[0])
                    {
                        break;
                    }
                    specifiedDevice.SendData(Class19.CMD_PRG2);
                    Array.Clear(array2, 0, array2.Length);
                    specifiedDevice.ReceiveData(array2);
                    byte[] array3 = new byte[8];
                    Buffer.BlockCopy(array2, 0, array3, 0, 8);
                    if (array3.smethod_4(Class15.CUR_MODEL))
                    {
                        specifiedDevice.SendData(Class19.CMD_ACK);
                        Array.Clear(array2, 0, array2.Length);
                        specifiedDevice.ReceiveData(array2);
                        if (array2[0] == Class19.CMD_ACK[0])
                        {
                            if (!flag && Class15.CUR_PWD != "DT8168")
                            {
                                i    = Class15.ADDR_PWD;
                                num5 = 8;
                                byte[] data = new byte[4]
                                {
                                    82,
                                    (byte)(i >> 8),
                                    (byte)i,
                                    8
                                };
                                Array.Clear(array2, 0, array2.Length);
                                specifiedDevice.SendData(data, 0, 4);
                                specifiedDevice.ReceiveData(array2);
                                string text = "";
                                for (num = 0; num < 8; num++)
                                {
                                    char c = Convert.ToChar(array2[num + 4]);
                                    if ("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz\b".IndexOf(c) < 0)
                                    {
                                        break;
                                    }
                                    text += c;
                                }
                                if (string.IsNullOrEmpty(text))
                                {
                                    Array.Clear(array2, 0, array2.Length);
                                    specifiedDevice.SendData(Class19.CMD_ENDR);
                                    specifiedDevice.ReceiveData(array2);
                                    if (array2[0] != Class19.CMD_ACK[0])
                                    {
                                        break;
                                    }
                                    flag = true;
                                }
                                else
                                {
                                    if (text != Class15.CUR_PWD)
                                    {
                                        Array.Clear(array2, 0, array2.Length);
                                        specifiedDevice.SendData(Class19.CMD_ENDR);
                                        specifiedDevice.ReceiveData(array2);
                                        if (array2[0] != Class19.CMD_ACK[0])
                                        {
                                            break;
                                        }
                                        Class15.CUR_PWD = "";
                                        PasswordForm passwordForm = new PasswordForm();
                                        if (passwordForm.ShowDialog() == DialogResult.OK)
                                        {
                                            num12++;
                                            if (num12 < 3)
                                            {
                                                continue;
                                            }
                                            this.OnFirmwareUpdateProgress(this, new FirmwareUpdateProgressEventArgs(0f, "Password error more than three times, quit communication!", true, true));
                                        }
                                        else
                                        {
                                            this.OnFirmwareUpdateProgress(this, new FirmwareUpdateProgressEventArgs(0f, "", true, true));
                                        }
                                        return;
                                    }
                                    Array.Clear(array2, 0, array2.Length);
                                    specifiedDevice.SendData(Class19.CMD_ENDR);
                                    specifiedDevice.ReceiveData(array2);
                                    if (array2[0] != Class19.CMD_ACK[0])
                                    {
                                        break;
                                    }
                                    flag = true;
                                }
                                continue;
                            }
                            List <int> list3 = new List <int>();
                            List <int> list4 = new List <int>();
                            list3.Add(Class15.ADDR_CHANNEL);
                            list4.Add(Class15.ADDR_CHANNEL + 16);
                            for (num2 = 1; num2 < 8; num2++)
                            {
                                num8 = Class15.ADDR_EX_CH + (num2 - 1) * ChannelForm.SPACE_CH_GROUP;
                                list3.Add(num8);
                                list4.Add(num8 + 16);
                            }
                            num8 = Class15.ADDR_EX_ZONE_LIST;
                            list3.Add(num8);
                            list4.Add(num8 + 32);
                            num3 = 0;
                            num4 = 0;
                            for (num = 0; num < list3.Count; num++)
                            {
                                num9  = list3[num];
                                num10 = list4[num];
                                for (i = num9; i < num10; i += num5)
                                {
                                    num6 = i % 32;
                                    num5 = ((i + 32 <= num10) ? (32 - num6) : (num10 - i));
                                    num4++;
                                }
                            }
                            num8 = 0;
                            num  = 0;
                            while (true)
                            {
                                if (num < list3.Count)
                                {
                                    num9  = list3[num];
                                    num10 = list4[num];
                                    i     = num9;
                                    while (i < num10)
                                    {
                                        if (!this.method_0())
                                        {
                                            if (num8 >> 16 != i >> 16)
                                            {
                                                byte[] array4 = new byte[8]
                                                {
                                                    67,
                                                    87,
                                                    66,
                                                    4,
                                                    0,
                                                    0,
                                                    0,
                                                    0
                                                };
                                                num8      = i >> 16 << 16;
                                                array4[4] = (byte)(num8 >> 24);
                                                array4[5] = (byte)(num8 >> 16);
                                                array4[6] = (byte)(num8 >> 8);
                                                array4[7] = (byte)num8;
                                                Array.Clear(array2, 0, array2.Length);
                                                specifiedDevice.SendData(array4, 0, array4.Length);
                                                specifiedDevice.ReceiveData(array2);
                                                if (array2[0] != Class19.CMD_ACK[0])
                                                {
                                                    goto end_IL_02a2;
                                                }
                                            }
                                            num6 = i % 32;
                                            num5 = ((i + 32 <= num10) ? (32 - num6) : (num10 - i));
                                            num7 = i - num8;
                                            byte[] data2 = new byte[4]
                                            {
                                                82,
                                                (byte)(num7 >> 8),
                                                (byte)num7,
                                                (byte)num5
                                            };
                                            Array.Clear(array2, 0, array2.Length);
                                            specifiedDevice.SendData(data2, 0, 4);
                                            if (!specifiedDevice.ReceiveData(array2))
                                            {
                                                goto end_IL_02a2;
                                            }
                                            Array.Copy(array2, 4, array, i, num5);
                                            if (this.OnFirmwareUpdateProgress != null)
                                            {
                                                this.OnFirmwareUpdateProgress(this, new FirmwareUpdateProgressEventArgs((float)(++num3) * 5f / (float)num4, i.ToString(), false, false));
                                            }
                                            i += num5;
                                            continue;
                                        }
                                        specifiedDevice.SendData(Class19.CMD_ENDR);
                                        specifiedDevice.ReceiveData(array2);
                                        return;
                                    }
                                    num++;
                                    continue;
                                }
                                byte[] array5 = new byte[16];
                                Array.Copy(array, Class15.ADDR_CHANNEL, array5, 0, array5.Length);
                                BitArray bitArray = new BitArray(array5);
                                list.Add(Class15.ADDR_CHANNEL);
                                list2.Add(Class15.ADDR_CHANNEL + 16);
                                for (num = 0; num < 128; num++)
                                {
                                    if (bitArray[num])
                                    {
                                        num9  = Class15.ADDR_CHANNEL + 16 + num * ChannelForm.SPACE_CH;
                                        num10 = num9 + ChannelForm.SPACE_CH;
                                        list.Add(num9);
                                        list2.Add(num10);
                                    }
                                }
                                for (num2 = 1; num2 < 8; num2++)
                                {
                                    num8 = Class15.ADDR_EX_CH + (num2 - 1) * ChannelForm.SPACE_CH_GROUP;
                                    Array.Copy(array, num8, array5, 0, array5.Length);
                                    bitArray = new BitArray(array5);
                                    list.Add(num8);
                                    list2.Add(num8 + 16);
                                    for (num = 0; num < 128; num++)
                                    {
                                        if (bitArray[num])
                                        {
                                            num9  = num8 + 16 + num * ChannelForm.SPACE_CH;
                                            num10 = num9 + ChannelForm.SPACE_CH;
                                            list.Add(num9);
                                            list2.Add(num10);
                                        }
                                    }
                                }
                                byte[] array6 = new byte[32];
                                num8 = Class15.ADDR_EX_ZONE_LIST;
                                Array.Copy(array, num8, array6, 0, array6.Length);
                                bitArray = new BitArray(array6);
                                list.Add(num8);
                                list2.Add(num8 + 32);
                                for (num = 0; num < 250; num++)
                                {
                                    num8 = Class15.ADDR_EX_ZONE_LIST + 32;
                                    if (bitArray[num])
                                    {
                                        num9  = num8 + num * ZoneForm.SPACE_ZONE;
                                        num10 = num9 + ZoneForm.SPACE_ZONE;
                                        list.Add(num9);
                                        list2.Add(num10);
                                    }
                                }
                                for (num = 0; num < this.START_ADDR.Length; num++)
                                {
                                    list.Add(this.START_ADDR[num]);
                                    list2.Add(this.END_ADDR[num]);
                                }
                                num3 = 0;
                                num4 = 0;
                                for (num = 0; num < list.Count; num++)
                                {
                                    num9  = list[num];
                                    num10 = list2[num];
                                    for (i = num9; i < num10; i += num5)
                                    {
                                        num6 = i % 32;
                                        num5 = ((i + 32 <= num10) ? (32 - num6) : (num10 - i));
                                        num4++;
                                    }
                                }
                                num8 = 0;
                                num  = 0;
                                while (true)
                                {
                                    if (num < list.Count)
                                    {
                                        num9  = list[num];
                                        num10 = list2[num];
                                        i     = num9;
                                        while (i < num10)
                                        {
                                            if (!this.method_0())
                                            {
                                                if (num8 >> 16 != i >> 16)
                                                {
                                                    byte[] array7 = new byte[8]
                                                    {
                                                        67,
                                                        87,
                                                        66,
                                                        4,
                                                        0,
                                                        0,
                                                        0,
                                                        0
                                                    };
                                                    num8      = i >> 16 << 16;
                                                    array7[4] = (byte)(num8 >> 24);
                                                    array7[5] = (byte)(num8 >> 16);
                                                    array7[6] = (byte)(num8 >> 8);
                                                    array7[7] = (byte)num8;
                                                    Array.Clear(array2, 0, array2.Length);
                                                    specifiedDevice.SendData(array7, 0, array7.Length);
                                                    specifiedDevice.ReceiveData(array2);
                                                    if (array2[0] == Class19.CMD_ACK[0])
                                                    {
                                                        goto IL_08b4;
                                                    }
                                                    goto IL_0a1f;
                                                }
                                                goto IL_08b4;
                                            }
                                            specifiedDevice.SendData(Class19.CMD_ENDR);
                                            specifiedDevice.ReceiveData(array2);
                                            return;

IL_08b4:
                                            num6 = i % 32;
                                            num5 = ((i + 32 <= num10) ? (32 - num6) : (num10 - i));
                                            num7 = i - num8;
                                            byte[] array8 = new byte[4]
                                            {
                                                82,
                                                (byte)(num7 >> 8),
                                                (byte)num7,
                                                (byte)num5
                                            };
                                            Array.Clear(array2, 0, array2.Length);
                                            specifiedDevice.SendData(array8, 0, 4);
                                            if (specifiedDevice.ReceiveData(array2))
                                            {
                                                if (Class15.smethod_18(array8, array2, 4))
                                                {
                                                    Array.Copy(array2, 4, array, i, num5);
                                                    if (this.OnFirmwareUpdateProgress != null)
                                                    {
                                                        num11 = 5f + (float)(++num3) * 95f / (float)num4;
                                                        this.OnFirmwareUpdateProgress(this, new FirmwareUpdateProgressEventArgs(num11, i.ToString(), false, false));
                                                    }
                                                    i += num5;
                                                    continue;
                                                }
                                                goto IL_0a51;
                                            }
                                            goto IL_0a38;
                                        }
                                        num++;
                                        continue;
                                    }
                                    Array.Clear(array2, 0, array2.Length);
                                    specifiedDevice.SendData(Class19.CMD_ENDR);
                                    specifiedDevice.ReceiveData(array2);
                                    if (array2[0] != Class19.CMD_ACK[0])
                                    {
                                        break;
                                    }
                                    MainForm.ByteToData(array);
                                    if (this.OnFirmwareUpdateProgress != null)
                                    {
                                        this.OnFirmwareUpdateProgress(this, new FirmwareUpdateProgressEventArgs(100f, "", false, true));
                                    }
                                    return;

IL_0a51:
                                    specifiedDevice.SendData(Class19.CMD_ENDR);
                                    specifiedDevice.ReceiveData(array2);
                                    break;
IL_0a38:
                                    specifiedDevice.SendData(Class19.CMD_ENDR);
                                    specifiedDevice.ReceiveData(array2);
                                    break;
IL_0a1f:
                                    specifiedDevice.SendData(Class19.CMD_ENDR);
                                    specifiedDevice.ReceiveData(array2);
                                    break;
                                }
                                break;
                            }
                        }
                        break;
                    }
                    if (this.OnFirmwareUpdateProgress != null)
                    {
                        this.OnFirmwareUpdateProgress(this, new FirmwareUpdateProgressEventArgs(0f, Class15.SZ_MODEL_NOT_MATCH, true, true));
                    }
                    return;

                    continue;                    // Roger Clark. There is a known compiler warning here. This may be an artifact caused by the decompiler
end_IL_02a2:
                    break;
                }
                if (this.OnFirmwareUpdateProgress != null)
                {
                    this.OnFirmwareUpdateProgress(this, new FirmwareUpdateProgressEventArgs(0f, Class15.SZ_COMM_ERROR, true, true));
                }
            }
        }
        catch (TimeoutException ex)
        {
            Console.WriteLine(ex.Message);
            if (this.OnFirmwareUpdateProgress != null)
            {
                this.OnFirmwareUpdateProgress(this, new FirmwareUpdateProgressEventArgs(0f, Class15.SZ_COMM_ERROR, false, false));
            }
        }
        finally
        {
            if (specifiedDevice != null)
            {
                specifiedDevice.Dispose();
            }
        }
    }
Пример #10
0
    // Test function added by Roger Clark to read all 1Mb from the external Flash chip on the GD-77.
    // It outputs the data dummp to the file c:\\gd-77_datadump.bin

    // However depending on the mode in which the GD-77 is booted,  addresses 0x000000 - 0x01FFFF  (for codeplug mode)
    // or addresses 0x030000 - 0x03FFFF for DMR-ID mode are returned with anything other than 0x00 in them

    // Its strange that address 0x020000 - 0x02FFFF does not seem to be accessible
    // Note. Valid transfer lengths seem to be 8,16 or 32 bytes.  64 bytes does not work, as it just returns 0x00 in addresses after 32 bytes

    public void readDataFlash()
    {
        byte[]          usbBuf          = new byte[160];// buffer for individual transfers
        int             blockLength     = 16;
        SpecifiedDevice specifiedDevice = null;

        try
        {
            specifiedDevice = SpecifiedDevice.FindSpecifiedDevice(HID_VID, HID_PID);//0x152A HID_PID
            if (specifiedDevice == null)
            {
                if (this.OnFirmwareUpdateProgress != null)
                {
                    this.OnFirmwareUpdateProgress(this, new FirmwareUpdateProgressEventArgs(0f, "Device not found", true, true));
                }
            }
            else
            {
                int    numBlocks        = MainForm.transferLength / blockLength;
                int    startBlock       = MainForm.startAddress / blockLength;
                byte[] flashreadcommand = new byte[5] {
                    0x00, 0x00, 0x00, 0x00, 0x00
                };                                                                      // CMD (1=read, otherwise stop) + Address as LSB to MSB
                int block_counter = 0;
                for (int block = startBlock; block < (startBlock + numBlocks); block++)
                {
                    if (block_counter == 0)
                    {
                        Array.Clear(usbBuf, 0, usbBuf.Length);
                        specifiedDevice.SendData(CodeplugComms.CMD_PRG); // Send PROGRA command to initiate comms
                        specifiedDevice.ReceiveData(usbBuf);             // Wait for response
                        if (usbBuf[0] != CodeplugComms.CMD_ACK[0])
                        {
                            if (this.OnFirmwareUpdateProgress != null)
                            {
                                this.OnFirmwareUpdateProgress(this, new FirmwareUpdateProgressEventArgs(0f, "No ACK1", true, true));
                            }
                            break;// Exit if not ack
                        }

                        Array.Clear(usbBuf, 0, usbBuf.Length);
                        specifiedDevice.SendData(CodeplugComms.CMD_PRG255); // Send command to initiate flash readout
                        specifiedDevice.ReceiveData(usbBuf);                // Wait for response
                        if (usbBuf[0] != CodeplugComms.CMD_ACK[0])
                        {
                            if (this.OnFirmwareUpdateProgress != null)
                            {
                                this.OnFirmwareUpdateProgress(this, new FirmwareUpdateProgressEventArgs(0f, "No ACK2", true, true));
                            }
                            break;// Exit if not ack
                        }
                    }

                    ulong adr = (ulong)(block * 16);
                    flashreadcommand[0] = (byte)1;                                          // READ
                    flashreadcommand[1] = (byte)((adr & 0x000000ff) >> 0);
                    flashreadcommand[2] = (byte)((adr & 0x0000ff00) >> 8);
                    flashreadcommand[3] = (byte)((adr & 0x00ff0000) >> 16);
                    flashreadcommand[4] = (byte)((adr & 0xff000000) >> 24);
                    specifiedDevice.SendData(flashreadcommand);
                    Array.Clear(usbBuf, 0, usbBuf.Length);
                    specifiedDevice.ReceiveData(usbBuf);

                    Buffer.BlockCopy(usbBuf, 0, MainForm.CommsBuffer, (block * blockLength), blockLength);// Extract the 16 bytes from the response

                    if (this.OnFirmwareUpdateProgress != null)
                    {
                        this.OnFirmwareUpdateProgress(this, new FirmwareUpdateProgressEventArgs((float)(block + 1 - startBlock) * 100 / (float)numBlocks, "", false, false));
                    }

                    block_counter++;
                    if (block_counter >= 256)
                    {
                        block_counter = 0;

                        flashreadcommand[0] = (byte)2;                                      // STOP
                        flashreadcommand[1] = (byte)0;
                        flashreadcommand[2] = (byte)0;
                        flashreadcommand[3] = (byte)0;
                        flashreadcommand[4] = (byte)0;
                        specifiedDevice.SendData(flashreadcommand);
                        specifiedDevice.ReceiveData(usbBuf);// Wait for response
                        if (usbBuf[0] != CodeplugComms.CMD_ACK[0])
                        {
                            if (this.OnFirmwareUpdateProgress != null)
                            {
                                this.OnFirmwareUpdateProgress(this, new FirmwareUpdateProgressEventArgs(0f, "No STOPACK1", true, true));
                            }
                            break;// Exit if not ack
                        }
                    }

                    if (getCancelComm())
                    {
                        break;
                    }
                }

                if (block_counter != 0)
                {
                    flashreadcommand[0] = (byte)2;                                      // FINAL STOP
                    flashreadcommand[1] = (byte)0;
                    flashreadcommand[2] = (byte)0;
                    flashreadcommand[3] = (byte)0;
                    flashreadcommand[4] = (byte)0;
                    specifiedDevice.SendData(flashreadcommand);
                    specifiedDevice.ReceiveData(usbBuf);// Wait for response
                    if (usbBuf[0] != CodeplugComms.CMD_ACK[0])
                    {
                        if (this.OnFirmwareUpdateProgress != null)
                        {
                            this.OnFirmwareUpdateProgress(this, new FirmwareUpdateProgressEventArgs(0f, "No STOPACK2", true, true));
                        }
                    }
                }
            }
        }
        catch (TimeoutException ex)
        {
            Console.WriteLine(ex.Message);
            if (this.OnFirmwareUpdateProgress != null)
            {
                this.OnFirmwareUpdateProgress(this, new FirmwareUpdateProgressEventArgs(0f, "Comms error", true, true));
            }
        }
        finally
        {
            if (specifiedDevice != null)
            {
                specifiedDevice.Dispose();
            }
        }

        if (this.OnFirmwareUpdateProgress != null)
        {
            this.OnFirmwareUpdateProgress(this, new FirmwareUpdateProgressEventArgs(100f, "", false, true));
        }
    }
Пример #11
0
        public static int UploadFirmare(string fileName, FirmwareLoaderUI progessForm = null)
        {
            byte[] encodeKey = null;
            _progessForm = progessForm;

            switch (outputType)
            {
            case OutputType.OutputType_GD77:
                encodeKey = new Byte[4] {
                    (0x61 + 0x00), (0x61 + 0x0C), (0x61 + 0x0D), (0x61 + 0x01)
                };
                break;

            case OutputType.OutputType_GD77S:
                encodeKey = new Byte[4] {
                    (0x47), (0x70), (0x6d), (0x4a)
                };
                break;

            case OutputType.OutputType_DM1801:
                encodeKey = new Byte[4] {
                    (0x74), (0x21), (0x44), (0x39)
                };
                break;

            case OutputType.OutputType_RD5R:
                encodeKey = new Byte[4] {
                    (0x53), (0x36), (0x37), (0x62)
                };
                break;

            case OutputType.OutputType_UNKNOWN:
                return(-99);
            }

            _specifiedDevice = SpecifiedDevice.FindSpecifiedDevice(VENDOR_ID, PRODUCT_ID);
            if (_specifiedDevice == null)
            {
                _progessForm.SetLabel(String.Format(FirmwareLoaderUI.StringsDict["Error._Cant_connect_to_the"] + " {0}", getModelName()));
                //Console.WriteLine("Error. Can't connect to the GD-77");
                return(-1);
            }

            byte[] fileBuf = File.ReadAllBytes(fileName);
            if (Path.GetExtension(fileName).ToLower() == ".sgl")
            {
                Dictionary <FirmwareLoader.OutputType, byte> firmwareModelTag = new Dictionary <FirmwareLoader.OutputType, byte>();
                byte headerModel = 0x00;

                firmwareModelTag.Add(OutputType.OutputType_GD77, 0x1B);
                firmwareModelTag.Add(OutputType.OutputType_GD77S, 0x70);
                firmwareModelTag.Add(OutputType.OutputType_DM1801, 0x4F);
                firmwareModelTag.Add(OutputType.OutputType_RD5R, 0x5C);                         // valid value for DM5R firmware v2.1.7

                // Couls be a SGL file !
                fileBuf = checkForSGLAndReturnEncryptedData(fileBuf, encodeKey, ref headerModel);
                if (fileBuf == null)
                {
                    _progessForm.SetLabel(FirmwareLoaderUI.StringsDict["Error._Missing_SGL!_in_.sgl_file_header"]);
                    //Console.WriteLine("Error. Missing SGL! in .sgl file header.");
                    _specifiedDevice.Dispose();
                    _specifiedDevice = null;
                    return(-5);
                }

                _progessForm.SetLabel(FirmwareLoaderUI.StringsDict["Firmware_file_confirmed_as_SGL"]);

                if (firmwareModelTag[FirmwareLoader.outputType] != headerModel)
                {
                    MessageBox.Show(FirmwareLoaderUI.StringsDict["Error._The_firmware_doesnt_match_the_transceiver_model."], FirmwareLoaderUI.StringsDict["Error"], MessageBoxButtons.OK, MessageBoxIcon.Error);
                    _specifiedDevice.Dispose();
                    _specifiedDevice = null;
                    return(-10);
                }
            }
            else
            {
                _progessForm.SetLabel(FirmwareLoaderUI.StringsDict["Firmware_file_is_unencrypted_binary"]);
                fileBuf = encrypt(fileBuf);
            }


            if (fileBuf.Length > 0x7b000)
            {
                _progessForm.SetLabel(FirmwareLoaderUI.StringsDict["Error._Firmware_file_too_large"]);
                _specifiedDevice.Dispose();
                _specifiedDevice = null;
                return(-2);
            }

            if (sendInitialCommands(encodeKey) == true)
            {
                int respCode = sendFileData(fileBuf);
                if (respCode == 0)
                {
                    _progessForm.SetLabel(FirmwareLoaderUI.StringsDict["Success"]);
                    MessageBox.Show(String.Format(FirmwareLoaderUI.StringsDict["Firmware_update_complete"] + " {0}", getModelName()), FirmwareLoaderUI.StringsDict["Success"], MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    switch (respCode)
                    {
                    case -1:
                        _progessForm.SetLabel(FirmwareLoaderUI.StringsDict["Error._Firmware_file_too_large"]);
                        break;

                    case -2:
                    case -3:
                    case -4:
                    case -5:
                        _progessForm.SetLabel(FirmwareLoaderUI.StringsDict["Error"] + " " + respCode + " " + FirmwareLoaderUI.StringsDict["While_sending_data_file"]);
                        break;
                    }
                    _specifiedDevice.Dispose();
                    _specifiedDevice = null;
                    return(-3);
                }
            }
            else
            {
                _progessForm.SetLabel(String.Format(FirmwareLoaderUI.StringsDict["Error_while_sending_initial_commands._Is_the_{0}_in_firmware_update_mode?"], getModelName()));
                _specifiedDevice.Dispose();
                _specifiedDevice = null;
                return(-4);
            }
            _specifiedDevice.Dispose();
            _specifiedDevice = null;
            return(0);
        }