I have a situation where I'm connected to a device via serial/rs232; Upon establishing a connection to the device, it'll start pumping sensor-specific data back to the client.
I have two goals: a) Receive continuous stream of sensor-status-updates data and hand off to the UI b) Send command and wait for reply
var messageObserver = Observable.Defer(() => serialPort.TakeWhile(
s => s != string.Empty))
.Repeat();
from here, I'd like to start processing the sensor-specific data: Currently I'm trying this
this.messageObserver.Where(s => Regex.Match(s, @"...").Success)
.Subscribe(o => OnDataArrival(o));
which works great ..until I want to execute code like this
public string GetFirmwareVersion()
{
var firmwareObserver =
this.messageObserver.Where(s => Regex.Match(s, @"...").Success)
.Take(1)
.PublishLast();
var connectable = firmwareObserver.Connect();
// send command for firmware version
port.Send(new byte[] { 0x9 });
// wait for a reply up to 10 seconds
var data = firmwareObserver.Timeout(TimeSpan.FromSeconds(10)).Wait();
connectable.Dispose();
return data;
}
I never receive anything back and an exception is thrown (obviously from the TimeOut).
If I comment out the initial subscriber (that fires off OnDataArrival
), the GetFirmwareVersion
code works!
So I think what I'm interested in learning are recommended avenues to accomplish two goals: a) process data coming in over the wire as its being received b) Connect & Wait while we still process data coming in