Python:将套接字绑定到/ dev / rfcommX

时间:2012-09-21 10:18:13

标签: python sockets rfcomm

我有一个脚本可以通过串口在嵌入式设备上加载固件。该脚本将串行端口的/ dev / xxx地址作为参数。 我有一个特殊的设置,我通过蓝牙这样做。 我设法通过蓝牙连接到python中的模块。但是我找不到任何关于如何在/ dev / rfcomm0等串行设备中注册本地套接字的示例/信息。

####### ORCA Bluetooth uploader Script #######
#As an input argument the script needs the BT Adress of the racer.
#if none is given, the script looks for nearby devices.

import bluetooth
import subprocess
import sys
import os

 #Check if we received a BT ADDR
if sys.argv[-1] != 'bt_comm.py':
  #Define the Pin for binding and the connection Port
  #Our Pin is 0000
  pin = "0000"
  #The Bootoader is listening on Port 1 for a connection
  port = 1
  rfcomm_port = 0
  #The BT Addr we get from the command line input
  #TAG:TODO:Check the input for sanity
  addr = sys.argv[-1]

  #Build the Firmware image
  subprocess.call("python ./px_mkfw.py --image ../../Firmware_orca/Images/px4fmu.bin  >    orca_fw",shell=True)

  #Create an RFCOMM Socket
  server_sock=bluetooth.BluetoothSocket( bluetooth.RFCOMM )

  #Connect to the device
  server_sock.connect((addr, port))
  print "Connected to ",addr

这里我需要一些方法在/ dev / rfcomm0

注册server_sock
  #Call the px_uploader script
  subprocess.call("python ./px_uploader.py --port /dev/rfcomm%d orca_fw" % rfcomm_port,shell=True)

  #Close the connection
  server_sock.close()

else:
  #Look for BT Devices and print them
  discovered_devices = bluetooth.discover_devices(lookup_names = True, flush_cache = True, duration = 20)
  if discovered_devices is not None:
    for addr in discovered_devices:
      print "Found bluetooth device: ", addr
  else:
    print "Could not find any bluetooth device nearby"

0 个答案:

没有答案