Connecting to Ganglion with Python

coopapapcoopapap Montreal
edited October 2023 in General Discussion

Hi there. I'm looking to connect to my ganglion through python in order to visualize real-time data. Is this possible or do i need a third-party software/package?

The code I have now is found below, though it does not work. COM7 cannot seem to be found, though I can visualize it in my device manager and indeed get the following when I use the command 'mode' in my command prompt:

Status for device COM7:

Baud:            256000
Parity:          None
Data Bits:       8
Stop Bits:       1
Timeout:         ON
XON/XOFF:        OFF
CTS handshaking: OFF
DSR handshaking: OFF
DSR sensitivity: OFF
DTR circuit:     OFF
RTS circuit:     ON

So I know it is operating. I wonder perhaps if there is a driver that needs to be installed? I've found a cyton driver installation online, but not for the ganglion.

Any information you can provide would be of great benefit. Thank you!

PYTHON CODE -----------------------------------------

import serial
import matplotlib.pyplot as plt
from drawnow import drawnow
import numpy as np

# Initialize variables for data storage
eeg_data = []

# Create a function to update the plot
def plot_data():
    plt.plot(eeg_data, 'r-')

# Create a serial connection to the OpenBCI Ganglion
ser = serial.Serial('COM7', 115200)  # Use 'COM7' as your serial port

# Main loop for data streaming
while True:
    try:
        # Read data from the serial port
        line = ser.readline().decode('utf-8').strip()
        values = line.split(',')

        # Assuming the data format is 'timestamp, eeg_data'
        if len(values) == 2:
            timestamp, eeg_value = values
            eeg_data.append(float(eeg_value))

            # Limit the number of data points to plot
            if len(eeg_data) > 1000:
                eeg_data.pop(0)

            # Create the real-time plot
            plt.clf()
            drawnow(plot_data)

    except KeyboardInterrupt:
        ser.close()
        break

# Close the serial connection
ser.close()

Comments

Sign In or Register to comment.