Example

Top  Previous 

 

Examples


 

Example1:

 

Our embedded platform is dsPIC33EP256GP502 . Uart1 connects RS485 Transceiver, and TX_transmit Enable is controlled by RB2.  Logic High will enable RS485 transmit and disable RS485 receiver.   Every 0.5 sec (Base sample time), we will do the following operations:

1.Do single writing holding register at address 40001 to value 21 to server 1 (Server ID=1) and Server 2 (Server ID=2).  
2.Do multiple writing holding registers at address 40002 to 40005 value 10.8 and -31.77 to server 1 (Server ID=3) and Server 2 (Server ID=4).  Word order is big-endian.
3.Do reading input registers at address 30001 to 30004 to server which has server ID=1. Data type is 32 bits of unsigned integer, and big-endian.
4.Do reading discrete registers at address 10001 to 10007 to server which has server ID=2.

 

Please see screenshot of model below:

 

example7_emModbusMaster1

 

In "Setup Target" block, we choose "PIC24/Dspic30/Dspic33"  platform.   Double click "emModbusMasterSetup", we will see the Modbus Master parameters settings below:

 

example7_ModbusMasterSetup_param1

 

If you click on tab "Read Command Seqs", you will see the settings for "Read Command Seqs" below:

 

example7_ModbusMasterSetup_param2

 

 

Double click "embedded Modbus Master write", we will see the parameters settings below:

 

emModbusMasterWrite_param

 

 

 

It means we will writing to single holding register and 2 Modbus servers with ID =1 and 2 in command sequence No 1.

 

Double click "embedded Modbus Master write 1", we will see the parameters settings below::

 

example7_ModbusMasterWrite1_param

 

 

It means we will writing to multiple holding register in command Seq No 2,  and 2 Modbus servers with ID =3 and 4. If input port data type is uint32/int32/Single, word-endian is big-endian.

 

Double click "embedded Modbus Master Read", we will see the parameters settings below:

 

emModbusMasterRead_param

 

It means we will read input registers  in command seq No 1, and Modbus server with ID =1. Output port data type is 32 bits of unsigned integer , word-endian is big-endian. Data length is 2 x 32 bits.

 

Double click "embedded Modbus Master Read 1", we will see the parameters settings below:

 

example7_ModbusMasterRead1_param

 

It means we will read discrete registers  in command seq No 2, and Modbus server with ID =2. Output port data type is Boolean , Data vector length is 7.

 

Right click on any empty space of simulink model, pop up context menu, click on menu item  "Build Embedded Target and open its IDE"

 

 

embedded_build2

 

 

It will start build, and popup dialog window to input base sampling period:

 

embedded_sample

 

 

 

Input your base sample time and click OK.  If any error occurs, it will stop building, and display error information.  The error block will display in Yellow color.  If build successfully, it will popup window to ask you firmware directory for your IDE project.

 

embedded_projectDir

 

 

If you give out the correct IDE project directory,  Simulink will open IDE software automatically.  And you can compile firmware in your IDE, and program into Target by emulator IDE supported.

 

If your firmware program into target successfully, Connect your target Uart1 (RS485/RS422) to your PC,  you can use PC Modbus slave  software: mbslave.exe to view result below:

 

example7_Modbuspoll_result1

 

 

In above figures,  we set up 6 windows from Mbslave1 to Mbslave6.

 

From Mbslave1 and Mbslave2, we saw slave ID=1 and 2, the holding register value at address 0 (0-based without prefix) or address 40001 (1-based with "4x" prefix) is 21 which matches our model (block "embedded Modbus Master write" ).

 

From Mbslave3 and Mbslave4, we saw slave ID=3 and 4, the holding register value at address 0 and  1 (0-based without prefix) or address 40001 and  40002 (1-based with "4x" prefix) is 10.8, the holding register value at address 2 and 3 (0-based without prefix) or address 40003 and  40004 (1-based with "4x" prefix) is -31.77,  which match our model (block "embedded Modbus Master write 1" ).

 

From Mbslave5 and Mbslave6, we cannot verify the result for Modbus Master reading because we did not put "probe" in simulink block to view reading results.  In example 3, we will put "probe".

 

Please open "Your embedded creator library folder"/examples/example7_emMBMaster1.slx

 

Example2:

 

All conditions are the same as example1.  But we delete "AND" block, directly use "AllWriteReadDone" block to replace. Please see screenshot of model below:

 

example7_emModbusMaster2

 

 

The test results are the same as example1.  Similarly, we cannot verify Modbus Master reading result because of no "Probe".   In example 3, we will put "probe".

 

Please open "Your embedded creator library folder"/examples/example7_emMBMaster2.slx

 

Example3:

 

All conditions are the same as example2  But we add "embedded Modbus slave/Debug Setup" block and lots of "probe" blocks . Please see screenshot of model below:

 

example7_emModbusMaster3

 

 

In our hardware environment, we should add "Modbus RTU/ASCII Dual Masters adaptor" as debugger/monitor.  Double click on block "embedded Modbus Slave/Debug Setup", we will see the parameters settings below::

 

example7_ModbusMasterDebugSetup_param

 

In above figure settings, the red rectangular parameters are important, the others are "Don't care". Please set up them according to your hardware.

 

Please plug into  "Modbus RTU/ASCII Dual Masters adaptor" to your PC USB Port.  And if you are the first time to use this adaptor, run  software "ConfigTool.exe" to config debug/monitor tool:

 

DebugTool_Setting

 

 

In above configuration, The first  Uart setting must match Target including baud rate and physical interface (RS485/RS422/RS232). The second part setting can be different, but you must make sure  parameter "PC Side USB/Bluetooth Serial Port Baud Rate" in "emModbusServerDebugSetup" block matches it.

After your "Modbus RTU/ASCII Dual Masters adaptor" connects to Target (dspic33, Uart2) and PC USB, right click on any empty space of simulink model, pop up context menu, click on menu item  "Build Embedded Target and open its IDE"

 

embedded_build2

 

 

It will start build, and popup dialog window to input base sampling period:

 

embedded_sample

 

 

 

Input your base sample time and click OK.  If any error occurs, it will stop building, and display error information.  The error block will display in Yellow color.  If build successfully, it will popup window to ask you firmware directory for your IDE project.

 

embedded_projectDir

 

If you give out the correct IDE project directory,  Simulink will open IDE software automatically.  And you can compile firmware in your IDE, and program into Target by emulator IDE supported.

 

If your firmware program into target successfully, Connect your target Uart1 (RS485/RS422) to your PC,  you can use PC Modbus slave  software: mbslave.exe to view result below:

 

example7_Modbuspoll_result3

 

 

 

In above figures,  we set up 6 windows Mbslave1 to Mbslave6.

 

For Mbslave1 to Mbslave4, the results are the same as example1. We can know writings are OK.

In Mbslave5, we set up input registers value = 33 (32 bits of unsigned integer, big-endian) at address 0 and 1 ( 0-based without 3x prefix) or at address 30001 and 30002 (1-based with 3X prefix) , and set up input registers value = 212 (32 bits of unsigned integer, big-endian) at address 2 and 3 ( 0-based without 3x prefix) or at address 30003 and 30004 (1-based with 3X prefix).

In Mbslave6, we set up discrete registers value = [ 1 0 1 0 1 1 1] at address from 0 to 6 (0-based without 1x prefix) or 10001 to 10007 (1-based with 1X prefix).

 

Now we can run simulation. By select stop time= inf, and click "Run" button,  you will see result below:

 

example7_simulink_result

 

From block "Display4ReadInputStatus", value is 1 (true).  So block "embedded Modbus Master Read" (Read input regs) success. And in block "Disply4inputRegs", we saw values are 33 and 212 which match "Mbslave5" settings in mbslave.exe software.

 

From block "Display4ReadDiscreteStatus", value is 1 (true).  So block "embedded Modbus Master Read1" (Read discrete regs) success. And in block "Disply4iDiscretes", we saw values are [ 1 0 1 0 1 1 1]  which match "Mbslave6" settings in mbslave.exe software.

 

Please open "Your embedded creator library folder"/examples/example7_emMBMaster3.slx  (You must change "PC Com Port for Debug or Monitor" in emModbusServerDebugSetup block according to your physical USB port number)