Hi, I did a function to monitor COM port , and run this in system.unitl.invokeAsynchronous. but GUI has no response when I click a button to execute this function (OpenCOMDevice in my attached code), meanwhile I see it can receive the bytes from COM port and write to tags in background. can anyone help on this? I searched a lot topic about this, but no luck
import sys, jarray, traceback
from java.lang import String, Thread
from java.util import Date
from org.apache.commons.lang3 import StringEscapeUtils
logger = system.util.getLogger('clientCOMIO')
def OpenCOMDevice(devicepath):
system.tag.writeBlocking(['[default]%s/Active' % devicepath], [1])
system.util.invokeAsynchronous(COMReadAndWrite(devicepath),devicepath)
logger.debug("device opened:" +devicepath)
def CloseCOMDevice(devicepath):
system.tag.writeBlocking(['[default]%s/Active' % devicepath], [0])
logger.debug("device closed:" +devicepath)
def COMReadAndWrite(devicepath):
values=system.tag.readBlocking(['[default]%s/Port' % devicepath,'[default]%s/BitRate' % devicepath,'[default]%s/HandShake' % devicepath,'[default]%s/FlowControl' % devicepath\
,'[default]%s/Parity' % devicepath,'[default]%s/DataBit' % devicepath,'[default]%s/StopBit' % devicepath])
port = values[0].value
bitrate = values[1].value
handshake = values[2].value
flowcontrol = values[3].value
parity = values[4].value
databit = values[5].value
stopbit = values[6].value
# port = system.tag.read('[default]%s/Port' % devicepath).value
# bitrate=system.tag.read('[default]%s/BitRate' % devicepath).value
# handshake=system.tag.read('[default]%s/HandShake' % devicepath).value
# flowcontrol=system.tag.read('[default]%s/FlowControl' % devicepath).value
# parity=system.tag.read('[default]%s/Parity' % devicepath).value
# databit=system.tag.read('[default]%s/DataBit' % devicepath).value
# stopbit=system.tag.read('[default]%s/StopBit' % devicepath).value
started = False
try:
system.serial.configureSerialPort(port, bitrate, databit,handshake, flowcontrol, parity, stopbit)
system.serial.openSerialPort(port)
active = system.tag.readBlocking(['[default]%s/Active' % devicepath])[0].value
while active:
receivedbytes = system.serial.readBytes(port,32,1000)
started=-1
for i in range(len(receivedbytes)):
if receivedbytes[i] <> 0:
started=i
break
if started > -1:
ended = -1
for i in range(started,len(receivedbytes)):
if receivedbytes[i]==0:
ended=i
break
# verify received data
system.tag.writeAsync(['[default]%s/LastTrafficTime' % devicepath,'[default]%s/Received' % devicepath,'[default]%s/LastError' % devicepath], [Date(),receivedbytes[started:ended],''],None)
send=system.tag.readBlocking(['[default]%s/Send' % devicepath])[0].value
if (len(send)>0):
system.serial.writeBytes(port,send)
system.tag.writeAsync(['[default]%s/Send' % devicepath], [''])
active = system.tag.readBlocking(['[default]%s/Active' % devicepath])[0].value
Thread.sleep(100)
finally:
system.serial.closeSerialPort(port)
CloseCOMDevice(devicepath)