Open hlynsi opened 2 years ago
Instead of opening and closing connection via the same instance of Plc(), would it be any better idea to create a new instance everytime card is detected in/out of range?
Is your program continually scanning the PLC for changes? Unless you have connection starvation issues, you could implement IDisposable and open your connection after the constructor is complete then close the connection as part of the Dispose Process. You then don't need to worry about continually opening and closing your connection. But that is only if the option is available for you to maintain the connection.
The program does not scan the PLC for changes and the only interaction with the PLC is what I described above. There is no way to predict when the user scans the card.
But the program continuously waits for a card to be read so having the connection open could be a possibility.
I have made a plan for improvements and will share with you how it goes.
My apologies, you are writing to the PLC... So then yes you can hold a connection instance open. It should not impact the PLC scan times as you are only writing a small block of information, even if it occurred 20 times per second.
Just because you open the connection, it does not provide you any indication that the connection remains open from one write command to the other, therefore you would not know if the connection closed until the write was attempted. You would need to manage your processes as to how you retry whether that is automated or someone must attempt to rescan.
Your core loop should look something like this:
If (!plc.IsConnected)
plc.open();
plc.write(...);
That still means that every write call can fail because the connection was closed - because the isConnected function does not and can not actively do that. But once you recover from the exception that occurs in write when the connection is closed you can retry and ensure that the connection is re-established if necessary.
I wouldn't recommend closing the plc each time unless you need to.
I am using this library in the Siemens RFID reader example application running on an S7-1500 OpenController. When the RFID card is detected in range or leaves range I write data to PLC. This works fine for a while but eventually data is not written to the PLC anymore. When this happens the application has to be restarted and then it writes again to the PLC (for a while).
Does anyone here have a suggestion for the cause?
In the constructor I have
I have defined these methods:
I have this where the card is detected
Finally this is where the card is detected out of range