Example

Top  Previous 

 

Examples


 

Example1:

 

Our embedded platform is dsPIC33EP256GP502 . Uart2 connects RS485 Transceiver, and TX_transmit Enable is controlled by RB11.  Logic High will enable RS485 transmit and disable RS485 receiver.   Our embedded Modbus Server ID=1, and supports holding registers address range: 40001 to 40010, baud rate=19200

 

Please see screenshot of model below:

 

example5_emModbusWriteHoldings

 

 

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

 

example2_setup_param

 

In our hardware environment, we use "Modbus RTU/ASCII Dual Masters adaptor" as debugger/monitor.  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.

Double click "emMBSlaveWriteHoldings1", we will see the "emMBSlaveWriteHoldings1" parameters settings below:

 

example5_WriteHoldings1_param

 

It means this block will receive holding register from Modbus server ID=1 Uart No =2 and holding register data type is "16 bits of unsigned integer".  This block's in-port "StartAddr" connects Constant block with value= 1 which denotes holding register starting address is 40001.  This block's in-port "Data" connects "Counter Limited" block output,  which provide 0 to 1034 increasing counter output value (uint16 data type) to in-port "Data". So we will see Modbus server (ID=1) holding register 40001 increase 1 from 0 to 1034 every sampling period.

 

Double click "emMBSlaveWriteHoldings2", we will see the "emMBSlaveWriteHoldings2" parameters settings below:

 

example5_WriteHoldings2_param

 

 

It means this block will receive holding register from Modbus server ID=1 Uart No=2 and Input register data type is "32 bits of signed integer" (big-endian).  This block's in-port "StartAddr" connects Constant block with value= 2 which denotes holding register starting address is 40002.  This block's in-port "Data" connects "Constant" block output,  which provide vector [2.7 -23.8 ] to in-port "Data". So we will see Modbus server (ID=1) holding register 40002 value= 0 and holding register 40003=27 (big-endian, and factor =10, so 2.7x10 =27) . We also see holding register 40004 value= -1 and holding register 40005=-238 (big-endian, and factor =10, so -23.8 x10 = -238.  The first word is -1 not 0 due to 2's complement reason)

 

"Probe" block is just for watching "emMBSlaveWriteHoldings" block output.  For any block output except "Constant block", if its name does not contain "Right Arrow" (à),  you must use "Probe" block to watch its value.

 

Before you build this simulink model,  you must create the firmware project by your IDE.  and remember the firmware project directory name. In our example, it is microchip MPLAX IDE software, you must use MCC to configure timer1 as 1ms timer and interrupt enabled.   You must use MCC to enable Uart2 with interrupt enabled and both "software Transmit Buffer Size" and  "software Receive Buffer Size" =255 or 254.   Timer1 interrupt priority is below UART (you can choose equal too).  We put our MCC configuration file BasePrj.mc3 into example folder for your reference.  You must modify according to your hardware.  You'd better compile your firmware which is created by MCC to identify any error by MCC.

 

After your "Modbus RTU/ASCII Dual Masters adaptor" connects to Target (dspic33) 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_build

 

 

 

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, you can use ModScan32 software to view result below:

 

example5_ModSAN_result1

 

 

However, we see all holding regsiter values are 0. we can not see address 40001 value changing from 0 to 1034 every 0.5 sec.  All results seem not what we expect.

The reason is Both input port Data for "emMBSlaveWriteHoldings1" and "emMBSlaveWriteHoldings2"  are from PC Side.  If we didn't run simulink, embedded target didn't receive holding register writing command, so all holding register value are zero.

 

Disconnect ModScan32 software, we can use Simulink directly view results.  By select stop time= inf, and click "Run" button,  you will see result below:

 

example5_simulink_result

 

 

You will see all results in all "Display" Blocks.  You may see Output Data Display=54, but Counter Data Display =56.   The reason is synchronization. Counter Data is from PC Simulink block data directly, but Output Data is from probe which is from communication between PC and embedded target, it has some delay.

 

Notes: You can run both ".ModScan32 " and "Simulink" at the same time. But you must enable "Modbus RTU/ASCII Dual Masters adaptor" Bluetooth, .and "ModScan32" cannot use the same COM port as  Simulink.  We recommend to use Simulink instead of ".ModScan32 " because " ModScan32 "  cannot watch general variables.  Simulink can watch any variables by "Probe"  (also called emProbe) blocks.

 

Now, we can stop simulink, and run ModScan32 software, you can use ModScan32 software to view result below:

 

example5_ModSAN_result2

 

 

 

We can see holding register 40001 value =55 (fixed, no increasing because PC Simulink stop running),   and 40002 is 0, 40003 is 27, 40004 is -1 , 40005 is -238.

 

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