B4R Code Snippet ModBus RTU

OGmac

Member
Licensed User
ModBusB4R.png


Modbus Function Code Support

Function CodeRegister Type
1Read Coil
2Read Discrete Input
3Read Holding Registers
4Read Input Registers
5Write Single Coil
16Write Multiple Holding Registers


B4R:
    ' initial Paramter
    Modbus.Initial(5,6,2,9600)
    ' Set Registers
    Modbus.CO(0)  = True      ' Coil Register     00000
    Modbus.DI(0)  = True      ' Discrete Input    10001
    Modbus.IR(0)  = 25        ' Input Register    30001
    Modbus.HR(0)  = 50        ' Holding Register  40001
    Modbus.HR(7)  = 100       ' Holding Register  40008
You can use Radzio! Modbus Master Simulator

monitor.png
 

Attachments

Last edited:

barx

Well-Known Member
Licensed User
Hi @OGmac
I'm looking to create a little project that requires ModBus. I've looked at your Code module above and have a question.

Can it only 'Set' Registers? I can see subs that are named as though they receive data but:
  1. They are private subs, as though they are for internal use only.
  2. Following the code, if you make one of the subs public and call it (not actually run the code yet) it seems as though it writes to the stream and then when data is received, it would call the sub again in the select case statement.
Maybe I am just reading it wrong and not fully understanding the ModBus protocol.

If the code module is able to read registers, could you give a simple example of how it is done?

Thanks
 

OGmac

Member
Licensed User
Hi @OGmac
I'm looking to create a little project that requires ModBus. I've looked at your Code module above and have a question.

Can it only 'Set' Registers? I can see subs that are named as though they receive data but:
  1. They are private subs, as though they are for internal use only.
  2. Following the code, if you make one of the subs public and call it (not actually run the code yet) it seems as though it writes to the stream and then when data is received, it would call the sub again in the select case statement.
Maybe I am just reading it wrong and not fully understanding the ModBus protocol.

If the code module is able to read registers, could you give a simple example of how it is done?

Thanks
If I understand correctly, your goal is to read the registers of a device. For this purpose, you need a code that is in Master mode, but the above code is related to the slave mode. To better understand modbus, I suggest you read this website.
http://www.simplymodbus.ca/index.html
 
Top