Unslave 3.0 - HTTP API Support
We want to make Unslave a perfect developer-friendly Modbus slave simulator. This is why in version 3.0.0 we have added a built-in HTTP API.
Having an API means that the configuration is no longer limited to a static JSON file. With an API You can update the state of all simulated slaves at runtime, or add new ones, without the need to change the config file and restarting Unslave.
Enabling the API
Before trying to use the API, make sure to enable it in config.json
:
"api":{
"enable":true,
"port":9000
}
...
During first API run, you will be asked for a permission to bind to a network socket. You need to answer 'Yes' in order to use the API.
If the configuration is correct and the API is up, you will see console output similar to this:
...
INFO: HTTP API Listening at http://localhost:9000/
Using the API
Unslave implements a RESTful API with JSON payload. Below you can see some examples of API requests.
Set Register Value (Coil)
PUT /slaves/1/registers/C100
content-type:application/json
{
"value": true
}
Set Register Value (Holding Register)
PUT /slaves/1/registers/HR100
content-type:application/json
{
"value": 1234
}
Set Exception Response
PUT /slaves/1/registers/C100
content-type:application/json
{
"exception": 2
}
Set Slave Online/Offline
PUT /slaves/1
content-type:application/json
{
"isOnline": true
}
Try it for Yourself
- Download Unslave
- Configure the API as shown above
- Download Postman extension for Google Chrome (or use your preferred tool to make HTTP calls)
- Run Unslave and try making an API request, as shown in the demonstration: