VB.NET Parsing serial data asynchronously, getting the data from a shared function to instances
VB.NET 2010, .NET 4
Hello,
I've been struggling with the logic of this for a while and was hoping for a little insight.
I am working on an application for controlling an industrial machine composed of many devices attached to the computer. Most if it is reasonable straight forward, but I'm having trouble thinking about the RS-232 devices. One of the RS-232 devices is a fancy power supply with many functions. The ones that are relevant right now are voltage read, voltage write, current read, and current write.
The design pattern I'm trying to implement is one in which each attached device is represented by an object that has methods for reading/writing etc and properties for things like COM port etc.
For this power supply, clearly the Port reference should be shared among the instances, so I have a base class (say, PowerSupply) with a Protected Shared Property Port As IO.Ports.SerialPort. Then I have a couple classes that inherit from this class, PowerSupplyVoltage and PowerSupplyCurrent that each define read/write methods and properties for holding the last read/written values.
My issue is in parsing the response from the power supply using the Port.DataRecieved event on the shared SerialPort object. The protocol the power supply uses specifies 开发者_运维问答what data it's sending in each response packet. So, basically, it might say something equivalent to "The voltage is 100V" or "The current is 1A" or "The voltage has been set to 150V" etc.
I'm trying to parse this in a shared function. Only during the parsing process would I determine which instance's read/written value to update. I can't access the read/written value properties of the instances from within the shared function. My brain hurts from trying to think how to do this right. The best I could think of is creating some kind of PowerSupplyCommunicator object that would maintain references to PowerSupplyVoltage and PowerSupplyCurrent instances along with the shared Port and DataRecievedHandler functions. This seems kind of kludgy though. Any thoughts on a smart way to go about this?
Thanks a lot in advance, Brian
I would suggest having the shared object export an Event, which would be raised whenever a parameter messages comes over the serial port. The various object instances could then subscribe to the event, and update themselves as appropriate.
A slight variation on this approach would be for the shared object to provide a "register" method by which instances can supply a delegate that they'd like to have the shared object call when a message comes in for them. Conceptually, this would be very similar to an event, except that the shared object could examine the incoming message and only call the delegate of the instance to which it directed.
A third variation would be to have the object instances support one or more interfaces like INotifyVoltageChanged, INotifyCurrentChanged, etc. and call some method in the shared object to register themselves.
It's important when using any of these approaches, however, to note that if one of the individual object instances is abandoned, the shared object will likely keep a reference to it forever. If approach #3 is used, the shared object could hold a WeakReference to the object rather than a normal ("strong") reference. That would slow down slightly the handling of update notifications, but would avoid the risk of having an abandoned object live forever. Certainly the individual object instances should implement IDisposable, and should unsubscribe from any notifications when Disposed, but unless weak references are used the imperative that Dispose actually be called is stronger than with many other types of objects. Note that unless weak references are used, a Finalizer will be worthless.
精彩评论