public TimerDeviceImpl(IProtocol protocol, ITimer timer = null) { _protocol = protocol; _currentPacket = null; if (timer == null) timer = new Timer(); _timer = timer; _timer.Elapsed += TimerForTimeouts_Elapsed; _protocol.IncomingData += ProtocolOnIncomingData; _stream = null; }
private void TimerForTimeouts_Elapsed(object sender, TimerEventArg e) { const string errorString = "Таймаут при ожидании ответа от устройства"; _timer.Stop(); if (_currentPacket == null) { _protocol.Close(); ErrorHandler(this, InternalErrorString); return; } if (_currentPacket.RetryCount != -1) { --_currentPacket.RetryCount; if (_currentPacket.RetryCount <= 0) { _currentPacket = null; _protocol.Close(); ErrorHandler(this, errorString); return; } SendCurrentPacket(); } else { SendCurrentPacket(); } }
private void SendNextPacket() { var binaryPacketData = GetNextPacket(); if (binaryPacketData == null) { _protocol.Close(); FinishedHandler(this); return; } _process += 1; ProcessHandler(this, _process); var p = IsNextPacketPresent() ? MiddlePacketStamp : LastPacketStamp; var packet = new Packet { WaitResponseTimeout = p.WaitResponseTimeout, DataBytes = binaryPacketData, OkResponse = p.OkResponse, BadResponse = p.BadResponse, RetryCount = p.RetryCount }; _currentPacket = packet; SendCurrentPacket(); }
private void InitPacketStamps() { FirstPacketStamp = new Packet { BadResponse = "BAD", OkResponse = "GO", DataBytes = null, RetryCount = -1, WaitResponseTimeout = 3000, DelayBetweenPacket = 300 }; MiddlePacketStamp = new Packet { BadResponse = "BAD", OkResponse = "GO", DataBytes = null, RetryCount = -1, WaitResponseTimeout = 1000, DelayBetweenPacket = 300 }; LastPacketStamp = new Packet { BadResponse = "BAD", OkResponse = "GO", DataBytes = null, RetryCount = -1, WaitResponseTimeout = 10000, DelayBetweenPacket = 300 }; }
public bool StartFlashing(Stream stream) { _currentLine = ""; _stream = stream; _process = 0; const string errorString = @"Ошибочный файл."; if (!_protocol.Open()) return false; InitPacketStamps(); var xmlString = ReadXmlSettingFromFirmwareFile(stream); if (!GetPacketParametrsByXml(xmlString)) { ErrorHandler(this, errorString); _protocol.Close(); return false; } var packetCount = (stream.Length - xmlString.Length -1 ) / PacketLenght; PacketHandler(this, packetCount, PacketLenght); ProcessHandler(this, _process); var buf = new byte[PacketLenght]; stream.Read(buf, 0, PacketLenght); var packet = new Packet { BadResponse = FirstPacketStamp.BadResponse, DataBytes = buf, OkResponse = FirstPacketStamp.OkResponse, RetryCount = FirstPacketStamp.RetryCount, WaitResponseTimeout = FirstPacketStamp.WaitResponseTimeout }; _currentPacket = packet; SendCurrentPacket(); return true; }
public void TestPacket() { var p = new Packet(); var data = new byte[0x200]; const string okResponce = "OK"; const string badResponce = "BAD"; const int waitResponce = 3000; const int retryCount = 3; p.DataBytes = data; p.OkResponse = okResponce; p.BadResponse = badResponce; p.WaitResponseTimeout = waitResponce; p.RetryCount = retryCount; Assert.AreEqual(p.DataBytes, data); Assert.AreEqual(p.OkResponse, okResponce); Assert.AreEqual(p.BadResponse, badResponce); Assert.AreEqual(p.WaitResponseTimeout, waitResponce); Assert.AreEqual(p.RetryCount, retryCount); }