A persistent RESTful API wrapper for CODESYS automation software, allowing for seamless integration with other systems and automation of CODESYS operations.
- Persistent CODESYS Session: Maintains a single running instance of CODESYS for improved performance
- RESTful API: Provides standard HTTP endpoints for all CODESYS operations
- Session Management: Start, stop, and monitor CODESYS sessions
- Project Operations: Create, open, save, close, and compile projects
- POU Management: Create and modify Program Organization Units
- Script Execution: Execute arbitrary CODESYS scripts
- Authentication: Secure access with API keys
- Windows Service: Run as a background service with auto-recovery
- Comprehensive Logging: Detailed activity and error logging
- Windows OS with CODESYS 3.5 or later installed
- Python 3.x installed
- Note: Only the PERSISTENT_SESSION.py script maintains compatibility with CODESYS IronPython environment
- Administrator privileges (for service installation)
-
Clone this repository:
git clone https://github.com/johannesPettersson80/codesys-api.git
-
Navigate to the project directory:
cd codesys-api
-
Install required packages:
pip install requests pywin32
-
Run the installation script:
install.bat
If you prefer not to install as a Windows service, use:
start_server.bat
-
Verify the installation:
python example_client.py
For detailed installation instructions, see the Installation Guide and CODESYS Script Compatibility Guide.
All API requests require an API key in the header:
Authorization: ApiKey YOUR_API_KEY
POST /api/v1/session/start
: Start CODESYS sessionPOST /api/v1/session/stop
: Stop CODESYS sessionGET /api/v1/session/status
: Get session statusPOST /api/v1/session/restart
: Restart CODESYS session
POST /api/v1/project/create
: Create new projectPOST /api/v1/project/open
: Open existing projectPOST /api/v1/project/save
: Save current projectPOST /api/v1/project/close
: Close current projectPOST /api/v1/project/compile
: Compile projectGET /api/v1/project/list
: List recent projects
POST /api/v1/pou/create
: Create new POUPOST /api/v1/pou/code
: Set POU codeGET /api/v1/pou/list
: List POUs in project
POST /api/v1/script/execute
: Execute arbitrary script
GET /api/v1/system/info
: Get system informationGET /api/v1/system/logs
: Get system logs
The repository includes an example client (example_client.py
) demonstrating basic operations:
import requests
# API configuration
API_BASE_URL = "http://localhost:8080/api/v1"
API_KEY = "admin" # Default API key
# Call API with authentication
def call_api(method, endpoint, data=None):
headers = {"Authorization": f"ApiKey {API_KEY}"}
url = f"{API_BASE_URL}/{endpoint}"
if method.upper() == "GET":
response = requests.get(url, headers=headers)
elif method.upper() == "POST":
response = requests.post(url, json=data, headers=headers)
return response.json()
# Start a session
result = call_api("POST", "session/start")
print(f"Session started: {result}")
# Create a project
project_data = {"path": "C:/Temp/TestProject.project"}
result = call_api("POST", "project/create", project_data)
print(f"Project created: {result}")
For a complete example workflow, see the example_client.py file.
The CODESYS REST API consists of several key components:
- HTTP REST API Server: Processes incoming requests and routes them to handlers
- CODESYS Session Manager: Maintains and monitors the persistent CODESYS instance
- Script Execution Engine: Generates and executes scripts in the CODESYS environment
- Authentication System: Validates API keys and controls access
For more information about the architecture, see the Project Summary.
Server settings can be configured by editing HTTP_SERVER.py
:
# Constants
SERVER_HOST = '0.0.0.0' # Listen on all interfaces
SERVER_PORT = 8080 # HTTP port
CODESYS_PATH = r"C:\Program Files\CODESYS 3.5\CODESYS\CODESYS.exe"
API keys are stored in api_keys.json
:
{
"admin": {"name": "Admin", "created": 1620000000.0}
}
- Installation Guide: Detailed installation instructions
- Implementation Checklist: Development progress and status
- Python 2.7 Compatibility: Notes on Python 2.7 compatibility
- Project Summary: Overview of implementation details
- API returns "Unauthorized": Check that you're using the correct API key
- Service fails to start: Verify CODESYS path is correct and CODESYS is installed
- Connection refused: Ensure the service is running and the port is not blocked
Check the following log files for error messages:
codesys_api_server.log
: Main API server logsession.log
: CODESYS session logcodesys_api_service.log
: Windows service log (if running as a service)
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- CODESYS Group for the CODESYS automation software and scripting API
- Python community for excellent libraries and tools
- All contributors to this project