Code to generate continuous beep

3

The code below does the following, it reads a log file in real time and every time the line of the code has the letter N a beep emits from the windows.

At first this code is working.

To understand what I'm doing, I access a machine via putty, so I record a log of the session.

In this machine there are several sensors connected, so when a sensor is triggered on the screen it changes the status to ON, so when it turns OFF.

The code works every time the status is ON.

Every time it changes the status is included a line in the log, because it is a log of the putty session.

So if I play "turn-off" the beep goes with it.

But I would like if the last status, ie if last line read with the status ON the beep was active. Until q between a new line in the file with FF (from OFF).

I'm trying to get the FF to compare FF in the condition, I tried to use While .. but the beep lasts only the time it was programmed.

Does anyone qualify?

import time
import winsound

def monitorar(path):
with open(path, 'r') as arq:
    while True:
        nova_linha = arq.readline()
        nova_linha = nova_linha.replace('\n', '')
        if 'N' in nova_linha:
            yield nova_linha
            frequency = 2500  # Set Frequency To 2500 Hertz
            duration = 1000  # Set Duration To 1000 ms == 1 second
            winsound.Beep(2500, 1000)
        else:
            time.sleep(0.0)

caminho_arquivo = 'C:/loop/putty.log'
for idx, linha in enumerate(monitorar(caminho_arquivo)):
print("{:5d}: {}".format(idx, linha))
    
asked by anonymous 10.12.2017 / 01:34

1 answer

1

The most practical way to keep the beep playing by using winsound is to separate the part that checks the logical condition of the part that makes the sound:

import time
import winsound

def monitorar(path):
    duration = 1000
    frequency = 2500
    ligado = False
    with open(path, 'r') as arq:
        while True:
            nova_linha = arq.readline()
            while nova_linha: 
            #Isto para quando nova_linha == '', um valor Falso
            #isto é - quando não há mais linhas para serem lidas
                if 'N' in nova_linha:
                   ligado = True
                elif 'FF' in nova_linha:
                   ligado = False
                yield nova_linha
                nova_linha = arq.readline()
            if ligado:
                winsound.Beep(2500, 1000)

caminho_arquivo = 'C:/loop/putty.log'
for idx, linha in enumerate(monitorar(caminho_arquivo)):
    print("{:5d}: {}".format(idx, linha))

Unfortunately, the winsound.Beep stops playing when the sound is played, so if the sensor is turned on and then turned off, the beep continues to sound for the duration of the last beep. Another negative aspect of the execution lock is that the beep is never touched while the program checks the log. Therefore, the beep duration should be thought of as convenient - a very high value reduces the shutdown response time, and a very low value causes the beep to "blink" too much as it is not touched during the log reading.

    
24.01.2018 / 03:05