Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,10 @@ This cell-sim is designed to mimic a LiPo battery pack for develpoment of the su
1. install python if you don't have it already
2. install the requirements `pip install -r requirements.txt`
3. connect the board via USB
4. Find the port, something like `/dev/cu.usbmodem1101` on Mac or `COM3` on Windows (look in your device manager)
5. Update the port in example_set_voltages.py
![Port](docs/port.png)
6. Power the board with the 12V input (supply: 1A minimum, 3A recommended)
7. run the example python script `python example_set_voltages.py`
8. Voltages should be set to 3.5V, then rainbow from 1V to 4V across the 16 channels and current should be close to 0A.
4. Power the board with the 12V input (supply: 1A minimum, 3A recommended)
5. run the example python script `python example_set_voltages.py`
6. Voltages should be set to 3.5V, then rainbow from 1V to 4V across the 16 channels and current should be close to 0A.

![Test Output](docs/test-output.png)

Expand Down
68 changes: 65 additions & 3 deletions example_set_voltages.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,70 @@
#!/usr/bin/env python3
from lib.cellsim import CellSim
import threading
import time
from queue import Queue

import serial.tools.list_ports

from lib.cellsim import CellSim


def find_cellsim_port():
"""Scan all serial ports to find a CellSim device."""
ports = list(serial.tools.list_ports.comports())
if not ports:
print("No serial ports found!")
return None

print("Scanning for CellSim device...")

# Queue for the first found port
port_queue = Queue()
stop_event = threading.Event()

def try_port(port):
if stop_event.is_set():
return
try:
print(f"Trying port {port.device}...")
test_cellsim = CellSim(port.device)
response = test_cellsim.client.send_command("PING")
test_cellsim.close()
if response and "OK:PONG" in response[0]:
print(f"Found CellSim device on port {port.device}")
port_queue.put(port.device)
stop_event.set() # Signal other threads to stop
except Exception as e:
print(f"Port {port.device} failed: {str(e)}")

# Start a thread for each port
threads = []
for port in ports:
thread = threading.Thread(target=try_port, args=(port,))
thread.daemon = True # Make threads daemon so they exit when main thread exits
threads.append(thread)
thread.start()

# Wait for either a port to be found or all threads to complete
while True:
if not port_queue.empty():
stop_event.set() # Signal remaining threads to stop
return port_queue.get()

if all(not t.is_alive() for t in threads):
break

time.sleep(0.1)

print("No CellSim device found!")
return None


def main():
# Replace with your serial port (e.g., '/dev/ttyUSB0' on Linux/Mac or 'COM3' on Windows)
port = '/dev/cu.usbmodem1101'
# Find the CellSim device
port = find_cellsim_port()
if port is None:
exit(1)

cellsim = CellSim(port)

# Test setting all voltages at once
Expand Down Expand Up @@ -55,5 +116,6 @@ def main():

cellsim.close()


if __name__ == '__main__':
main()