Is there a proper sequence for opening a communication handle and retrieving data?
I have a serial device that sends data every minute, with a data length of approximately 55 characters. I monitored the received data for around 80 minutes (80 counts), and during this time, I consistently received 55-character messages. However, after this period, I started experiencing data loss, where the received data length dropped to about 26 characters.
I tried using Node-RED to retrieve the data, and I never observed any data loss or character drops—it always remained at 55 characters for days.
Currently, my process is as follows in pac control:
Open the outgoing communication.
Listen for incoming data.
Check the number of characters waiting.
Retrieve the string.
Clear the received buffer.
Close the communication.
Loop to #1 with 1 second delay.
I have also set the serial communication timeout to 30 seconds.
Could the issue be related to how I am handling the communication process, or is there a better approach to ensure data integrity over time? Any insights would be greatly appreciated.
ReceiveString - from the help file: This command is not recommended for receiving binary messages, since EOM characters may occur within the binary message. Use Receive N Characters instead.
Use GetNumCharsWaiting and ReceiveNChars to receive your data. You will need to do this in a loop with a short delay until you get all the data you are expecting. If you receive a partial message when done calling ReceiveNChars then just dump what was received and wait for the next full payload. This can happen when you first open the communication handle since the device may be mid-transmission. You will probably want your own timer running to detect the end of transmission/timeout if no more characters are waiting and you don’t have a complete payload. Or use another method to determine you have a partial message.
The time it takes to get a complete message once it begins should be easy to calculate to set your timeouts appropriately. 8n1 is 10 bits/character. At 115200 baud, the full payload will arrive in less than 5 milliseconds. So a 5 ms delay in your receive loop will get the full payload on the second iteration. You can timeout after 10ms if you don’t have a full payload and there are no characters waiting.
You should not need to call ClearCommunicationReceiveBuffer if communication is being handled properly.
Monitor each communication command for its status. Check the status variable, and if there is an error, then you can close the communication handle and re-open, otherwise leave it open all the time.
Got an error. When I open outgoing connection i got error :
-49 = No more connections are available. Maximum number of connections of this type already in use. After this error I couldn’t even open the connection tried even stopping and starting the chart. After I restarted then it started to work. Not sure what that error means.
Here is the datapoints I am plotting in graph. The yellow marked is where the i couldn’t open the communication. After i restarted the controller it started to work. Whereas the red one, not sure what happened over there.
If you get an error (other than timeout) on any of the receive commands, you need to close the communication handle before you open it again. This might be why you are getting that error, the communication handle is not open, but it hasn’t been closed either.
Please read the “Notes” in the help file on each of the communication commands you are using, there are details in there that you must pay attention to if you want your strategy to be reliable.
Will look on that, maybe that is the issue. But I couldn’t open the communication handle it shows its closed. Only way to make it working is I need to restart the controller.
In this code, I am constantly running the Accept incoming communication the error code I get is -47 (Communication handle already open), Maybe this is an error? Like you mentioned I might need to run my own down timer whenever I see the data, then start a 1 minute down timer and then look for the data?
Removed all other communication functions and used only outgoing communication. Ran the program for past 2 days. Zero drops in data, the communication is handled well. Thanks philip.