B4R Code Snippet ModBus RTU

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

  • Modbus.bas
    6.2 KB · Views: 356
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
 

peacemaker

Expert
Licensed User
What is the exact pinout of MAX485 board in this circuit diagram ? I mean how exactly controlled the direction ?
 

OGmac

Member
Licensed User
What is the exact pinout of MAX485 board in this circuit diagram ? I mean how exactly controlled the direction ?

B4X:
    Private RS485    As Pin
    Private Send     As Boolean = True
    Private Recv     As Boolean = False  
.
.
.
'Tx  Transmition Pin RS485
'Rx  Recive      Pin RS485
'Dir Dirction    Pin RS485
'Buad Rate       RS485
Public Sub Initial(Rx As Byte,Tx As Byte,Dir As Byte,BudeRate As ULong)
    softserial.Initialize(BudeRate, Rx, Tx)
    sstream.Initialize(softserial.Stream, "Sstream_newdata", Null)
    RS485.Initialize(Dir,RS485.MODE_OUTPUT)
    RS485.DigitalWrite(Recv)
End Sub
 

peacemaker

Expert
Licensed User
B4X:
    Private RS485    As Pin
    Private Send     As Boolean = True
    Private Recv     As Boolean = False 
.
.
.
'Tx  Transmition Pin RS485
'Rx  Recive      Pin RS485
'Dir Dirction    Pin RS485
'Buad Rate       RS485
Public Sub Initial(Rx As Byte,Tx As Byte,Dir As Byte,BudeRate As ULong)
    softserial.Initialize(BudeRate, Rx, Tx)
    sstream.Initialize(softserial.Stream, "Sstream_newdata", Null)
    RS485.Initialize(Dir,RS485.MODE_OUTPUT)
    RS485.DigitalWrite(Recv)
End Sub
1628791796669.png


What pins are on the MAX485 board ?
 

peacemaker

Expert
Licensed User
My question is about blue board pins - what is yellow connected 2 pins ? What is the blue board ?

Now there are RS485 converter board with just Tx, Rx and GND pins.
 
Top