private Guid AddElement ( string Marker, DebuggerPrimitive.ActionType Queuetype, MemoryType Memorytype, int Address, byte[] Recording_Buffe, int Count, bool CycleElement, int Period, ProceedingCompleetedDelegate Callback, ProceedingProgressChangedDelegate PartCallback) { Guid m_ret = Guid.NewGuid(); Trace.WriteLine("Добавление запроса в очередь :" + m_ret.ToString(), "DebuggerEngine"); DebuggerPrimitive.QueueElement m_newQuery = new DebuggerPrimitive.QueueElement(); if (Count > BufferSize) { //Добавляем часть для чтения частями m_newQuery.isReadLongBuffer = true; m_newQuery.ReadLongBuffer = new DebuggerPrimitive.ReadLongData(Count); m_newQuery.iAddress = Address; m_newQuery.PartCallBack = PartCallback; } else { m_newQuery.isReadLongBuffer = false; m_newQuery.ReadLongBuffer = null; } m_newQuery.Marker = Marker; m_newQuery.Period = Period; m_newQuery.ID = m_ret; m_newQuery.Address = Address; m_newQuery.Callback = Callback; m_newQuery.Count = Count; m_newQuery.CycleElement = CycleElement; m_newQuery.Memorytype = Memorytype; m_newQuery.Queuetype = Queuetype; m_newQuery.Recording_Buffer = Recording_Buffe; //Блокируем список и добавляем элемент lock (_AddElementInQueue) { _AddElementInQueue.Add(m_newQuery); } return(m_ret); }
/// <summary> /// Поток опроса контроллера /// </summary> /// <param name="Sender">Указатель на порт</param> private void Questioning_TH() { Trace.WriteLine("Запуск потока опроса.", "DebuggerEngine"); this.RaiseEngineStatusChangedEvent(new DebuggerEngineStatusChangedEventArgs(DebuggerEngineStatus.Worked, null)); DateTime m_STime = DateTime.Now; Trace.WriteLine("Открытие порта " + port.PortName, "DebuggerEngine"); try { m_port.Open(); } catch (Exception ex) { this.RaiseEngineStatusChangedEvent(new DebuggerEngineStatusChangedEventArgs(new Exception(Error = "Ошибка окрытия порта: " + ex.Message), DebuggerEngineStatus.Stopped)); goto end; } //Проверка на наличие контроллера String m_retType = m_port.ReadControllerType(); if ( m_type == ProcessorType.AT89C51ED2 && m_retType != "89C51ED2" || m_type == ProcessorType.MB90F347 && m_retType == "89C51ED2" || m_retType == null ) { this.RaiseEngineStatusChangedEvent(new DebuggerEngineStatusChangedEventArgs(new Exception("Контроллер не найден"), DebuggerEngineStatus.Stopped)); goto end; } // Определение размеров буфера if (this.Parameters.ProcessorType == ProcessorType.AT89C51ED2) { this._BufferSize = 8; } else if (this.Parameters.ProcessorType == ProcessorType.MB90F347) { try { this._BufferSize = ((Relkon4SerialPort)this._serialPort).BufferSize = this.ReadMB90F347BufferSize(this._serialPort); } catch (Exception ex) { Trace.WriteLine("Ошибка чтения размера буфера с контроллера: " + ex.Message); this._BufferSize = ((Relkon4SerialPort)this._serialPort).BufferSize = 64; } } while (true) { //Добавление новых элементов if (this.EngineStatus == DebuggerEngineStatus.Stopping) { goto end; } if (_AddElementInQueue.Count != 0) { lock (_AddElementInQueue) { foreach (DebuggerPrimitive.QueueElement m_Add in _AddElementInQueue) { _Queue.Enqueue(m_Add); _countElementInQueue++; } List <int> t; _AddElementInQueue.Clear(); } } //Задержка когда отладчик работает, но ничего не делает if (_Queue.Count == 0 || _statusEngine != DebuggerEngineStatus.Worked) { Thread.Sleep(500); this.RaiseRequestTimeChangedEvent(new EventArgs <int>(0)); continue; } DebuggerPrimitive.QueueElement m_Queue = new DebuggerPrimitive.QueueElement(); //m_Queue.CycleElement = false; try { m_Queue = _Queue.Dequeue(); //Удаление элементов из очереди if (_AddElementOutQueue.Count != 0 && _AddElementOutQueue.Contains(m_Queue.ID)) { lock (_AddElementOutQueue) { m_Queue.CycleElement = false; _AddElementOutQueue.Remove(m_Queue.ID); m_Queue.isRuningReadLongBuffer = false; continue; } IList <int> i; } if (m_Queue.Period != 1 && !m_Queue.isRuningReadLongBuffer) { if (m_Queue.CurrentCycle != 0) { m_Queue.CurrentCycle--; continue; } else { m_Queue.CurrentCycle = m_Queue.Period; } } m_STime = DateTime.Now; switch (m_Queue.Queuetype) { case DebuggerPrimitive.ActionType.Reading: #region Чтение данных из контроллера if (this.EngineStatus == DebuggerEngineStatus.Stopping) { goto end; } try { int m_Address = 0; int m_Count = 0; if (m_Queue.isReadLongBuffer) { m_Address = m_Queue.iAddress; m_Count = Math.Min(_BufferSize, m_Queue.Count - (m_Queue.iAddress - m_Queue.Address)); m_Queue.isRuningReadLongBuffer = true; } else { m_Address = m_Queue.Address; m_Count = m_Queue.Count; } Byte[] m_ret = null; //Запрос к контроллеру try { m_ret = m_port.ReadFromMemory(m_Queue.Memorytype, m_Address, (Int32)m_Count); } catch (Exception) { } //Если мы не получили ответ if (m_ret != null && m_Queue.Callback != null) { _ReadPacket++; //Загрузка большого размера архива if (m_Queue.isReadLongBuffer) { m_Queue.ReadLongBuffer.SetSegmentArray(m_ret, (Int16)(m_Queue.iAddress - m_Queue.Address)); if (m_Queue.PartCallBack != null) { double value = ((double)(m_Queue.iAddress - m_Queue.Address)) / m_Queue.Count; this.asyncOp.Post(this.exReadPartDelegate, new object[] { m_Queue.PartCallBack, new object[] { value, m_Queue.iAddress, m_ret } }); } if (m_Count == _BufferSize) { m_Queue.iAddress = (m_Queue.iAddress + m_Count); if (!m_Queue.CycleElement) { _Queue.Enqueue(m_Queue); } } else { m_Queue.isRuningReadLongBuffer = false; m_Queue.iAddress = m_Queue.Address; this.asyncOp.Post(this.exReadPartDelegate, new object[] { m_Queue.CallBack, new object[] { m_Queue.Marker, m_Queue.ReadLongBuffer.ReadBuffer } }); } continue; } //Обратный вызов this.asyncOp.Post(this.exReadPartDelegate, new object[] { m_Queue.CallBack, new object[] { m_Queue.Marker, m_ret } }); } else { //Загрузка большого размера архива if (m_Queue.isReadLongBuffer && !m_Queue.CycleElement) { _Queue.Enqueue(m_Queue); } _ErrorReadPacket++; //Обратный вызов this.asyncOp.Post(this.exReadPartDelegate, new object[] { m_Queue.CallBack, new object[] { m_Queue.Marker, new Bytep[0] } }); } } catch (Exception Ex) { Trace.WriteLine(Ex.Message, "DebuggerEngine"); //Если произошла ошибка помещаем элемент в очередь if (!m_Queue.CycleElement) { _Queue.Enqueue(m_Queue); } _ErrorReadPacket++; } #endregion break; case DebuggerPrimitive.ActionType.Writing: #region Запись данный в контроллер if (this.EngineStatus == DebuggerEngineStatus.Stopping) { goto end; } try { m_port.WriteToMemory(m_Queue.Memorytype, m_Queue.Address, m_Queue.Recording_Buffer); _WritePacket++; } catch { _ErrorWritePacket++; } #endregion break; } } catch (Exception Ex) { Trace.WriteLine("Exception " + Ex.Message, "DebuggerEngine"); } finally { int m_Seconds = new DateTime(DateTime.Now.Ticks - m_STime.Ticks).TimeOfDay.Milliseconds; this.RaiseRequestTimeChangedEvent(new EventArgs <int>(m_Seconds)); if (m_Queue.CycleElement) { _Queue.Enqueue(m_Queue); } else if ((!m_Queue.isRuningReadLongBuffer)) { _countElementInQueue--; } } this.RaiseEngineStatusChangedEvent(new DebuggerEngineStatusChangedEventArgs(DebuggerEngineStatus.Stopped, null)); } end: this.asyncOp.PostOperationCompleted(null, null); _Queue.Clear(); _countElementInQueue = 0; Trace.WriteLine("Закрытие порта " + m_port.PortName, "DebuggerEngine"); m_port.Close(); this.EngineStatus = DebuggerEngineStatus.Stopped; }