This Express.js application provides a REST API to control Modbus RTU relays through RS485 via USB.
- Connect your USB-RS485 converter to your computer
- Install dependencies:
npm install
- Update the serial port in
app.jsif needed (default is/dev/ttyUSB0)
npm startThe server will start on port 3000.
- GET
/relay/:number - Returns the state of a specific relay
- Example:
GET http://localhost:3000/relay/1
- POST
/relay/:number - Sets the state of a specific relay
- Body:
{ "state": true }or{ "state": false } - Example:
POST http://localhost:3000/relay/1
- POST
/relays - Sets multiple relay states at once
- Body:
{ "states": [true, false, true] } - Example:
POST http://localhost:3000/relays
Default Modbus RTU settings:
- Baud Rate: 9600
- Data Bits: 8
- Stop Bits: 1
- Parity: None
- Device ID: 1
Adjust these settings in app.js if your device requires different parameters.