static IObservable <string> GetDeviceName(string portName, LedState ledState, LedState visualIndicators, EnableType heartbeat) { return(Observable.Create <string>(observer => { var transport = default(SerialTransport); var writeOpCtrl = HarpCommand.OperationControl(DeviceState.Standby, ledState, visualIndicators, heartbeat, EnableType.Enable, false); var cmdReadWhoAmI = HarpCommand.ReadUInt16(Registers.WhoAmI); var cmdReadMajorHardwareVersion = HarpCommand.ReadByte(Registers.HardwareVersionHigh); var cmdReadMinorHardwareVersion = HarpCommand.ReadByte(Registers.HardwareVersionLow); var cmdReadMajorFirmwareVersion = HarpCommand.ReadByte(Registers.FirmwareVersionHigh); var cmdReadMinorFirmwareVersion = HarpCommand.ReadByte(Registers.FirmwareVersionLow); var cmdReadTimestampSeconds = HarpCommand.ReadUInt32(Registers.TimestampSecond); var cmdReadDeviceName = HarpCommand.ReadByte(Registers.DeviceName); var cmdReadSerialNumber = HarpCommand.ReadUInt16(Registers.SerialNumber); var whoAmI = 0; var timestamp = 0u; var hardwareVersionHigh = 0; var hardwareVersionLow = 0; var firmwareVersionHigh = 0; var firmwareVersionLow = 0; var serialNumber = default(ushort?); var messageObserver = Observer.Create <HarpMessage>( message => { switch (message.Address) { case Registers.OperationControl: transport.Write(cmdReadWhoAmI); transport.Write(cmdReadMajorHardwareVersion); transport.Write(cmdReadMinorHardwareVersion); transport.Write(cmdReadMajorFirmwareVersion); transport.Write(cmdReadMinorFirmwareVersion); transport.Write(cmdReadTimestampSeconds); transport.Write(cmdReadSerialNumber); transport.Write(cmdReadDeviceName); break; case Registers.WhoAmI: whoAmI = message.GetPayloadUInt16(); break; case Registers.HardwareVersionHigh: hardwareVersionHigh = message.GetPayloadByte(); break; case Registers.HardwareVersionLow: hardwareVersionLow = message.GetPayloadByte(); break; case Registers.FirmwareVersionHigh: firmwareVersionHigh = message.GetPayloadByte(); break; case Registers.FirmwareVersionLow: firmwareVersionLow = message.GetPayloadByte(); break; case Registers.TimestampSecond: timestamp = message.GetPayloadUInt32(); break; case Registers.SerialNumber: if (!message.Error) { serialNumber = message.GetPayloadUInt16(); } break; case Registers.DeviceName: var deviceName = nameof(Device); if (!message.Error) { var namePayload = message.GetPayload(); deviceName = Encoding.ASCII.GetString(namePayload.Array, namePayload.Offset, namePayload.Count); } Console.WriteLine("Serial Harp device."); if (!serialNumber.HasValue) { Console.WriteLine($"WhoAmI: {whoAmI}"); } else { Console.WriteLine($"WhoAmI: {whoAmI}-{serialNumber:x4}"); } Console.WriteLine($"Hw: {hardwareVersionHigh}.{hardwareVersionLow}"); Console.WriteLine($"Fw: {firmwareVersionHigh}.{firmwareVersionLow}"); Console.WriteLine($"Timestamp (s): {timestamp}"); Console.WriteLine($"DeviceName: {deviceName}"); Console.WriteLine(); observer.OnNext(deviceName); observer.OnCompleted(); break; default: break; } }, observer.OnError, observer.OnCompleted); transport = new SerialTransport(portName, messageObserver); transport.IgnoreErrors = true; transport.Open(); transport.Write(writeOpCtrl); return transport; }).Timeout(TimeSpan.FromMilliseconds(500)) .OnErrorResumeNext(Observable.Return(nameof(Device))) .SubscribeOn(Scheduler.Default) .FirstAsync()); }
/// <summary> /// Returns a <see cref="HarpMessage"/> command to initialize the device operation control register. /// </summary> /// <param name="operationMode">The desired operation mode of the device.</param> /// <param name="ledState">Specifies whether the operation mode LED should report the device state.</param> /// <param name="visualIndicators">Specifies whether any visual indicator LEDs should be enabled on the Harp device.</param> /// <param name="heartbeat">Specifies whether to enable or disable the heartbeat register.</param> /// <param name="replies">Specifies whether write commands should report back the state of the register.</param> /// <param name="dumpRegisters">Specifies whether the state of all registers should be reported after initialization.</param> /// <returns>A valid <see cref="HarpMessage"/> command to set the device operation mode.</returns> public static HarpMessage OperationControl(DeviceState operationMode, LedState ledState, LedState visualIndicators, EnableType heartbeat, EnableType replies, bool dumpRegisters) { int operationFlags; operationFlags = heartbeat == EnableType.Enable ? 0x80 : 0x00; operationFlags += ledState == LedState.On ? 0x40 : 0x00; operationFlags += visualIndicators == LedState.On ? 0x20 : 0x00; operationFlags += replies == EnableType.Enable ? 0x00 : 0x10; operationFlags += dumpRegisters ? 0x08 : 0x00; operationFlags += operationMode == DeviceState.Active ? 0x01 : 0x00; return(WriteByte(Registers.OperationControl, (byte)operationFlags)); }