Debugging Guideο
This guide covers debugging techniques, common issues, and troubleshooting steps for the Makcu Python Library.
Enable Debug Modeο
Debug mode provides detailed logging and helps identify issues:
Synchronous API:
from makcu import create_controller
# Enable debug mode
makcu = create_controller(debug=True)
Asynchronous API:
from makcu import create_async_controller
# Enable debug mode
makcu = await create_async_controller(debug=True)
Debug Output Examplesο
With debug mode enabled, youβll see detailed command flow:
[2025.123.456] [INFO] Makcu Controller v2.1.3 starting...
[2025.123.457] [DEBUG] Scanning for devices (VID:1A86, PID:55D3)
[2025.123.458] [INFO] Found device on COM3
[2025.123.459] [DEBUG] Opening serial connection: COM3 @ 4000000 baud
[2025.123.461] [INFO] Connected successfully
[2025.123.462] [DEBUG] Sent command #42: km.move(100,50)
[2025.123.464] [DEBUG] Command #42 completed in 0.002s
[2025.123.465] [DEBUG] Response: OK
Interactive Debug Consoleο
Use the command-line debug console for real-time troubleshooting:
python -m makcu --debug
Console Features:
Real-time command execution
Live response monitoring
Connection status display
Performance timing
Error reporting
Example Debug Session:
Makcu Debug Console v2.1.3
===========================
Connecting to device...
Connected to Makcu device on COM3
> km.version()
km.MAKCU
> km.move(50,25)
km.move(50,25)
Common Issues & Solutionsο
Device Not Foundο
Symptoms:
- MakcuConnectionError: No Makcu device found
- Device not detected on any port
Solutions:
Check Physical Connection:
# Test specific port python -m makcu --testPort COM3
Verify Device Manager (Windows): - Open Device Manager - Look for βUSB Serial Deviceβ under βPorts (COM & LPT)β - Note the COM port number - Check for driver issues (yellow warning icons)
Check USB Cable: - Try different USB cable - Ensure cable supports data (not power-only) - Test with different USB port
Device Power Cycle:
# In code - retry with delay import time from makcu import create_controller, MakcuConnectionError for attempt in range(3): try: makcu = create_controller(debug=True) break except MakcuConnectionError: print(f"Attempt {attempt + 1} failed, retrying...") time.sleep(2)
Permission Deniedο
Symptoms:
- PermissionError: [Errno 13] Permission denied
- Access is denied (Windows)
Solutions:
Linux/macOS:
# Add user to dialout group
sudo usermod -a -G dialout $USER
# Or run with sudo (not recommended)
sudo python your_script.py
# Check port permissions
ls -l /dev/ttyUSB*
Windows:
Run command prompt/IDE as Administrator
Check if another application is using the port
Close Arduino IDE, PuTTY, or other serial applications
Command Timeoutsο
Symptoms:
- MakcuTimeoutError: Command timed out after 0.1s
- Intermittent command failures
Solutions:
Increase Timeout:
# Default timeout is 0.1s (100ms) makcu = create_controller(timeout=0.5) # 500ms timeout
Check USB Connection: - Use high-quality USB cable - Avoid USB hubs when possible - Try different USB ports
Reduce Command Frequency:
import asyncio # Add small delays for stability async def stable_clicking(): for _ in range(10): await makcu.click(MouseButton.LEFT) await asyncio.sleep(0.01) # 10ms between clicks
Intermittent Disconnectionsο
Symptoms:
- Device randomly disconnects
- MakcuConnectionError during operation
Solutions:
Enable Auto-Reconnection:
makcu = create_controller(auto_reconnect=True, debug=True) # Or with custom retry settings makcu = create_controller( auto_reconnect=True, reconnect_attempts=5, reconnect_delay=1.0 )
Connection Status Monitoring:
@makcu.on_connection_change def handle_connection(connected: bool): if connected: print("Device reconnected!") else: print("Device disconnected - attempting reconnection...")
Robust Error Handling:
from makcu import MakcuConnectionError async def robust_operation(): max_retries = 3 for attempt in range(max_retries): try: await makcu.click(MouseButton.LEFT) return # Success except MakcuConnectionError: if attempt < max_retries - 1: print(f"Retry {attempt + 1}/{max_retries}") await asyncio.sleep(0.5) else: raise # Final attempt failed
Performance Issuesο
Symptoms: - Commands take longer than expected - High CPU usage - Memory leaks
Solutions:
Disable Debug Mode in Production:
# Debug mode adds logging overhead makcu = create_controller(debug=False)
Use Batch Operations:
# Instead of individual commands for i in range(100): makcu.move(1, 0) # Slow # Use batch execution commands = [lambda: makcu.move(1, 0) for _ in range(100)] makcu.batch_execute(commands) # Fast
Optimize Connection Checks:
# Use cached connection status if makcu.is_connected(): # Fast - cached makcu.click(MouseButton.LEFT) # Avoid repeated connection attempts with makcu: # Context manager ensures connection for _ in range(1000): makcu.move(1, 0) # No connection check per call
Memory Usageο
Monitor Memory Usage:
import psutil
import gc
def monitor_memory():
process = psutil.Process()
memory_mb = process.memory_info().rss / 1024 / 1024
print(f"Memory usage: {memory_mb:.1f} MB")
# Check before and after operations
monitor_memory()
# ... your Makcu operations ...
gc.collect() # Force garbage collection
monitor_memory()
Memory Optimization:
# Use context managers for automatic cleanup
async with await create_async_controller() as makcu:
# Operations here
pass # Automatic cleanup when exiting
# Explicit cleanup for long-running applications
makcu.disconnect()
del makcu
Advanced Debuggingο
Custom Loggingο
Set up custom logging for detailed troubleshooting:
import logging
# Configure detailed logging
logging.basicConfig(
level=logging.DEBUG,
format='%(asctime)s [%(levelname)s] %(name)s: %(message)s',
handlers=[
logging.FileHandler('makcu_debug.log'),
logging.StreamHandler()
]
)
# Create controller with debug enabled
makcu = create_controller(debug=True)
Raw Command Monitoringο
Monitor raw serial communication:
# Access low-level transport
response = await makcu.transport.async_send_command(
"km.version()",
expect_response=True,
timeout=0.1
)
print(f"Raw response: {response}")
Performance Profilingο
Profile command execution times:
import time
import statistics
# Measure command performance
timings = []
for _ in range(100):
start = time.perf_counter()
makcu.click(MouseButton.LEFT)
end = time.perf_counter()
timings.append((end - start) * 1000) # Convert to ms
print(f"Average: {statistics.mean(timings):.2f}ms")
print(f"Min: {min(timings):.2f}ms")
print(f"Max: {max(timings):.2f}ms")
print(f"Std Dev: {statistics.stdev(timings):.2f}ms")
Network Debugging (if applicable)ο
For remote debugging or networked applications:
import socket
import json
# Simple debug server
class MakcuDebugServer:
def __init__(self, port=8888):
self.sock = socket.socket()
self.sock.bind(('localhost', port))
self.sock.listen(1)
print(f"Debug server listening on port {port}")
def handle_commands(self, makcu):
conn, addr = self.sock.accept()
try:
while True:
data = conn.recv(1024).decode()
if not data:
break
# Execute command and return result
try:
result = eval(f"makcu.{data}")
response = {"status": "ok", "result": str(result)}
except Exception as e:
response = {"status": "error", "error": str(e)}
conn.send(json.dumps(response).encode())
finally:
conn.close()
Test Suite Analysisο
Run comprehensive tests to identify issues:
python -m makcu --runtest
Test Categories:
Connection Tests: - Port detection - Device recognition
Functionality Tests: - Mouse movement accuracy - Button control - Scrolling behavior
Performance Tests: - Command execution speed - Memory usage - CPU utilization
Reliability Tests: - Auto-reconnection - Error recovery - Long-running stability
Interpreting Test Results:
test_connect_to_port PASSED (0.046s) β Good performance
test_mouse_movement FAILED β Check USB connection
test_button_control PASSED (0.001s) β Excellent timing
test_performance SLOW (0.150s) β οΈ Performance regression
Debugging Checklistο
When encountering issues, work through this checklist:
Hardware: - [ ] USB cable connected securely - [ ] Device powered on - [ ] Try different USB port - [ ] Test with different USB cable
Software: - [ ] Latest Makcu library version installed - [ ] Python version 3.8+ - [ ] No conflicting serial applications running - [ ] Proper permissions (Linux/macOS)
Connection: - [ ] Device appears in Device Manager (Windows) - [ ] Correct COM port identified - [ ] Port not in use by other applications - [ ] Baud rate properly configured
Code: - [ ] Debug mode enabled - [ ] Proper error handling implemented - [ ] Timeouts configured appropriately - [ ] Auto-reconnection enabled if needed
Performance: - [ ] Debug mode disabled in production - [ ] Batch operations used where possible - [ ] Memory usage monitored - [ ] Connection checks optimized
Getting Helpο
If youβre still experiencing issues:
Check GitHub Issues: - Search existing issues: https://github.com/SleepyTotem/makcu-py-lib/issues - Create new issue with debug logs
Enable Comprehensive Logging:
import logging logging.basicConfig(level=logging.DEBUG) makcu = create_controller(debug=True)
Include System Information: - Operating system and version - Python version - Makcu library version - Device firmware version - Full error traceback
Provide Minimal Reproducible Example:
from makcu import create_controller, MouseButton # Minimal code that reproduces the issue makcu = create_controller(debug=True) makcu.click(MouseButton.LEFT) # Fails here
Community Resources: - GitHub Discussions: https://github.com/SleepyTotem/makcu-py-lib/discussions - Stack Overflow with tag makcu