hansrune / HANmeter

A python3 tool to read/decode/debug AMS/HAN data from smart electrical meter
GNU Lesser General Public License v3.0
5 stars 1 forks source link

Send to output #2

Closed espenbo closed 4 years ago

espenbo commented 4 years ago

Hello I could not get the script to send output to a text file python3 HANdomo.py -f /dev/ttyUSB0 > test.txt

If you add in the script 'sys.stdout.flush()' it works.

---------- Orginal ----------------

def gen_callback(fields):
    global npower, sumpower, lastupdatetime, nenergy, sumenergy
    if logdata:
        outputFile.flush()
    if debugfields:
        for key in sorted(fields.keys()):
            print(key,"=",fields[key])
        print("")
    if poweridx == 0:

----------- Endret ------------

def gen_callback(fields):
    global npower, sumpower, lastupdatetime, nenergy, sumenergy
    if logdata:
        outputFile.flush()
    if debugfields:
        for key in sorted(fields.keys()):
            print(key,"=",fields[key])
        print("")
        sys.stdout.flush()
    if poweridx == 0:

Then it works to use python3 HANdomo.py -f /dev/ttyUSB0 > test.txt``

hansrune commented 4 years ago

I added the flush after the callback has been called in HANobis.py so that output is flushed no matter what the callback is doing. You should then be able to output to a file or pipe without buffering delays.

When run as a service, it is always unbuffered as python3 is started in unbuffered (-u) mode