Code to generate continuous beep



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

    The beginning of 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 connected sensors, so when a sensor is triggered on the screen it changes the status to ON, then when it triggers OFF.

    The code works, every time the status gets ON bipa.

    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'm playing "slip-off" the beep accompanies.

    But I would like if the last status, that is, if the last line deals with the status ON the beep became active. Until q between a new line in the file with FF(OFF).

    To catch that part, rs.. tried to reverse the IF to compare FF on condition, tried to use While.. but 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))



  • The most practical way to keep the beep playing using winsound, consists of separating 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 locks the execution when touching the sound, and therefore if the sensor is switched on and then switched off, the beep continues to sound until the last beep duration. Another negative aspect of the execution latch is that the beep is never played while the program checks the log. Therefore, the duration of the beep should be thought according to the convenience - a very high value reduces the response time of the shutdown, and a very low value makes the beep too "pillar" as this is not touched during the log reading loop.


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2