void OnTimer2(object sender, System.Timers.ElapsedEventArgs e) { lock (Locker) { _currentFlashStatus = FlasherStatus.Timeout; } }
void OnSerialDataReceived(object sender, SerialDataReceivedEventArgs e) { Debug.WriteLine(String.Format("readyread: {0}", System.Threading.Thread.CurrentThread.ManagedThreadId)); lock (Locker) { var sp = sender as SerialPort; if (sp == null) { return; } _timer.Stop(); var len = sp.Read(ReadBuf, _readBufOffset, sp.BytesToRead); _readBufOffset += len; switch (_currentFlashStatus) { case FlasherStatus.WaitReady: { var response = Encoding.ASCII.GetString(ReadBuf, 0, _readBufOffset); if (response == "bad") { _currentFlashStatus = FlasherStatus.Bad; } else if (response == "ready") { _currentFlashStatus = FlasherStatus.ReadyToSendNextPacket; _readBufOffset = 0; } else if (_readBufOffset >= 5) { _currentFlashStatus = FlasherStatus.WrongPacket; } else { _timer.Interval = 1000; _timer.Start(); } } break; case FlasherStatus.WaitResponse: { var response = Encoding.ASCII.GetString(ReadBuf, 0, _readBufOffset); if (response == "bad") { _currentFlashStatus = FlasherStatus.Bad; } else if (response == "good") { _currentFlashStatus = FlasherStatus.ReadyToSendNextPacket; _readBufOffset = 0; } else if (_readBufOffset >= 4) { _currentFlashStatus = FlasherStatus.WrongPacket; } else { _timer.Interval = 1000; _timer.Start(); } } break; case FlasherStatus.WaitLastResponse: { var response = Encoding.ASCII.GetString(ReadBuf, 0, _readBufOffset); if (response == "bad") { _currentFlashStatus = FlasherStatus.Bad; } else if (response == "good") { _currentFlashStatus = FlasherStatus.LastPacket; _readBufOffset = 0; } else if (_readBufOffset >= 4) { _currentFlashStatus = FlasherStatus.WrongPacket; } else { _timer.Interval = 1000; _timer.Start(); } } break; default: { _currentFlashStatus = FlasherStatus.WrongPacket; } break; } } }
void bgWorker_DoWork(object sender, DoWorkEventArgs e) { var worker = sender as BackgroundWorker; _readBufOffset = 0; var startAddress = (UInt32)_minAddress; var iterators = ((uint)_maxAddress - startAddress) / 128; iterators += 4; var curIter = 0; SetMaxValueForProgressBar((int)iterators); SetValueForProgressBar(curIter); Debug.WriteLine(String.Format("background: {0}", System.Threading.Thread.CurrentThread.ManagedThreadId)); if (worker == null) { return; } var portName = e.Argument.ToString(); var portStatus = String.Format("Порт {0} открыт", portName); SetTextForProgressBar(String.Format("Попытка открыть {0} порт", portName)); using (var sp = new SerialPort(portName)) { try { sp.DataReceived += OnSerialDataReceived; sp.Parity = Parity.None; sp.DataBits = 8; sp.BaudRate = 38400; sp.Handshake = Handshake.None; sp.StopBits = StopBits.One; sp.Open(); SetValueForProgressBar(++curIter); } catch (Exception) { e.Result = String.Format("Не получилось открыть {0} порт", portName); return; } SetTextForProgressBar(portStatus); _timer = new System.Timers.Timer { Interval = 5000 }; _timer.Elapsed += OnTimer2; try { sp.Write(new ASCIIEncoding().GetBytes("start"), 0, 5); _currentFlashStatus = FlasherStatus.WaitReady; _timer.Start(); var isProcess = true; var tempMaxAddress = _maxAddress; var crcIsSended = false; while (isProcess) { System.Threading.Thread.Sleep(0); switch (_currentFlashStatus) { case FlasherStatus.WaitReady: case FlasherStatus.WaitResponse: case FlasherStatus.WaitLastResponse: break; case FlasherStatus.Ready: case FlasherStatus.ReadyToSendNextPacket: { SetValueForProgressBar(++curIter); _timer.Stop(); var packet = new List <byte>(); UInt16 crc; if (startAddress >= 0x10000) { // всё, прошили if (crcIsSended) { packet.Add(6); packet.Add(4); packet.Add(0); packet.Add(0); crc = chksm(packet.ToArray()); packet.Add((byte)(crc >> 8)); packet.Add((byte)crc); codeList(ref packet); _timer.Interval = 7000; _timer.Start(); _currentFlashStatus = FlasherStatus.WaitLastResponse; sp.Write(packet.ToArray(), 0, packet.Count); break; } // посылаем crc packet.Add(10); packet.Add(3); packet.Add(0); packet.Add(0); var crcOfMemory = CalculateCrc(); for (var i = 0; i < 4; ++i) { packet.Add(crcOfMemory[i]); } crc = chksm(packet.ToArray()); packet.Add((byte)(crc >> 8)); packet.Add((byte)crc); codeList(ref packet); _timer.Interval = 7000; _timer.Start(); crcIsSended = true; _currentFlashStatus = FlasherStatus.WaitResponse; sp.Write(packet.ToArray(), 0, packet.Count); break; } if (startAddress >= tempMaxAddress) { // закончились полезные данные packet.Add(6); packet.Add(2); packet.Add((byte)_maxAddress); packet.Add((byte)(_maxAddress >> 8)); crc = chksm(packet.ToArray()); packet.Add((byte)(crc >> 8)); packet.Add((byte)crc); codeList(ref packet); startAddress = 0x10000; tempMaxAddress = 0x100000; _currentFlashStatus = FlasherStatus.WaitResponse; sp.Write(packet.ToArray(), 0, packet.Count); _timer.Interval = 7000; _timer.Start(); break; } packet.Add(134); packet.Add(1); packet.Add((byte)startAddress); packet.Add((byte)(startAddress >> 8)); for (var i = 0; i < 128; ++i) { packet.Add(_buffer[startAddress + i]); } crc = chksm(packet.ToArray()); packet.Add((byte)(crc >> 8)); packet.Add((byte)crc); codeList(ref packet); _currentFlashStatus = FlasherStatus.WaitResponse; sp.Write(packet.ToArray(), 0, packet.Count); _timer.Interval = 3000; _timer.Start(); startAddress += 128; } break; case FlasherStatus.Timeout: { isProcess = false; e.Result = "Превышено время ожидания ответа от устройства"; } break; case FlasherStatus.WrongPacket: { isProcess = false; e.Result = "Принят не верный ответ от устройства"; } break; case FlasherStatus.Bad: { isProcess = false; e.Result = "Устройство отрапортовало об ошибке"; } break; case FlasherStatus.LastPacket: { SetValueForProgressBar(++curIter); isProcess = false; e.Result = "Устройство прошито"; } break; } } SetValueForProgressBar((int)iterators); } catch (Exception exc) { e.Result = exc.Message; } finally { _timer.Stop(); sp.Close(); } } }