Code to generate continuous beep

Asked

Viewed 155 times

3

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

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 triggered on the screen it changes the status to ON, so when it disables OFF.

The code works, every time the status gets paged.

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

So if I keep playing "on-off" the beep accompanies.

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

To catch this part, rs.. I tried to invert the IF to compare FF in the condition, I tried to use While.. but the beep only lasts 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))
  • That one with and all that is beneath it until the time.sleep(0.0) should not be indented with 4 more characters?

  • I do not understand, I only know if you copy and paste this code the way it is works

  • humm has already solved its problem ?

1 answer

1


The most practical way to keep beeping 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, the winsound.Beep Stop the execution by playing the sound, and therefore if the sensor is on and then off, the beep continues to ring until the last beep lasts. 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 out as per convenience - a very high value reduces the response time of the shutdown, and a very low value causes the beeper to be "blinking" too much, as it is not played during the log read loop.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.