简体   繁体   English

从“挂起”蓝牙 python 连接中检测和恢复

[英]Detecting and recovering from “hanging” bluetooth python connection

I have a bluetooth connection between a Raspberry Pi 3b+ and an Arduino Mega, using the RPi built-in module, and an HC-05 module on the Arduino.我使用 RPi 内置模块和 Arduino 上的 HC-05 模块在 Raspberry Pi 3b+ 和 Arduino Mega 之间建立了蓝牙连接。 Bi-directional communication works like a charm, for minutes to sometimes, hours at a time.双向沟通就像一个魅力,几分钟到有时,一次几个小时。

Then, at seemingly random times, the python code hangs, blocked on the sock.recv() function.然后,在看似随机的时间,python 代码挂起,被sock.recv() function 阻塞。 I can kill it via ctrl-c, and restart it, and it usually reconnects without a problem.我可以通过 ctrl-c 杀死它,然后重新启动它,它通常会毫无问题地重新连接。

I know Bluetooth is a little finicky and so while any suggestions of how to make the communication more robust and extend the runtime-until-hanging are definitely appreciated, what I'm more interested in is how to detect this "hanging" and recover from it.我知道蓝牙有点挑剔,所以虽然任何关于如何使通信更加健壮和延长运行时直到挂起的建议都值得赞赏,但我更感兴趣的是如何检测这种“挂起”并从中恢复它。 ie: I want to just kill the connection and try to reconnect from within the Python program, rather than me having to see this myself and react to it.即:我只想终止连接并尝试从 Python 程序中重新连接,而不是我自己必须看到并对此做出反应。

This is what I have so far in python:这是我迄今为止在 python 中所拥有的:

#!/usr/bin/python3

import datetime
import socket
import sys
import time

import bluetooth

COMMAND_START_CHAR = '<'
COMMAND_END_CHAR = '>'
LOGFILE = 'bt.log'


def SearchForFullCommand(buffer):
  """Puts fully formed commands from buffer into a list, returning the remaining buffer.

  We expect commands to be demarcated by COMMAND_START_CHAR and COMMAND_END_CHAR.  The
  buffer may have zero or more such commands. This function finds all demarcated commands,
  strips off those demarcations, and returns the remaining buffer.  Any text that arrives
  before a COMMAND_START_CHAR is discarded.

  Args:
    buffer: string representing the text received so far.

  Returns:
    A 2-tuple, where the first element is the remaining buffer, and the second element
    is a potentially empty list of identified commands.
  """
  commands = []
  while COMMAND_END_CHAR in buffer:
    end_pos = buffer.find(COMMAND_END_CHAR)
    if COMMAND_START_CHAR in buffer[:end_pos]:
      start_pos = buffer.find(COMMAND_START_CHAR) + len(COMMAND_START_CHAR)
      commands.append(buffer[start_pos:end_pos])
    buffer = buffer[end_pos+len(COMMAND_END_CHAR):]
  return (buffer, commands)  # no command found


def Log(s):
  """Appends message s to the logfile."""
  with open(LOGFILE, 'a') as f:
    f.write('%s\n' % s)


def ConnectBluetooth(address, port):
  """Attempts to make one connection to the given address and port."""
  sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
  sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  try:
    sock.connect((address, port))
  except (bluetooth.btcommon.BluetoothError) as e:
    Log('Failed to connect: %s' % e)
    return None
  return sock


def ConnectBluetoothRetry(address, port, seconds):
  """Attempts to make connections for a number of seconds, exiting program on fail."""
  second = 0
  while second < seconds:
    sock = ConnectBluetooth(address, port)
    if sock:
      Log('Connected after %d seconds' % second)
      return sock
    time.sleep(1)
    second += 1
  Log('Failed to connect after %d seconds' % second)
  sys.exit()


def main():
  """Sends sequential numbers over bluetooth, and receives & parses anything sent."""
  sys.stderr = open(LOGFILE, 'a')

  start = time.time()
  timestring = datetime.datetime.fromtimestamp(start).strftime('%Y-%m-%d %H:%M:%S')
  Log('Started at %s' % timestring)

  bd_addr = '98:D3:11:FC:42:16'
  port = 1
  sock = ConnectBluetoothRetry(bd_addr, port, 10)

  buffer = ''
  x = 0

  while True:
    try:
      recv = sock.recv(1024)
    except (bluetooth.btcommon.BluetoothError) as e:
      Log('Failed to receive: %s' % e)
      sock.close()
      sock = ConnectBluetoothRetry(bd_addr, port, 10)
    Log('.. %s (len=%d) after running for %.3f hours' % (
        recv, len(recv), (time.time() - start) / 60**2))
    buffer += recv.decode()
    buffer, commands = SearchForFullCommand(buffer)
    if commands:
      for n, command in enumerate(commands):
        Log('Received full command #%d: %s' % (n, command))

    send = COMMAND_START_CHAR+str(x)+COMMAND_END_CHAR
    try:
      sock.send(send)
    except (bluetooth.btcommon.BluetoothError) as e:
      Log('Failed to send %s: %s' % (send, e))
      sock.close()
      sock = ConnectBluetoothRetry(bd_addr, port, 10)
    Log('Sent %s' % send)

    x += 1
    time.sleep(1)


main()

When working well, the python log file looks like this:运行良好时,python 日志文件如下所示:

.. b'646>' (len=4) after running for 0.843 hours
Received full command #0: 646
Sent <2526>
.. b'<647>' (len=5) after running for 0.843 hours
Received full command #0: 647
Sent <2527>
.. b'<' (len=1) after running for 0.844 hours
Sent <2528>
.. b'648>' (len=4) after running for 0.844 hours

Then, noticing it had stopped working, I killed it, and then restarted it:然后,注意到它已经停止工作,我将其杀死,然后重新启动它:

KeyboardInterrupt
Started at 2020-05-03 11:15:07
Failed to connect: [Errno 16] Device or resource busy
Failed to connect: [Errno 16] Device or resource busy
Failed to connect: [Errno 16] Device or resource busy
Failed to connect: [Errno 16] Device or resource busy
Failed to connect: [Errno 16] Device or resource busy
Failed to connect: [Errno 16] Device or resource busy
Failed to connect: [Errno 16] Device or resource busy
Failed to connect: [Errno 16] Device or resource busy
Failed to connect: [Errno 16] Device or resource busy
Failed to connect: [Errno 16] Device or resource busy
Failed to connect after 10 seconds

I try again:我再试试:

Started at 2020-05-03 11:15:42
Failed to connect: [Errno 112] Host is down
Failed to connect: [Errno 112] Host is down
Failed to connect: [Errno 112] Host is down
Connected after 3 seconds
.. b'1146><1147><1148><1149><1150><1151><1152><1153><1154><1155><1156><1157><1158><1159><1160><1161><1162><1163><1164><1165><1166><1' (len=127) after running for 0.005 hours
Received full command #0: 1147
Received full command #1: 1148
Received full command #2: 1149

... and it runs for another hour or two, before hanging again. ...它又运行了一两个小时,然后再次挂起。 I'm not physically moving either sender or receiver - they're within a foot of each other - so it's not range.我没有在物理上移动发送者或接收者 - 它们彼此相距一英尺 - 所以它不是范围。 Though I have tried disconnecting the Arduino, and repowering it, and they do reconnect without a problem within the still-running Python process.虽然我尝试断开 Arduino 并重新启动它,但它们在仍在运行的 Python 进程中重新连接没有问题。

The Arduino code, though I don't think its as relevant, is here: Arduino 代码虽然我认为它不相关,但在这里:

long n = 1;

void setup() 
{
    Serial.begin(9600);

    // HC-05 default serial speed for communcation mode is 9600
    Serial1.begin(9600);  
}

void loop() 
{
    Serial1.print("<");
    Serial1.print(n);
    Serial1.print(">");
    if(Serial1.available() > 0){ // Checks whether data is comming from the serial port
      Serial.println(Serial1.readString());} // Reads the data from the serial port
    delay(1000);
    n++;
}

Thanks for any help or suggestions!感谢您的任何帮助或建议!

I could not get the socket connection to last more than a few hours despite a few days of different approaches & efforts;尽管几天不同的方法和努力,我无法让套接字连接持续超过几个小时; I only went down the socket rabbit hole because I was unable to figure out how to use pySerialTransfer, or even just serial, over bluetooth.我只去了套接字兔子洞,因为我无法弄清楚如何通过蓝牙使用 pySerialTransfer,甚至只是串行。

So, going back to the serial approach, after a bit more effort and investigation, I was ultimately able to get that approach working for a stable (over 1 day at this point) bluetooth connection between Raspberry Pi & HC-05 on Arduino.所以,回到串行方法,经过更多的努力和调查,我最终能够让这种方法在 Arduino 上的 Raspberry Pi 和 HC-05 之间实现稳定的(此时超过 1 天)蓝牙连接。

Code is here .代码在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM