/// <summary> /// 扫描节点 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void scanNode(FormProgress sender, DoWorkEventArgs e) { for (int i = ScanStartAddr; i <= ScanEndAddr; i++) { int ret; UInt32[] appVersion = new UInt32[1], appType = new UInt32[1]; ret = USB2CAN.CAN_BL_NodeCheck(DeviceIndex, CANIndex, (UInt16)i, appVersion, appType, 10); if (ret == USB2CAN.CAN_SUCCESS) { this.listViewNodeList.BeginUpdate(); //数据更新,UI暂时挂起,直到EndUpdate绘制控件,可以有效避免闪烁并大大提高加载速度 ListViewItem lvi = new ListViewItem(); lvi.Text = i.ToString(); if (appType[0] == USB2CAN.CAN_BL_BOOT) { lvi.SubItems.Add("ROOT"); } else { lvi.SubItems.Add("APP"); } lvi.SubItems.Add(String.Format("v{0}.{1}", (((appVersion[0] >> 24) & 0xFF) * 10) + (appVersion[0] >> 16) & 0xFF, (((appVersion[0] >> 8) & 0xFF) * 10) + appVersion[0] & 0xFF)); this.listViewNodeList.Items.Add(lvi); this.listViewNodeList.EndUpdate(); //结束数据处理,UI界面一次性绘制。 } sender.SetProgress(i - ScanStartAddr + 1, "正在检测节点:" + i.ToString()); if (sender.CancellationPending) { e.Cancel = true; return; } } }
void upgrateFirmware(FormProgress sender, DoWorkEventArgs e) { int FirmwareFileSize; UInt16 NodeAddr; int ret; //如果文件不存在,就提示错误 if (File.Exists(this.textBoxFilePath.Text)) { BinaryReader br; FirmwareFileSize = (int)new FileInfo(this.textBoxFilePath.Text).Length; try { //将固件文件读取到数据缓冲区中 br = new BinaryReader(new FileStream(this.textBoxFilePath.Text, FileMode.Open)); //判断当前所选择的节点 if (this.listViewNodeList.Items.Count <= 0) { //当前节点列表中没有节点 MessageBox.Show(this, "节点列表中无节点,请先扫描节点!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning); br.Close(); return; } else { if (this.checkBoxAllNode.Checked) { NodeAddr = 0; } else { if (this.listViewNodeList.SelectedIndices[0] < 0) { //没有选择任何节点 MessageBox.Show(this, "请先选择节点!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning); br.Close(); return; } else { NodeAddr = UInt16.Parse(this.listViewNodeList.Items[this.listViewNodeList.SelectedIndices[0]].SubItems[0].Text); } } } //MessageBox.Show(this, "NodeAddr = " + NodeAddr, "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning); //发送跳转到Bootloader的命令 sender.SetProgress(0, "跳转到Bootloader..."); ret = USB2CAN.CAN_BL_Excute(DeviceIndex, CANIndex, NodeAddr, USB2CAN.CAN_BL_BOOT); if (ret != USB2CAN.CAN_SUCCESS) { MessageBox.Show(this, "执行固件跳转命令失败!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning); br.Close(); return; } //延时 System.Threading.Thread.Sleep(500); //检测当前节点是否为Bootloader if (!this.checkBoxAllNode.Checked) { UInt32[] appVersion = new UInt32[1], appType = new UInt32[1]; ret = USB2CAN.CAN_BL_NodeCheck(DeviceIndex, CANIndex, NodeAddr, appVersion, appType, 10); if (ret != USB2CAN.CAN_SUCCESS) { MessageBox.Show(this, "节点检测失败!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning); br.Close(); return; } else { if (appType[0] != USB2CAN.CAN_BL_BOOT) { MessageBox.Show(this, "当前节点固件类型不是Bootloader,请重新点击升级固件!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning); br.Close(); return; } } } //发送擦出固件命令 sender.SetProgress(0, "擦出固件..."); ret = USB2CAN.CAN_BL_Erase(DeviceIndex, CANIndex, NodeAddr, (UInt32)FirmwareFileSize, 10000); if (ret != USB2CAN.CAN_SUCCESS) { MessageBox.Show(this, "擦出固件失败!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning); br.Close(); return; } if (this.checkBoxAllNode.Checked) { System.Threading.Thread.Sleep(1000); } //循环发送数据 UInt32 AddrOffset = 0; UInt32 PackSize = 512; byte[] DataBuffer = new byte[PackSize]; for (AddrOffset = 0; AddrOffset < FirmwareFileSize; AddrOffset += PackSize) { UInt32 read_data_num = (UInt32)br.Read(DataBuffer, 0, (int)PackSize); ret = USB2CAN.CAN_BL_Write(DeviceIndex, CANIndex, NodeAddr, AddrOffset, DataBuffer, read_data_num, 1000); if (ret != USB2CAN.CAN_SUCCESS) { MessageBox.Show(this, "写数据失败!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning); br.Close(); return; } if (this.checkBoxAllNode.Checked) { System.Threading.Thread.Sleep(20); } sender.SetProgress((int)(AddrOffset + read_data_num), "正在发送固件数据..."); } //执行固件 ret = USB2CAN.CAN_BL_Excute(DeviceIndex, CANIndex, NodeAddr, USB2CAN.CAN_BL_APP); if (ret != USB2CAN.CAN_SUCCESS) { MessageBox.Show(this, "执行固件跳转命令失败!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning); br.Close(); return; } System.Threading.Thread.Sleep(100); //获取固件信息 if (!this.checkBoxAllNode.Checked) { UInt32[] appVersion = new UInt32[1], appType = new UInt32[1]; ret = USB2CAN.CAN_BL_NodeCheck(DeviceIndex, CANIndex, NodeAddr, appVersion, appType, 10); if (ret == USB2CAN.CAN_SUCCESS) { string AppTypeStr; if (appType[0] == USB2CAN.CAN_BL_BOOT) { AppTypeStr = "BOOT"; } else { AppTypeStr = "APP"; } this.listViewNodeList.Items[this.listViewNodeList.SelectedIndices[0]].SubItems[1].Text = AppTypeStr; string AppVersionStr; AppVersionStr = String.Format("v{0}.{1}", (((appVersion[0] >> 24) & 0xFF) * 10) + (appVersion[0] >> 16) & 0xFF, (((appVersion[0] >> 8) & 0xFF) * 10) + appVersion[0] & 0xFF); this.listViewNodeList.Items[this.listViewNodeList.SelectedIndices[0]].SubItems[2].Text = AppVersionStr; } } br.Close(); } catch (IOException ep) { MessageBox.Show(this, ep.Message, "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } } else { MessageBox.Show(this, "无法打开固件文件,是否选择了固件文件?", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning); } }