python – Code to generate continuous beep

Question:

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

At first this code is working.

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

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

The code works, every time the status is ON beeps.

Every time it changes the status a line is added to the log, as it is a log of the putty session.

So if I play "on-off" the beep follows.

But I would like that if the last status, that is, if the last line handles the status ON, the beep would be active. Until q enter a new line in the file with FF(from OFF).

I'm picking up on this part, lol… I tried to invert the IF to compare FF in the condition, I tried to use While… but the beep only lasts as long as 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))

Answer:

The most practical way to keep the beep playing using winsound , is to separate the part that checks the logical condition from 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, winsound.Beep hangs when playing the sound, so if the sensor is turned on and then turned off, the beep continues sounding for the duration of the last beep. Another negative aspect of the execution lock is that the beep is never played while the program checks the log. Therefore, the duration of the beep should be thought of for convenience – too high a value reduces the turn-off response time, and too low a value makes the beep too "blinking" as it is not played during the loop. reading the log.

Scroll to Top
AllEscort