我與bitalino板工作,我想打印使用Python的數據,但是當我運行正確的代碼,它讓我看到消息全球名「藍牙」沒有定義
全局名稱「藍牙'未定義
根據我的電腦板通過藍牙連接。我不知道問題是什麼,你能幫我嗎? PD:我使用的是Mac OS X.
這是代碼的一部分,其中的問題可能是:
try:
import bluetooth
from bluetooth import discover_devices
except ImportError:
pass
import serial
from serial.tools import list_ports
import time
import math
import numpy
class BITalino(object):
def __init__(self):
"""
BITalino class: interface to the BITalino hardware.
"""
self.socket = None
self.analogChannels = []
self.number_bytes = None
self.macAddress = None
self.serial = False
def find(self, serial=False):
"""
Search for bluetooth devices nearby
Output: tuple with name and mac address of each device found
"""
try:
if serial:
nearby_devices = list(port[0] for port in list_ports.comports() if 'bitalino' or 'COM' in port[0])
else:
nearby_devices = discover_devices(lookup_names=True)
return nearby_devices
except:
return -1
def open(self, macAddress=None, SamplingRate=1000):
"""
Connect to bluetooth device with the mac address provided.
Configure the sampling Rate.
Kwargs:
macAddress (string): MAC address of the bluetooth device
SamplingRate(int): Sampling frequency (Hz); values available: 1000, 100, 10 and 1
Output: True or -1 (error)
"""
Setup = True
while Setup:
if macAddress != None:
try:
if ":" in macAddress and len(macAddress) == 17:
self.socket = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
self.socket.connect((macAddress, 1))
else:
self.socket = serial.Serial(macAddress, 115200)
self.serial = True
time.sleep(2)
# Configure sampling rate
if SamplingRate == 1000:
variableToSend = 0x03
elif SamplingRate == 100:
variableToSend = 0x02
elif SamplingRate == 10:
variableToSend = 0x01
elif SamplingRate == 1:
variableToSend = 0x00
else:
self.socket.close()
raise TypeError, "The Sampling Rate %s cannot be set in BITalino. Choose 1000, 100, 10 or 1." % SamplingRate
return -1
variableToSend = int((variableToSend<<6)|0x03)
self.write(variableToSend)
Setup = False
except Exception, e:
print e
return -1
else:
raise TypeError, "A MAC address or serial port is needed to connect"
return -1
else:
self.macAddress = macAddress
return True
那麼應該如何代碼,如果'進口bluetooth'無法正常使用?你在它周圍放了一個'try..except',但是這隻會讓名字不確定。 –
我應該帶外面的進口藍牙?,我不知道它是否與mac和bluetooh連接或代碼有關的問題... –