Skip to content

Add socket into backend. That allow record/stop by python#56

Closed
chenxinfeng4 wants to merge 1 commit into
Aharoni-Lab:masterfrom
chenxinfeng4:dev-socket
Closed

Add socket into backend. That allow record/stop by python#56
chenxinfeng4 wants to merge 1 commit into
Aharoni-Lab:masterfrom
chenxinfeng4:dev-socket

Conversation

@chenxinfeng4

Copy link
Copy Markdown

The code for complete issue #55

image
Use python3 to start / stop Control Pannel.

Prospect:

  • Use python3 to synchronize Miniscope software with other DAQ system.

  • Synchronize multiple Miniscope software.

Demo:

1. Use python3 commands to synchronize

# %%
import socket
import time
# %% create connection
tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serve_ip = 'localhost'
serve_port = 20172       #default backend Socket PORT [20172, 20173, 20174, 20175, ...]
tcp_socket.connect((serve_ip, serve_port))


def send_read(send_data):
    send_data_byte = send_data.encode("utf-8")
    tcp_socket.send(send_data_byte)

    from_server_msg = tcp_socket.recv(1024)
    print(from_server_msg.decode("utf-8"))

# %% Supported commands
cmds = ['query_record', 'start_record', 'stop_record']

for send_data in cmds:
    send_read(send_data)
    time.sleep(5)

image

2. Use bind hotkey to synchronize

Further, I can bind global-hotkeys to start_record(F2) and stop_record(F4).
Multiple DAQ can bind the same hotkey to start together.

# !pip install global-hotkeys
import socket
import time
from global_hotkeys import *

# Flag to indicate the program whether should continue running.
is_alive = True

# %% create connection
def send_read(send_data):
    try:
        tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        serve_ip = 'localhost'
        serve_port = 20172       #default ArControl Recorder Socket PORT
        tcp_socket.connect((serve_ip, serve_port))
        send_data_byte = send_data.encode("utf-8")
        tcp_socket.send(send_data_byte)

        from_server_msg = tcp_socket.recv(1024)
        print(from_server_msg.decode("utf-8"))
        del tcp_socket
    except Exception:
        pass
    

bindings = [
    [["f2"], lambda: send_read("start_record"), None],
    [["f4"], lambda: send_read("stop_record"), None],
]

register_hotkeys(bindings)

# Finally, start listening for keypresses
start_checking_hotkeys()

print("Ready. \n1. [F2] to start. \n2. [F4] to stop.")
# Keep waiting until the user presses the exit_application keybinding.
# Note that the hotkey listener will exit when the main thread does.
while is_alive:
    time.sleep(2)

@daharoni

Copy link
Copy Markdown
Member

Hi @chenxinfeng4, thanks for the PR and major apoloigies that is sat here for so so long. Programmatic start/stop addresses a real need.

I am closing this now because of where the software is headed, rather than anything wrong with the idea. We are just cleaning up a few things in this package and will release v2 as the last major release. After this, all active development moves to mio (https://github.com/miniscope/mio), a Python library for Miniscope DAQ and IO.

In the meantime, recording can be trigged externally.

Thanks again for the contribution!

@daharoni daharoni closed this Jul 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants