Hello,
You need 'PySerial' in advance. Then try the script as below, which comes from its makers site (
tocos-wireless.com/jp/tech/misc/python_twelite/twelite_read_write.py
).
then try
# python3 thisScript.py /dev/ttyUSB?
where ttyUSB? is your assigned ToCoStick port, which can be addressed by dmsg or lsusb.
UART reader, writer for TWE-Lite Std App
[Reading]
Read ‘:??81????’ data from serial port and interpret its content
[Writing]
Read from standard input line by line and output to serial port
q[Enter] --> exit the script
:??????[Enter] --> output ‘:…’ command
from serial import *
from sys import stdout, stdin, stderr, exit
import threading
ser = None
t1 = None
bTerm = False
def printPayload(l):
if len(l) < 3: return False
print (" command = 0x%02x (other)" % l[1])
print (" src = 0x%02x" % l[0])
print (" payload =",)
for c in l[2:]:
print ("%02x" % c,)
print ("(hex)")
return True
def printPayload_0x81(l):
if len(l) != 23: return False
ladr = l[5] << 24 | l[6] << 16 | l[7] << 8 | l[8]
print (" command = 0x%02x (data arrival)" % l[1])
print (" src = 0x%02x" % l[0])
print (" src long = 0x%08x" % ladr)
print (" dst = 0x%02x" % l[9])
print (" pktid = 0x%02x" % l[2])
print (" prtcl ver = 0x%02x" % l[3])
print (" LQI = %d / %.2f [dbm]" % (l[4], (7*l[4]-1970)/20.))
ts = l[10] << 8 | l[11]
print (" time stmp = %.3f [s]" % (ts / 64.0))
print (" relay flg = %d" % l[12])
vlt = l[13] << 8 | l[14]
print (" volt = %04d [mV]" % vlt)
dibm = l[16]
dibm_chg = l[17]
di = {}
di_chg = {}
for i in range(1,5):
di[i] = 0 if (dibm & 0x1) == 0 else 1
di_chg[i] = 0 if (dibm_chg & 0x1) == 0 else 1
dibm >>= 1
dibm_chg >>= 1
print (" DI1=%d/%d DI2=%d/%d DI3=%d/%d DI4=%d/%d" % (di[1], di_chg[1], di[2], di_chg[2], di[3], di_chg[3], di[4], di_chg[4]))
ad = {}
er = l[22]
for i in range(1,5):
av = l[i + 18 - 1]
if av == 0xFF:
ad[i] = -1
else:
ad[i] = ((av * 4) + (er & 0x3)) * 4
er >>= 2
print (" AD1=%04d AD2=%04d AD3=%04d AD4=%04d [mV]" % (ad[1], ad[2], ad[3], ad[4]))
return True
def readThread():
global ser, bTerm
while True:
if bTerm: return
line = ser.readline().rstrip()
bCommand = False
bStr = False
if len(line) > 0:
c = line[0]
if isinstance(c, str):
if c == ':': bCommand = True
bStr = True
else:
if c == 58: bCommand = True
print ("\n%s" % line)
if not bCommand: continue
try:
lst = {}
if bStr:
lst = map(ord, line[1:].decode('hex'))
else:
import codecs
s = line[1:].decode("ascii")
lst = codecs.decode(s, "hex_codec")
csum = sum(lst) & 0xff
lst = lst[0:len(lst)-1]
if csum == 0:
if lst[1] == 0x81:
printPayload_0x81(lst)
else:
printPayload(lst)
else:
print ("checksum ng")
except:
if len(line) > 0:
print ("...skip (%s)" % line)
def DoTerminate():
global t1, bTerm
bTerm = True
print ("... quitting")
time.sleep(0.5)
exit(0)
if __name__=='__main__':
if len(sys.argv) != 2:
print ("%s {serial port name}" % sys.argv[0])
exit(1)
try:
ser = Serial(sys.argv[1], 115200, timeout=0.1)
print ("open serial port: %s" % sys.argv[1])
except:
print ("cannot open serial port: %s" % sys.argv[1])
exit(1)
t1=threading.Thread(target=readThread)
t1.setDaemon(True)
t1.start()
while True:
try:
l = stdin.readline().rstrip()
if len(l) > 0:
if l[0] == 'q':
DoTerminate()
if l[0] == ':':
cmd = l + "\r\n"
print ("--> "+ l)
ser.write(cmd)
except KeyboardInterrupt:
DoTerminate()
except SystemExit:
exit(0)
except:
print ("... unknown exception detected")
break
exit(0)