/// <summary> /// Method to run periodic command routine. /// In this routine, send EchoBack command till succeeds 5 times and AppVersion. /// </summary> /// <param name="ComPortAcc">ComPortAccess class contains COM port abstract object.</param> /// <param name="Sequence">Sequence class to run routine.</param> /// <param name="TimerCount">Passed time.</param> /// <returns></returns> public override bool Routine( ComPortAccess ComPortAcc, ComPortSendRecvSequence Sequence, int TimerCount = 0) { int EchoBackOkCount = 0; var Command = new Command_00_00(); while (true) { if (Sequence.SendAndRecvRoutine(ComPortAcc, Command)) { EchoBackOkCount++; } if (EchoBackOkCount > 10) { EchoBackOkCount = 0; break; } } while (!Sequence.SendAndRecvRoutine(ComPortAcc, new Command_02_00())) { } return(false); }
/// <summary> /// A sequence to disconnect port. /// </summary> /// <param name="obj"></param> /// <returns></returns> public override object Sequence(ComPortAccess ComPortAcc) { try { ComPortAcc.Disconnect(); return(null); } catch (Exception ex) { this.OnNotifyRecvExceptionEvent(new NotifyConnectExceptionEventArgs(ex)); return(null); } }
/// <summary> /// Call a sequence method in other thread. /// </summary> /// <param name="obj"></param> public Task StartSequence(ComPortAccess ComPortAcc) { Task <object> task = Task <object> .Run(() => { return(this.Sequence(ComPortAcc)); }); Task ContinuationTask = task.ContinueWith((Antecedent) => { object Result = Antecedent.Result; if (Result != null) { if (Result is bool) { bool SeqResult = (bool)Result; if (SeqResult) { this.OnTaskFinishedEvent( new SequenceChangedEventArgs( this.FinishedConnectionState, SeqResult)); } else { this.OnTaskFinishedEvent( new SequenceChangedEventArgs( this.BaseConnectionState, SeqResult)); } } else { this.OnTaskFinishedEvent( new SequenceChangedEventArgs(this.FinishedConnectionState)); } } else { this.OnTaskFinishedEvent( new SequenceChangedEventArgs(this.FinishedConnectionState)); } }); return(ContinuationTask); }
/// <summary> /// A sequence to send and receive command. This sequence continues /// semi-permanently, till the sequence is stopped by action. /// </summary> /// <param name="ComPortAcc">Port information to access serial port.</param> /// <returns>Always null.</returns> public override object Sequence(ComPortAccess ComPortAcc) { this.IsRunning = true; this.DoesContinue = true; CommandRoutine Routine = new InitCommandRoutine(); Routine.Routine(ComPortAcc, this); Routine = new PeriodicCommandRoutine(); while (this.DoesContinue) { Routine.Routine(ComPortAcc, this); } this.IsRunning = false; return(null); }
/// <summary> /// A routine to send command and receive response, and notify the result. /// </summary> /// <param name="ComPortAcc"></param> /// <param name="Command"></param> /// <returns></returns> public bool SendAndRecvRoutine(ComPortAccess ComPortAcc, ACommand Command) { try { byte[] ResData; ComPortAcc.SendAndRecv(Command.CmdData, out ResData); Command.ResData = ResData; Command.Check(); this.OnNotifySendReceiveData(new NotifySendReceiveDataEventArgs(Command)); return(true); } catch (CommandException CmdExpt) { this.OnNotifyRecvExceptionEvent(new NotifyCommandException(CmdExpt)); return(false); } }
/// <summary> /// Method to run periodic command routine. /// </summary> /// <param name="ComPortAcc">ComPortAccess class contains COM port abstract object.</param> /// <param name="Sequence">Sequence class to run routine.</param> /// <param name="TimerCount">Passed time.</param> /// <returns></returns> public override bool Routine( ComPortAccess ComPortAcc, ComPortSendRecvSequence Sequence, int TimerCount = 0) { foreach (ACommand Command in this.CommandQueue) { Thread.Sleep(1); Command.UpdateCmdData(); try { Sequence.SendAndRecvRoutine(ComPortAcc, Command); } catch (Exception ex) { Console.WriteLine(ex.Message); } } this.Log(CommandQueue); return(false); }
/// <summary> /// A sequence to connect port. /// </summary> /// <param name="obj"></param> /// <returns></returns> public override object Sequence(ComPortAccess ComPortAcc) { bool Result = false; this.IsRunning = true; try { Result = ComPortAcc.Connect(); return(Result); } catch (Exception ex) { this.OnNotifyRecvExceptionEvent(new NotifyConnectExceptionEventArgs(ex)); return(false); } finally { this.IsRunning = false; } }
/// <summary> /// Abstract method to run main sequence to access COM port. /// </summary> /// <param name="ComPortAcc">ComPortAccess object used to access COM port.</param> /// <returns>Result of the sequence.</returns> public abstract object Sequence(ComPortAccess ComPortAcc);
public abstract bool Routine( ComPortAccess ComPortAcc, ComPortSendRecvSequence Sequence, int TimerCount = 0);