0

I'm using a serial connection via Bluetooth to read results from an IMU. I'm done writing some low level methods setting up the basic connection. But when writing the other classes (e.g. visualize or calculate) I'm continuously closing and reopening the serial connection and the Bluetooth port. This puts stress on the Bluetooth driver and the port will not open after a certain amount of uses.

I'm using Spyder with Python 3.4 on a Windows 7 64bit laptop with an integrated Bluetooth device. I can't change the OS but i can install software.

Is there a way I can keep the serial_connection class running whilst programming other classes which use the serial_connection class?

Bellerofont
  • 1,081
  • 18
  • 17
  • 16
Kevinrvr
  • 1
  • 2

1 Answers1

1

well, you can come with strategies to hot-reload components using some triggers by using the importlib module:

from importlib import reload
import module_with_your_code

def refresh(self):
    # make your context in a clean slate before reloading
    reload(module_with_your_code)
    # init your context

that you can trigger with various events (be a signal like USR1:

import signal, os

def on_sigusr1(a,b):
    refresh()

signal.signal(signal.SIGUSR1, on_sigusr1)
print("> reload me by doing: kill -USR1 {}".format(os.getpid())

or using a sys.stdin event… it's all up to you).

but instead of messing with your python interpreter, my best advice to you is to split your code in different processes. Have one process talk to the bluetooth stuff and accept data through some RPC (whether it's a raw TCP socket, or a file pipe, or an HTTP REST API, or a pubsub message broker it's all up to you!).

Then you implement a second module that spawns a client to that RPC and does the real deal.

Finally, if you must have both codes interact without distant calls, you can easily replace the two processes and remote calls with Queues and asyncio.

zmo
  • 24,463
  • 4
  • 54
  • 90