|
楼主 |
发表于 2011-2-13 22:59:25
|
显示全部楼层
在Python下写了一个类库,基本实现了之前用VC做的功能。类库代码(文件名:btcomm.py)
- #btcomm.py
- # class to handle the communication via bluetooth with NXT
- # need pyserial, download from: http://pyserial.sourceforge.net/
- # by Grant, 2011-2-13
- import sys, time
- import serial
- class BTComm:
- def __init__(self, comport = "COM8"):
- self.serial = serial.Serial()
- self.serial.port = comport #"COM8"
- self.serial.baudrate = 96000
- self.serial.timeout = 3
- self.serial.bytesize = 7
- self.serial.parity = 'O'
- self.serial.stopbits = 1
- def btopen(self):
- try:
- self.serial.open()
- if self.serial.isOpen():
- print("Bluetooth connection ok!")
- return(0)
- else:
- return(-1)
- except Exception as ex:
- print("Error!\n%s" % ex)
- if self.serial.isOpen():
- self.serial.close()
- def btclose(self):
- if self.serial.isOpen():
- self.serial.close()
- # read message string from NXT
- def btreadmessage(self):
- if self.serial.isOpen():
- n = self.serial.inWaiting()
- if n:
- readdata = slef.serial.read(n)
- return(readdata)
- else:
- return("")
-
- # send a message string to NXT (by default: mailbox 1).
- def btsendmessage(self, strMessage, mailbox = 0):
- if self.serial.isOpen():
- ilength = len(strMessage)
- senddata = bytes([0x0, 0x80, 0x9])
- senddata = (chr(ilength+5)).encode() + senddata
- senddata = senddata + chr(mailbox).encode() + (chr(ilength+1)).encode()
- senddata = senddata + strMessage.encode() + b'\0'
- icount = self.serial.write(senddata)
- return(icount)
- # send a logic value (0 / 1) to NXT.
- def btsetlogic(self, bLogic, mailbox = 0):
- if self.serial.isOpen():
- senddata = bytes([0x6, 0x0, 0x80, 0x9])
- senddata = senddata + chr(mailbox).encode() + chr(0x2).encode()
- senddata = senddata + (chr(bLogic)).encode()
- senddata = senddata + (chr(0x0)).encode()
- icount = self.serial.write(senddata)
- return(icount)
- # send a value to NXT. not ready yet.
- def btsendvalue(self, value, mailbox = 0):
- if self.serial.isOpen():
- senddata = bytes([0x6, 0x0, 0x80, 0x9])
- senddata = senddata + chr(mailbox).encode() + chr(0x5).encode()
- for i in range(4):
- senddata = senddata + (chr(value & 0xff)).encode('latin-1')
- value = int(value / 256)
- # senddata = senddata + (chr(value)).encode()
- senddata = senddata + (chr(0x0)).encode()
- icount = self.serial.write(senddata)
- return(icount)
- # function to read back the voltage of battery, in mili-volt
- def btreadnxtbattery(self):
- if self.serial.isOpen():
- self.serial.write(bytes([0x2, 0x0, 0x0, 0xb]))
- time.sleep(1)
-
- n = self.serial.inWaiting()
- if n:
- c = self.serial.read(n)
- batterymilivolt = c[n-2] + c[n-1]*256
- return(batterymilivolt)
- else:
- print("No data back from serial port!")
- return(0)
- else:
- print("Bluetooth port connect fail!")
- return(-1)
- # function to read back the firmware version.
- def btgetfirmwareversion(self):
- if self.serial.isOpen():
- senddata = bytes([0x2, 0x0, 0x01, 0x88])
- self.serial.write(senddata)
- time.sleep(1)
- n = self.serial.inWaiting()
- if n:
- c = self.serial.read(n)
- firmwareversion = str(c[8]) + "." + str(c[7])
- return(firmwareversion)
- else:
- return("")
-
复制代码
|
|