private TFrameEndSignalRes TransmitFrame(out byte[] buff, ICommunicationLayer ComPortCli) { buff = get_buffor(); NULL_message message = pool.GetEmptyISesDBuffer(); message.userDataLength = (ushort)buff.Length; for (int i = 0; i < buff.Length; i++) { message.WriteByte(buff[i]); } // czas na wyslanie ramki: ComPortCli.Flush(); string msg = "sending frame: " + buff.Length.ToString() + " size"; m_TraceSource.TraceVerbose(382, "TransmitFrame", msg); TFrameEndSignalRes lastRes = ComPortCli.FrameEndSignal(message); m_TraceSource.TraceVerbose(382, "TransmitFrame", "Frame has been sent"); message.ReturnEmptyEnvelope(); return(lastRes); }
private bool SendReceive() { m_TraceSource.TraceVerbose(198, "SendReceive", "SendReceive has started"); Thread.Sleep(c_WaitAfterAndBeforeCommunication); bool toBeReturned = true; string msg = ""; State m_state = State.transmiting; byte[] buff = null; List <byte> receivebuff = new List <byte>(1024); CAS.Lib.RTLib.Processes.Stopwatch mySW = new CAS.Lib.RTLib.Processes.Stopwatch(); byte oChar = byte.MaxValue; while (m_state != State.leaving) { switch (m_state) { case State.transmiting: { m_state = State.receiving; try { ComPortCli.Flush(); } catch (Exception ex) { msg = String.Format (msg, "Error: an exception has been thrown by the Flush : {0}", ex.Message); m_TraceSource.TraceError(185, m_state.ToString(), msg); } try { switch (TransmitFrame(out buff, ComPortCli)) { case TFrameEndSignalRes.Success: break; case TFrameEndSignalRes.DisInd: msg = "Error: disconnected while sending frame"; m_TraceSource.TraceError(174, m_state.ToString(), msg); m_state = State.error; break; } } catch (Exception ex) { msg = String.Format (msg, "Error: an exception has been thrown by the TransmitFrame : {0}", ex.Message); m_TraceSource.TraceError(185, m_state.ToString(), msg); if (!ComPortCli.Connected) { m_state = State.error; } } break; } case State.receiving: receivebuff.Clear(); mySW.StartReset(); m_state = State.gettingChar; msg = "State.receiving - ComPortCli.GetChar( out oChar, c_FrameTimeout ) "; m_TraceSource.TraceVerbose(198, m_state.ToString(), msg); try { switch (ComPortCli.GetChar(out oChar, c_FrameTimeout)) { case TGetCharRes.Success: break; case TGetCharRes.Timeout: m_state = State.error; msg = "Timeout: no data received"; m_TraceSource.TraceInformation(208, m_state.ToString(), msg); break; case TGetCharRes.DisInd: msg = "Error: disconected while waitin for next frame"; m_TraceSource.TraceError(214, m_state.ToString(), msg); m_state = State.error; break; } } catch (Exception ex) { msg = String.Format("Error: an exception has been thrown by the GetChar: {0}", ex.Message); m_TraceSource.TraceError(224, m_state.ToString(), msg); if (!ComPortCli.Connected) { m_state = State.error; } } break; case State.gettingChar: receivebuff.Add(oChar); try { switch (ComPortCli.GetChar(out oChar, c_CharTimeout)) { case TGetCharRes.Success: break; case TGetCharRes.Timeout: m_state = State.leaving; if (!CheckData(buff, receivebuff)) { m_state = State.error; } break; case TGetCharRes.DisInd: msg = "Error: disconected while waitin for data"; m_TraceSource.TraceError(254, m_state.ToString(), msg); m_state = State.error; break; } } catch (Exception ex) { msg = String.Format("Error: an exception has been thrown by the GetChar : {0}", ex.Message); m_TraceSource.TraceError(264, m_state.ToString(), msg); if (!ComPortCli.Connected) { m_state = State.error; } } break; case State.error: toBeReturned = false; m_state = State.leaving; break; default: break; } } Thread.Sleep(c_WaitAfterAndBeforeCommunication); return(toBeReturned); }