private void AsyncReceiveData() { if (this.InvokeRequired) { try { AsyReceiveDataHandler dd = new AsyReceiveDataHandler(AsyncReceiveData); this.Invoke(dd, new object[] { }); } catch (System.Exception ex) { ex.ToString(); MessageBox.Show("AsyncReceiveData"); } } else { string strTemp = ""; #region uploadingFirmware if (bUploadingFirmware) { douUploadingFirmware(serialPort1.ReadByte()); return; } else { strTemp = serialPort1.ReadExisting().ToString(); } #endregion if (bReqUploadingFirmware==true && strTemp == "C") { bReqUploadingFirmware = false; bUploadingFirmware = true; Console.WriteLine("Start uploading..."); } //Console.WriteLine(strTemp); #region dealwith backspace string backspaceStr = StringToHexString(strTemp); //Console.WriteLine(backspaceStr); if ((strTemp.Length == 1 && backspaceStr == "08") || (strTemp.Length == 2 && backspaceStr == "2008") || (strTemp.Length == 3 && backspaceStr == "082008")) { if (Environment.TickCount - lastBackSpaceTimeTick < 50) return; lastBackSpaceTimeTick = Environment.TickCount; txtSP.Text = txtSP.Text.Remove(txtSP.Text.Length - 1, 1); this.txtSP.SelectionStart = this.txtSP.Text.Length; this.txtSP.ScrollToCaret(); return; } #endregion if (!fileIsUploading && !bIsGetingFileList) { strTemp = strTemp.Replace("\n\r", "\r\n"); this.txtSP.AppendText(strTemp); } if (strTemp.IndexOf("\r\n")<0) { lastgetSerialBuffTick = Environment.TickCount; strSerialBuff = strSerialBuff+strTemp; return; } strSerialBuff = strSerialBuff + strTemp; if (strSerialBuff.IndexOf("file.rename") >= 0 || strSerialBuff.IndexOf("file.remove") >= 0 || strSerialBuff.IndexOf("file.compile") >= 0) this.btnFilelist.PerformClick(); #region dealwith uploading if (fileIsUploading == true) { //Console.WriteLine(strSerialBuff); if (strSerialBuff.IndexOf("stdin:1") >= 0) { fileIsUploading = false; toolStripStatusLabel1.Text = "Upload file failed"; this.btnUpload.Text = "Upload"; sendData2SerialPort("\r\n"); txtSP.AppendText("\r\n Error happened, stop download!\r\n"); MessageBox.Show(this, "File: '" + fileUploadFileName + "' upload failed", "Stop", MessageBoxButtons.OK, MessageBoxIcon.Stop); //this.btnFilelist.PerformClick(); strSerialBuff = ""; return; } if (indexFileData == stringFileData.Length) {//send ok sendData2SerialPort("file.close();s=nil;\r\n"); indexFileData++; txtSP.AppendText("."); //txtSP.AppendText(strSerialBuff); toolStripProgressBar1.Value = 99; toolStripStatusLabel2.Text = "99%"; } else if (indexFileData > stringFileData.Length) { fileIsUploading = false; txtSP.AppendText("\r\n-->Upload file: '" + fileUploadFileName + "' successful<--\r\n"); sendData2SerialPort("\r\n"); //this.Enabled = true; toolStripStatusLabel1.Text = "Upload Successfully"; toolStripProgressBar1.Value = 100; toolStripStatusLabel2.Text = "100%"; this.btnUpload.Text = "Upload"; this.btnFilelist.PerformClick(); } else { String sdata = stringFileData[indexFileData++]; fileUploadTimeTick = Environment.TickCount; //Thread.Sleep(10); //txtSP.AppendText("Recieve: " + strSerialBuff); //txtSP.AppendText("Send: s(\'" + sdata + "')\r\n"); //Console.WriteLine("Recieve: " + strSerialBuff); //Console.WriteLine("Send: s(\'" + sdata + "')\r\n"); if (txtSP.Lines[txtSP.Lines.Length - 1].Length > 30) { txtSP.AppendText("\r\n"); } txtSP.AppendText("."); string strData = "s('" + sdata + "')\r\n"; Thread.Sleep(50); sendData2SerialPort(strData); string pg = string.Format("{0:G}", indexFileData * 100 / stringFileData.Length); toolStripProgressBar1.Value = indexFileData * 100 / stringFileData.Length; toolStripStatusLabel2.Text = pg + "%"; } strSerialBuff = ""; return; } #endregion #region dealwith geting filelist if (bIsGetingFileList == true) { //txtSP.AppendText(strSerialBuff); lGetingFileListTimeTick = Environment.TickCount; fileListString = fileListString + strSerialBuff; strSerialBuff = ""; return; } #endregion strSerialBuff = ""; } }
private void AsyncReceiveData() { if (this.InvokeRequired) { try { AsyReceiveDataHandler dd = new AsyReceiveDataHandler(AsyncReceiveData); this.Invoke(dd, new object[] { }); } catch (System.Exception ex) { Console.WriteLine(ex.ToString()); MessageBox.Show("InvokeRequired" + ex.ToString()); } } else { if (bReqUploadingFirmware == true && serialPort1.ReadByte() == 'C') { bReqUploadingFirmware = false; bUploadingFirmware = true; Console.WriteLine("Start uploading..."); } else if (bUploadingFirmware) { doUploadingFirmware(serialPort1.ReadByte()); } else { int len = serialPort1.BytesToRead; byte[] bytes = new byte[len]; serialPort1.Read(bytes, 0, len); string strTemp = System.Text.Encoding.UTF8.GetString(bytes); strSerialBuffTick = Environment.TickCount; strSerialBuff = strSerialBuff + strTemp; recvSerData = recvSerData + (UInt32)strTemp.Length; rxReadBytes.Text = recvSerData.ToString(); } } }