2

I use teraterm as my client-console and save the console log somewhere.

When I open the teraterm and do something on my COM1 serial port (attached with a device), it will print messages on the screen and write to the log file simultaneously,

if I want to watch the log , to detect if any special word occurs .

I thought if I use the "open" method , I can only get the whole content at the opened time , but can not get the latest content the teraterm write after the opened time?

How could I achieve this? Thank you all in advance~

newBike
  • 14,385
  • 29
  • 109
  • 192
  • Duplicate: [How can I tail a log file in Python?](http://stackoverflow.com/questions/12523044/how-can-i-tail-a-log-file-in-python) – Alp Jun 18 '13 at 04:40

2 Answers2

0

Periodically check file size:

import os
import time

log_path = '/tmp/com1.log'
special_word = 'magic'

lastsize = 0
while 1:
    if os.path.exists(log_path) and os.path.getsize(log_path) > lastsize:
        with open(log_path) as f:
            f.seek(lastsize)
            for line in f:
                if special_word in line:
                    print('Special word found')
            lastsize = f.tell()
    time.sleep(1)
falsetru
  • 357,413
  • 63
  • 732
  • 636
0

You could use teraterm's macro langauge, it allows the developers to extend the behaviour of teraterm.

For your specific case, teraterm's macro language provides wait family of functions to wait for a particular sequence of word (or) pattern. Mind that teraterm macro language is not python, but it has its own syntax.

; wait for special world 'hello'
wait 'hello'
if result == 0 goto your_own_logic

There are other ways if you want to have a python only solution. Instead of using teraterm to connect with serial port, you could use pyserial python library to connect with serial port.

Arunprasad Rajkumar
  • 1,374
  • 1
  • 15
  • 31