public override void SetOut(int number, int value) { if (number < 0 || number > 10) { throw new ArgumentOutOfRangeException("number", $"Number == {number} must be 0..10"); } if (value < 0 || value > 100) { throw new ArgumentOutOfRangeException("value", $"Value == {value} must be 0..100"); } using (var client = Driver.OpenClient()) { var bt = TbProtocol.GetParcel_ChangeOut(_address, number, value, _canAnswer); client.Write(bt); if (!_canAnswer) { return; } var addr = WaitResponse(client, TbProtocol.ExtractAddressFrom); if (addr != _address) { throw new InvalidOperationException("Address mismatches {addr} != {address}"); } return; } }
static void Test_GetCrc(Action <byte, IEnumerable <byte> > afterGetCrc, byte fromAddr, byte toAddr, byte repeatCount, byte answerEnable, IEnumerable <byte> cmd) { var parcel = TbProtocol.GetParcel(fromAddr, toAddr, repeatCount, answerEnable == 1, cmd); var crc = GetCrc(parcel); afterGetCrc?.Invoke(crc, parcel); }
T WaitResponse <T>(IDriverClient client, Func <IList <byte>, T> extractFunc, int addr = 0) { while (true) { var parcel = client.Read(TbProtocol.StartByte, TbProtocol.EndByte, TbProtocol.MaxParcelSize); var result = TbProtocol.FindParcel(parcel, addr == 0 ? _address : addr); if (result != null) { return(extractFunc(parcel)); } } }
public override void SetTime(DateTime dt) { using (var client = Driver.OpenClient()) { var bt = TbProtocol.GetParcel_SetClock(dt, _canAnswer); client.Write(bt); if (!_canAnswer) { return; } var addr = WaitResponse(client, TbProtocol.ExtractAddressFrom); } }
public override void UpdateSensors(IList <Sensor> sensors) { using (var client = Driver.OpenClient()) { var bt = TbProtocol.GetParcel_GetSensors(_address); client.Write(bt); var values = WaitResponse(client, TbProtocol.ExtractSensorValues); var count = Math.Min(values.Count, sensors.Count); for (int i = 0; i < count; ++i) { sensors[i].Value = values[i]; } } }
public override bool Ping() { try { using (var client = Driver.OpenClient()) { var bt = TbProtocol.GetParcel_GetAddress(); client.Write(bt); var addr = WaitResponse(client, TbProtocol.ExtractAddressFrom); return(true); } } catch (TimeoutException ex) { Trace.WriteLine(ex); return(false); } }