We have a serial port that is connected to hundreds of physical devices on the same cable. We have protocols, such as Modbus and Hart, for processing request and response between the application and devices. The question is about channel reference number management. When the device does not use a channel, the channel must be closed.
public class SerialPortChannel { int refCount = 0; public void AddReference() { refCount++; } public void ReleaseReference() { refCount--; if (refCount <= 0) this.ReleasePort();
For each connected device, we create an object for the device, for example
device = new Device(); device.Attach(channel); //this calls channel.AddReference()
When the device turns off,
device.Detach(channel); //this calls channel.ReleaseReference()
I am not convinced of the reference reference model. Is there a better way to deal with this problem in .NET World?
user90150
source share