I was able to get this to work after some testing. I got stuck on my data coming from the scanner being in 3 or 4 chunks, requiring multiple reads to get all the data. That turned out to be a setting on my serial gateway, it was set to use inter-character time gap as the delimiter, I changed it to use 0x0d0a instead and now my class reads it all as 1 message.
I unfortunately have no idea what the maximum message size from the serial-ethernet gateway is, the only thing I can find in the docs is the max TCP buffer size, which is 16k, so I'm using that as my message size for the time being. All of the data I'm currently sending is way under that limit anyways, I'm currently in the ~ 36 bytes range for my lot IDs and ~50 bytes for my machine load point IDs.
Non BufferedReader TCP Listener Class
class TCPClientListener(Thread):
connectionRetryInterval = SOCKET_RETRY_PERIOD
def __init__(self, deviceConfig, concurrentQueue):
self.setDaemon(1)
self.hostName = deviceConfig['hostname']
self.port = deviceConfig['port']
self.cq = concurrentQueue
self.byteArray = jarray.zeros(1024*16, 'b')
return
def readTCPData(self):
try:
sis = self.socket.inputStream
# Check the interrupted flag without clearing it
while not self.isInterrupted():
try:
newByte = sis.read()
if newByte == -1:
continue
self.byteArray[0] = newByte
self.socket.setSoTimeout(1)
self.readByteCount = sis.read(self.byteArray, 1, len(self.byteArray)-1)
message = String(self.byteArray, 0, self.readByteCount)
if message:
self.cq.offer(message)
self.socket.setSoTimeout(1200)
except IOException as ioe:
self.socket.setSoTimeout(1200)
continue
except Throwable as t:
logger.warn("Java exception while reading TCP socket data", t)
except Exception as e:
logger.warn("Jython exception while reading TCP socket data", shared.later.PythonAsJavaException(e))
finally:
self.socket.close()
return
def run(self):
while not self.interrupted():
try:
self.socket = Socket(self.hostName, self.port)
logger.infof("Connected to endpoint %s:%s, awaiting data stream", self.hostName, self.port)
self.socket.setSoTimeout(1200)
self.readTCPData()
except InterruptedException as ie:
self.interrupt()
except IOException as ioe:
logger.warnf("Unable to connect to endpoint, retrying in %d seconds", self.connectionRetryInterval/1000, ioe)
self.sleep(self.connectionRetryInterval)
except Throwable as t:
logger.warn("Java exception while attempting to monitor TCP socket", t)
except Exception as e:
logger.warn("Jython exception while attempting to monitor TCP socket", shared.later.PythonAsJavaException(e))
logger.info("Disconnected from endpoint")
return
def stop(self):
self.interrupt()
return