private void ResponseThreadMethod() { int length = Marshal.SizeOf(typeof(CommandDefinition.CommandFormat)); do { while (Connected) { try { byte[] response = new byte[length]; int dataRead = _networkStream.Read(response, 0, response.Length); if (dataRead > 0) { CommandDefinition.CommandFormat commandResponse = _commandResponse.Deserialize(response); if (commandResponse.CommandAck.Command == CommandEnum.SignOfLife) { SignOfLifeSequence = BitConverter.ToUInt32(commandResponse.Payload, 0); if (_signOfLifeTimer != null) { _signOfLifeTimer.Change(_signOfLifeTimerDueTime, Timeout.Infinite); } } else if (commandResponse.CommandAck.Command == CommandEnum.AdaptiveModeSpeed) { _adaptiveSpeed = BitConverter.ToSingle(commandResponse.Payload, 0); _speedMsgOpMode = (OperatingMode)commandResponse.Action.SubAction; SpeedMsgEvent.Set(); } else { _commandResponse = commandResponse; _responseEvent.Set(); if (ApcsUpdate != null) { ApcsUpdate(commandResponse.CommandAck.Command, commandResponse.Action.Action, commandResponse.Action.SubAction, commandResponse.Payload); } } } else { throw new Exception("Data read from APCS has size 0. Restarting Connection."); } } catch (Exception ex) { if (/*not terminated?*/ !_cancelEvent.WaitOne(0)) { Debug.Assert(Lgr != null); Lgr.LogError(ex); } if (_tcpClient != null && _tcpClient.Connected) { //CloseConnection(); _tcpClient = null; } } } }while (!_cancelEvent.WaitOne(ApcsDelay)); }
private CommandDefinition.CommandFormat ReplyCreate(CommandEnum command, BooleanValue isAck, ActionEnum action, byte subAction, byte[] data) { CommandDefinition.CommandAckStruct /*can't name it command*/ cmd = new CommandDefinition.CommandAckStruct(command, BooleanValue.False, isAck); CommandDefinition.ActionStruct /*can't name it action*/ act = new CommandDefinition.ActionStruct(ActionEnum.Response, subAction); CommandDefinition.CommandFormat packet = new CommandDefinition.CommandFormat(cmd, act, data); return(packet); }
private CommandDefinition.CommandFormat ConstructCommand(CommandEnum command, BooleanValue ackReq, ActionEnum action, byte subAction) { CommandDefinition.CommandAckStruct cmdAck = new CommandDefinition.CommandAckStruct(command, ackReq, BooleanValue.False); CommandDefinition.ActionStruct a = new CommandDefinition.ActionStruct(action, subAction); CommandDefinition.CommandFormat cmd = new CommandDefinition.CommandFormat(cmdAck.CommandWithAck, a.ActionAndSubAction); return(cmd); }
public void SendAdaptiveSpeedFeedbackConfigResponse(AdaptiveSpeedFeedbackConfig mode, float frequency) { //everton byte[] f = Reflection.Serialize(freq, typeof(float)); byte[] data = BitConverter.GetBytes(frequency); CommandDefinition.CommandFormat packet = ReplyCreate(CommandEnum.AdaptiveSpeedFeedbackConfig, BooleanValue.True, ActionEnum.Response, (byte)mode, data); ReplySend(packet); }
private void SendAck(CommandDefinition.CommandFormat command) { CommandDefinition.CommandFormat /*can't name it command*/ cmd = new CommandDefinition.CommandFormat(); cmd.CommandAck.Command = command.CommandAck.Command; cmd.CommandAck.IsAck = BooleanValue.True; cmd.Action.Action = ActionEnum.Response; cmd.Action.SubAction = command.Action.SubAction; cmd.Payload = command.Payload; ReplySend(cmd); }
public void SendSignOfLife(uint sequence) { try { byte[] data = BitConverter.GetBytes(sequence); CommandDefinition.CommandFormat packet = ReplyCreate(CommandEnum.SignOfLife, BooleanValue.False, ActionEnum.UnsolicitedMsg, 0, data); ReplySend(packet); } catch { /*suppress*/ } }
public void SendOperatingModeResponse(OperatingMode mode, short minimum, short maximum) { byte[] frqMax = BitConverter.GetBytes(maximum); byte[] frqMin = BitConverter.GetBytes(minimum); byte[] data = new byte[frqMin.Length + frqMax.Length]; frqMax.CopyTo(data, frqMin.Length); frqMin.CopyTo(data, 0); CommandDefinition.CommandFormat packet = ReplyCreate(CommandEnum.OperatingMode, BooleanValue.True, ActionEnum.Response, (byte)mode, data); ReplySend(packet); }
private void ProcessCommand(CommandDefinition.CommandFormat command) { if (ProcessCommandEvent != null) { try { ProcessCommandEvent(command); } catch { /*nobody to report to*/ } } if (command.CommandAck.AckRequired == BooleanValue.True) { SendAck(command); } }
private void ServerAgent() { while (!_serverTerminate) { try { WIZnet_W5100.Enable(SPI.SPI_module.SPI1, (Cpu.Pin)FEZ_Pin.Digital.Di10, (Cpu.Pin)FEZ_Pin.Digital.Di7, true); NetworkInterface.EnableStaticIP(ServerAddress, ServerSubnet, ServerGateway, ServerMac); NetworkInterface.EnableStaticDns(ServerGateway); try { ListenerClose(/*_listenerSocket == null*/); /*just to ensure tidiness*/ _listenerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); try { IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, ServerPort); _listenerSocket.Bind(endPoint); _listenerSocket.Listen(1); /*blocks...*/ SessionClose(/*_sessionSocket == null*/); /*just to ensure tidiness*/ using (_sessionSocket = _listenerSocket.Accept()) /*using overkill, but what th' hey...*/ try { SignOfLifeStart(/*_signOfLifeTimer != null*/); try { byte[] buffer = new byte[/*extra for safety*/ 2 * CommandDefinition.PacketSize]; while (/*read OK?*/ _sessionSocket.Receive(buffer, buffer.Length, SocketFlags.None) >= 1 /*blocks...*/) { CommandDefinition.CommandFormat command = new CommandDefinition.CommandFormat(); command = command.Deserialize(buffer); ProcessCommand(command); } } catch { SendSignOfLife(uint.MaxValue - 4); } finally { SignOfLifeStop(/*_signOfLifeTimer == null*/); } } catch { SendSignOfLife(uint.MaxValue - 3); } finally { SessionClose(/*_sessionSocket == null*/); } } catch { SendSignOfLife(uint.MaxValue - 2); } finally { ListenerClose(/*_listenerSocket == null*/); } } catch { SendSignOfLife(uint.MaxValue - 1); } finally { WIZnet_W5100.ReintializeNetworking(); } } catch { SendSignOfLife(uint.MaxValue); } finally { Thread.Sleep(/*1s*/ 1000 /*ms*/); Program.ResetBoard(); } } }
public bool SetPWMOutput(PWMOutputConfig?config, bool confirm) { if (/*terminated?*/ _cancelEvent.WaitOne(0)) { return(false); } CommandDefinition.CommandFormat command = ConstructCommand(CommandEnum.PWMOutput, BooleanValue.True, ActionEnum.Set, (byte)config); CommandDefinition.CommandFormat?response = SendCommand(command, confirm); if (confirm && (!ConfirmSetActionResponse(response, CommandEnum.PWMOutput, (byte)config))) { return(false); } return(true); }
public bool SetScanEnergyMode(ScanEnergyMode mode, bool confirm) { if (/*terminated?*/ _cancelEvent.WaitOne(0)) { return(false); } CommandDefinition.CommandFormat command = ConstructCommand(CommandEnum.ScanMode, BooleanValue.True, ActionEnum.Set, (byte)mode); CommandDefinition.CommandFormat?response = SendCommand(command, confirm); if (confirm && !ConfirmSetActionResponse(response, CommandEnum.ScanMode, (byte)mode)) { return(false); } return(true); }
public bool SetCurrentPulseWidth(PulseWidth pulseWidth, bool confirm) { if (/*terminated?*/ _cancelEvent.WaitOne(0)) { return(false); } CommandDefinition.CommandFormat command = ConstructCommand(CommandEnum.PulseWidth, BooleanValue.True, ActionEnum.Set, (byte)pulseWidth); CommandDefinition.CommandFormat?response = SendCommand(command, confirm); if (confirm && !ConfirmSetActionResponse(response, CommandEnum.PulseWidth, (byte)pulseWidth)) { return(false); } CurrentPulseWidth = pulseWidth; return(true); }
public bool SetAdaptiveModeTriggerRatio(OperatingMode mode, float ratio, bool confirm) { if (/*terminated?*/ _cancelEvent.WaitOne(0)) { return(false); } CommandDefinition.CommandFormat command = ConstructCommand(CommandEnum.AdaptiveModeToTrigRatio, BooleanValue.True, ActionEnum.Set, (byte)mode); BitConverter.GetBytes(ratio).CopyTo(command.Payload, 0); CommandDefinition.CommandFormat?response = SendCommand(command, confirm); if (confirm && (!ConfirmSetActionResponse(response, CommandEnum.AdaptiveModeToTrigRatio, (byte)mode) || ratio != BitConverter.ToSingle(response.Value.Payload, 0))) { return(false); } return(true); }
public bool SetAdaptiveSpeedFeedbackConfiguration(AdaptiveSpeedFeedbackConfig config, float freq, bool confirm) { if (/*terminated?*/ _cancelEvent.WaitOne(0)) { return(false); } CommandDefinition.CommandFormat command = ConstructCommand(CommandEnum.AdaptiveSpeedFeedbackConfig, BooleanValue.True, ActionEnum.Set, (byte)config); BitConverter.GetBytes(freq).CopyTo(command.Payload, 0); CommandDefinition.CommandFormat?response = SendCommand(command, confirm); if (confirm && (!ConfirmSetActionResponse(response, CommandEnum.AdaptiveSpeedFeedbackConfig, (byte)config) || freq != BitConverter.ToSingle(response.Value.Payload, 0))) { return(false); } return(true); }
public bool SetStaticPulseFrequency(OperatingMode mode, uint freq, bool confirm) { if (/*terminated?*/ _cancelEvent.WaitOne(0)) { return(false); } CommandDefinition.CommandFormat command = ConstructCommand(CommandEnum.StaticPulseFreq, BooleanValue.True, ActionEnum.Set, (byte)mode); BitConverter.GetBytes(freq).CopyTo(command.Payload, 0); CommandDefinition.CommandFormat?response = SendCommand(command, confirm); if (confirm && (!ConfirmSetActionResponse(response, CommandEnum.StaticPulseFreq, (byte)mode) || freq != BitConverter.ToInt32(response.Value.Payload, 0))) { return(false); } return(true); }
public bool SetConfigPulseWidth(PulseWidth?pulseWidth, float microseconds, bool confirm) { if (/*terminated?*/ _cancelEvent.WaitOne(0)) { return(false); } CommandDefinition.CommandFormat command = ConstructCommand(CommandEnum.ConfigPulseWidth, BooleanValue.True, ActionEnum.Set, (byte)pulseWidth); BitConverter.GetBytes(microseconds).CopyTo(command.Payload, 0); CommandDefinition.CommandFormat?response = SendCommand(command, confirm); if (confirm && (!ConfirmSetActionResponse(response, CommandEnum.ConfigPulseWidth, (byte)pulseWidth) || microseconds != BitConverter.ToSingle(response.Value.Payload, 0))) { return(false); } return(true); }
public bool SetPWMOutputEnable(PWMOutputConfig? /*enabled?*/ Enb, bool /*confirm*/ Cnf) { string /*trace text*/ trc = MethodBase.GetCurrentMethod().Name + "(" + ((Enb != null) ? Enb.ToString() : "null") + ", " + Cnf.ToString() + ")"; bool /*status (returned)*/ sts = false; try { CommandDefinition.CommandFormat /*command */ cmd = ConstructCommand( CommandEnum.PWMOutput, BooleanValue.True, ActionEnum.Set, (byte)Enb); CommandDefinition.CommandFormat? /*response*/ rsp = SendCommand(cmd, Cnf); sts = !Cnf || ConfirmSetActionResponse(rsp, CommandEnum.PWMOutput, (byte)Enb); } catch (Exception ex) { Lgr.LogError(ex); } return(sts); }
private void ReplySendBase(CommandDefinition.CommandFormat reply) { try { lock (_sessionSocket) { byte[] buffer = reply.Serialize(); if (_sessionSocket.Send(buffer) < 1) { throw new Exception(); } } } catch { SessionClose(/*_sessionSocket == null*/); /*make ServerAgent reconnect*/ throw; } }
public bool GetScanEnergyMode(out ScanEnergyMode?mode, bool confirm) { mode = null; if (/*terminated?*/ _cancelEvent.WaitOne(0)) { return(false); } CommandDefinition.CommandFormat command = ConstructCommand(CommandEnum.ScanMode, BooleanValue.False, ActionEnum.Get, 0); CommandDefinition.CommandFormat?response = SendCommand(command, confirm); if (response != null && response.Value.CommandAck.Command == CommandEnum.ScanMode && response.Value.Action.Action == ActionEnum.Response) { mode = (ScanEnergyMode)ScanEnergyMode.ToObject(typeof(ScanEnergyMode), response.Value.Action.SubAction); return(true); } else { return(false); } }
public bool GetPWMOutput(out PWMOutputConfig?config, bool confirm) { config = null; if (/*terminated?*/ _cancelEvent.WaitOne(0)) { return(false); } CommandDefinition.CommandFormat command = ConstructCommand(CommandEnum.PWMOutput, BooleanValue.False, ActionEnum.Get, 0); CommandDefinition.CommandFormat?response = SendCommand(command, confirm); if (response != null && response.Value.CommandAck.Command == CommandEnum.PWMOutput && response.Value.Action.Action == ActionEnum.Response) { config = (PWMOutputConfig)response.Value.Action.SubAction; return(true); } else { return(false); } }
public bool GetConfigPulseWidth(PulseWidth?pulseWidth, out float?time, bool confirm) { time = null; if (/*terminated?*/ _cancelEvent.WaitOne(0)) { return(false); } CommandDefinition.CommandFormat command = ConstructCommand(CommandEnum.ConfigPulseWidth, BooleanValue.True, ActionEnum.Get, (byte)pulseWidth); CommandDefinition.CommandFormat?response = SendCommand(command, confirm); if (response != null) { time = BitConverter.ToSingle(response.Value.Payload, 0); return(true); } else { return(false); } }
public bool GetStaticPulseFrequency(OperatingMode?selectedMode, out OperatingMode?mode, out int?freq, bool confirm) { mode = null; freq = null; if (/*terminated?*/ _cancelEvent.WaitOne(0)) { return(false); } CommandDefinition.CommandFormat command = ConstructCommand(CommandEnum.StaticPulseFreq, BooleanValue.False, ActionEnum.Get, (byte)selectedMode); CommandDefinition.CommandFormat?response = SendCommand(command, confirm); if (response != null && response.Value.CommandAck.Command == CommandEnum.StaticPulseFreq && response.Value.Action.Action == ActionEnum.Response) { mode = (OperatingMode)OperatingMode.ToObject(typeof(OperatingMode), response.Value.Action.SubAction); freq = BitConverter.ToInt32(response.Value.Payload, 0); return(true); } else { return(false); } }
public bool GetAdaptiveModeTriggerRatio(OperatingMode?selectedMode, out OperatingMode?mode, out float?ratio, bool confirm) { mode = null; ratio = null; if (/*terminated?*/ _cancelEvent.WaitOne(0)) { return(false); } CommandDefinition.CommandFormat command = ConstructCommand(CommandEnum.AdaptiveModeToTrigRatio, BooleanValue.False, ActionEnum.Get, (byte)selectedMode); CommandDefinition.CommandFormat?response = SendCommand(command, confirm); if (response != null && response.Value.CommandAck.Command == CommandEnum.AdaptiveModeToTrigRatio && response.Value.Action.Action == ActionEnum.Response) { mode = (OperatingMode)OperatingMode.ToObject(typeof(OperatingMode), response.Value.Action.SubAction); ratio = BitConverter.ToSingle(response.Value.Payload, 0); return(true); } else { return(false); } }
public bool GetAdaptiveSpeedFeedbackConfiguration(out AdaptiveSpeedFeedbackConfig?config, out float?freq, bool confirm) { config = null; freq = null; if (/*terminated?*/ _cancelEvent.WaitOne(0)) { return(false); } CommandDefinition.CommandFormat command = ConstructCommand(CommandEnum.AdaptiveSpeedFeedbackConfig, BooleanValue.False, ActionEnum.Get, 0); CommandDefinition.CommandFormat?response = SendCommand(command, confirm); if (response != null && response.Value.CommandAck.Command == CommandEnum.AdaptiveSpeedFeedbackConfig && response.Value.Action.Action == ActionEnum.Response) { config = (AdaptiveSpeedFeedbackConfig)OperatingMode.ToObject(typeof(AdaptiveSpeedFeedbackConfig), response.Value.Action.SubAction); freq = BitConverter.ToSingle(response.Value.Payload, 0); return(true); } else { return(false); } }
public bool GetCurrentPulseWidth(out PulseWidth?width, bool confirm) { width = null; if (/*terminated?*/ _cancelEvent.WaitOne(0)) { return(false); } CommandDefinition.CommandFormat command = ConstructCommand(CommandEnum.PulseWidth, BooleanValue.False, ActionEnum.Get, 0); CommandDefinition.CommandFormat?response = SendCommand(command, confirm); if (response != null && response.Value.CommandAck.Command == CommandEnum.PulseWidth && response.Value.Action.Action == ActionEnum.Response) { CurrentPulseWidth = (PulseWidth)PulseWidth.ToObject(typeof(PulseWidth), response.Value.Action.SubAction); width = CurrentPulseWidth; return(true); } else { return(false); } }
private CommandDefinition.CommandFormat?SendCommand(CommandDefinition.CommandFormat command, bool expectingReply) { CommandDefinition.CommandFormat?response = null; try { if (Connected) { byte[] cmd = command.Serialize(); _responseEvent.Reset(); _networkStream.Write(cmd, 0, cmd.Length); for (int i = 0; i < _sendCommandRetrys; i++) { if (_responseEvent.WaitOne(_apcsCommandTimeout)) { if (_commandResponse.CommandAck.Command == command.CommandAck.Command && _commandResponse.Action.Action == ActionEnum.Response) { response = _commandResponse; _responseEvent.Reset(); break; } } } } #if false else { throw new Exception("SendCommand called while not connected to APCS."); } #endif } catch (Exception ex) { if (/*not terminated?*/ !_cancelEvent.WaitOne(0)) { Debug.Assert(Lgr != null); Lgr.LogError(ex); } } return(response); }
public bool GetOperatingMode(out OperatingMode?mode, out short?minFreq, out short?maxFreq, bool confirm) { mode = null; minFreq = null; maxFreq = null; if (/*terminated?*/ _cancelEvent.WaitOne(0)) { return(false); } CommandDefinition.CommandFormat command = ConstructCommand(CommandEnum.OperatingMode, BooleanValue.False, ActionEnum.Get, 0); CommandDefinition.CommandFormat?response = SendCommand(command, confirm); if (response != null && response.Value.CommandAck.Command == CommandEnum.OperatingMode && response.Value.Action.Action == ActionEnum.Response) { mode = (OperatingMode)OperatingMode.ToObject(typeof(OperatingMode), response.Value.Action.SubAction); minFreq = BitConverter.ToInt16(response.Value.Payload, 0); maxFreq = BitConverter.ToInt16(response.Value.Payload, 2); return(true); } else { return(false); } }
public bool GetPWMOutputEnable( out PWMOutputConfig? /*enabled?*/ Enb, bool /*confirm*/ Cnf) { CommandDefinition.CommandFormat /*command*/ cmd = ConstructCommand(CommandEnum.PWMOutput, BooleanValue.False, ActionEnum.Get, 0); CommandDefinition.CommandFormat? /*response; might be null*/ rsp = SendCommand(cmd, Cnf); Enb = null; if ((rsp != null) && (rsp.Value.CommandAck.Command == CommandEnum.PWMOutput) && (rsp.Value.Action.Action == ActionEnum.Response)) { Enb = (PWMOutputConfig)rsp.Value.Action.SubAction; return(true); } else { return(false); } }
public void SendScanModeResponse(ScanEnergyMode mode) { CommandDefinition.CommandFormat packet = ReplyCreate(CommandEnum.ScanMode, BooleanValue.True, ActionEnum.Response, (byte)mode); ReplySend(packet); }
public void SendStaticPulseFreqResponse(OperatingMode mode, int frequency) { byte[] data = BitConverter.GetBytes(frequency); CommandDefinition.CommandFormat packet = ReplyCreate(CommandEnum.StaticPulseFreq, BooleanValue.True, ActionEnum.Response, (byte)mode, data); ReplySend(packet); }