Community discussions

MikroTik App
 
User avatar
mrangics
just joined
Topic Author
Posts: 2
Joined: Wed Oct 11, 2023 6:30 pm
Contact:

KNOT - Modbus RTU - Write Registers

Mon Jul 15, 2024 10:40 am

Hi,

I'm pretty fine reading any kind of registers with the KNOT device via the transceive command, like
/iot modbus transceive address=1 function=1 data=00000006 (read 6 coil registers from 0 - 5, it will return a decimal number what can be processed to become digitals)
/iot modbus transceive address=1 function=2 data=00000009 (read 9 discrete registers from 0 - 8, it will return a decimal number what can be processed to become digitals)
/iot modbus transceive address=1 function=3 data=000000AA (read 170 holding registers from 0 - 169, it will return 170 decimal numbers in an array)
/iot modbus transceive address=1 function=4 data=000000AA (read 170 input registers from 0 - 169, it will return 170 decimal numbers in an array)

I would like to write the coil registers with function 5 or 15 and write holding registers with function 6 or 16, but based on the description, I'm not able to find how to do that.
Any example code is appreciated.

Thank you.
 
jaclaz
Forum Guru
Forum Guru
Posts: 2171
Joined: Tue Oct 03, 2023 4:21 pm

Re: KNOT - Modbus RTU - Write Registers

Mon Jul 15, 2024 8:29 pm

I think that the issue is that the data does not represent the same things with different functions.

Or if you prefer each function has different arguments.

Check this:
https://www.fernhillsoftware.com/help/d ... tocol.html

with this:
function=3 data=000000AA
you are actually sending
03 (read) 0000=First input address (from) 00AA=Register count (amount)

Function 5 has arguments:
05 (write) First coil address (offset) Coil value (can only be two values 0000 or FF00 or 0/1, off/on) <- here it has to be understood if FF should be 00FF or FF00 in Mikrotik syntax, likely FF00, but needs to be verified

Check also this online parser:
https://rapidscada.net/modbus/
try using this value (last two bytes are checksum, that i presume ROS calculates automatically, to play with the parser, add - say - ABCD, the parser will throw an error but provide the correct checksum that you can use to replace the ABCD placeholder):
10050000FF008F7B
Part of Data Package 	Description 	Value
10 	Slave address 	0x10 (16)
05 	Function code 	0x05 (5) - Write Single Coil
00 00 	Output address 	Physical: 0x0000 (0)
			Logical: 0x0001 (1)
FF 00 	Output value 	On
8F 7B 	CRC 	0x8F7B (36731)
value:
1003000000AAC6F4
Part of Data Package 	Description 	Value
10 	Slave address 	0x10 (16)
03 	Function code 	0x03 (3) - Read Holding Registers
00 00 	Starting address 	Physical: 0x0000 (0)
				Logical: 0x0001 (1)
00 AA 	Quantity 	0x00AA (170)
C6 F4 	CRC 	0xC6F4 (50932)
 
User avatar
DenSyo77
newbie
Posts: 27
Joined: Tue Jan 09, 2024 10:38 am
Contact:

Re: KNOT - Modbus RTU - Write Registers

Sat Dec 14, 2024 6:41 am

write single holding register as integer:
/iot modbus transceive address=1 function=6 values=a2,a1,v2,v1
a2 - high byte of register address
a1 - low byte of register address
v2 - high byte of value
v1 - low byte of value

as hexadecimal:
/iot modbus transceive address=1 function=6 data=A2A1V2V1

set value 10 to register at address 1
/iot modbus transceive address=1 function=6 values=0,1,0,10
/iot modbus transceive address=1 function=6 data=0001000A

write multiple holding registers as integer:
/iot modbus transceive address=1 function=16 values=a2,a1,c2,c1,b,v12,v11,v22,v21...
a2 - high byte of first register address
a1 - low byte of first register address
c2 - high byte of registers count
c1 - low byte of registers count
b - count of bytes next (count of bytes in message)
v12,v11 - high and low bytes of first value
v22,v21 - high and low bytes of second value

as hexadecimal:
/iot modbus transceive address=1 function=16 data=A2A1C2C1BBV2V1V2V1

set value 10 to register at address 1 and value 20 to register at address 2
/iot modbus transceive address=1 function=16 values=0,1,0,2,4,0,10,0,20
/iot modbus transceive address=1 function=16 data=0001000204000A0014

REST API
POST http://router/rest/iot/modbus/transceive
{"address":1,"function":16,"values":"0,1,0,2,4,0,10,0,20"}

And let remind once again, this applies not only to KNOT, you can connect USB/RS485 adapter to any MikroTik with USB and install IoT package.