Communication
This article describes the reading or switching of inputs and outputs or registers and other things
Digital inputs and outputs
Read an input
The function getInput(inputName)/getOutput(outputName) is required to read out the value of an input/output. The function requires 1 parameter as a string. The string must describe an input/output, e.g. "INPUT_1","OUTPUT_4".
The function then returns a 0 or 1, depending on the current value of the input.
getInput("INPUT_1");
or
getOutput("OUTPUT_4");Read an output
Use getOutput(outputName) to read an output. As shown in the preceding example, the name is passed as a string, such as OUTPUT_4, and the return value is 0 or 1.
Set or switch output
To switch an output, the setOutput command must be entered in the CodeArea.
The first parameter specifies which output is to be switched. Possible correct values are "OUTPUT_1" to "OUTPUT_14", as well as "TOOL_OUTPUT_1" and "TOOL_OUTPUT_2" for the UserIO.
The second parameter specifies the value of the corresponding output. Permissible values are 1 or 0
setOutput("OUTPUT_1", 1);Register
Various registers are available in horstFX:
- 64 Int Register
- 64 Float Register
- 128 Bool Register
Set register
The function
| Command | Index | Example value |
|---|---|---|
| setFloatRegister(index, value) | 0-63 | 5.0 |
| setIntRegister(index, value) | 0-63 | 5 |
| setBoolRegister(index, value) | 0-127 | true |
is required to write a value to a position in the register.
Read register
The function
| Command | Index |
|---|---|
| getFloatRegister(index) | 0-63 |
| getIntRegister(index) | 0-63 |
| getBoolRegister(index) | 0-127 |
is required to read a value from the corresponding register.
horstCOSMOS
Process data can be saved using the recordData(key, value, category) method. The key is a character string with which the data can be identified again later. The value to be saved is specified under value. This can be a character string or a number. Optionally, a character string can be specified as the third parameter as a category.
Data saved via recordData() is automatically synchronised daily with horstCOSMOS, our IOT platform. If this synchronisation is to take place immediately, this can be achieved using the sendRecordedData() function.
Example
recordData("data 1", 2.34, "pressure");
sendRecordedData();Modbus
Modbus can be started using the command modbusConnect(ip, port, tcp). The parameter ip specifies the IP address as a string. The parameter port specifies the port as a number. The last parameter tcp specifies as a boolean whether you want a TCP connection (true) or a UDP connection (false).
The UnitID can be specified using the setUnitID() method. There are also the methods closeConnection(), readInputsDiscrete(ref, count), readInputDiscrete(ref), readCoils(ref, count), readCoil(ref), writeCoil(ref, value), readInputRegisters(ref, count), readInputRegister(ref), readRegisters(ref, count), readRegister(ref), writeRegisters(ref, values) und writeRegister(ref, value).
Example:
var m = modbusConnect("127.0.0.1", 8502, true);
showInfo(modbusReadInputsDiscrete(m, 0, 10).toString(16));
showInfo(modbusReadCoils(m, 1000, 3).toString());
modbusWriteCoils(m, 1000, [true,false,true]);
showInfo(modbusReadCoils(m, 1000, 3).toString());
showInfo(modbusReadInputRegisters(m, 600, 1).toString());
modbusWriteRegisters(m, 1000, [2,3]);
showInfo(modbusReadRegisters(m, 1000, 2).toString());