that is possible, i’m trying to record it failing
In this file, I see the time cut off at 7749.570691 seconds. this was supposed to run over night. I believe that communication stopped from the tcp server. I was expecting an error but i dont see one. Another observation I made is that when i trigger data that there may be no response, but the wireshark looks normal.
capture where data flow stops.pcapng (98.5 KB)
I have purchased a higher end tcp device to see if that changes anything. I have also started looking into creating a java socket running on the gateway startup event. I am not sure if I am able to use a message handler on the same script to pass a value to a memory tag.
# TCP Socket Reader for R101 Tab Inserter
# Fully gateway-safe, threaded, logs everything
import system
import time
from java.net import Socket, SocketTimeoutException
from java.io import BufferedInputStream, IOException
from java.lang import Thread, Runnable
# Connection settings
HOST = "192.168.141.43"
PORT = 4001
DELIMITER = '\r' # CR-terminated messages
# Target tags
TAG_BUTTON = "[Sample_Tags]Devices/R101_Tab_Inserter/Button_Input_Alias"
TAG_DATE = "[Sample_Tags]Devices/R101_Tab_Inserter/Date"
TAG_HEAD = "[Sample_Tags]Devices/R101_Tab_Inserter/Head Number"
TAG_SERIAL = "[Sample_Tags]Devices/R101_Tab_Inserter/Interface Box Serial"
TAG_POSITION = "[Sample_Tags]Devices/R101_Tab_Inserter/Position"
TAG_TIME = "[Sample_Tags]Devices/R101_Tab_Inserter/Time"
# Logger
logger = system.util.getLogger("TCP-SOCKET-R101")
class SocketReader(Runnable):
def run(self):
while True:
sock = None
stream = None
buffer = ""
try:
logger.info("Attempting TCP connection to %s:%s" % (HOST, PORT))
sock = Socket(HOST, PORT)
sock.setKeepAlive(True)
sock.setTcpNoDelay(True)
sock.setSoTimeout(5000) # optional read timeout
logger.info("TCP connected successfully")
stream = BufferedInputStream(sock.getInputStream())
byte_buf = bytearray(1024)
while True:
try:
count = stream.read(byte_buf)
if count == -1:
raise Exception("Socket closed by peer")
buffer += byte_buf[:count].decode("ascii", errors="ignore")
# Process each complete message
while DELIMITER in buffer:
msg, buffer = buffer.split(DELIMITER, 1)
self.handleMessage(msg.strip())
except SocketTimeoutException:
# No data received, just continue
continue
except Exception as e:
logger.warn("Error reading from socket: %s" % str(e))
break # break inner loop to reconnect
except IOException as e:
logger.error("IO Exception during connection: %s" % str(e))
except Exception as e:
logger.error("Unexpected error: %s" % str(e))
finally:
# Cleanup
try:
if stream:
stream.close()
if sock:
sock.close()
except:
pass
logger.info("Socket closed, reconnecting in 5s")
time.sleep(5)
def handleMessage(self, msg):
try:
logger.info("RX: %s" % msg)
parts = [p.strip() for p in msg.split(',')]
if len(parts) != 6:
logger.warn("Unexpected message format: %s" % msg)
return
head_number = int(parts[0])
button = int(parts[1])
position = parts[2]
serial = int(parts[3])
time_str = parts[4]
date_str = parts[5]
system.tag.writeBlocking(
[
TAG_HEAD,
TAG_BUTTON,
TAG_POSITION,
TAG_SERIAL,
TAG_TIME,
TAG_DATE
],
[
head_number,
button,
position,
serial,
time_str,
date_str
]
)
except Exception as e:
logger.warn("Failed to parse or write message: %s (%s)" % (msg, str(e)))
# Start the thread
t = Thread(SocketReader(), "TCP-Socket-R101")
t.setDaemon(True)
t.start()
logger.info("Socket thread launched")
It is vitally important that any long-lived thread cooperate with java's interrupt machinery, so your startup event can stop the prior thread. And you need to register the prior thread in a persistent global, so you can find it to interrupt it.
Failing to do this will leak gobs of memory at every project save, and eventually crash your gateway.
I thought I gave you enough keywords for a suitable forum search, but I guess not.
https://forum.inductiveautomation.com/search?q=long-lived%20thread%20interrupt%20order%3Alatest
So I went ahead and purchased a moxa tcp server instead. I am not sure what fundamentally changed, but it has not disconnected once. The data does look slightly different. Are there any tcp devices or brands that are typically recommended by inductive automation or should they all theoretically work?
Here is the wireshark without any drops.
succesful capture without dropping .pcapng (117.5 KB)
I don't recall seeing a recommendation from IA, but I recommend Moxa for use with my drivers.
I will be using them from now on. As an alternative to this device, I may end up using a siemens rs232 module to utilize the PLC Tag long term.