examples/API Test/README.md
Difficulty: 🟡 Intermediate | Time: ~10 minutes | License: GPL / Pro
A Python client for the Serial Studio API that allows external programs to control and configure Serial Studio through a TCP connection. Provides an interactive REPL, command-line interface, and automated testing capabilities.
The Serial Studio API exposes a TCP server on port 7777 (default) that allows external programs to:
This Python client provides:
Serial Studio running with API Server enabled
Python 3.8 or later (no additional dependencies required)
cd "examples/API Test"
python test_api.py send io.manager.getStatus
Expected output:
{
"isConnected": false,
"paused": false,
"busType": 0,
"configurationOk": false
}
Monitor Serial Studio in real-time with automatic status updates:
# Start live monitor (default 1 second updates)
python test_api.py monitor
# Fast updates (every 500ms)
python test_api.py monitor --interval 500
# Compact mode (append instead of clearing screen)
python test_api.py monitor --compact
The live monitor shows:
Press Ctrl+C to exit the monitor.
The interactive mode provides an easy way to explore and experiment with the API with a full-featured shell:
python test_api.py interactive
~/.serial_studio_api_historyInside the REPL, you can use these special commands:
| Command | Description |
|---|---|
help | Show help and available commands |
list | List all API commands with descriptions |
clear | Clear the screen |
quit / exit | Exit the REPL |
$ python test_api.py interactive
Serial Studio API Interactive Client
Connected to 127.0.0.1:7777
Type 'help' for available commands, 'list' to see all API commands, or 'quit' to exit.
ss> io.manager.getStatus
{
"isConnected": false,
"paused": false,
"busType": 0,
"configurationOk": false,
"readOnly": false,
"readWrite": false,
"busTypeName": "Serial Port"
}
ss> io.driver.uart.setBaudRate {"baudRate": 115200}
{
"baudRate": 115200
}
ss> io.driver.uart.getConfiguration
{
"port": "",
"baudRate": 115200,
"dataBits": 8,
"parity": "None",
"stopBits": 1,
"flowControl": "None"
}
ss> list
Available commands (158 total):
api.getCommands - Get list of all available commands
io.manager.connect - Connect to the currently configured device
io.manager.disconnect - Disconnect from the current device
csv.player.open - Open CSV file for playback
console.send - Send data to device
project.file.open - Open project file
...
ss> quit
Goodbye!
# Query connection status
python test_api.py send io.manager.getStatus
# Set UART baud rate (key=value format - works on all shells)
python test_api.py send io.driver.uart.setBaudRate -p baudRate=115200
# Set network address
python test_api.py send io.driver.network.setRemoteAddress -p address=192.168.1.100
# Multiple parameters
python test_api.py send io.driver.modbus.addRegisterGroup -p type=0 startAddress=0 count=10
# Boolean parameters
python test_api.py send io.manager.setPaused -p paused=true
# JSON format (alternative)
python test_api.py send io.driver.uart.setBaudRate --params '{"baudRate": 115200}'
# Show all API commands with descriptions
python test_api.py list
# Output as JSON (for scripting)
python test_api.py list --json
Execute multiple commands from a JSON file:
python test_api.py batch commands.json
Example commands.json:
[
{
"command": "io.manager.setBusType",
"id": "set-bus",
"params": {"busType": 0}
},
{
"command": "io.driver.uart.setBaudRate",
"id": "set-baud",
"params": {"baudRate": 115200}
},
{
"command": "io.driver.uart.getConfiguration",
"id": "get-config"
}
]
# Monitor connection status in real-time
python test_api.py monitor
# Fast updates (500ms)
python test_api.py monitor --interval 500
# Compact output (no screen clearing)
python test_api.py monitor --compact
# Run all automated tests
python test_api.py test
# Verbose output
python test_api.py test --verbose
Use python test_api.py list to see all commands, or run python test_api.py interactive and type list in the REPL.
api.* - API introspection (1 command)io.manager.* - I/O manager control (12 commands)io.driver.uart.* - UART/Serial configuration (12 commands)io.driver.network.* - Network (TCP/UDP) configuration (10 commands)io.driver.ble.* - Bluetooth Low Energy (9 commands)csv.export.* - CSV file export control (3 commands)csv.player.* - CSV file playback (9 commands)console.* - Console/terminal control (11 commands)dashboard.* - Dashboard configuration (7 commands)project.* - Project management (19+ commands)
project.file.* - File operations (new, open, save)project.group.* - Group management (add, delete, duplicate)project.dataset.* - Dataset management (add, delete, duplicate, setOption)project.action.* - Action management (add, delete, duplicate)project.parser.* - Frame parser code (getCode, setCode)project.*.list - List operations (groups.list, datasets.list, actions.list)io.driver.modbus.* - Modbus RTU/TCP configuration (21 commands)io.driver.canbus.* - CAN Bus configuration (9 commands)mqtt.* - MQTT client configuration (27 commands)mdf4.export.* - MDF4 file export control (3 commands)mdf4.player.* - MDF4 file playback (9 commands)io.driver.audio.* - Audio input/output (13 commands)For full command reference, see API_REFERENCE.md.
# Interactive mode
python test_api.py interactive
ss> io.manager.setBusType {"busType": 0}
ss> io.driver.uart.setBaudRate {"baudRate": 115200}
ss> io.driver.uart.setParity {"parityIndex": 0}
ss> io.driver.uart.setDataBits {"dataBitsIndex": 3}
ss> io.driver.uart.setStopBits {"stopBitsIndex": 0}
ss> io.driver.uart.getPortList
ss> io.driver.uart.setPortIndex {"portIndex": 0}
ss> io.manager.connect
# Command-line mode
python test_api.py send io.manager.setBusType -p busType=1
python test_api.py send io.driver.network.setSocketType -p socketTypeIndex=0
python test_api.py send io.driver.network.setRemoteAddress -p address=192.168.1.100
python test_api.py send io.driver.network.setTcpPort -p port=8080
python test_api.py send io.manager.connect
# Interactive mode for easier device discovery
python test_api.py interactive
ss> io.manager.setBusType {"busType": 2}
ss> io.driver.ble.getStatus
ss> io.driver.ble.startDiscovery
# Wait a few seconds...
ss> io.driver.ble.getDeviceList
ss> io.driver.ble.selectDevice {"deviceIndex": 0}
ss> io.driver.ble.getServiceList
ss> io.driver.ble.selectService {"serviceIndex": 0}
ss> io.driver.ble.getCharacteristicList
ss> io.driver.ble.setCharacteristicIndex {"characteristicIndex": 0}
ss> io.manager.connect
python test_api.py send io.manager.setBusType -p busType=4
python test_api.py send io.driver.modbus.setProtocolIndex -p protocolIndex=1
python test_api.py send io.driver.modbus.setHost -p host=192.168.1.50
python test_api.py send io.driver.modbus.setPort -p port=502
python test_api.py send io.driver.modbus.setSlaveAddress -p address=1
python test_api.py send io.driver.modbus.setPollInterval -p intervalMs=1000
python test_api.py send io.driver.modbus.addRegisterGroup -p type=2 startAddress=0 count=10
python test_api.py send io.manager.connect
# Data must be Base64 encoded
echo -n "Hello World" | base64 # SGVsbG8gV29ybGQ=
python test_api.py send io.manager.writeData -p data=SGVsbG8gV29ybGQ=
python test_api.py send csv.export.setEnabled -p enabled=true
python test_api.py send csv.export.getStatus
python test_api.py send csv.export.close
python test_api.py send csv.export.setEnabled -p enabled=false
# Get current dashboard configuration
python test_api.py send dashboard.getStatus
# Set operation mode (0=ProjectFile, 1=DeviceSendsJSON, 2=QuickPlot)
python test_api.py send dashboard.setOperationMode -p mode=2
# Set visualization refresh rate (FPS)
python test_api.py send dashboard.setFPS -p fps=60
# Set data points per plot
python test_api.py send dashboard.setPoints -p points=500
# Query individual settings
python test_api.py send dashboard.getOperationMode
python test_api.py send dashboard.getFPS
python test_api.py send dashboard.getPoints
All messages are JSON objects terminated by a newline (\n).
{
"type": "command",
"id": "unique-request-id",
"command": "io.manager.getStatus",
"params": {"paused": true}
}
{
"type": "response",
"id": "unique-request-id",
"success": true,
"result": {
"isConnected": false,
"paused": false
}
}
{
"type": "response",
"id": "unique-request-id",
"success": false,
"error": {
"code": "INVALID_PARAM",
"message": "Invalid port: 70000. Valid range: 1-65535"
}
}
INVALID_JSON: Malformed JSON messageINVALID_MESSAGE_TYPE: Unknown or missing message typeUNKNOWN_COMMAND: Command not recognizedINVALID_PARAM: Parameter value out of range or invalidMISSING_PARAM: Required parameter not providedEXECUTION_ERROR: Command failed during executionOPERATION_FAILED: Operation could not be completedio.manager.setBusType)| Index | Name | License |
|---|---|---|
| 0 | UART (Serial) | GPL/Pro |
| 1 | Network (TCP/UDP) | GPL/Pro |
| 2 | Bluetooth LE | GPL/Pro |
| 3 | Audio | Pro |
| 4 | Modbus (RTU/TCP) | Pro |
| 5 | CAN Bus | Pro |
| 6 | MQTT | Pro |
dashboard.setOperationMode)| Index | Name | Description |
|---|---|---|
| 0 | ProjectFile | Uses a project file to define dashboard structure and data parsing |
| 1 | DeviceSendsJSON | Device sends pre-formatted JSON data (no parsing needed) |
| 2 | QuickPlot | Automatically detects and plots CSV-formatted data |
Problem: Connection refused or Connection timed out
Solutions:
python test_api.py --host 127.0.0.1 --port 7777 send io.manager.getStatus
Problem: "Invalid JSON parameters" error
Solutions:
Use key=value format (easier on all shells):
python test_api.py send io.driver.uart.setBaudRate -p baudRate=115200
On Windows PowerShell, escape JSON quotes carefully:
python test_api.py send io.driver.uart.setBaudRate --params '{\"baudRate\": 115200}'
Use interactive mode for complex commands
Problem: UNKNOWN_COMMAND error
Solutions:
List all available commands:
python test_api.py list
Check if command requires Pro license (see API_REFERENCE.md)
Verify command spelling and case (commands are case-sensitive)
# Get status and extract specific field
STATUS=$(python test_api.py send io.manager.getStatus --json)
IS_CONNECTED=$(echo $STATUS | jq -r '.result.isConnected')
if [ "$IS_CONNECTED" = "true" ]; then
echo "Already connected"
else
python test_api.py send io.manager.connect --json
fi
#!/bin/bash
# Example: Create a script to configure and connect to your device
python test_api.py send io.manager.setBusType -p busType=0
python test_api.py send io.driver.uart.setBaudRate -p baudRate=115200
python test_api.py send io.driver.uart.setPortIndex -p portIndex=0
python test_api.py send csv.export.setEnabled -p enabled=true
python test_api.py send io.manager.connect
echo "Setup complete! Device connected and logging to CSV."
#!/usr/bin/env python3
# Example: Create your own Python wrapper for the Serial Studio API
import socket
import json
def send_command(command, params=None):
"""Send a command to Serial Studio API and return the response."""
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(("127.0.0.1", 7777))
msg = {
"type": "command",
"id": "custom-1",
"command": command
}
if params:
msg["params"] = params
sock.sendall((json.dumps(msg) + "\n").encode())
response = json.loads(sock.recv(65536).decode())
sock.close()
return response
# Example usage
status = send_command("io.manager.getStatus")
print(f"Connected: {status['result']['isConnected']}")
send_command("io.driver.uart.setBaudRate", {"baudRate": 115200})
# Add your application logic here
Localhost Only: The API Server binds to localhost (127.0.0.1) by default and is not accessible from the network
No Authentication: The API does not require authentication - anyone with local access can control Serial Studio
Firewall: Ensure your firewall blocks external access to port 7777
Production Use: Do not expose the API Server to untrusted networks
test_api.py - Main Python client (CLI, REPL, tests)README.md - This documentation fileAPI_REFERENCE.md - Complete API command referenceThis example is dual-licensed:
See the main LICENSE file in the repository root for details.
Found a bug or have a suggestion? Please report it on the GitHub issue tracker.