public DialogUnitSelect() { InitializeComponent(); base.Size = new Size(base.Size.Width, (int)(FormPICkit2.ScalefactH * (float)base.Size.Height)); ushort num = 0; while (num < 8) { Constants.PICkit2USB pICkit2USB = PICkitFunctions.DetectPICkit2Device(num, readFW: false); if (pICkit2USB != Constants.PICkit2USB.notFound) { string text = PICkitFunctions.GetSerialUnitID(); if (text == "PIC18F2550") { text = "<bootloader>"; } listBoxUnits.Items.Add(" " + num.ToString() + " " + text); num = (ushort)(num + 1); continue; } break; } }
// PICusb.FormPICusb private bool detectPICkit2(bool showFound, bool detectMult) { Constants.PICkit2USB pICkit2USB; if (detectMult) { //FormPICusb.pk2number = 0; pICkit2USB = PICkitFunctions.DetectPICkit2Device(0, false); if (pICkit2USB != Constants.PICkit2USB.notFound) { Constants.PICkit2USB pICkit2USB2 = PICkitFunctions.DetectPICkit2Device(1, false); if (pICkit2USB2 != Constants.PICkit2USB.notFound) { //DialogUnitSelect dialogUnitSelect = new DialogUnitSelect(); //dialogUnitSelect.ShowDialog(); } } } pICkit2USB = PICkitFunctions.DetectPICkit2Device(0, true); bool result; if (pICkit2USB == Constants.PICkit2USB.found) { //this.downloadPICkit2FirmwareToolStripMenuItem.Enabled = true; //this.chkBoxVddOn.Enabled = true; //if (!FormPICusb.selfPoweredTarget) //{ // this.numUpDnVDD.Enabled = true; //} //this.deviceToolStripMenuItem.Enabled = true; if (showFound) { string serialUnitID = PICkitFunctions.GetSerialUnitID(); if (serialUnitID[0] == '-') { //this.displayStatusWindow.Text = "DevBugger found and connected."; this.Text = "PICusb"; } else { if (serialUnitID == "DEVBUGV3") { //this.displayStatusWindow.Text = "DevBugger found and connected."; //this.Text = "PICusb"; } else { //this.displayStatusWindow.Text = "DevBugger connected."; //this.Text = "PICusb"; } } } result = true; } else { //this.downloadPICkit2FirmwareToolStripMenuItem.Enabled = false; //this.chkBoxVddOn.Enabled = false; //this.numUpDnVDD.Enabled = false; //this.deviceToolStripMenuItem.Enabled = false; //this.disableGUIControls(); if (pICkit2USB == Constants.PICkit2USB.firmwareInvalid) { //this.displayStatusWindow.BackColor = Color.Yellow; //this.downloadPICkit2FirmwareToolStripMenuItem.Enabled = true; //this.displayStatusWindow.Text = "The OS v" + PICkitFunctions.FirmwareVersion + " must be updated.\nUse the Tools menu to download a new OS."; //this.oldFirmware = true; result = false; } else { if (pICkit2USB == Constants.PICkit2USB.bootloader) { //this.displayStatusWindow.BackColor = Color.Yellow; //this.downloadPICkit2FirmwareToolStripMenuItem.Enabled = true; //this.displayStatusWindow.Text = "The programmer has no Operating System.\nUse the Tools menu to download an OS."; //this.bootLoad = true; result = false; } else { //this.displayStatusWindow.BackColor = Color.Salmon; //this.displayStatusWindow.Text = "DevBugger not found. Check USB connections and \nuse Tools->Check Communication to retry."; result = false; } } } return(result); }
public static bool ReadHexAndDownload(string fileName, ref ushort pk2num) { try { FileInfo fileInfo = new FileInfo(fileName); TextReader textReader = fileInfo.OpenText(); byte[] array = new byte[35]; string text = textReader.ReadLine(); if (text != null) { PICkitFunctions.EnterBootloader(); PICkitFunctions.ResetPk2Number(); Thread.Sleep(3000); pk2num = 0; int i; for (i = 0; i < 10; i++) { if (PICkitFunctions.DetectPICkit2Device(pk2num, readFW: true) == Constants.PICkit2USB.bootloader) { if (PICkitFunctions.VerifyBootloaderMode()) { break; } } else { pk2num++; } Thread.Sleep(500); } if (i == 10) { textReader.Close(); return(false); } } PICkitFunctions.BL_EraseFlash(); bool flag = false; while (text != null) { if (text[0] == ':' && text.Length >= 11) { int num = int.Parse(text.Substring(1, 2), NumberStyles.HexNumber); int num2 = int.Parse(text.Substring(3, 4), NumberStyles.HexNumber); int num3 = int.Parse(text.Substring(7, 2), NumberStyles.HexNumber); if (flag && (num2 & 0x10) == 0) { PICkitFunctions.BL_WriteFlash(array); for (int j = 0; j < array.Length; j++) { array[j] = byte.MaxValue; } } flag = ((num2 & 0x10) == 16); if (num3 == 0 && num2 >= 8192 && num2 < 32736) { if (!flag) { int num4 = num2 & 0xFFE0; array[0] = (byte)(num4 & 0xFF); array[1] = (byte)((num4 >> 8) & 0xFF); array[2] = 0; } if (text.Length >= 11 + 2 * num) { int num5 = num2 & 0xF; int num6 = num5 + num; int num7 = 3; if (flag) { num7 = 19; } for (int k = 0; k < 16; k++) { if (k >= num5 && k < num6) { uint num8 = uint.Parse(text.Substring(9 + 2 * (k - num5), 2), NumberStyles.HexNumber); array[num7 + k] = (byte)(num8 & 0xFF); } } } } if (num3 == 1) { break; } } text = textReader.ReadLine(); } PICkitFunctions.BL_WriteFlash(array); textReader.Close(); return(true); } catch { return(false); } }