In one of my project, I need to take data from a serial device.
I will initialize the serial port connection once and i will continuously monitor bytes at port. If I get the bytes then i will go and read the data in the serial port. I will open the connection only once.
This is working fine in normal condition. Once after opening the connection, if the cable got disconnected or removed, i wont get any error.
Any method is there to check whether the connection is alive or not?
Please provide if there is any other way to ensure the connection is alive
There are timeouts for this purpose. If you don't get any data within a given time limit, the connection is bad.
The actual timeout value will depend on the frequency of the data output.
Be careful in using Bytes at port, because it may report a partial packet length (further characters may be received immediately afterwards).
If the device packet has a fixed termination character, it would be better to enable it in the VISA Configure serial port.
Set a proper timeout and use a single Read, specifying a number of characters to read bigger than the expected packet length.
If the Read does not return the packet, there is a problem.
In my project, i will get always bytes at port value is zero. I will get great than zero once the instrument done its work. In between if cable got removed also, the bytes at port property wont give any error.
The only way to see if a device is still connected through and RS-232 port is to attempt to talk to it. Does the instrument accept commands so that you can verify it is still there every so often?
Typically a serial port will not report anything about the cable plugged/unplugged status.
So you will never get a VISA error about this.
What's your device and what hardware protocol is used (none, hardware)? Is the cable a minimal RX, TX, GROUND?
You may use a VISA property node of type Serial Settings -> Modem Line Settings -> Break State (and similar) to monitor the state of various serial pins and see if anything changes connecting and disconnecting the cable. However, as expressed by the property names, those lines are (were?) typically used by serial modems; it's not likely that they can be used for this purpose with another type of device.