标签归档:serial-port

Python:发出哔哔声

问题:Python:发出哔哔声

我正在尝试使该程序发出哔哔声。我在Windows机器上。我看了http://docs.python.org/library/winsound.html

但不确定如何使用条形码扫描仪对此进行编程。

这是我的串行条形码扫描仪代码。

ser = serial.Serial()
ser.baudrate = 9600

#for windows
ser.port = 2 #for COM3

ser.open()
ser.write('hello')
ser.close()

更新:因为我用哔哔声打扰了我的同事。我可以让它穿过耳机的音频插孔吗?

I’m trying to get the program to give me a beeping noise. I’m on a windows machine. I’ve looked at http://docs.python.org/library/winsound.html

But not sure how I can program this with a barcode scanner.

Here is my code for the serial barcode scanner.

ser = serial.Serial()
ser.baudrate = 9600

#for windows
ser.port = 2 #for COM3

ser.open()
ser.write('hello')
ser.close()

UPDATE: Since I’m annoying my co-workers with the beep. Can I get it to come through the audio jack for headphones?


回答 0

在Windows上,如果只想让计算机发出哔声:

import winsound
frequency = 2500  # Set Frequency To 2500 Hertz
duration = 1000  # Set Duration To 1000 ms == 1 second
winsound.Beep(frequency, duration)

winsound.Beep()无论你希望发生蜂鸣可以使用。

On Windows, if you want to just make the computer make a beep sound:

import winsound
frequency = 2500  # Set Frequency To 2500 Hertz
duration = 1000  # Set Duration To 1000 ms == 1 second
winsound.Beep(frequency, duration)

The winsound.Beep() can be used wherever you want the beep to occur.


回答 1

跨平台的方法是print '\a'。这会将ASCII Bell字符发送到stdout,并有望产生提示音(“ alert”(警告))。请注意,许多现代的终端仿真器都提供了忽略响铃字符的选项。

由于您使用的是Windows,因此您会很高兴听到Windows拥有自己的Beep API(您自己可以使用),该API允许您发送任意长度和音高的蜂鸣声。请注意,这是仅Windows的解决方案,因此,print '\a'除非您真的关心赫兹和毫秒,否则您应该选择它。

可通过winsound模块访问Beep API :http : //docs.python.org/library/winsound.html

The cross-platform way to do this is to print('\a'). This will send the ASCII Bell character to stdout, and will hopefully generate a beep (a for ‘alert’). Note that many modern terminal emulators provide the option to ignore bell characters.

Since you’re on Windows, you’ll be happy to hear that Windows has its own (brace yourself) Beep API, which allows you to send beeps of arbitrary length and pitch. Note that this is a Windows-only solution, so you should probably prefer print('\a') unless you really care about Hertz and milliseconds.

The Beep API is accessed through the winsound module: http://docs.python.org/library/winsound.html


回答 2

Linux。

$ apt-get install beep

$ python
>>> os.system("beep -f 555 -l 460")

要么

$ beep -f 659 -l 460 -n -f 784 -l 340 -n -f 659 -l 230 -n -f 659 -l 110 -n -f 880 -l 230 -n -f 659 -l 230 -n -f 587 -l 230 -n -f 659 -l 460 -n -f 988 -l 340 -n -f 659 -l 230 -n -f 659 -l 110 -n -f 1047-l 230 -n -f 988 -l 230 -n -f 784 -l 230 -n -f 659 -l 230 -n -f 988 -l 230 -n -f 1318 -l 230 -n -f 659 -l 110 -n -f 587 -l 230 -n -f 587 -l 110 -n -f 494 -l 230 -n -f 740 -l 230 -n -f 659 -l 460

Linux.

$ apt-get install beep

$ python
>>> os.system("beep -f 555 -l 460")

OR

$ beep -f 659 -l 460 -n -f 784 -l 340 -n -f 659 -l 230 -n -f 659 -l 110 -n -f 880 -l 230 -n -f 659 -l 230 -n -f 587 -l 230 -n -f 659 -l 460 -n -f 988 -l 340 -n -f 659 -l 230 -n -f 659 -l 110 -n -f 1047-l 230 -n -f 988 -l 230 -n -f 784 -l 230 -n -f 659 -l 230 -n -f 988 -l 230 -n -f 1318 -l 230 -n -f 659 -l 110 -n -f 587 -l 230 -n -f 587 -l 110 -n -f 494 -l 230 -n -f 740 -l 230 -n -f 659 -l 460

回答 3

我在寻找相同的东西,但在寻找Linux shell。

这个话题给我一个答案,-谢谢-

也许更pythonic方式:

import os
beep = lambda x: os.system("echo -n '\a';sleep 0.2;" * x)
beep(3)

注意事项:

  • 睡眠值(此处为0.2),取决于默认蜂鸣声的长度(秒)
  • 为了简化起见,我选择使用os.system而不是subprocess.Popen(可能是不好的)
  • “ -n”表示echo不再显示
  • 最后 ‘;’ 之后sleep对于生成的文本序列(* x)是必需的
  • 也通过ssh在X术语上进行了测试

I was searching for the same but for Linux shell.

The topic brought me to an answer, -thanks-

Maybe more pythonic manner :

import os
beep = lambda x: os.system("echo -n '\a';sleep 0.2;" * x)
beep(3)

Notes :

  • the sleep value (here 0.2), depends on the length (seconds) of your default beep sound
  • I choosed to use os.system rather then subprocess.Popen for simplicity (it could be bad)
  • the ‘-n’ for echo is to have no more display
  • the last ‘;’ after sleep is necessary for the resulting text sequence (*x)
  • also tested through ssh on an X term

回答 4

有一个Windows答案和一个Debian答案,所以这是Mac版本

假设您只是在这里寻找一种快速的方法来制作可自定义的警报声音,而不是专门在Windows上获得的压电蜂鸣声:

os.system( "say beep" )

免责声明:如果您担心有人对您的蜂鸣代码进行黑客攻击,则可以用os.systemsubprocess模块的调用代替。

请参阅:如何在Mac OS X 10.6中发出硬件蜂鸣声

There’s a Windows answer, and a Debian answer, so here’s a Mac one:

This assumes you’re just here looking for a quick way to make a customisable alert sound, and not specifically the piezeoelectric beep you get on Windows:

os.system( "say beep" )

Disclaimer: You can replace os.system with a call to the subprocess module if you’re worried about someone hacking on your beep code.

See: How to make the hardware beep sound in Mac OS X 10.6


回答 5

跨平台方式:

import time
import sys
for i in range(1,6):
    sys.stdout.write('\r\a{i}'.format(i=i))
    sys.stdout.flush()
    time.sleep(1)
sys.stdout.write('\n')

The cross-platform way:

import time
import sys
for i in range(1,6):
    sys.stdout.write('\r\a{i}'.format(i=i))
    sys.stdout.flush()
    time.sleep(1)
sys.stdout.write('\n')

回答 6

我为此目的制作了一个包装

您可以像这样使用它:

from pybeep.pybeep import PyVibrate, PyBeep
PyVibrate().beep()
PyVibrate().beepn(3)
PyBeep().beep()
PyBeep().beepn(3)

它取决于sox,仅支持python3。

I have made a package for that purpose.

You can use it like this:

from pybeep.pybeep import PyVibrate, PyBeep
PyVibrate().beep()
PyVibrate().beepn(3)
PyBeep().beep()
PyBeep().beepn(3)

It depends on sox and only supports python3.


回答 7

使用pygame任何平台上

使用的好处pygame是可以使其在任何OS平台上都能工作。下面的示例代码虽然适用于GNU / Linux

首先按照此处的详细说明安装pygame模块。python3

$ sudo pip3 install pygame

pygame模块可以播放.wav.ogg从任意文件位置的文件。这是一个例子:

#!/usr/bin/env python3
import pygame
pygame.mixer.init()
sound = pygame.mixer.Sound('/usr/share/sounds/freedesktop/stereo/phone-incoming-call.oga')
sound.play()

Using pygame on any platform

The advantage of using pygame is that it can be made to work on any OS platform. Below example code is for GNU/Linux though.

First install the pygame module for python3 as explained in detail here.

$ sudo pip3 install pygame

The pygame module can play .wav and .ogg files from any file location. Here is an example:

#!/usr/bin/env python3
import pygame
pygame.mixer.init()
sound = pygame.mixer.Sound('/usr/share/sounds/freedesktop/stereo/phone-incoming-call.oga')
sound.play()