Пример #1
0
 public void TestDeviceImplementation()
 {
     var xmlString = File.ReadAllText(@"xml_loader_prefix.xml");
     const int packetLenght = 34;
     const int packetCount = 0x100;
     var buffer = new Byte[packetLenght * packetCount + xmlString.Length + 1];
     Array.Copy(Encoding.ASCII.GetBytes(xmlString), buffer, xmlString.Length);
     buffer[xmlString.Length] = 0x00;
     var memoryStream = new MemoryStream(buffer);
     var protocol = new FakeProtocol();
     var timer = new FakeTimer();
     var device = new TimerDeviceImpl(protocol, timer) {PacketLenght = packetLenght};
     _errorString = "";
     _finished = false;
     device.FinishedHandler += device_FinishedHandler;
     device.ErrorHandler += device_ErrorHandler;
     device.ProcessHandler += device_ProcessHandler;
     device.PacketHandler += device_PacketHandler;
     Assert.IsTrue(device.StartFlashing(memoryStream));
     for (var i = 0; i < 100; ++i)
         timer.Advance(3500);
     var iterCount = 0;
     while (!_finished) {
         protocol.Process();
         iterCount++;
         if (iterCount == 100)
             timer.Advance(1001);
     }
     Assert.AreEqual("", _errorString);
     Assert.AreEqual(packetCount + 1 + 100, protocol.PacketCount);
 }
 void bgWorker_DoWork(object sender, DoWorkEventArgs e)
 {
     var worker = sender as BackgroundWorker;
     SetMaxValueForProgressBar(1000);
     SetValueForProgressBar(0);
     Debug.WriteLine(String.Format("background: {0}", Thread.CurrentThread.ManagedThreadId));
     if (worker == null)
         return;
     var portSetting = e.Argument as SerialPortSetting;
     if (portSetting == null)
         return;
     SetTextForProgressBar("Открываем порт " + portSetting.PortName);
     var device = new TimerDeviceImpl(new SerialProtocol(portSetting.PortName, portSetting.Baudrate));
     device.ProcessHandler += device_ProcessHandler;
     device.ErrorHandler += device_ErrorHandler;
     device.FinishedHandler += device_FinishedHandler;
     device.PacketHandler += device_PacketHandler;
     try {
         using (var stream = new FileStream(_hexFilename, FileMode.Open)) {
             if (!device.StartFlashing(stream)) {
                 e.Result = "Ошибка открытия последовательного порта";
                 return;
             }
             SetTextForProgressBar("Ожидаем ответа от таймера");
             _inProcess = true;
             _resultString = "";
             while (_inProcess) {
                 Thread.Sleep(20);
             }
         }
     } catch (Exception) {
         e.Result = "Не получилось открыть файл";
     }
     e.Result = _resultString;
 }