private bool ReopenPort() { bool result = false; InvokeWriteStateLbl("Reopening port"); InvokeSwitchProgressBarMode(ProgressBarStyle.Marquee); InvokeWriteLogString("Reopening port...\r\n"); try { nmeaPort.Close(); nmeaPort.PortBaudRate = BaudRate.baudRate115200; nmeaPort.IsRawModeOnly = true; Thread.Sleep(1000); nmeaPort.Open(); Thread.Sleep(2000); action = UCNL_FW_ACTION.FW_UPDATE_INIT; result = RequestTransferSize(); } catch (Exception ex) { ProcessException(ex, false); } return(result); }
private void startUploadingBtn_Click(object sender, EventArgs e) { SetCtrlState(false); startUploadingBtn.Enabled = false; if ((nmeaPort != null) && (nmeaPort.IsOpen)) { try { nmeaPort.Close(); } catch (Exception ex) { ProcessException(ex, false); } } nmeaPort = new NMEASerialPort(new SerialPortSettings(PortName, InitialBaudrate, Parity.None, DataBits.dataBits8, StopBits.One, Handshake.None)); nmeaPort.NewNMEAMessage += new EventHandler <NewNMEAMessageEventArgs>(nmeaPort_NewMessage); nmeaPort.PortError += new EventHandler <SerialErrorReceivedEventArgs>(nmeaPort_PortError); nmeaPort.RawDataReceived += new EventHandler <RawDataReceivedEventArgs>(nmeaPort_RawData); try { nmeaPort.Open(); } catch (Exception ex) { ProcessException(ex, true); SetCtrlState(true); startUploadingBtn.Enabled = true; } if (!startUploadingBtn.Enabled) { packetIdx = 0; repeats = 0; if (isEmptyDeviceChb.Checked) { action = UCNL_FW_ACTION.FW_UPDATE_INIT; nmeaPort.IsRawModeOnly = true; RequestTransferSize(); } else { action = UCNL_FW_ACTION.REQUEST_DEVICE_INFO; nmeaPort.IsRawModeOnly = false; RequestDevInfo(); } } }
private void UpdateFinished(string description) { action = UCNL_FW_ACTION.IDLE; InvokeWriteStateLbl("Idle"); InvokeSwitchProgressBarMode(ProgressBarStyle.Continuous); InvokeSetProgressBarValue(0); timer.Stop(); InvokeWriteLogString(string.Format("Firmware update finished: {0}\r\n", description)); if (this.InvokeRequired) { this.Invoke((MethodInvoker) delegate { SetCtrlState(true); }); } else { SetCtrlState(true); } InvokeCtrlEnabled(startUploadingBtn, true); }
private void nmeaPort_RawData(object sender, RawDataReceivedEventArgs e) { if (nmeaPort.IsRawModeOnly) { timer.Stop(); int result = e.Data[0]; if (result == 49) { repeats = 0; List <byte> tpacket = new List <byte>(); for (int n = 0; (n < packetSize) && (fw_bytes_rPos < fw_bytes.Length); n++) { tpacket.Add(fw_bytes[fw_bytes_rPos++]); } packetCrc = crc.CRC32(0, tpacket.ToArray()); tpacket.AddRange(BitConverter.GetBytes(packetCrc)); action = UCNL_FW_ACTION.FW_BLOCK; InvokeWriteLogString("DEVICE: Next packet\r\n"); if (fw_bytes_rPos >= fw_bytes.Length) { tpacket.AddRange(fw_tableOffsets); } packet = tpacket.ToArray(); RequestTransferBlock(); if (packetIdx == 0) { InvokeSwitchProgressBarMode(ProgressBarStyle.Continuous); } InvokeSetProgressBarValue(packetIdx); if (packetIdx < totalPackets) { packetIdx++; } } else if (result == 50) { action = UCNL_FW_ACTION.FW_BLOCK; RequestTransferBlock(); InvokeWriteLogString("DEVICE: Bad packet\r\n"); } else if (result == 51) { UpdateFinished("SUCCESS"); } else if (result == 52) { // 4 UpdateFinished("FAIL: device timeout (4)"); } else if (result == 53) { // 5 UpdateFinished("FAIL: Firmware integrity error. Try again with \"Device is empty option\""); } else { UpdateFinished(string.Format("FAIL: ({0})", result)); } } }
private void nmeaPort_NewMessage(object sender, NewNMEAMessageEventArgs e) { NMEAProprietarySentence nmeaMsg = new NMEAProprietarySentence(); bool isParsed = false; InvokeWriteLogString(string.Format("<< {0}", e.Message)); #region try parse try { var tempMsg = NMEAParser.Parse(e.Message); if (tempMsg is NMEAProprietarySentence) { nmeaMsg = (tempMsg as NMEAProprietarySentence); isParsed = true; } else { InvokeWriteLogString(string.Format("Skipping sentence {0}", (tempMsg as NMEAStandartSentence).SentenceID)); } } catch (Exception ex) { ProcessException(ex, false); } #endregion if (isParsed) { timer.Stop(); if (nmeaMsg.Manufacturer == ManufacturerCodes.UCN) { if (nmeaMsg.SentenceIDString == "L") { repeats = 0; UCNL_SRV_CMD cmdID = (UCNL_SRV_CMD)(int)nmeaMsg.parameters[0]; switch (cmdID) { case UCNL_SRV_CMD.DEV_INFO_VAL: { // $PUCNL,x,x,c--c InvokeWriteLogString(Utils.ParseDevInfoStr((string)nmeaMsg.parameters[2])); action = UCNL_FW_ACTION.INVOKE_FW_REQ; if (isExtraPauseBeforeFWUPDATE) { MessageBox.Show("Turn off S_MODE in your device and press OK", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); } RequestFWUpdate(IsRedNAVRedNODE); break; } case UCNL_SRV_CMD.ACK: { // $PUCNL,x,x,c--c InvokeWriteLogString("DEVICE: Firmware update mode ON\r\n"); nmeaPort.IsRawModeOnly = true; if (InitialBaudrate != BaudRate.baudRate115200) { action = UCNL_FW_ACTION.PORT_REOPENING; ReopenPort(); } else { action = UCNL_FW_ACTION.FW_UPDATE_INIT; RequestTransferSize(); } break; } } } } else { InvokeWriteLogString(string.Format("Skipping sentence {0}", nmeaMsg.SentenceIDString)); } } }