private void Looper_Tick(object sender, EventArgs e) { if (SerPort.IsOpen) { while (SerPort.BytesToRead > 0) { int cint = SerPort.ReadByte(); char c = Convert.ToChar(cint); str += c; if (c == '\n') { LogList.Items.Insert(0, str.Substring(0, str.Length - 2)); str = ""; } } } else { try { SerPort.Open(); LogList.Items.Insert(0, "Port Open"); } catch { LogList.Items.Insert(0, "Cannot Open Port"); } } }
private void PortChecker_Tick(object sender, EventArgs e) { if (SerPort.IsOpen) { if (SerPort.BytesToRead > 0) { bot_status = (int)SerPort.ReadByte(); } BotCtrl(); } }
private void Player_Tick(object sender, EventArgs e) { if (SerPort.IsOpen) { if (SerPort.BytesToRead > 0) { bot_status = (int)SerPort.ReadByte(); if (bot_status == 1 && is_playing) { event_cnt = 0; SendNextInstruct(); stop_watch.Reset(); } else if (bot_status == 0 && is_playing) { SendNextInstruct(); } else if (bot_status == 2) { is_playing = false; event_cnt = 0; } else if (bot_status == 3) { stop_watch.Reset(); stop_watch.Start(); } else if (bot_status == 4) { stop_watch.Stop(); if (is_cali) { CalcAutoAdj(); ApplyToList(); } } } } if (is_cali) { time_taken = Convert.ToDouble(stop_watch.ElapsedMilliseconds) / (double)1000; } else { time_taken_tp = Convert.ToDouble(stop_watch.ElapsedMilliseconds) / (double)1000; } }
private void StartBtn_Click(object sender, EventArgs e) { LoadProgress.Value = 0; if (!File.Exists(FilePathTxt.Text)) { Log("File Does Not Exist"); bootload_enabled = false; ButtonsSet(true, true, false); return; } AddToList(ref FilePathTxt, FilePathTxt.Text); for (int i = 0; i < 65536; i++) { flash_data[i] = 0xFF; } StreamReader sr; sr = new StreamReader(FilePathTxt.Text); string str = ""; bool fail_flag = false; do { try { str = sr.ReadLine(); if (str == null) { Log("End of File Reached"); } int record_type = Convert.ToInt32(str.Substring(7, 2), 16); if (record_type == 0) { int byte_cnt = Convert.ToInt32(str.Substring(1, 2), 16); int addr = Convert.ToInt32(str.Substring(3, 4), 16); for (int i = 0, j = 9; i < byte_cnt; i++, j += 2) { flash_data[addr + i] = Convert.ToInt16(str.Substring(j, 2), 16); if ((addr + i) > last_addr) { last_addr = (short)(addr + i); } } } else if (record_type == 1) { Log("End of Flash Reached"); break; } } catch { fail_flag = true; } }while (str != null); if (fail_flag) { Log("File Not Read"); bootload_enabled = false; ButtonsSet(true, true, false); } else { Log("File Successfully Read"); Log("Waiting for MCU Ready Signal"); bootload_enabled = true; ButtonsSet(false, false, true); previous_addr = -1; retry_cnt = 0; page_num = (short)((last_addr + 256) / 256); LoadProgress.Maximum = page_num * 256; page_num++; if (SerPort.IsOpen) { while (SerPort.BytesToRead > 0) { SerPort.ReadByte(); } } } sr.Close(); }
private void ProcessSend() { if (SerPort.IsOpen == false || bootload_enabled == false) { Thread.Sleep(250); return; } while (SerPort.BytesToRead >= 2) { while (SerPort.BytesToRead != 2) { SerPort.ReadByte(); } int c0 = SerPort.ReadByte(); int c1 = SerPort.ReadByte(); int addr = c0 + (c1 << 8); if (addr == 0xFFFF) { stopwatch.Start(); byte[] b = new byte[2] { (byte)(page_num % 256), (byte)(page_num >> 8), }; SerPort.Write(b, 0, 2); Log("MCU Sent Ready Signal"); Log("Replied Number of Pages: " + Convert.ToString(page_num, 10)); } else if (addr == page_num) { stopwatch.Stop(); long time_ms = stopwatch.ElapsedMilliseconds; stopwatch.Reset(); double time_s = (double)time_ms / 1000.0; Log("Bootloader Finished in " + Convert.ToString(time_s) + " Seconds"); bootload_enabled = false; ButtonsSet(true, true, false); } else { if (previous_addr == addr) { Log("Page Re-requested: " + Convert.ToString(addr, 10)); retry_cnt++; } else { retry_cnt = 0; } if ((previous_addr == addr || previous_addr + 1 == addr) && retry_cnt < 3) { previous_addr = (short)addr; checksum = 0; byte[] b = new byte[256]; while ((SerPort.WriteBufferSize - 256) < SerPort.BytesToWrite) { ; } if (page_num < 6) { for (int i = addr * 256, j = 0; j < 256; i++, j++) { b[0] = (byte)flash_data[i]; checksum += flash_data[i]; SerPort.Write(b, 0, 1); ProgressSet(Convert.ToInt32(Math.Min(i, LoadProgress.Maximum))); } } else { for (int i = addr * 256, j = 0; j < 256; i++, j++) { b[j] = (byte)flash_data[i]; checksum += flash_data[i]; } SerPort.Write(b, 0, 256); ProgressSet(Convert.ToInt32(Math.Min((addr * 256) + 256, LoadProgress.Maximum))); } b[0] = (byte)(checksum % 256); b[1] = (byte)(checksum >> 8); SerPort.Write(b, 0, 2); } else { Log("Fatal Error, Received: 0x" + Convert.ToString(addr, 16).ToUpper() + ", Checksum was: 0x" + Convert.ToString(checksum, 16).ToUpper() + ", Retried: " + Convert.ToString(retry_cnt)); bootload_enabled = false; ButtonsSet(true, true, false); } } } }
private void PortProcessor_Tick(object sender, EventArgs e) { if (SerPort.IsOpen) { int c; while (SerPort.BytesToRead != 0) { c = SerPort.ReadByte(); if (c >= 0x80) { addr = (c & 0x7F) >> 1; data = 0; cnt = 0; sign_flag = c % 2; } else { data += (c << (7 * cnt)); cnt++; if (cnt == 5) { double data_; if ((c & 0x40) == 0x40) { data -= (c << (7 * (cnt - 1))); if ((c & 0x20) == 0x20) { data_ = (double)data * Math.Pow(10, -1 * (double)(c & 0x1F)); } else { data_ = (double)data * Math.Pow(10, (double)(c & 0x1F)); } } else { data_ = (double)data; } if (sign_flag != 0) { data_ *= -1; } try { DataList.Items[addr].SubItems[1].Text = Convert.ToString(data_); } catch { } } } } } else { SerPort.BaudRate = 19200; try { SerPort.PortName = "COM6"; SerPort.Open(); } catch { try { SerPort.PortName = "COM3"; SerPort.Open(); } catch { } } } }