Home

Data Sheet

image

Contents

1. 88 pC Modbus 7 01 RTOS Interface MB OS Init This function is called by pwC Modbus to initialize the RTOS interface for the RTOS you are using You would typically create the Modbus Rx Task and setup the mechanism needed to signal this task when a packet is received or an RTU timeout occurred for the channel Prototype void MB OS Init void Arguments None Returned Value None Notes Warnings None Called By MB Init inmb c Example 89 uC Modbus 7 02 RTOS Interface MB OS Exit This function is called by MB Exit see mb c to gracefully terminate the Modbus task In the case of uC OS II we would simply delete the Modbus Rx Task and the message queue In the case of uC OS IIl we would simply delete the Modbus Rx Task since the message queue is built into the task Prototype void MB OS Exit void Arguments None Returned Value None Notes Warnings None Called By MB Exit inmb c Example 90 pC Modbus 7 03 RTOS Interface MB_OS_RxSignal This function signals the reception of a complete packet It is called by either the RTU timer interrupt for each channel that has not received characters within the timeout period or by Modbus ASCII channels when a line feed character i e 0x0A is received Prototype void MB OS RxSignal MODBUS CH pch Arguments pch specifies a pointer to the Modbus channel data structure associated with the received
2. Example 48 pC Modbus 3 17 Using uC Modbus MB_ModeSet This function allows you to change the Modbus mode of a channel You would typically not need to use this function because the channel s mode would have been set inMB CfgCh Prototype void MB ModeSet MODBUS CH pch CPU INTO8U mode Arguments pch is a pointer to the channel returned by MB CfgCh This pointer specifies onto which channel the Modbus master will be communicating on mode specifies whether you want the channel to support ASII or RTU mode and thus you must pass either MODBUS MODE ASCII or MODBUS MODE RTU respectively Returned Value None Notes Warnings None Called By Your Modbus master or slave application Example 49 pC Modbus 3 18 Using uC Modbus S MB_NodeAddrSet This function allows you to change the node address that the channel will respond to You would typically not need to use this function because the channel s address would have been set in MB CfgCh void MB NodeAddrSet MODBUS CH pch CPU INTO8U addr Arguments pch is a pointer to the channel returned by MB CfgCh This pointer specifies onto which channel the Modbus master will be communicating on This channel must have been configured as a Modbus slave addr is the node number and can be anything from 1 to 255 Returned Value None Notes Warnings None Called By Your Modbus slave application Example 50 pC Modbus
3. default perr MODBUS ERR RANGE break As shown in the example above computations are performed when a value is changed 75 pC Modbus 5 10 uC Modbus S MB FileRd MB FileRd is called when a Modbus master sends a Function Code 20 command MB FileRd reads a single integer value from a file As mentionned in the Modbus specifications a file is an organization of records Each file can contain up to 10 000 records addressed from 0 to 9999 You must map the File Record Ix to the actual application s corresponding data MB FileRd should only be called by uHC Modbus Prototype CPU INTI6U MB FileRd CPU INT16U file nbr CPU INT16U record nbr CPU INT16U ix CPU INTO8U record len CPU INT16U perr Arguments file nbr Is the number of the desired file record nbr Is the desired record within the file a number between 0 and 9999 ix Is the desired entry in the specified record record len Is the total length of the record perr Is a pointer to a variable that will contain an error code based on the outcome of the call Your code thus needs to return one of the following error codes MODBUS_ERR_NONE the specified file record entry is valid and your code is returning its current value MODBUS ERR FILE if the specified file nbr is not a valid file number in your product MODBUS_ERR_RECORD if the specified record_nbr is not a valid record number in the specified file MODBUS ERR IX if the s
4. specifies the type of parity checking used when you use Modbus ASCII mode if you want to use parity checking Allowable values for this argument are MODBUS PARITY NONE MODBUS PARITY ODD and MODBUS PARITY EVEN specifies the number of stop bits used by the UART You can either specify 1 or 2 The typical value is 1 but check with the Modbus master node to see if you need to specify 2 this argument specifies whether a Modbus master is allowed to send write commands to this Modbus channel This argument can either be MODBUS WR EN or MODBUS WR DIS In other words if you don t want a Modbus master to change values in your product simply specify MODBUS WR DIS Note that your application code can actually change this setting at run time by calling MB WrEnSet see section 3 06 The function returns a pointer to the created channel which you can use when calling other functions Notes Warnings None Called By Your Modbus master or slave application Example 21 pC Modbus 3 02 Using uC Modbus MB_ChToPortMap This function allows you to change the logical mapping to physical mapping for each channel In other words this function allows you to change the port assignment associated with each wC Modbus channels Prototype void MB ChToPortMap MODBUS CH pch CPU INTO8U port nbr Arguments pch is a pointer to the channel returned by MB CfgCh to map port nbr sp
5. existing files than create new ones from scratch so that you don t forget anything An example of these files is provided with HC Modbus cpu h This file defines CPU compiler specific data types The code below shows an example of the data types needed by pC Modbus for an ARM CPU and the IAR Embedded Workbench compiler typedef void CPU_VOID typedef unsigned char CPU_CHAR typedef unsigned char CPU_BOOLEAN typedef unsigned char CPU INTO8U typedef signed char CPU INTOS8S typedef unsigned short CPU INTI16U typedef signed short CPU_INT16S typedef unsigned int CPU_INT32U typedef signed int CPU_INT32S typedef float CPU_FP32 typedef double CPU FP64 typedef void CPU FNCT PTR void uC Modbus You also need to specify the type of alignment to use as well as the endianness of the processor define CPU CFG ALIGN TYPE CPU ALIGN TYPE 32 define CPU CFG ENDIAN TYPE CPU ENDIAN TYPE LITTLE You also need to define code to disable and enable interrupts In fact the code to disable interrupts should save the state of the interrupt enable setting and then disable interrupts This is done by an assembly language function called CPU SR Save The code to re enable interrupts should simply restore the state saved by CPU SR Save This would be done by a function called CPU_SR_Restore The state of the interrupt enable setting is stored in a local variable of type CPU_SR as
6. 3 19 Using uC Modbus S MB_WrEnSet This function allows you to enable or disable writes to parameters in your product In other words this allows channels to act as read only channels You would typically not need to use this function because the channel read write privilege would have been set inMB CfgCh void MB WrEnSet MODBUS CH pch CPU INTO8U wr_en Arguments pch is a pointer to the channel returned by MB CfgCh This pointer specifies onto which channel the Modbus master will be communicating on This channel must have been configured as a Modbus slave wr en wr en determines whether writes are enabled or not You must pass either MODBUS WR EN or MODBUS WR DIS Returned Value None Notes Warnings None Called By Your Modbus master or slave application Example 51 pC Modbus 4 00 Configuring pC Modbus Configuration of uC Modbus is done at compile time via about 20 define constants Configuration values are found in mb cfg h which should be placed in your product s directory or you can copy the define constants in a header file of your choice It s recommended that you copy the mb cfg h file that is provided with the pC Modbus distribution and modify its content instead of creating mb cfg h from scratch This way you have a better chance of not forgetting any define constants Default values are shown in RED 4 01 Configuring pC Modbus MODBUS CFG SLAVE EN This define constant specifies whe
7. Prototype CPU FP32 MB HoldingRegRdFP CPU_INT16U reg CPU INT16U perr Arguments reg Is the desired holding register to read and can be a number between MODBUS CFG FP START IX and 65535 depending on your product It is up to you to decide what application variable is assigned to each holding register number Note that if your product doesn t have any floating point registers but a large number of holding registers you can set MODBUS CFG FP START IX to 65535 perr Is a pointer to a variable that will contain an error code based on the outcome of the call Your code thus needs to return one of the following error codes MODBUS ERR NONE if the holding register number you specified is a valid holding register and you are able to have code access the value of this holding register MODBUS ERR RANGE if the holding register number passed as an argument is not a valid holding register number for your product Returned Value MB HoldingRegRdFP returns the current value of the specified floating point holding register as a 32 bit IEEE 754 unsigned value If an invalid holding register number is specified you should return CPU_FP32 0 70 uC Modbus Notes Warnings Code is enabled when both MODBUS CFG FCO3 EN is set to DEF ENABLED and MODBUS CFG FP ENis set to DEF ENABLED in your products mb cfg h file Holding registers and input registers are completely different and can be assigned to different variables Called By M
8. function code MODBUS ERR BYTE COUNT If the expected number of bytes to receive doesn t correspond to the number of bytes received Notes Warnings None Called By Your Modbus master application Example 27 uC Modbus 3 06 Using uC Modbus M MBM_FC03_HoldingRegRd This function is called from YOUR application code to read 16 bit holding registers from a Modbus slave Prototype CPU INT16U MBM FC03 HoldingRegRd MODBUS CH pch Arguments pch slave node start _ addr p_reg_tbl nbr regs CPU_INTO8U slave_node CPU INT16U start addr CPU INT16U p reg tbl CPU INT16U nbr regs is a pointer to the channel returned by MB CfgCh This pointer specifies onto which channel the Modbus master will be communicating on Of course och must have been configured as a Master when you configured the channel specifies the slave node address that you desire to read the registers from This can be a number between 1 and 255 but needs to match the number assigned to the slave node specifies the start address of the holding registers This can be from 0 to 65535 Note that the start address must be a number lower than MODBUS CFG FP START IX of the slave if you intend to have _ floating point registers i e you set MODBUS CFG FP EN1O DEF ENABLED inmb cfg hin the slave is a pointer to an array of unsigned 16 bit values that will receive the value of all the registers you are reading The size
9. AppLED respectively CPU INTO8U AppCoilTb1 20 CPU BOOLEAN AppStatus CPU BOOLEAN AppRunning CPU BOOLEAN AppLED void MB CoilWr CPU INT16U coil CPU BOOLEAN coil val CPU INT16U perr CPU_INTO8U ix CPU INTO8U bit nbr perr MODBUS ERR NONE if coil lt 20 sizeof CPU INTO8U ix coil 8 bit nbr coil 8 CPU CRITICAL ENTER if coil val TRUE AppCoilTbl ix 1 lt lt bit nbr else AppCoilTbl ix amp 1 lt lt bit_nbr CPU CRITICAL EXIT else switch coil case 200 AppStatus coil val break 201 AppRunning coil_val break case 202 AppLED coil val break case default perr MODBUS ERR RANGE break 61 pC Modbus 5 03 pC Modbus S MB DIRd MB DIRd is called when a Modbus master sends a Function Code 2 command MB DIRd read the value of a single discrete input MB DIRd should only be called by wC Modbus Prototype CPU_BOOLEAN MB DIRd CPU_INT16U di CPU_INT16U perr Arguments di Is the discrete input number that you want to read and can be a number between 0 and 65535 depending on your product It is up to you to decide which discrete input is assigned to what variable in your product perr Is a pointer to a variable that will contain an error code based on the outcome of the call Your code thus needs to return one of the following error codes MODBUS ERR NONE if the discrete
10. AppPresFuel MB InRegRdFP CPU_INT16U reg CPU INT16U perr FP32 val perr MODBUS ERR NONE switch reg case MODBUS CFG FP SI CPU_CRIT val Ap CPU_CRIT rn case MODBUS CPU_CRIT val Ap CPU_CRIT return case MODBUS val Ap CPU_CRIT return default perr MODBUS ERR RANGE return CAL EN CAL EX val CRG FP SI TICAL EN pTempFuel TICAL EX val CFG FP S1 CAL EN CAL EX val CFG FP SI CPU CRIT CAL EN pTempAir pPresAir TART IX TER 0 TO TART IX TER TO TART IX TER TO TART IX TER 0 pPresFuel CAL EX val TO CPU FP32 0 67 uC Modbus 5 06 HC Modbus S MB HoldingRegRd MB HoldingRegrRd is called when a Modbus master sends a Function Code 3 command MB HoldingRegRd read the value of a single holding register Integer holding registers are numbered from 0 through MODBUS CFG FP START IX 1 MODBUS FP START IX allows you to specify the start of floating point see section 5 07 MD HoldingRegRdFP MB HoldingRegRd should only be called by uHC Modbus Prototype CPU INT16U MB HoldingRegRd CPU INTI6U LEG CPU INT16U perr Arguments reg Is the desired holding register to read and can be a number between 0 and MODBUS CFG FP START IX 1 depending on your product It is up to you to decide what application variable is assigned to each holding register nu
11. RANGE if the holding register number passed as an argument is not a valid holding register number for your product 72 uC Modbus Returned Value None Notes Warnings Code is enabled when either MODBUS CFG FC06 EN is set to DEF ENABLED or MODBUS CFG FC16 ENissetto DEF ENABLED in your product s mb cfg h file Called By MBS FC06 HoldingRegWr and MBS FC16 HoldingRegWr inmbs_core c Example In this example our product has 2 integer variables that we want to assign to holding registers Your systems Engineer decided to assign Modbus holding register numbers 1004 and 1005 to the two integer values You will notice that we disable interrupts to access the variables This is done in case your CPU is an 8 bit CPU and data accesses to 16 bit values are not atomic CPU INT16U AppCtr1 CPU INT16U AppCtr2 void MB HoldingRegWr CPU_INT16U reg CPU INT16U reg val CPU INT16U err perr MODBUS ERR NONE switch reg case 1004 CPU CRITICAL ENTER AppCtrl reg val CPU CRITICAL EXIT Break case 1005 CPU CRITICAL ENTER AppCtr reg val CPU CRITICAL EXIT break default perr MODBUS ERR RANGE break 73 pC Modbus 5 09 pC Modbus S MB_HoldingRegWrFP MB HoldingRegWrFP is called when a Modbus master sends a Function Code 6 and Function Code 16 command MB HoldingRegWrFP writes a_ single floating point holding register value Floating point holding registers are n
12. from address or index 0 to MODBUS CFG FP START IX 1 and floating point input registers and holding registers from MODBUS CFG FP START IX to 65535 53 HC Modbus 4 09 Configuring uC Modbus MODBUS CFG FC01 EN When set to DEF ENABLED this define determines whether uC Modbus will support Coil Read commands Function Code 1 When set to DEF DISABLED code will not be generated for this command 4 10 Configuring uC Modbus MODBUS CFG FC02 EN When set to DEF ENABLED this define determines whether uC Modbus will support Discrete Input Read commands Function Code 2 When set to DEF DISABLED code will not be generated for this command 4 11 Configuring uC Modbus MODBUS CFG FC03 EN When set to DEF ENABLED this define determines whether uC Modbus will support Holding register Read commands Function Code 3 When set to DEF DISABLED code will not be generated for this command 4 12 Configuring pC Modbus MODBUS CFG FC04 EN When set to DEF ENABLED this define determines whether uC Modbus will support Input register Read commands Function Code 4 When set to DEF DISABLED code will not be generated for this command 4 13 Configuring uC Modbus MODBUS CFG FC05 EN When set to DEF ENABLED this define determines whether uC Modbus will support Coil Write commands Function Code 5 When set to DEF DISABLED code will not be generated for this command 4 14 Configuring pC Modbus MODBUS CFG FC06 EN When set to D
13. input number you specified is a valid discrete input and you are able to have code access the value of this discrete input MODBUS ERR RANGE if the discrete input number passed as an argument is not a valid discrete input number for your product Returned Value MB DIRd returns the current value of the specified discrete input TRUE or FALSE If an invalid discrete input number is specified you should return FALSE Notes Warnings Code is enabled when MODBUS CFG FC02 EN is set to DEF ENABLED in your product s mb cfg h file Called By MBS FCO02 DIRd inmbs_core c 62 uC Modbus Example In this example our product has 19 discrete inputs 16 of these discrete are placed in AppDITbl by your application The other three discrete inputs actually represent the status of three switches that your application reads and places the status into the following variables AppSwStart AppSwStop and AppSwReset A pressed switch is indicated by a TRUE and a released switch is represented by a FALSE Your systems Engineer decided to assign Modbus discrete input numbers 100 101 and 102 to the three switches and the other discrete inputs to 103 through 118 CPU BOOLEAN AppDITb1 16 CPU BOOLEAN AppSwStart CPU BOOLEAN AppSwStop CPU BOOLEAN AppSwReset CPU BOOLEAN MB DIRd CPU INT16U ala 5 CPU_INT16U perr perr MODBUS ERR NONE switch di case 100 return AppSwStart case 101 return AppSwStop case 102 re
14. of the array needs to be at least nbr regs Note that you can cast the unsigned values to signed values As far as the Modbus protocol is concerned it sends and receives 16 bit values and the interpretation of what these values mean is application specific specifies the number of registers you want to read from the slave 28 uC Modbus Returned Value MODBUS ERR NONE if the call was successful MODBUS ERR RX if a response was not received from the slave within the timeout specified for this channel see MB CfgCh MODBUS ERR SLAVE ADDR If the transmitted slave address doesn t correspond to the received slave address MODBUS ERR FC If the transmitted function code doesn t correspond to the received function code MODBUS ERR BYTE COUNT If the expected number of bytes to receive doesn t correspond to the number of bytes received Notes Warnings MODBUS CFG FP START IX corresponds to that of the slave Called By Your Modbus master application Example 29 pC Modbus 3 07 Using pC Modbus M MBM_FC03_HoldingRegRdFP This function is called from YOUR application code to read 32 bit floating point registers from a Modbus slave Prototype CPU_INT16U MBM FCO3 HoldingRegRdFP MODBUS CH pch Arguments pch slave_node start _ addr p_reg_tbl nbr regs CPU_INTO8U slave_node CPU_INT16U start_addr CPU EP32 p reg tbl CPU INT16U nbr_ regs is a pointer to the channel returned by MB C
15. packet Returned Value None Notes Warnings None Called By MB RTU TmrUpdate Of MB ASCII RxByte inmb c Example 91 pC Modbus 7 04 RTOS Interface MB OS RxWait This function waits for a response from a slave MB OS RxWait is called from a Modbus master task after it sent a command to a slave and is waiting for a response If the response is not received within the timeout specified when the channel was configured see MB_CfgCh then this function returns to the caller and notifies it of the timeout Prototype void MB OS RxWait MODBUS CH pch CPU_INT16U perr Arguments peh specifies a pointer to the Modbus channel data structure associated with the received packet perr is a pointer to an error code indicating the outcome of the call and can be one of the following errors MODBUS_ERR_NONE the call was successful MODBUS ERR TIMED OUT response was not received within the specified timeout MODBUS ERR NOT MASTER You called this function from a non master channel MODBUS ERR INVALID An invalid error occurred Refer to MB OS C for details Notes Warnings None Called By MBM FC IN MBM CORE C Example 92 pC Modbus 7 05 RTOS Interface Configuration If you use WC OS II you need to configure the following define constants OS Q EN OS SEM EN MB OS CFG RX TASK ID MB OS CFG RX TASK PRIO MB OS CFG RX TASK STK SIZE The
16. record nbr ix CPU_CRITICAL EXIT E return val 77 pC Modbus 5 11 uC Modbus S MB FileWr MB FileWr is called when a Modbus master sends a Function Code 21 command MB FileWr writes a single integer value to a file As mentionned in the Modbus specifications a file is an organization of records Each file can contain up to 10 000 records addressed from 0 to 9999 You must map the File Record Ix to the actual application s corresponding data MB FileWr should only be called by uHC Modbus Prototype void MB FileWr CPU_INT16U file nbr Arguments file nbr record nbr ix record len val perr CPU INT16U record nbr CPU INT16U ix CPU INTO8U record len CPU INT16U val CPU INT16U perr Is the number of the desired file Is the desired record within the file a number between 0 and 9999 Is the desired entry in the specified record Is the total length of the record Is the value to write to the file record Is a pointer to a variable that will contain an error code based on the outcome of the call Your code thus needs to return one of the following error codes MODBUS_ERR_NONE the specified file record entry is valid and your code is returning its current value MODBUS ERR FILE if the specified file nbr is not a valid file number in your product MODBUS_ERR_RECORD if the specified record_nbr is not a valid record number in the specified file MODBUS ERR IX if
17. size needs to be as large as the number of Modbus channels If you use Modbus Master you need to enable semaphore services If you use WC OS III you need to configure the following def ine constants OS CFG Q EN OS CFG SE MB OS CFG RX TASK PRIO MB OS CFG RX TASK STK SIZE The size of the message queue will be set to the number of channels i e MODBUS CFG MAX CH inmb os c If you use Modbus Master you need to enable semaphore services These constants need to be defines in you application 93 pC Modbus 8 00 No OS Interface HC Modbus S can be configured to work in a single threaded environment no RTOS needed The No OS port uses the same RTOS interface layer provided by uC Modbus This layer is explained in Section 7 00 Figure 8 1 shows a flow diagram of receive model in a environment that doesn t use an RTOS i if MODBUS CFG RTU EN DEF ENABLED RTU Timer a 4 Modbus Rx Polling Function Called by your application UART Rx MB OS RxSignal pch ISR UART Rx Figure 8 1 uC Modbus Rx Polling Model 1 Queue Structure F8 1 1 HC Modbus uses a queue structure indicating the channel that has received a packet Your application must call MB OS RxTask to poll the status of the queue if the queue contains at least one element then this function will call MB RxTask which simply parses the packet and formulates a response that will be forwarded appropriat
18. slave MODBUS FC08 BUS EXCEPT CTR You want to read the counter of exceptions detected by the slave MODBUS FC08 SLAVE MSG CTR You want to read the number of message received and processed by the slave 40 uC Modbus MODBUS FC08 SLAVE NO RESP CTR You want to read the number of messages that have not been replied to because of bad CRCs invalid commands etc sub fnct corresponds to a sub function argument for the function At this time wC Modbus does not support sub functions Returned Value MODBUS ERR NONE if the call was successful MODBUS ERR RX if a response was not received from the slave within the timeout specified for this channel see MB CfgCh MODBUS ERR SLAVE ADDR If the transmitted slave address doesn t correspond to the received slave address MODBUS ERR DIAG If you specified an invalid diagnostic function code i e not one of the function described in the fnct argument MODBUS_ERR_SUB_FNCT If you specified an invalid sub function Notes Warnings None Called By Your Modbus master application Example 41 pC Modbus 3 13 Using uC Modbus M MBM FC15 CoilWr This function is called from YOUR application code to write to multiple coils on a Modbus slave Prototype CPU INT16U MBM FC15 CoilWr MODBUS CH pch Arguments pch slave node slave addr p 2011 tbl nbr coils CPU_INTO8U slave_node CPU_INT16U slave_addr CPU_INTO8U p coil tbl CPU_I
19. slave addr CPU BOOLEAN coil val is a pointer to the channel returned by MB CfgCh This pointer specifies onto which channel the Modbus master will be communicating on Of course och must have been configured as a Master when you configured the channel specifies the slave node address that you desire to change the coil value This can be a number between 1 and 255 but needs to match the number assigned to the slave node specifies the address of the coil that you want to change This can be from 0 to 65535 is the desired value of the coil and can be either MODBUS COIL OFF Or MODBUS COIL ON 34 uC Modbus Returned Value MODBUS ERR NONE if the call was successful MODBUS ERR RX if a response was not received from the slave within the timeout specified for this channel see MB CfgCh MODBUS ERR SLAVE ADDR If the transmitted slave address doesn t correspond to the received slave address MODBUS ERR FC If the transmitted function code doesn t correspond to the received function code MODBUS ERR BYTE COUNT If the expected number of bytes to receive doesn t correspond to the number of bytes received MODBUS ERR COIL ADDR If you specified an invalid coil address Notes Warnings None Called By Your Modbus master application Example 35 pC Modbus 3 10 Using uC Modbus M MBM FC06 HoldingRegWr This function is called from YOUR application code to write to a single 16 bit ho
20. to as frames There are two types of frames that the Modbus protocol operates with an ASCII frame and a Remote Terminal Unit RTU frame The ASCII frame is a frame based on ASCII hexadecimal characters while the RTU frame is strictly a binary implementation ASCII mode is easier to implement and debug but offers roughly half the data transfer speed of RTU mode With uC Modbus you can use either mode since implementation and testing has been done by Micrium HC Modbus can support any number of communication channels The mode of operation on each channel can either be ASCII or RTU and is selectable on a per channel basis Figure 1 1 shows the relationship between a product designed using uC Modbus and other Modbus masters and slaves products The Serial Channels are typically RS 232C or RS 485 asynchronous serial interfaces typically using a UART Universal Asynchronous Receiver Transmitter Modbus Master i e Client Your Product Serial Channels i RS 232C or RS 485 Running pC Modbus Modbus Master Slave Modbus Slave i e Server Figure 1 1 Relationship between Modbus based products uC Modbus Masters also known as Clients initiate all data transfers to one or more Slaves also known as Servers in a system In other words only a Master Client can read or write values from to a Slave Server HC Modbus can be made to look like having multiple master or slave ports In fact HC M
21. uS 2 76 800 456 uS 2 115 200 304 US 2 256 000 137 uS 2 For example if a channel is configured for 19 200 Baud then an end of packet in RTU mode is assumed to occur when no bytes are received after 1800 uS microseconds If your RTU timer is setup to interrupt every millisecond then you would need roughly two such interrupts before you conclude that a packet was received We decided to assume that a packet is received after at least the time it would take to receive 5 0 bytes instead of 3 5 bytes Also because of the asynchronous feature of the 87 uC Modbus timer with respect to received bytes we decided to count at least TWO RTU interrupts to conclude that a packet was received You can have better granularity for the timeout if you increase the RTU timer interrupt rate However this also increases the amount of overhead you are placing on your CPU F7 1 5 When the RTU timer interrupt occurs the timeout counter for each of the channels that are configured for RTU mode are decremented When a counter reaches 0 a signal is set to the Modbus Rx Task for that channel This tells the Modbus Rx Task that a packet was received on that channel and needs to be processed The signal is also performed by calling MB OS RxSignal In order to provide the RTOS functionality described above you need to define three functions in a file called MB OS C MB OS Init MB OS Exit MB OS RxSignal MB OS RxWait
22. used for the serial port The choices are MODBUS PARITY NONE MODBUS PARITY ODD and MODBUS PARITY EVEN The most common is MODBUS PARITY NONE specifies the number of stop bits used The choices are typically 1 or 2 1 stop bit is the most common 81 pC Modbus 6 03 BSP MB CommRxTxISR x Handler Most UARTs allow you to generate an interrupt when either a byte is received or when a byte has been sent If your UART generates an interrupt when either a byte is received or when one has been sent then you would need to write a function that determines whether the interrupt was caused by a received by or by a byte sent In this case you would write a function called MBS_CommRxTxISR_x Handler where the x indicates the physical UART example 1 2 3 The pseudo code for this function is shown below The code in RED is code that you have to write You should COPY all the other code as is void MB CommRxTxISR_x Handler void CPU INTO8U EG CPU INTO8U ch MODBUS CH pch pch for amp MB ChTbl 0 ch 0 ch lt MODBUS MAX CH ch if pch gt PortNbr port nbr if Rx Interrupt c Read byte from UART Clear Rx Interrupt pch gt RxCtr MB RxByte pch c Pass byte to Modbus to process if Tx Interrupt pch gt TxCtr MB TxByte pch Send next byte in respons Clear Tx Interrupt break else pcht Clear spurious interrupts 82 pC Modbus 6 04 BS
23. 5 5 06 5 07 5 08 5 09 5 10 5 11 6 00 6 01 6 02 6 03 6 04 6 05 6 06 6 07 6 08 uC Modbus Configuring HC Modbus MODBUS CFG_RTU_EN 0 53 Configuring wC Modbus MODBUS CFG _MAX_CH 53 Configuring HC Modbus MODBUS CFG BUF SIZE 0 53 Configuring HC Modbus MODBUS_CFG_FP_EN c0 53 Configuring wC Modbus MODBUS_CFG_FP_START_IX 53 Configuring wC Modbus MODBUS_CFG_FCO0O1_EN 0 54 Configuring wC Modbus MODBUS_CFG_FCO02_EN 54 Configuring HC Modbus MODBUS CFG FCO3 EN 54 Configuring HC Modbus MODBUS_CFG_FC04_EN 00 54 Configuring wC Modbus MODBUS CFG FC05 EN 54 Configuring HC Modbus MODBUS_CFG_FCO0O6_EN 54 Configuring HC Modbus MODBUS CFG FC08 EN 55 Configuring wC Modbus MODBUS CFG FC15 EN 0000 55 Configuring HC Modbus MODBUS_CFG_FC16_EN 0 55 Configuring HC Modbus S MODBUS_CFG_FC20_EN 55 Configuring wC Modbus S MODBUS CFG _FC21_EN 55 Configuring phC Modbus RAM Memory Requirements 56 HC Modbus S Accessing application data srsrrnnnnnrrrrnnnnnnnnnrn 57 HC Modbus S MB CoilRd rrrrrrrrnnnnnnnnrrvrvnnrrrrnnrnnnnnnvvvnnersennnnn 58 HC Modbus S MB CoilWr rrrrrrrrrnnnnnrrrrnnnnnnnnrrnnnnnrrrrnnnnnennnnnn 60 HC Modbus S MB DIR rrrrrrnnnrr
24. 535 is an array of IEEE 754 floating point values corresponding to the desired values of the holding registers in the slave specifies the number of registers you want to change Of course the array pointed to by p_ reg tbl must contain the corresponding number of values 46 uC Modbus Returned Value MODBUS ERR NONE if the call was successful MODBUS ERR RX if a response was not received from the slave within the timeout specified for this channel see MB CfgCh MODBUS ERR SLAVE ADDR If the transmitted slave address doesn t correspond to the received slave address MODBUS ERR FC If the transmitted function code doesn t correspond to the received function code MODBUS ERR BYTE COUNT If the expected number of bytes to receive doesn t correspond to the number of bytes received Notes Warnings None Called By Your Modbus master application Example 47 uC Modbus 3 16 Using uC Modbus MB Init As mentioned in the previous section MB Init needs to be called to initialize HC Modbus When called mp 1nit creates one task that handles processing of all frames sent to your product See section 7 for details Prototype void MB Init CPU INT32U freq Arguments freq corresponds to the RTU timer interrupt frequency you intend to use If you don t use Modbus RTU in your product simply pass 0 Returned Value None Notes Warnings None Called By Your Modbus master or slave application
25. 800 uS microseconds If your RTU timer is setup to interrupt every millisecond then you would need roughly two such interrupts before you conclude that a packet was received We decided to assume that a packet is received after at least the time it would take to receive 5 0 bytes instead of 3 5 bytes Also because of the asynchronous feature of the timer with respect to received bytes we decided to count at least TWO RTU interrupts to conclude that a packet was received You can have better granularity for the timeout if you increase the RTU timer interrupt rate However this also increases the amount of overhead you are placing on your CPU 95 uC Modbus F8 1 5 When the RTU timer interrupt occurs the timeout counter for each of the channels that are configured for RTU mode are decremented When a counter reaches 0 a signal is set to the Modbus Rx Task for that channel This tells the Modbus Rx Task that a packet was received on that channel and needs to be processed The signal is also performed by calling MB OS RxSignal 96 pC Modbus 9 00 HC Modbus Program Flow This section describes the path taken by messages received and replied to by a Modbus channel Each channel contains 4 buffers as shown in figure 9 1 along with variables used to manage these buffers UART RxCtr RxBufPtr RxFrameNDataByte RxFrameDatal RxFrameCRC TxFrameNDataByte TxFrameDatal TxFrameCRC eTXCEE TxBufByteCtr T
26. 9 04 10 00 Licensing References Contacts BSP MB RIU Trmdniluaseanaoskkevomemadlevomn nad hia BSP MB RIV TiredNuaaaaatkasatnmalesoatmmatn din BSP MB RIU TSAR Handle sccisctccstcctincatentixaintixatictinsanets BALONA IGILO RTOS Interface MB_OS_Init eeecceeeeeeeeeeeeeeeeeeeeneeeeeeeeeeeeeeeees RTOS Interface MB_OS_EXit eee cece eeeeeeeee cree ee eeeeeeneeeeeeeeeeeeeeees RTOS Interface MB OS RxSignal errnnnnnnnnnnnnnnrrnrnnnnnnnnrrrnnnnnnn RTOS Interface MB OS RxWait rrrrnnnnnnnnnnnnnnnrrrrnnnnnnnnrrrrnnnnnn RTOS Interface Configuration cccccccceceeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeees No OS Interface 22 2 ccc ccc cecccecceccecececcececeeceeeaeeceecaeeceeceecaueceeeeeaeeeaees HC Modbus Program Flow rrrrnnnnnnnnnnnnnnnrrrnnnnnnnnnnrnnnnnrrnnnnnnnnnnnnn pC Modbus S ASCII Rx and TX msrnnnnnnrrrrnnnnnnnnnrnnnnrrrrrnnnnnerrnnn HC Modbus S RTU Rx and Tx mnnnnrnnnnnvrrrrnnnnnnnnrrnnnnrrrrrnnnnnnrrenn pC Modbus M ASCII Rx and Tx mrrnnnnnnrrvnnnnnnnnnrnnnnnrrrrrnnnnnnnrnnn pC Modbus M RTU Rx and Tx mrsrnnnnnrrrrrnnnnnnnnrrnnnnrrrrrnnnnnnnrnnn Acronyms Abbreviations and Mnemonics rrrrrrrrrrrrrrrrrrrrrrrrerrnr 108 108 108 pC Modbus Revision History Version Date Description V1 00 2004 09 08 Initial Release V1 61 2005 07 29 Converted code to use pC CPU files Removed dependencies on stdlib functions Improved t
27. BS FCO03 HoldingRegRd IiNmbs core c Example In this example our product has 4 floating point variables that we want to assign to holding registers Your systems Engineer decided to assign Modbus holding register numbers MODBUS CFG FP START IX 0 MODBUS CFG FP START IX 1 MODBUS CFG FP START IX 2 and MODBUS CFG FP START IX 3 to the four floating point values You will notice that we disable interrupts to access the variables This is done in case your CPU does not allow atomic access to the 32 bit floating point values CPU FP32 AppTempAir CPU FP32 AppTempFuel CPU FP32 AppPresAir CPU FP32 AppPresFuel CPU FP32 MB HoldingRegRdFP CPU INT16U reg CPU INT16U err CPU_FP32 val perr MODBUS ERR NONE switch reg case MODBUS CFG FP START IX 0 CPU CRITICAL ENTER val AppTempAir CPU_CRITICAL EXIT return val mu case MODBUS CFG FP START IX 1 TICAL ENTER AppTempFuel RITICAL EXIT n val JU case MODBUS CFG FP START IX 2 CPU CRITICAL ENTER val AppPresAir CPU_CRITICAL EXIT return val case MODBUS_CFG_FP_START_IX 3 CPU_CRITICAL ENTER val AppPresFuel CPU CRITICAL EXIT return val default perr MODBUS ERR RANGE return CPU FP32 0 71 pC Modbus 5 08 pC Modbus S MB HoldingRegWr MB HoldingRegWr is called when a Modbus master sends a Function Code 6 and Function Code 16 command MB HoldingRegWr writes a si
28. EF ENABLED this define determines whether uC Modbus will support writing to a single Holding Register commands Function Code 6 When set to DEF DISABLED code will not be generated for this command 54 pC Modbus 4 15 Configuring pC Modbus MODBUS CFG FC08 EN When set to DEF ENABLED this define determines whether uC Modbus will support diagnostic loopback commands Function Code 8 When set to DEF DISABLED code will not be generated for this command 4 16 Configuring pC Modbus MODBUS CFG FC15 EN When set to DEF ENABLED this define determines whether uC Modbus will support the Multiple Coil Write command Function Code 15 When set to DEF DISABLED code will not be generated for this command 4 17 Configuring pC Modbus MODBUS CFG FC16 EN When set to DEF ENABLED this define determines whether uC Modbus will support the Multiple Holding Register Write command Function Code 16 When set to DEF DISABLED code will not be generated for this command 4 18 Configuring uC Modbus S MODBUS CFG FC20 EN When set to DEF ENABLED this define determines whether pC Modbus will support the File Read command Function Code 20 When set to DEF DISABLED code will not be generated for this command 4 19 Configuring pC Modbus S MODBUS CFG FC21 EN When set to DEF ENABLED this define determines whether pC Modbus will support the File Write command Function Code 21 When set to DEF DISABLED code will not be generated fo
29. FALSE Notes Warnings Code is enabled when MODBUS CFG FCO1 EN is set to DEF ENABLED in your product s mb cfg h file Called By MBS FCO1 CoilRd iNmbs core c 58 uC Modbus Example In this example our product has 163 coils 160 coils are placed in a table called AppCoilTbl The other three coils are actually variables that we treat as coils to allow a Modbus master to read the status of those values The first 160 coils are assigned coil numbers 0 to 159 Coil numbers 200 201 and 202 correspond to the following application variables AppStatus AppRunning and AppLED respectively CPU_INTO8U AppCoilTb1 20 CPU BOOLEAN AppStatus CPU BOOLEAN AppRunning CPU BOOLEAN AppLED CPU BOOLEAN MB CoilRd CPU INT16U coil CPU INT16U perr CPU INTO8U ix CPU INTOS8U bit nbr perr MODBUS ERR NONE if coil lt 20 sizeof CPU INTO08U ix coil 8 bit nbr coil 8 if AppCoilTbl ix amp 1 lt lt bit nbr return TRUE else return FALSE return val else switch coil case 200 return AppStatus case 201 return AppRunning case 202 return AppLED default perr MODBUS ERR RANGE return 0 59 pC Modbus 5 02 pC Modbus S MB CoilWr MB CoilWr is called when a Modbus master sends a Function Code 5 and Function Code 15 command MB CoilWwr changes the value of a single coil MB CoilWr should only be called by pC Modbus Prototyp
30. FC calls MB OS Wait to wait for a response from the slave but with a timeout If the response is not received within the specified timeout see MB CfgCh then we flush the Rx buffer If a response is received we call MBM RxReply to parse the response MB CommRxTxISR Handler mb bsp c Characters received on a UART are processed by the MB CommRxTxISR Handler unless the UART has a separate interrupt for Rx and Tx In this case the function would be called MB CommRxISR_Handler The received character is extracted from the UART and passed to the MB RxByte function for processing MB RxByte mb c MB RxByte determines whether the character received needs to be passed to the ASCII or RTU handler If RTU the character is passed to MB RTU RxByte 104 uC Modbus MB RTU RxByte mb c MB RTU RxByte places received bytes in RxBuf Because in RTU frames are delimited by time MB RTU RxByte resets the RTU timer for the channel indicating that we didn t receive an end of frame yet The received byte is simply placed in the receive buffer RxBuf Signaling of a complete frame is done by timing out on the RTU timer for that channel See MB RTU TmrUpdate inmb c MBM RxReply mbm core c MBM RxReply determines whether the channel is set for ASCII or RTU and calls MB ASCII Rx Or MB RTU Rx to receive the packet MB RTU Rx mb c MB RTU Rx determines if the packet received contains the proper fo
31. L ENTER val CPU INT16U AppTemp CPU CRITICAL EXIT return val case 1001 CPU CRITICAL ENTER val AppCtr CPU CRITICAL EXIT return val case 1002 CPU CRITICAL ENTER val CPU INT16U AppPres CPU CRITICAL EXIT return val case 1003 CPU RITICAL ENTER val AppRxPktCtr CPU_CRITICAL EXIT return val QA default perr MODBUS ERR RANGE return 0 65 pC Modbus 5 05 HC Modbus S MB_InRegRdFP MB InRegRdFP is called when a Modbus master sends a Function Code 4 command MB InRegRdFP read the value of a single input register but it assumes that you are trying to access a floating point variable Floating point input registers are numbered from MODBUS CFG FP START IX to 65535 or less if you don t have a lot of floating point registers MODBUS CFG FP START IX allows you to specify the start of floating point MB InRegRdFP should only be called by uC Modbus Prototype CPU_FP32 MB InRegRdFP CPU INT16U reg CPU INT16U perr Arguments reg Is the desired input register to read and can be a number between MODBUS CFG FP START IX and 65535 depending on your product It is up to you to decide what application variable is assigned to each input register number Note that if your product doesn t have any floating point registers but a large number of input registers you can set MODBUS CFG FP START IX to 65535 perr Is a pointer to a variable tha
32. MB CommRxTxISR Handler unless the UART has a separate interrupt for Rx and Tx In this case the function would be called MB CommRxISR_Handler The received byte is extracted from the UART and passed to the MB RxByte function for processing MB RxByte mb c MB RxByte determines whether the byte received needs to be passed to the ASCII or RTU handler If RTU the byte is passed to MB RTU RxByte MB RTU RxByte mb c MB RTU RxByte places received bytes in RxBuf Because in RTU frames are delimited by time MB RTU RxByte resets the RTU timer for the channel indicating that we didn t receive an end of frame yet The received byte is simply placed in the receive buffer RxBuf Signaling of a complete frame is done by timing out on the RTU timer for that channel See MB RTU TmrUpdate inmb c MB OS RxTask mb os c All Modbus communications is handled by a single Rx Task called MB OS RxTask The task waits for a message from the RTU timer handler that indicates that a complete frame has been received The message is actually a pointer to the Modbus channel where the message was received from MB OS RxTask calls MB RxTask mb c which in turn calls MBS RxTask mbs core c MBS RxTask determines whether the message was an ASCII or RTU message and calls MBS ASCII Task mbs core c Of MBS RTU Task MBS CORE C respectively to do the actual processing of the message received MBS RTU Task mbs core c At t
33. MBM_FCO03_HoldingRegRd 0 28 3 07 Using uC Modbus M MBM_FCO03_HoldingRegRdFP 30 3 08 Using uC Modbus M MBM_FC04_InRegRd cceeeeeeee 32 3 09 Using uC Modbus M MBM FCO05 CoilWr rrrrrrrrrrnnnnnnrrnnnnnnnn 34 3 10 Using uC Modbus M MBM_FC06_HoldingRegWr 2 36 3 11 Using uC Modbus M MBM_FC06_HoldingRegWrFP 38 3 12 Using uC Modbus M MBM_FC08_Diag eeeeeeeeeeeeeeees 40 3 13 Using uC Modbus M MBM FC15 CoilWr rrrrrrrrrrnnnnnnrrrnnnnnnr 42 3 14 Using uC Modbus M MBM FC16 HoldingRegWrN 44 3 15 Using uC Modbus M MBM_FC16_HoldingRegWrNFP 46 3 16 Using uC Modbus MB Init oorrrrrrrrrrrnnnnrrrrrnnnnrrrrrnnnnnnrrrrnnnnnnn 48 3 17 Using uC Modbus MB_ModeSet ccecececeeeeeeeeeeeeeeeeeeees 49 3 18 Using uC Modbus S MB_NodeAddrSet 2 eeeeeeeeeeeeeees 50 3 19 Using uC Modbus S MB_WrEnSet mrrrrrrrrnnnnrrrrrrnnnnnnrrrnnnnnnn 51 4 00 Configuring WC MOADUG ccccceeceeceeeeeeeeeeeeeeeeeeeeeeeeeeeeeees 52 4 01 Configuring HC Modbus MODBUS_CFG_SLAVE_EN 52 4 02 Configuring HC Modbus MODBUS_CFG_MASTER_EN 52 4 03 Configuring HC Modbus MODBUS CFG ASCII EN assesses 52 4 04 4 05 4 06 4 07 4 08 4 09 4 10 4 11 4 12 4 13 4 14 4 15 4 16 4 17 4 18 4 19 4 20 5 00 5 01 5 02 5 03 5 04 5 0
34. Micrium Copyright 2008 2012 Micrium All Rights reserved uC Modbus V2 13 00 uC Modbus S and pC Modbus M User s Manual www Micrium com pC Modbus Table of Contents Revision History 22 ancopcdnnidatidasasanobadentdeassadedanebobb bandas saadatabhooansushbancd 5 1 00 I 1 de 0 01 og 101 cee cele ene evade EE 6 1 01 TOPO GIES recut ceerenreeeceeasearet eureenienyeresbeetceube nui eey eube eutneu beau snsauaiet 8 1 02 HC Modbus Architecture ccccccccceeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeees 10 2 00 Directories and Files ussasssanagraakamkendenndanned e 13 2 01 Directories and Files Target Independent Source Code 13 2 02 Directories and Files RTOS Interface mnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn 14 2 03 Directories and Files Product Specific Files eeceeeeeeeeeees 15 2 04 Directories and Files CPU and Compiler Specific Files 15 3 00 Using HC Modbus cccccccceeeeeceeeeceeeeeeeeeeeeeeeenaeeeeeeeeeeeeeees 17 3 01 Using wC Modbus MB_CfgCn ccccceeeeeceeeeeeeseeeeeeeteeees 20 3 02 Using uC Modbus MB_ChToPortMap eeeeeeeeeeeeeeeeeeeeeees 22 3 03 Using wC Modbus MB Exit cceeeeceeeeeeeeeeeeeeseeeeeeeeeeeeeees 23 3 04 Using uC Modbus M MBM FCO01 CoilRd rrrrrrrrrnnnnnnnrnnnnnnnn 24 3 05 Using uC Modbus M MBM_FC02_DIRd eeeeeeeeeeeeeees 26 3 06 Using uC Modbus M
35. Modbus 3 14 Using uC Modbus M MBM FC16 HoldingRegWrN This function is called from YOUR application code to write to multiple 16 bit holding registers on a Modbus slave Prototype CPU_INT16U MBM FC16 HoldingRegWrN MODBUS CH pch CPU INTO8U slave node CPU INT16U slave addr CPU INT16U p reg tbl CPU INT16U nbr reg Arguments pch is a pointer to the channel returned by MB CfgCh This pointer specifies onto which channel the Modbus master will be communicating on Of course och must have been configured as a Master when you configured the channel slave node specifies the slave node address of the holding registers you want to change This can be a number between 1 and 255 but needs to match the number assigned to the slave node slave addr specifies the start address of the holding registers that you want to change This can be from 0 to 65535 p reg tbl is an array of values corresponding to the desired values of the holding registers in the slave If the holding registers you are changing are signed values simply cast the value to unsigned Modbus reads and writes 16 bit values and doesn t really care about the sign nbr reg specifies the number of registers you want to change Of course the array pointed to by p reg tbl must contain the corresponding number of values 44 uC Modbus Returned Value MODBUS ERR NONE if the call was successful MODBUS ERR RX if a response was not received f
36. NT16U nbr coils is a pointer to the channel returned by MB CfgCh This pointer specifies onto which channel the Modbus master will be communicating on Of course och must have been configured as a Master when you configured the channel specifies the slave node address that you desire to change the coil values This can be a number between 1 and 255 but needs to match the number assigned to the slave node specifies the start address of the coils that you want to change This can be from 0 to 65535 is an array of values corresponding to the desired values for the coils The format is assumed to be as follows p coil tbl 0 8 7 1 p_coil tbl 1 16 15 9 specifies the number of coils you are changing Of course the array pointed to by p coil tbl must contain the corresponding number of entries 42 uC Modbus Returned Value MODBUS ERR NONE if the call was successful MODBUS ERR RX if a response was not received from the slave within the timeout specified for this channel see MB CfgCh MODBUS ERR SLAVE ADDR If the transmitted slave address doesn t correspond to the received slave address MODBUS ERR FC If the transmitted function code doesn t correspond to the received function code MODBUS ERR BYTE COUNT If the expected number of bytes to receive doesn t correspond to the number of bytes received Notes Warnings None Called By Your Modbus master application Example 43 pC
37. P MB_CommRxintEn This function is called by pC Modbus to enable Rx interrupts from a UART void MB CommRxIntEn MODBUS CH pch switch pch gt PortNbr Enable Rx interrupts for specified UART 6 05 BSP MB_CommRxintDis This function is called by pC Modbus to disable Rx interrupts from a UART void MB CommRxIntDis MODBUS CH pch switch pch gt PortNbr Disable Rx interrupts for specified UART 6 06 BSP MB_CommTx1 This function is called by uC Modbus to send a SINGLE byte to the UART associated with the uC Modbus channel void MB CommTx1 MODBUS CH pch CPU INTOSU c switch pch gt PortNbr Write byte c to specified UART pC Modbus 6 07 BSP MB CommTxIntEn This function is called by uC Modbus to enable Tx interrupts from a UART void MB CommTxIntEn MODBUS_ CH pch switch pch gt PortNbr Enable Tx interrupts from specified UART 6 08 BSP MB CommTxIntDis This function is called by uC Modbus to disable Tx interrupts from a UART void MB CommTxIntDis MODBUS CH pch switch pch gt PortNbr Disable Tx interrupts from specified UART HC Modbus 6 09 BSP MB RTU Tmrinit This function is called by MB Init to initialize the RTU timer freq specifies the frequency used for the RTU timer interrupts void MB RTU_TmriInit CPU_INT32U freq 6 10 BSP MB RTU TmrExit This function is called by MB Exit to st
38. Parity NONE ODD or EVEN Ly Number of stop bits 1 or 2 ODBUS WR EN Enable EN or disable DIS writes MB CfgCh 1 Modbus Node for this slave channel ODBUS SLAVE This is a SLAVE 0 0 when a slave ODBUS MODE ASCII Modbus Mode _ASCII or _RTU 1 Specify UART 2 19200 Baud Rate 8 Number of data bits 7 or 8 ODBUS PARITY NONE Parity NONE ODD or EVEN 1 Number of stop bits 1 or 2 ODBUS WR EN Enable EN or disable DIS writes IMPORTANT If your application is using a RTOS interface once a uC Modbus S channel has been configured you do not need to do anything else in your code In other words a Modbus master can start communicating with your Modbus slave without having to add any additional code in your application tasks Refer to section 7 for details on how this works If your application is not using a RTOS interface once a uC Modbus S channel has been configured your application needs to call MB OS RxTask to poll the Modbus Slave channels Refer to section 8 for details on how this works MB CfgCh 1 Modbus Node for this slave channel ODBUS MASTER This is a MASTER OS TICKS PER SEC One second timeout waiting for slave response ODBUS MODE ASCII Modbus Mode ASCII or _RTU 2 Specify UART 3 19200 Baud Rate 8 Number of dat
39. S CFG FC06 EN is set to DEF ENABLED or MODBUS CFG FC16 ENissetto DEF ENABLED in your product s mb cfg h file Called By MBS FC06 HoldingRegWr and MBS FC16 HoldingRegWr inmbs_core c Example In this example our product has 2 floating point integer variables that we want to assign to floating point holding registers Your systems Engineer decided to assign MODBUS floating point holding register numbers MODBUS CFG FP START IX 0 and MODBUS CFG FP START IX 1 to the two floating point variables You will notice that we disable interrupts to access the variables This is done in case your CPU does not perform floating point data accesses atomically CPU_FP32 AppDiameter Modbus Holding Register MODBUS CFG FP START IX 0 Rf CPU_FP32 AppCircumference CPU_FP32 AppTempDegC Modbus Holding Register MODBUS CFG FP START IX 1 A CPU_FP32 AppTempDegF void MB HoldingRegWrFP CPU_INT16U reg CPU FP32 reg val CPU_INT16U perr CPU FP32 temp val perr MODBUS ERR NONE switch reg case MODBUS CFG FP START IX 0 temp val reg val CPU FP32 3 141592654 Compute circumference CPU_CRITICAL ENTER AppDiameter reg val AppCircumference temp val CPU_CRITICAL EXIT Break case MODBUS CFG FP START IX 1 temp val reg val CPU FP32 1 8 CPU FP32 32 0 C gt F Conversion CPU CRITICAL ENTER E AppTempDegC reg val AppTempDegF temp_val CPU CRITICAL EXIT break
40. a bits 7 or 8 ODBUS PARITY NONE Parity NONE ODD or EVEN 1 Number of stop bits 1 or 2 ODBUS WR EN Enable EN or disable DIS writes IMPORTANT Once a pC Modbus M channel has been configured your application code needs to call MBM FC functions as described in this section in order to obtain data from Modbus slaves connected to that channel Refer to section 8 for details on how this works 18 uC Modbus Your application interfaces to wC Modbus via a number of functions that allow you to change the behavior of channels For each interface functions uC Modbus applies to both Master or Slave channels wC Modbus S applies only to Slave channels and HC Modbus M applies only to Master channels pC Modbus 3 01 Using uC Modbus MB CfyCh This function is used to configure each Modbus channel in your product MB CfgCh MUST be called AFTER calling MB Init The function prototype is Prototype MODBUS CH MB CfgCh Arguments node addr master slave rx timeout modbus mode port npr baud bits CPU_INTO8U node _ addr CPU_INTO8U master slave CPU INT32U rx_timeout CPU INTO8U modbus mode CPU INTO8U port nbr CPU INT32U baud CPU INTO8U bits CPU INTO8U parity CPU INTO8U stops CPU INTO8U wr en is the node address of the channel as seen by the Modbus master connected to your product Each channel can be seen as having the same nod
41. accordingly MBS FC _ The response is placed in the TxFrameData buffer in binary format 98 uC Modbus MB ASCII Tx mb c This function is called when we need to send a response back to a Modbus master MB ASCII Tx simply converts the response which was placed in TxFrameData and converts it to ASCII The converted data is placed in the TxBuf The LRC of the outgoing frame is calculated by calling MB ASCII TxCalcLRC Note that the LRC is computed by summing up ALL the ASCII characters to be transmitted except the colon LRC and CR LF and then doing a twos complement In other words the LRC consist only of the node address function code and data sent to the Modbus master MB ASCII Tx thencalls MB Tx to setup transmission MB Tx mb c This function is called to send a message to a Modbus master Here we simply point the TxBufPtr at the beginning of the TxBuf and transmit the first byte by calling MB TxByte mb c in order to kick start transmission interrupts Note that in a lot of cases transmission interrupts occur ONLY after a character has been transmitted MB_TxByte mb c MB TxByte in turn calls MB CommTx1 mb bsp c which sends a byte to the UART and enables Tx interrupts 99 uC Modbus 9 02 HC Modbus S RTU Rx and Tx It might be useful to follow the code for the description provided below MB CommRxTxISR Handler mb bsp c Bytes received on a UART are processed by the
42. ce in mb data c More on this later This is code that YOU need to provide and mb data c typically resides in your product s directory since it can be different for each product This is the application independent slave code and it knows how to process Modbus ASCII and or Modbus RTU packets You should NOT have to modify this code The interface to the UARTs in your product is placed in the Board Support Package BSP file called mb bsp c This is a file that you provide in order to interface to uC Modbus Note that each channel can either communicate via RS 232C or RS 485 at the interface level This is code 10 pC Modbus that YOU need to provide and mb bsp c is either placed in your product s directory or provided by Micrium in the Micrium Software uC Modbus Ports lt CPU gt lt compiler gt directory This is the adaptation layer for the CPU or board you are using Your Application mb cfg h Data Access CPU Interface mb data c SPE uC Modbus mb c and mb h mb def h mb util c mbs core c mbm core c UART Driver RTOS Interface mb os c mo DSPE mb os h UART RS 232C or RS 485 Figure 1 5 Relationship between modules uC Modbus F1 5 5 F1 5 6 HC Modbus S can be used with or witouth a RTOS Real Time Operating System eviroment wC Modbus M assumes the presence of an RTOS However you can use just about any RTOS and the RTOS specifics are actually isolated in a file called mb os c Th
43. ch master needs to have a direct connection to each slave uC Modbus supports this topology since each product can have multiple communication channels Although RS 232C requires more point to point connections it offers the benefit of higher throughput since communications can occur concurrently instead of sequentially Modbus Modbus Master Master RS 232C Interfaces RS 232C Interfaces Your Product Your Product Running pC Modbus S Running pC Modbus S Figure 1 4 Multiple Modbus Masters and Slaves with RS 232C Modbus allows you to read or write integer floating point assuming the Daniels Extensions and discrete values from to your target system WC Modbus can read or write from to up to 65536 16 bit integer values up to 65536 32 bit floating point values up to 65536 coils and up to 65536 discrete inputs Integer and floating point requests may not be mixed in the same command Multiple integer values up to 125 and multiple floating point values up to 62 may be written via a single command Depending on the processor you are using you should be able to run uC Modbus with data rates from 9600 up to 256 000 baud The baud rate you can attain is actually limited to the performance of the CPU and not uC Modbus pC Modbus 1 02 HC Modbus Architecture Figure 1 5 sh also shows w ows how the HC Modbus communications stack fits in your product and hich source files are associated with each layer MB stands fo
44. d character is extracted from the UART and passed to the MB RxByte function for processing MB RxByte mb c MB RxByte determines whether the character received needs to be passed to the ASCII or RTU handler If ASCII the character is passed to MB ASCII RxByte 102 uC Modbus MB ASCII RxByte mb c MB ASCII RxByte places received characters in RxBuf If the received character is a colon character i e we reset the pointer to the beginning of the RxBuf because this signals a new message from a Modbus master We call MB OS RxSignal mb os c if the character received is a line feed i e 0x0A to indicate that the response was received This wakes up the task that sent the command to the slave and thus the MBM _FC _ function is resumed right after the MB OS RxWait call MBM_RxReply mbm core c MBM RxReply determines whether the channel is set for ASCII or RTU and calls MB ASCII Rx Or MB RTU Rx to receive the packet MB ASCII Rx mb c MB ASCII Rx determines if the packet received contains the proper format and checksum If we received a valid packet MB ASCII Rx returns to MBM RxReply which in turns returns to the MBM _FC _ function MBM FC mbm core c MBM FC _ then parses the response and returns the requested information to its caller 103 pC Modbus 9 04 uHC Modbus M RTU Rx and Tx It might be useful to follow the code for the descripti
45. d you are able to have code access the value of this input register MODBUS ERR RANGE if the input register number passed as an argument is not a valid input register number for your product returns the current value of the specified input register as an unsigned value Of course you can also return signed values but those need to be cast to CPU_INT16U You should note that the value will not be changed if you cast a signed variable to CPU_INT16U The Modbus master will receive the proper value and it s up to the Modbus master to properly retrieve the signed data If an invalid input register number is specified you should return 0 64 uC Modbus Notes Warnings Code is enabled when MODBUS CFG FC04 EN is set to DEF ENABLED in your product s mb cfg h file Called By MBS FC04 InRegRd iNmbs core c Example In this example our product has 4 integer variables that we want to assign to input registers Your systems Engineer decided to assign Modbus input register numbers 1000 1001 1002 and 1003 to the four integer values You will notice that we disable interrupts to access the variables This is done in case your CPU is an 8 bit CPU and data accesses to 16 bit values are not atomic CPU INT16S AppTemp CPU INT16U AppCtr CPU INT16S AppPres CPU INT16U AppRxPktCtr CPU INT16U MB InRegRd CPU INT16U reg CPU INT16U perr CPU INT16U val perr MODBUS ERR NONE switch reg case 1000 CPU CRITICA
46. e void MB CoilWr CPU INTI6U coil CPU BOOLEAN coil val CPU INT16U perr Arguments coil Is the coil number that you want to change and can be a number between 0 and 65535 depending on your product It is up to you to decide which coil is assigned to what variable in your product coil val Is the value you want to change the coil to and can be either DEF TRUE Of DEF FALSE perr Is a pointer to a variable that will contain an error code based on the outcome of the call Your code thus needs to return one of the following error codes MODBUS ERR NONE if the coil number you specified is a valid coil and you are able to have code access the value of this coil MODBUS ERR RANGE if the coil number passed as an argument is not a valid coil number for your product Returned Value None Notes Warnings Code is enabled when either MODBUS CFG FC05 EN is set to DEF ENABLED or MODBUS CFG FC15 ENissetto DEF ENABLED in your product s mb cfg h file Called By MBS FC05 CoilWr and MBS FC15 CoilWrMultiple in mbs core c 60 uC Modbus Example In this example our product has 163 coils 160 coils are placed in a table called AppCoilTbl The other three coils are actually variables that we treat as coils to allow a MODBUS master to read the status of those values The first 160 coils are assigned coil numbers 0 to 159 Coil numbers 200 201 and 202 correspond to the following application variables AppStatus AppRunning and
47. e address or have different node addresses for each channel specifies whether this channel is a Modbus Master or a Modbus Slave Values for this argument can either be MODBUS MASTER or MODBUS SLAVE specifies the amount of time that a Modbus master will wait for a response from a slave The time is specified in RTOS ticks consult your RTOS documentation to determine the tick rate specifies the operating mode ASCII or RTU and thus this argument can either be MODBUS MODE ASCII orMODBUS MODE RTU specifies which physical connection i e port is associated with the Modbus channel In other words it determines which UART will be associated with the Modbus channel port nbr are typically assigned from 0 to the maximum number of physical UARTs you have in your product minus one For example if your product has 4 UARTs and all of them can be assigned to a Modbus channel then the UARTs would be numbered from 0 to 3 However you don t have to number them from 0 the numbering scheme really depends on who writes the MB BSP C file is the baud rate of the Modbus channel You would typically specify a standard baud rate such as 9600 19200 38400 etc specifies the number of data bits used by the UART For RTU you d typically specify 8 For ASCII you can either specify 7 or 8 If you specify 7 bits you will probably also need to specify the parity see next argument 20 parity stops wr en Returned Value uC Modbus
48. e code for HC OS II uC OS IIl and Non OS environment are provided so you don t have to change this code if you use pC OS II or pC OS II in your product or you use uC Modbus S witouth any RTOS The file mb os h is only needed when WC Modbus S is used witouth any RTOS this is explained in Section 8 HC Modbus is independent of the CPU and the compiler you use However you need to provide information about the data types specific to your CPU and compiler For example you need to define the following data types CPU_BOOLEAN Boolean True or False Yes or No etc CPU_INTO8U 8 bit unsigned integer CPU_INT16U 32 bit unsigned integer CPU_INT32U 8 bit unsigned integer CPU_FP32 32 bit IEEE754 floating point Etc These data types are needed because pC Modbus never uses the standard C data types i e char short int long etc because they are non portable These data types need to be placed in a file called cpu h more on this later pC Modbus 2 00 Directories and Files The code for pC Modbus is found in the following directories 2 01 Directories and Files Target Independent Source Code Micrium Software uC Modbus Source This directory contains the UART OS and CPU independent source files This directory contains the following files mb c Master Slave independent code mb h mb def h Modbus Definitions mb util c ASCII convertions utilities mbes core c Slave specific code mb
49. e product that embeds uWC Modbus for the life of that product In other words a pC Modbus license is royalty free Contact Micrium for pricing information References UC OS II The Real Time Kernel 2 Edition Jean J Labrosse Elsevier 2002 ISBN 978 1578201037 HC OS III The Real Time Kernel Jean J Labrosse MicriumPress 2009 ISBN 978 0 9823375 3 0 Modicon Modbus Protocol Reference Guide PI MBUS 300 Rev J http www modicon com TECHPUBS TECHPUBNEW PI MBUS 300 pdf Contacts Micrium 949 Crestview Circle Weston FL 33327 USA 1 954 217 2036 1 954 217 2037 FAX e mail uC Modbus Micrium com WEB www Micrium com MODICON Inc Industrial Automation Systems One High Street North Andover Massachusetts 01845 USA 108
50. e transmitted function code doesn t correspond to the received function code MODBUS ERR BYTE COUNT If the expected number of bytes to receive doesn t correspond to the number of bytes received Notes Warnings None Called By Your Modbus master application Example 39 pC Modbus 3 12 Using pC Modbus M MBM_FC08_Diag This function is called from YOUR application code to perform a diagnostic check on a Modbus slave Prototype CPU INT16U MBM FCO8 Diag MODBUS CH pch CPU INTO8U slave node CPU INT16U fnct CPU_INT16U sub fnct CPU INT16U pval Arguments pch is a pointer to the channel returned by MB CfgCh This pointer specifies onto which channel the Modbus master will be communicating on Of course och must have been configured as a Master when you configured the channel slave_node specifies the slave node address of the slave you want to performa a diagnostic function to This can be a number between 1 and 255 but needs to match the number assigned to the slave node fnct specifies the function you want to perform on the slave and you must specify either MODBUS FC08 LOOPBACK CLR CTR You want to clear the loopback counters in the slave MODBUS FC08 BUS MSG CTR You want to read the counter of messages received by the slave This counter keeps track of all messages received whether processed or not MODBUS FC08 BUS CRC CTR You want to read the counter of bad CRCs detected by the
51. ecifies which physical connection i e port is associated with the Modbus channel In other words it determines which UART will be associated with the Modbus channel port nbr are typically assigned from 0 to the maximum number of physical UARTs you have in your product minus one For example if your product has 4 UARTs and all of them can be assigned to a Modbus channel then the UARTs would be numbered from 0 to 3 However you don t have to number them from 0 the numbering scheme really depends on who writes the MB _BSP C file Returned Value None Notes Warnings None Called By Your Modbus master or slave application Example 22 uC Modbus 3 03 Using uC Modbus MB Exit MB Exit should be called if you no longer want to run uC Modbus in your product Prototype void MB Exit void Arguments None Returned Value None Notes Warnings None Called By Your Modbus master or slave application Example 23 uC Modbus 3 04 Using uC Modbus M MBM FCO01 CoilRd This function is called from YOUR application code to read coils from a Modbus slave Prototype CPU INT16U MBM FCO1 CoilRd MODBUS CH pch Arguments pch slave addr Start addr pcoil tbl nbr coils CPU_INTO8U slave_addr CPU_INT16U start_addr CPU_INTO8U p coil tbl CPU_INT16U nbr coils is a pointer to the channel returned by MB CfgCh This pointer specifies onto which channel the Modbus master will be com
52. ection This flexibility also allows you to execute code whenever a data is read or written You must thus write the code for the following functions B CoilRd B Coilwr B DIRd InRegRd B InRegRdFP B HoldingRegRd B HoldingRegRdFP ldingRegWr ldingRegWrFP _FileRd B FileWr w D a mo O O vo hj SSS ESSE SEES 57 pC Modbus 5 01 uC Modbus S MB_CoilRd MB CoilRd is called when a Modbus master sends a Function Code 1 command MB CoilRd returns the value of a single coil MB CoilRd should only be called by pC Modbus Prototype CPU_BOOLEAN MB CoilRd CPU INT16U coil CPU INT16U perr Arguments coil Is the coil number that you want to read and can be a number between 0 and 65535 depending on your product It is up to you to decide which coil is assigned to what variable in your product perr Is a pointer to a variable that will contain an error code based on the outcome of the call Your code thus needs to return one of the following error codes MODBUS ERR NONE if the coil number you specified is a valid coil and you are able to have code access the value of this coil MODBUS_ERR_RANGE if the coil number passed as an argument is not a valid coil number for your product Returned Value MB CoilRd returns the current value of the specified coil number TRUE or FALSE If an invalid coil number is specified you should return
53. ely The frequency at which the poling function is called is defined by your application 94 F8 1 2 F8 1 3 F8 1 4 uC Modbus We assume that byte reception from a UART is interrupt driven Received bytes are placed in a receive buffer until a complete packet is received If the channel is configured for Modbus ASCII an end of packet is signaled by a line feed character i e 0x0A If the channel is configured for Modbus RTU then the end of a packet is signaled by not receiving bytes for at least the time it takes to send 3 5 bytes see Modbus RTU specification Assuming Modbus ASCII the end of a packet is signaled by calling MB OS RxSignal and specifying a pointer to the channel on which a packet was received If your product needs to support the Modbus RTU mode you will need to provide a timer that interrupts the CPU in order to keep track of end of packets Basically when bytes are received on a channel an RTU counter for that channel is reset to a value based on the baud rate of the channel see table 7 1 Table 7 1 RTU Timeouts based on channel Baud Rate Baud Rate RTU Timeout RTU Timeout Time for 3 5 Bytes Counts at 1 KHz 9 600 3646 uS 5 19 200 1823 uS 3 38 400 911 uS 2 76 800 456 uS 2 115 200 304 US 2 256 000 137 uS 2 For example if a channel is configured for 19 200 Baud then an end of packet in RTU mode is assumed to occur when no bytes are received after 1
54. fgCh This pointer specifies onto which channel the Modbus master will be communicating on Of course pch must have been configured as a Master when you configured the channel specifies the slave node address that you desire to read the registers from This can be a number between 1 and 255 but needs to match the number assigned to the slave node specifies the start address of the floating point holding registers This can be from MODBUS CFG FP START IX to 65535 of the slave and assumes that you enabled floating point support by setting MODBUS CFG FP _ENto DEF ENABLED inmb cfg hinthe slave is a pointer to an array of 32 bit IEEE 754 format floating point values that will receive the value of all the registers you are reading The size of the array needs to be at least nbr_regs specifies the number of registers you want to read from the slave 30 uC Modbus Returned Value MODBUS ERR NONE if the call was successful MODBUS ERR RX if a response was not received from the slave within the timeout specified for this channel see MB CfgCh MODBUS ERR SLAVE ADDR If the transmitted slave address doesn t correspond to the received slave address MODBUS ERR FC If the transmitted function code doesn t correspond to the received function code MODBUS ERR BYTE COUNT If the expected number of bytes to receive doesn t correspond to the number of bytes received Notes Warnings The floating point format cor
55. he architecture V2 00 2006 05 15 Simplified the code Made error return codes 16 bits instead of 8 Added support for Modbus Master V2 10 2006 08 10 Corrected bug with Modbus ASCII LRC calculation LRC was being computed on binary data instead of the raw ASCII message Our test tool was incorrectly calculating LRCs on the binary data and we followed the same scheme Removed all calls to MB TxErrChkCalc since the LRC and CRC calculations for Tx are done just before transmitting the response V2 11 2008 11 14 Added single thread support for Modbus Slave V2 12 2009 02 02 Now checking for broadcast messages in Modbus Slave We simply execute the command but we don t reply We also added a check so we don t read more registers than can fit in a frame V2 13 00 2012 05 17 Corrected bugs changed version numbering system improved MISRA C 2004 rules support corrected style issues uC Modbus 1 00 Introduction This document describes pC Modbus which implements the Modicon Modbus Protocol referred to as Modbus along with the Daniel s Extension to the Modbus protocol as specified by Daniel Flow Products For more details on the Modbus protocol please refer to Modicon s Modicon Modbus Protocol Reference Guide PI MBUS 300 Rev J The Modbus protocol consists of the reception and transmission of data in predefined packets hereby referred
56. his point we received a message from a Modbus master which was directed to the node address of the channel However we dont know yet whether the message is valid MBS RTU Task calls MB RTU Rx mb c which copies the frame received from the RxBuf to the RxFrameData buffer MBS RTU Task then calls MB RTU RxCalcCRC to determine whether the received CRC which is part of the message matches the calculated CRC of the message Note that the CRC is computed for ALL the bytes received except for the CRC portion itself In other words the CRC consist only of the node address function code and data sent by the Modbus master If we have a valid message we then call MBS FCxx Handler mbs core C to parse the received message and formulate a response back to the master The response is sent to the master by calling MB_RTU_Tx MBS FCxx Handler mbs core c This function determines what the master wants by looking at the Function Code in the received message The appropriate Modbus function code handler is called accordingly MBS FC _ The response is placed in the TxFrameData buffer in binary format 100 uC Modbus MB RTU Tx mb c This function is called when we need to send a response back to a Modbus master MB RTU_Tx simply copies the response which was placed in TxFrameData into the TxBuf The CRC of the outgoing frame is calculated by calling MB RTU TxCalcCRC Note that the CRC is computed o
57. lding registers on a Modbus slave Prototype CPU_INT16U MBM FCO6 HoldingRegWr MODBUS CH pch Arguments pch slave_node slave addr reg val CPU INTO8U slave node CPU INT16U slave addr CPU INT16U reg val is a pointer to the channel returned by MB CfgCh This pointer specifies onto which channel the Modbus master will be communicating on Of course och must have been configured as a Master when you configured the channel specifies the slave node address of the holding register you want to change This can be a number between 1 and 255 but needs to match the number assigned to the slave node specifies the address of the holding register that you want to change This can be from 0 to 65535 is the desired value of the holding register If the holding register you are changing is a signed value simply cast the value to unsigned Modbus reads and writes 16 bit values and doesn t really care about the sign 36 uC Modbus Returned Value MODBUS ERR NONE if the call was successful MODBUS ERR RX if a response was not received from the slave within the timeout specified for this channel see MB CfgCh MODBUS ERR SLAVE ADDR If the transmitted slave address doesn t correspond to the received slave address MODBUS ERR FC If the transmitted function code doesn t correspond to the received function code MODBUS ERR BYTE COUNT If the expected number of bytes to receive doesn t correspond
58. m core c Master specific code pC Modbus 2 02 Directories and Files RTOS Interface Micrium Software uC Modbus 0OS uCOS II Micrium Software uC Modbus 0S uCOS III These directories contains the code to interface to the wC OS II and uC OS I RTOSs and contains the following file mb os c See Section 7 00 If you interface pC Modbus to different RTOSs you would place an mb os c file in a separate directory In other words all RTOS interface files should be called mb os c but the specifics of the actual RTOS you use would be placed in a different directory When you build your product you obviously need to select only one RTOS interface the one specific to your RTOS Micrium Software uC Modbus 0S None This directory contains the code to use uC Modbus S in a single threaded environment witouth the need of an RTOS mb OS 6 See Section 8 00 mb os h See Section 8 00 pC Modbus 2 03 Directories and Files Product Specific Files 22 Product This directory contains your application code You need to provide the following files mb cfg h See Section 4 00 mb data c See Section 5 00 mb bsp c See Section 6 00 2 04 Directories and Files CPU and Compiler Specific Files Micrium Software uC CPU lt CPU type gt lt compiler gt This directory contains information about your CPU and the compiler you are using There are three files that you need to specify cpu h cpu_a asm It s preferable to modify
59. mb_cfg h in the slave is a pointer to an array of unsigned 16 bit values that will receive the value of all the registers you are reading The size of the array needs to be at least nbr regs Note that you can cast the unsigned values to signed values As far as the Modbus protocol is concerned it sends and receives 16 bit values and the interpretation of what these values mean is application specific specifies the number of registers you want to read from the slave 32 uC Modbus Returned Value MODBUS ERR NONE if the call was successful MODBUS ERR RX if a response was not received from the slave within the timeout specified for this channel see MB CfgCh MODBUS ERR SLAVE ADDR If the transmitted slave address doesn t correspond to the received slave address MODBUS ERR FC If the transmitted function code doesn t correspond to the received function code MODBUS ERR BYTE COUNT If the expected number of bytes to receive doesn t correspond to the number of bytes received Notes Warnings MODBUS CFG FP START IX corresponds to that of the slave Called By Your Modbus master application Example 33 pC Modbus 3 09 Using uC Modbus M MBM FC05 CoilWr This function is called from YOUR application code to write to a single coil on a Modbus slave Prototype CPU_INT16U MBM FCO5 CoilWr MODBUS CH pch Arguments pch slave node slave addr coil val CPU INTO8U slave node CPU INT16U
60. mber Note that if your product doesn t have any floating point registers but a large number of holding registers you can set MODBUS CFG FP START IX to 65535 perr Is a pointer to a variable that will contain an error code based on the outcome of the call Your code thus needs to return one of the following error codes MODBUS ERR NONE if the holding register number you specified is a valid holding register and you are able to have code access the value of this holding register MODBUS ERR RANGE if the holding register number passed as an argument is not a valid holding register number for your product Returned Value MB HoldingRegRd returns the current value of the specified holding register as an unsigned value Of course you can also return signed values but those need to be cast to CPU_INT16U You should note that the value will not be changed if you cast a signed variable to CPU_INT16U The Modbus master will receive the proper value and it s up to the Modbus master to properly retrieve the signed data If an invalid holding register number is specified you should return 0 68 uC Modbus Notes Warnings Code is enabled when MODBUS CFG_FC03 EN is set to DEF ENABLED in your product s mb cfg h file Called By MBS FCO3 HoldingRegRd INmbs core c Example In this example our product has 4 integer variables that we want to assign to holding registers Your systems Engineer decided to assign Modbus holding regis
61. mode at run time Setting MODBUS CFG RTU EN to DEF ENABLED doesn t mean that your product MUST use RTU mode it just means that the code to support MODBUS RTU will be included in the compilation 4 05 Configuring pC Modbus MODBUS CFG MAX CH HC Modbus allows you to provide multiple communication channels in your product Each channel allows a MODBUS master to request data from your product If your product only provides one channel you should set MODBUS CFG MAX CHO 1 4 06 Configuring pC Modbus MODBUS CFG BUF SIZE MODBUS protocol packets can contain up to 256 bytes of data To hold this data each pC Modbus channel allocates storage buffers TWO for received packets and TWO for transmit packets If your application sends and receives small packets you can reduce the buffer size in order to conserve RAM However we recommend that you leave MODBUS CFG BUF SIZE to it s default value of 255 With 255 a Modbus channel will require 1020 bytes of RAM for buffers 4 07 Configuring pC Modbus MODBUS_CFG_FP_EN When set to DEF_ENABLED this define constant is used to enable code generation for floating point support of the Daniels Flow Meter Floating Point Extension The default value should be DEF_DISABLED 4 08 Configuring pC Modbus MODBUS_CFG_FP_START_IX This define establishes the start address for floating point numbers use in Input Registers and Holding Registers Basically integer input registers and holding registers go
62. municating on Of course och must have been configured as a Master when you configured the channel specifies the slave node address that you desire to read the coil information from This can be a number between 1 and 255 but needs to match the number assigned to the slave node specifies the start addres of the coil number This can be from 0 to 65535 is a pointer to an array of 8 bit values that will receive the value of all the coils you are reading The size of the array needs to be at least nbr_coils 1 8 1 The format of the table is as follows p coil tbl 0 8 7 1 p coil tbl 1 16 15 9 specifies the number of coils you want to read from the slave 24 uC Modbus Returned Value MODBUS ERR NONE if the call was successful MODBUS ERR RX if a response was not received from the slave within the timeout specified for this channel see MB CfgCh MODBUS ERR SLAVE ADDR If the transmitted slave address doesn t correspond to the received slave address MODBUS ERR FC If the transmitted function code doesn t correspond to the received function code MODBUS ERR BYTE COUNT If the expected number of bytes to receive doesn t correspond to the number of bytes received Notes Warnings None Called By Your Modbus master application Example 25 pC Modbus 3 05 Using uC Modbus M MBM_FC02_DIRd This function is called from YOUR application code to read discrete inputs from a Modb
63. n ALL the bytes to be transmitted except the CRC itself In other words the CRC consist only of the node address function code and data sent to the Modbus master MB RTU Tx then calls MB Tx to setup transmission MB_Tx This function is called to send a message to a Modbus master Here we simply point the TxBufPtr at the beginning of the TxBuf and transmit the first byte by calling MB TxByte mb c in order to kick start transmission interrupts Note that in a lot of cases transmission interrupts occur ONLY after a character has been transmitted MB_TxByte MB TxByte in turn calls MB CommTx1 which sends a byte to the UART and enables Tx interrupts 101 uC Modbus 9 03 HC Modbus M ASCII Rx and Tx It might be useful to follow the code for the description provided below MBM FC mbm core c Your Modbus master application calls one of the MBM FC _ functions see section 3 to send a command to a slave This function creates a command frame to send to the Modbus slave which is sent by calling MBM TxCmd MBM_TxCmd mbm core c This function determines whether the Master channel is configured for Modbus ASCII or RTU and calls MB ASCII Tx OrMB RTU Tx accordingly MB ASCII Tx mb c In ASCII mode this function is called to send the command to a Modbus slave MB ASCII Tx simply converts the command which was placed in TxFrameData and converts it to ASCII The converted data is placed i
64. n the TxBuf The LRC of the outgoing frame is calculated by calling MB ASCII TxCalcLRC Note that the LRC is computed by summing up ALL the ASCII characters to be transmitted except the colon LRC and CR LF and then doing a twos complement In other words the LRC consist only of the node address function code and data sent to the Modbus slave MB ASCII Tx thencalls MB Tx to setup transmission MB Tx mb c This function is called to send a message to a Modbus slave Here we simply point the TxBufPtr at the beginning of the TxBuf and transmit the first byte by calling MB TxByte mb c in order to kick start transmission interrupts Note that in a lot of cases transmission interrupts occur ONLY after a character has been transmitted MB TxByte mb c MB TxByte in turn calls MB CommTx1 MB BSP C which sends a byte to the UART and enables Tx interrupts MB OS Wait mb os c When the command is sent MBM FC calls MB OS Wait to wait for a response from the slave but with a timeout If the response is not received within the specified timeout see MB CfgCh then we flush the Rx buffer If a response is received we call MBM RxReply to parse the response MB_CommRxTxISR_Handler mb bsp c Characters received on a UART are processed by the MB_CommRxTxISR_Handler unless the UART has a separate interrupt for Rx and Tx In this case the function would be called MB CommRxISR_Handler The receive
65. ngle holding register value Integer holding registers are numbered from 0 through MODBUS CFG FP START IX 1 MODBUS CFG FP START IX allows you to specify the start of floating point see section 5 09 MD HoldingRegWrFP MB HoldingRegWr should only be called by pC Modbus Prototype void MB HoldingRegWr CPU_INT16U reg CPU_INT16U reg val CPU INT16U perr Arguments reg Is the desired holding register to read and can be a number between 0 and MODBUS CFG FP START IX 1 depending on your product It is up to you to decide what application variable is assigned to each holding register number Note that if your product doesn t have any floating point registers but a large number of holding registers you can set MODBUS CFG FP START IX to 605539 reg val Is the desired value for the specified holding register and can be a number between 0 and 65535 Note that your product could have a signed 16 bit integer but this function will temporarily treat it as an unsigned value However the assignment is performed correctly and your application variable will have the sign set correctly perr Is a pointer to a variable that will contain an error code based on the outcome of the call Your code thus needs to return one of the following error codes MODBUS ERR NONE if the holding register number you specified is a valid holding register and you are able to have code access the value of this holding register MODBUS ERR
66. nterfaces used by HC Modbus Your application DOES NOT need to call this function The pseudo code for this function is shown below void MB CommExit void Disable all uC Modbus Rx interrupts Disable all uC Modbus Tx interrupts Remove interrupt vectors if needed pC Modbus 6 02 BSP MB_CommPortCfg This function is called by MB CfgCh to configure the UART communication settings for a channel MB CommPortCfg must NOT be called by your application The function prototype is shown below void MB CommPortCfg MODBUS CH pch port nbr baud bits parity stops pch port_nbr baud bits parity stops CPU_INTO8U CPU_INT32U CPU INTO8U CPU INTO8U CPU INTO8U is a pointer to the communication channel to configure This pointer is returned to your application when you call MB CfgCch is the physical port number associated with the pC Modbus communication channel For example wC Modbus channel 0 could be associated with your 5 UART In other words uC Modbus channels can be assigned to any physical serial port in your system there doesn t need to be a one to one correspondence is the desired baud rate for the channel You should write code to support the standard baud rates 9600 19200 38400 76800 115200 and 256000 baud is the number of bits used for the UART It s typically 7 or 8 The most common is 8 bits is the type of parity checking scheme
67. odbus allows you to have a combination of up to 250 master or slave ports from a single target system HC Modbus S indicates that your product contains the Modbus slave implementation of wC Modbus and pC Modbus M indicates that your product contains the Modbus master implementation of uC Modbus You should note that a product can contain both wC Modbus S and uC Modbus M at the same time However the master and the slave would be on separate ports HC Modbus 1 01 Topologies Figure 1 2 shows the relationship between multiple products slaves and a Modbus master assuming RS 485 Modbus Master RS 485 Bus Your Product Your Product Your Product Running pC Modbus S Running pC Modbus S Running pC Modbus S Figure 1 2 Relationship between Modbus Master and Slaves on RS 485 Network Figure 1 3 shows the relationship between multiple products slaves and multiple Modbus masters assuming RS 485 in the example with one of those products being HC Modbus M You will note that only one master can be present on each RS 485 network Modbus Master Your Product Running pC Modbus M RS 485 Bus Your Product Your Product Running pC Modbus S Running pC Modbus S Figure 1 3 Multiple Modbus Masters and Slaves on RS 485 Networks uC Modbus Figure 1 4 shows the relationship between multiple products slaves and multiple Modbus masters assuming RS 232C in the example As you can see with RS 232C ea
68. on provided below MBM FC mbm core c Your Modbus master application calls one of the MBM FC _ functions see section 3 to send a command to a slave This function creates a command frame to send to the Modbus slave which is sent by calling MBM TxCmd MBM_TxCmd mbm core c This function determines whether the Master channel is configured for Modbus ASCII or RTU and calls MB ASCII Tx OrMB RTU Tx accordingly MB RTU Tx mb c This function is called when we need to send a command to a Modbus slave MB RTU Tx simply copies the command which was placed in TxFrameData into the TxBuf The CRC of the outgoing frame is calculated by calling MB RTU TxCalcCRC Note that the CRC is computed on ALL the bytes to be transmitted except the CRC itself In other words the CRC consist only of the node address function code and data sent to the Modbus slave MB RTU Tx then calls MB Tx to setup transmission MB Tx 0 This function is called to send a message to a Modbus slave Here we simply point the TxBufPtr at the beginning of the TxBuf and transmit the first byte by calling MB TxByte mb c in order to kick start transmission interrupts Note that in a lot of cases transmission interrupts occur ONLY after a character has been transmitted MB_TxByte MB TxByte in turn calls MB CommTx1 which sends a byte to the UART and enables Tx interrupts MB OS Wait mb os c When the command is sent MBM
69. op RTU timer interrupts void MB RTU TmrExit void 6 11 BSP MB RTU TmriSR Handler This function is the ISR handler for RTU timer interrupts The pseudo code for this function is shown below void MB RTU TmrISR Handler void Clear the RTU timer interrupt source MB RTU TmrCtr Indicate that we had activities on this interrupt MB RTU TmrUpdate Check for RTU timers that have expired pC Modbus 7 00 RTOS Interface HC Modbus S migh use an RTOS interface wC Modbus M assumes the presence of an RTOS but it doesn t assume any specific RTOS In fact uC Modbus was designed to work with just about commercial RTOS by providing a simple RTOS interface layer HC Modbus is provided with a pC Modbus RTOS interface layer so you can start using wC Modbus if you are also using WC OS II or uC OS III in your product or use this interface layer as an example for your own RTOS Figure 7 1 shows a flow diagram of receive model if MODBUS CFG RTU EN DEF ENABLED RTU Timer oa 4 Sie beens See elie cece cele P TITT end Modbus Rx Task 1 UART Rx MB OS RxSignal pch ISR UART Rx Figure 7 1 uC Modbus Rx Tasking Model 86 F7 1 1 F7 1 2 F7 1 3 F7 1 4 pC Modbus HC Modbus uses a single task to receive packets from any channel The Modbus Rx Task simply waits for a Modbus packet from any channel Assuming pC OS II as the RTOS a message queue is used for this purpose Fo
70. pecified ix is not a valid index into the specified record 76 uC Modbus Returned Value MB FileRd returns the current value of the element in the file as an unsigned value Of course you can also return signed values but those need to be cast to CPU INT16U You should note that the value will not be changed if you cast a signed variable to CPU INT16U The Modbus master will receive the proper value and it s up to the Modbus master to properly retrieve the signed data If an error is detected you should return 0 Notes Warnings Code is enabled when MODBUS CFG FC20 EN is set to DEF ENABLED in your product s mb cfg h file Called By MBS FC20 FileRd iNmbs core c Example In this example we have two files that we implemented as an array of 16 bit integers define APP MAX FILES 2 define APP FILE MAX RECORDS 10 define APP FILE MAX VALUES 100 CPU INT16U AppFile APP MAX FILES APP FILE MAX RECORDS APP FILE MAX VALUES CPU INT16U MB FileRd CPU INT16U file nbr CPU INT16U record nbr CPU INT16U ix E CPU INTO8U record len CPU INT16U perr CPU_INT16U val perr MODBUS ERR NONE if file nbr gt APP MAX FILES perr MODBUS ERR FILE return 0 if record nbr gt APP FILE MAX RECORDS perr MODBUS ERR RECORD return 0 if ix gt APP FILE MAX VALUES perr MODBUS ERR IX return 0 CPU CRITICAL ENTER val AppFile file nbr
71. r ModBus MBS stands for ModBus Slave and MBM stands for ModBus Master A file that starts with mb_ indicates that the code in the file is independent of Modbus Slave or Master A file that starts with mbs contains Modbus Slave specific code and of course a file that will start with mbm will contain MODBUS Master specific code F1 5 1 F1 5 2 F1 5 3 F1 5 4 Your product needs to configure pC Modbus at compile time to establish the maximum number of channels your product will support whether some channels will support Modbus ASCII and or RTU whether the Daniels Extensions will be supported to provide floating point which Modbus function codes will be supported whether a product will be a Master a Slave or both etc Configuration is done by changing a C header file mb cfg h This is code that YOU need to provide and mb cfg h typically resides in your product s directory since it can be different for each product A Modbus master connected to your product that is running HC Modbus S can read or change just about ANY data in your application Access to your data read or write is done via a C file that you provide mb data c mb data c can read integers coils discrete inputs floating point values etc mb data c also allows you to execute ANY code when data is read or written For example if you change the diameter of a circle and need to compute the surface you can simply include the code to compute the surfa
72. r pC OS III the message queue is built into the task When a packet is received a pointer to the channel that received the packet is posted to the message queue indicating to the Modbus Rx Task which channel received the packet The Modbus Rx Task then simply parses the packet and formulates a response that will be forwarded appropriately We assume that byte reception from a UART is interrupt driven Received bytes are placed in a receive buffer until a complete packet is received If the channel is configured for Modbus ASCII an end of packet is signaled by a line feed character i e 0x0A If the channel is configured for Modbus RTU then the end of a packet is signaled by not receiving bytes for at least the time it takes to send 3 5 bytes see Modbus RTU specification Assuming Modbus ASCII the end of a packet is signaled by calling MB OS RxSignal and specifying a pointer to the channel on which a packet was received If your product needs to support the Modbus RTU mode you will need to provide a timer that interrupts the CPU in order to keep track of end of packets Basically when bytes are received on a channel an RTU counter for that channel is reset to a value based on the baud rate of the channel see table 7 1 Table 7 1 RTU Timeouts based on channel Baud Rate Baud Rate RTU Timeout RTU Timeout Time for 3 5 Bytes Counts at 1 KHz 9 600 3646 uS 5 19 200 1823 uS 3 38 400 911
73. r this command 55 pC Modbus 4 20 Configuring uC Modbus RAM Memory Requirements The amount of RAM required by each pC Modbus channel is shown in the table below The table assumes that pointers are 32 bits wide Table 3 1 RAM Requirements for each uC Modbus channel Data Type Data Type Size Elements for Specific Data Type Total Bytes Bytes CPU BOOLEAN 1 1 1 CPU INTO8U 1 8 1028 4 MODBUS CFG BUF SIZE CPU INT16U 7 13 30 gt 2 MODBUS CFG RTU EN CPU INT32U 4 4 16 CPU INTO8U 4 2 8 Total per uC Modbus channel 1983 see MB ChSize The global variable MB TotalRAMSize contains the total amount of RAM in bytes needed by pC Modbus for the configuration you specify Similarly MB ChSize contains the amount of RAM in bytes needed by each Modbus channel Both of these variables are 32 bit values and are actually declared as const and thus use 8 bytes of ROM and no RAM 56 pC Modbus 5 00 HC Modbus S Accessing application data HC Modbus S accesses your application data via interface functions that are defined in mb data c Specifically functions that YOU provided in this file are called by HC Modbus S to read and write coils integers floating point values and more It s up to you to decide how your data is accessed Specifically you can use tables functions switch statements etc Examples are provided in this s
74. responds to the Daniels Flow control extensions Specifically a register is assumed to be 32 bits and uses the IEEE 754 format Floating support must have been enabled in the slave you are communicating with and the start address of the floating point registers MODBUS CFG FP START IX corresponds to that of the slave Called By Your Modbus master application Example 31 pC Modbus 3 08 Using uC Modbus M MBM FC04 InRegRd This function is called from YOUR application code to read 16 bit input registers registers from a Modbus slave Prototype CPU_INT16U MBM FCO04 InRegRd MODBUS CH pch Arguments pch slave_node start _ addr p_reg_tbl nbr regs CPU_INT08U slave node CPU INT16U start addr CPU INT16U p reg tbl CPU INT16U mnbr regs is a pointer to the channel returned by MB CfgCh This pointer specifies onto which channel the Modbus master will be communicating on Of course och must have been configured as a Master when you configured the channel specifies the slave node address that you desire to read the registers from This can be a number between 1 and 255 but needs to match the number assigned to the slave node specifies the start address of the registers This can be from 0 to 65535 Note that the start address must be a number lower than MODBUS CFG FP START IX of the slave if you intend to have floating point registers i e you set MODBUS CFG FP EN to DEF ENABLED in
75. rmat and checksum If we received a valid packet MB RTU Rx returns to MBM RxReply which in turns returns to the MBM FC _ function MBM FC mbm core c MBM FC _ then parses the response and returns the requested information to its caller 105 pC Modbus 10 00 Acronyms Abbreviations and Mnemonics HC Modbus includes a number of acronyms abbreviations and mnemonics and some are listed in Table 10 1 This Means An Analog App Application Buf Buffer Cfg Configuration Ch Channel Comm Communication Ctr Counter DI Discrete Input Dis Disable DO Discrete Output En Enable Err Error FC Function Code FP Floating Point Id Identifier In Input Init Initialization ISR Interrupt Service Routine Ix Index MB Modbus MBM Modbus Master MBS Modbus Slave Nbr Number OS Operating System Out Output Pkt Packet Prio Priority 106 Rd Read Reg Register RTU Remote Terminal Unit Rx Receive Stk Stack Tmr Timer Tx Transmit Val Value Wr Write 107 HC Modbus pC Modbus Licensing pC Modbus is licensed on a per end product basis Specifically each different product that embeds pC Modbus in a commercial product requires a different license A license allows you to manufacture an unlimited number of units of th
76. rom MB OS RxTask calls MB RxTask mb c which in turn calls MBS RxTask mbs core c MBS RxTask determines whether the message was an ASCII or RTU message and calls MBS ASCII Task mbs core C or MBS RTU Task mbs core C respectively to do the actual processing of the message received MBS ASCII Task mbs core c At this point we received a message from a Modbus master which was directed to the node address of the channel However we don t know yet whether the message is valid MBS ASCII Task calls MB ASCII Rx mb c which converts the ASCII frame to a binary format The converted message is placed in RxFrameData MBS ASCII Task then calls MB ASCII RxCalcLRC to determine whether the received LRC which is part of the message matches the calculated LRC of the message Note that the LRC is computed by summing up ALL the ASCII characters in the received message except the colon LRC and CR LF and then doing a twos complement In other words the LRC consist only of the node address function code and data sent by the Modbus master If we have a valid message we then call MBS FCxx Handler to parse the received message and formulate a response back to the master The response is sent to the master by calling MB ASCII Tx MBS FCxx Handler mbs core c This function determines what the master wants by looking at the Function Code in the received message The appropriate Modbus function code handler is called
77. rom the slave within the timeout specified for this channel see MB CfgCh MODBUS ERR SLAVE ADDR If the transmitted slave address doesn t correspond to the received slave address MODBUS ERR FC If the transmitted function code doesn t correspond to the received function code MODBUS ERR BYTE COUNT If the expected number of bytes to receive doesn t correspond to the number of bytes received Notes Warnings None Called By Your Modbus master application Example 45 pC Modbus 3 15 Using pC Modbus M MBM_FC16_HoldingRegWrNFP This function is called from YOUR application code to write to multiple 32 bit floating point holding registers on a Modbus slave Prototype CPU_INT16U MBM FC16 HoldingRegWrNFP MODBUS CH pch Arguments pch slave_node slave addr preg tbl nbr reg CPU INTO8U slave node CPU INT16U slave addr CPU FP32 p reg tbl CPU INT16U nbr reg is a pointer to the channel returned by MB CfgCh This pointer specifies onto which channel the Modbus master will be communicating on Of course och must have been configured as a Master when you configured the channel specifies the slave node address of the floating point holding registers you want to change This can be a number between 1 and 255 but needs to match the number assigned to the slave node specifies the start address of the floating point holding registers that you want to change This can be from 0 to 65
78. rrrrrrrrrrrrrrnnnnnrrrrnnrrrrrrrrnnnnnnn 62 HC Modbus S MB InRegR eee eeeeeeeeeeeeeeeeeeeeeeeenneeeeeeeees 64 HC Modbus S MB_InRegRdFP c ececeeeeeeeeeeeeeeeeeeeeeeeees 66 HC Modbus S MB_HoldingRegRd cccceeeeeeeeeeeeeeeeeeeeees 68 pC Modbus S MB_HoldingRegRdFP ccceceeeeeeeeeeeeeees 70 pC Modbus S MB_HoldingRegwr cccceeeeeeeeeeeeeteeeeeeeees 72 HC Modbus S MB HoldingRegWrFP cccccecceeeeeeteeeeeeees 74 HC Modbus S MB FileRA rrrrrrrrrornnnnnrrrrrvrrrrrrrrrnnnnrrvvnnnnrennnnn 76 HC Modbus S MB FileWr rerrrrrrrnnnnnrrrrrnnnnnnnrrnnnnnrrrnnnnnnnnnnnn 78 Board Support Package BSP rrrrnnnnnvrrrnnnnnnnnnvnnnnnrrrrnnnnnnnnnrrnnnnnnn 80 BSP MB_ComMmmExit pciccoseutncoiteanccesasanstadovasedenntapssiindacsiincecssasntuns 80 BSP MB CommPonmGIgt sncincunninspetoccccstecscocuincucetencnmntintaeneten 81 BSP MB CommRxTxISR x Handler ccccccccccccecccceeeeeeeeeeeees 82 BSP MB CommRuxIntEn cccecccccccecceeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeees 83 BSP MB_CommRxlntDis vegene raeaaneeeateedr 83 BSP MB ComM A reene e aene esene ideis ieden ioen 83 BSP MB Commi XIMEN Os ssca yea noe enna encase 84 BSP MB Comm TXIMIDIS i cee ccce cee ccc Cen cccec cee cccke concede ek cnceceaneceeeeks 84 uC Modbus 6 09 6 10 6 11 7 00 7 01 7 02 7 03 7 04 7 05 8 00 9 00 9 01 9 02 9 03
79. shown below typedef CPU INT32U CPU SR define CPU CRITICAL ENTER cpu sr CPU SR Save define CPU CRITICAL EXIT CPU SR Restore cpu sr You should note that uC Modbus actually uses CPU CRITICAL ENTER and CPU CRITICAL EXIT to disable and re enable interrupts respectively cpu a asm This file contains the code for CPU SR Save and CPU SR Restore This code is typically written in assembly language since it generally accesses CPU registers which are not typically accessible from C However if your compiler allows you to manipulate CPU registers in C you would implement CPU SR Save and CPU SR Restore directly in C and call this file cpu c instead of cpu a asm 16 3 00 pC Modbus Using pC Modbus In order to use uC Modbus in your product you need to make sure you have the following elements Setup the pC CPU for the CPU YOU are using You need to create a cpu h and cpu_a asm files see section 2 04 Setup the BSP for the UARTs and the RTU timer YOU are using You need to create a mb bsp c file see section 5 You should note that HC Modbus includes a mb bsp c for diferrent processors and boards You can use these files as examples on how to write the BSP Setup the RTOS Interface for the RTOS YOU are using HC Modbus includes RTOS interfaces for wC OS II and pC OS III see section 6 If you are using a different RTOS you will need to provide an mb os c file You can act
80. t will contain an error code based on the outcome of the call Your code thus needs to return one of the following error codes MODBUS ERR NONE if the input register number you specified is a valid input register and you are able to have code access the value of this input register MODBUS ERR RANGE if the input register number passed as an argument is not a valid input register number for your product Returned Value MB InRegRdFP returns the current value of the specified floating point input register as a 32 bit IEEE 754 unsigned value If an invalid input register number is specified you should return CPU FP32 0 Notes Warnings Code is enabled when both MODBUS CFG FC04 EN is set to DEF ENABLED and MODBUS CFG FP ENis set to DEF ENABLED in your products mb cfg h file 66 Called MBS FC04 InRegRd iNmbs_ core c By Example In this example our product has 4 floating point variables that we want to assign to input registers numbers MODBUS CFG FP START IX 0 uC Modbus Your systems Engineer decided to assign MODBUS input register MODBUS CFG FP START IX 1 MODBUS CFG FP START IX 2 and MODBUS CFG FP START IX 3 to the four floating point values You will notice that we disable interrupts to access the variables This is done in case your CPU does not allow atomic access to the 32 bit floating point values CPU FP32 CPU FP32 CPU FP32 CPU FP32 CPU FP32 CPU_ AppTempAir AppTempFuel AppPresAir
81. ter numbers 1000 1001 1002 and 1003 to the four integer values You will notice that we disable interrupts to access the variables This is done in case your CPU is an 8 bit CPU and data accesses to 16 bit values are not atomic CPU INT16S AppTemp CPU INT16U AppCtr CPU INT16S AppPres CPU INT16U AppRxPktCtr CPU INT16U MB HoldingRegRd CPU INT16U reg CPU INT16U perr CPU INT16U val perr MODBUS ERR NONE switch reg case 1000 CPU CRITICAL ENTER val CPU INT16U AppTemp CPU CRITICAL EXIT return val case 1001 CPU CRITICAL ENTER val AppCtr CPU CRITICAL EXIT return val case 1002 CPU CRITICAL ENTER val CPU INT16U AppPres CPU CRITICAL EXIT return val case 1003 CPU RITICAL ENTER val AppRxPktCtr CPU_CRITICAL EXIT return val QA default perr MODBUS ERR RANGE return 0 69 pC Modbus 5 07 uC Modbus S MB_HoldingRegRdFP MB HoldingRegRdFP is called when a Modbus master sends a Function Code 3 command MB HoldingRegRdFP read the value of a single holding register but it assumes that you are trying to access a floating point variable Floating point holding registers are numbered from MODBUS CFG FP START IX to 65535 or less if you doesn t have a lot of floating point registers MODBUS CFG FP START IX allows you to specify the start of floating point MB HoldingRegRdFP should only be called by pwC Modbus
82. the specified ix is not a valid index into the specified record 78 uC Modbus Returned Value None Notes Warnings Code is enabled when MODBUS_FC21_EN is set to DEF ENABLED in your product s mb cfg hfile Called By MBS FC21 FileWwr iNmbs core c Example In this example we have two files that we implemented as an array of 16 bit integers define APP MAX FILES 2 define APP FILE MAX RECORDS 10 define APP FILE MAX VALUES 100 CPU INT16U AppFile APP MAX FILES APP FILE MAX RECORDS APP FILE MAX VALUES CPU INT16U MB FileWr CPU INT16U file nbr CPU INT16U record nbr CPU INT16U ix CPU INTO8U record len CPU INT16U val CPU INT16U perr perr MODBUS ERR NONE if file nbr gt APP MAX FILES perr MODBUS ERR FILE return if record nbr gt APP FILE MAX RECORDS perr MODBUS ERR RECORD return if ix gt APP FILE MAX VALUES perr MODBUS ERR IX return uv CPU _ CRITICAL ENTER AppFile file_nbr record nbr ix val CPU _ CRITICAL EXIT 79 pC Modbus 6 00 Board Support Package BSP HC Modbus can work with just about any UART You need to provide a few simple interface functions to work with your hardware These functions should be placed in a file called mb bsp c Micrium provides examples of mb bsp c as part of the HC Modbus release 6 01 BSP MB Commexit This function is called by MB Exit to close all serial i
83. ther your product will support Modbus slave or server mode Set this define to DEF_ENABLED to enable SLAVE code Set this define to DEF DISABLED to disable SLAVE code You must have purchased the HC Modbus S package in order to set this define to DEF ENABLED 4 02 Configuring pC Modbus MODBUS CFG MASTER EN This define constant specifies whether your product will support Modbus master or client mode Set this define to DEF ENABLED to enable MASTER code Set this define to DEF DISABLED to disable Master code You must have purchased the HC Modbus M package in order to set this define to DEF ENABLED 4 03 Configuring pC Modbus MODBUS CFG ASCII EN This define constant specifies whether your product will support the Modbus ASCII protocol Setting this value to DEF ENABLED allows any Modbus channel to be configured for Modbus ASCII mode Note that each channel must be configured to either Modbus ASCII or Modbus RTU mode at run time Setting MODBUS CFG ASCII ENO DEF ENABLED doesn t mean that your product MUST use ASCII mode it just means that the code to support Modbus ASCII will be included in the compilation pC Modbus 4 04 Configuring pC Modbus MODBUS CFG RTU EN This define constant specifies whether your product will support the MODBUS RTU protocol Setting this value to DEF ENABLED allows any Modbus channel to be configured for Modbus RTU mode Note that each channel must be configured to either MODBUS ASCII or MODBUS RTU
84. to the number of bytes received Notes Warnings None Called By Your Modbus master application Example 37 pC Modbus 3 11 Using pC Modbus M MBM FC06 HoldingRegWrFP This function is called from YOUR application code to write to a single 32 bit floating point holding registers on a Modbus slave Prototype CPU_INT16U MBM FC06 HoldingRegWrFP MODBUS CH pch CPU_INTO8U slave node CPU_INT16U slave addr CPU FP32 reg val Arguments pch is a pointer to the channel returned by MB CfgCh This pointer specifies onto which channel the Modbus master will be communicating on Of course pch must have been configured as a Master when you configured the channel slave node specifies the slave node address of the holding register you want to change This can be a number between 1 and 255 but needs to match the number assigned to the slave node slave addr specifies the address of the holding register that you want to change This can be from 0 to 65535 reg val is the desired floating point value of the holding register The floating point value assumes an IEEE 754 format 38 uC Modbus Returned Value MODBUS ERR NONE if the call was successful MODBUS ERR RX if a response was not received from the slave within the timeout specified for this channel see MB CfgCh MODBUS ERR SLAVE ADDR If the transmitted slave address doesn t correspond to the received slave address MODBUS ERR FC If th
85. turn AppSwReset case 103 case 104 case 105 case 106 case 107 case 108 case 109 case 0 case 1 case 2 case oY case 4 case oe case 6 case ek case 8 return AppDITbl di 1031 default perr MODBUS ERR RANGE return FALSE 63 pC Modbus 5 04 HC Modbus S MB_InRegRd MB InRegRd is called when a Modbus master sends a Function Code 4 command MB InRegRd read the value of a single input register Integer input registers are numbered from 0 through MODBUS CFG FP START IX 1 MODBUS CFG FP START IX allows you to specify the start of floating point see section 5 05 MD InRegRdFP MB InRegRd should only be called by uHC Modbus Prototype CPU INTI6U MB InRegRd CPU INT16U reg Arguments reg perr Returned Value MB InRegRd CPU INT16U perr Is the desired input register to read and can be a number between 0 and MODBUS CFG FP START IX 1 depending on your product It is up to you to decide what application variable is assigned to each input register number Note that if your product doesn t have any floating point registers but a large number of input registers you can set MODBUS CFG FP START IX to 65535 Is a pointer to a variable that will contain an error code based on the outcome of the call Your code thus needs to return one of the following error codes MODBUS ERR NONE if the input register number you specified is a valid input register an
86. ually model your RTOS interface from the one provided for wC OS II and pC OS III For pwC OS II and pC OS III don t forget to configure defines to setup the task priority and stack size should be placed in your application s app cfg h file If your product doesn t require the use of a RTOS you can used the No OS port This port is only for uC Modbus S pC Modbus M always requires a RTOS interface Initialize uC Modbus and configure your channels HC Modbus is initialized by simply calling MB Init and specifying the Modbus RTU frequency as an argument Once initialized you simply need to configure each Modbus channels using MB_CfgCh as shown in the example below Here our product has three Modbus ports a Modbus RTU port communicating at 9600 baud and a Modbus ASCII port communicating at 19200 baud and a Modbus ASCII Master port communicating at 19200 baud Both Modbus Slave ports assume Modbus address 1 but you can specify different node address for each one if you want HC Modbus MB Init 1000 Initialize uC Modbus RTU timer at 1000 Hz MB CfgCh 1 Modbus Node for this slave channel ODBUS SLAVE This is a SLAVE 0 0 when a slave ODBUS MODE RTU Modbus Mode ASCII or _RTU 1 Specify UART 1 9600 Baud Rate 8 Number of data bits 7 or 8 ODBUS PARITY NONE
87. umbered from MODBUS CFG _FP_START_IX to 65535 In other words MODBUS CFG FP START IX allows you to specify the start of floating point holding register addresses MB HoldingRegWrFP should only be called by pC Modbus Prototype void MB HoldingRegWrFP CPU_INT16U reg CPU_FP32 reg val CPU INT16U perr Arguments reg Is the desired holding register to read and can be a number between MODBUS CFG FP START IX and 65535 depending on your product It is up to you to decide what application variable is assigned to each floating point holding register number Note that if your product doesn t have any floating point registers but a large number of integer holding registers you can set MODBUS CFG FP START IX10 65535 reg val Is the desired value for the specified holding register and can be any IEEE 754 floating point value perr Is a pointer to a variable that will contain an error code based on the outcome of the call Your code thus needs to return one of the following error codes MODBUS ERR NONE if the floating point holding register number you specified is a valid floating point holding register and you are able to have code access the value of this floating point holding register MODBUS ERR RANGE if the floating point holding register number passed as an argument is not a valid floating point holding register number for your product 74 uC Modbus Returned Value None Notes Warnings Code is enabled when either MODBU
88. us slave Prototype CPU INT16U MBM FCO2 DIRd MODBUS CH pch Arguments pch slave addr start addr p di tbl nbr di CPU_INTO8U slave_addr CPU_INT16U start_addr CPU_INTO8U p di tbl CPU _INT16U nbr di is a pointer to the channel returned by MB CfgCh This pointer specifies onto which channel the Modbus master will be communicating on Of course och must have been configured as a Master when you configured the channel specifies the slave node address that you desire to read the coil information from This can be a number between 1 and 255 but needs to match the number assigned to the slave node specifies the start addres of the discrete input number This can be from 0 to 65535 is a pointer to an array of 8 bit values that will receive the value of all the discrete inputs you are reading The size of the array needs to be at least nbr di 1 8 1 The format of the table is p di tbl 0 8 7 1 p di tbl l 16 15 9 specifies the number of discrete inputs you want to read from the slave 26 uC Modbus Returned Value MODBUS ERR NONE if the call was successful MODBUS ERR RX if a response was not received from the slave within the timeout specified for this channel see MB CfgCh MODBUS ERR SLAVE ADDR If the transmitted slave address doesn t correspond to the received slave address MODBUS ERR FC If the transmitted function code doesn t correspond to the received
89. xBuffl TxBufPtr UART Figure 8 1 uC Modbus Buffer Management 97 pC Modbus 9 01 HC Modbus S ASCII Rx and Tx It might be useful to follow the code for the description provided below MB CommRxTxISR Handler mb bsp c Characters received on a UART are processed by the MB CommRxTxISR Handler unless the UART has a separate interrupt for Rx and Tx In this case the function would be called MB CommRxISR_Handler The received character is extracted from the UART and passed to the MB RxByte function for processing MB RxByte mb c MB RxByte determines whether the character received needs to be passed to the ASCII or RTU handler If ASCII the character is passed to MB ASCII RxByte MB ASCII RxByte mb c MB ASCII RxByte places received characters in RxBuf If the received character is a colon character i e we reset the pointer to the beginning of the RxBuf because this signals a new message from a Modbus master We signal the Rx Task if the character received is a line feed i e 0x0A and the message received is destined for the matching node address of the channel Signaling of the task is done by calling MB OS RxSignal mb_os c MB OS RxTask mb os c All Modbus communications is handled by a single Rx Task called MB OS RxTask The task waits for a message to be sent to it by MB ASCII RxByte The message is actually a pointer to the Modbus channel where the message was received f

Download Pdf Manuals

image

Related Search

Related Contents

Installation Manual  WPJ344 User Manual  3 TON CAPACITY JACK STANDS  #ライン・ワイプライン発生器 OPVG-W5 取扱説明書  Aspects SWF213SCT Installation Guide    180K/2DE (Page 1)  Verbatim P38-L1200-C30-B25-90-W  Installation Manual  Operating instructions  

Copyright © All rights reserved.
Failed to retrieve file