Home

Your Product

image

Contents

1. 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 CPU CRITICAL ENTER AppFile file nbr record nbr ix val CPU CRITICAL EXIT 77 uC Modbus 6 00 Board Support Package BSP uC 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 UC Modbus release 6 01 BSP MB Commexit This function is called by MB Exit to close all serial interfaces used by uC 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 uC Modbus 6 02 BSP MB CommpPortCfg 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 pch port nbr baud bits parity stops MODBUS CH CPU INTO8U CPU INT32U CPU INTO8U CPU INTO8U CPU INTO08U pch port_nbr baud bits parity stops is a pointer to the communication channel to configure
2. 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 101 uC Modbus Rd Read Reg Register RTU Remote Terminal Unit Rx Receive Stk Stack Tmr Timer Tx Transmit Val Value Wr Write 102 uC Modbus Licensing uC Modbus is licensed on a per end product basis Specifically each different product that embeds uUC Modbus in a commercial product requires a different license A license allows you to manufacture an unlimited number of units of the product that embeds UC Modbus for the life of that product In other words a UC Modbus license is royalty free Contact Micrium for pricing information References UC OS lII The Real Time Kernel 2 Edition Jean J Labrosse R amp D Technical Books 2002 ISBN 1 5782 0103 9 Modicon Modbus Protocol Reference Guide PI MBUS 300 Rev J http www modicon com TECHPUBS TECHPUBNEW PI MBUS 300 pdf CMP Books Inc Micrium 1601 W 23rd St Suite 200 949 Crestview Circle Lawrence KS 66046 9950 Weston FL 33327 USA USA 1 785 84
3. 0 57 D respectively uC Modbus 5 02 uC 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 CoilWwr should only be called by UC Modbus Prototype void MB CoilWr CPU INT16U 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 TRUE Or 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 Mo to 1 in your product s MB_CFG H Called By MBS_FCO5_CoilWr and MBS_FC15_CoilWrMultiple IN MBS_CORE DBUS_FC15_EN is set DBUS_FC05_EN is set to 1 or MOI file 58 Example In this example our product has 163 coils uC Modbus 160 coils are placed in a table called AppCoilTbl T
4. Configuring UC 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 Its recommended that you copy the MB_CFG H file that is provided with the uC 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 uC Modbus MODBUS SLAVE EN This define constant specifies whether your product will support Modbus slave or server mode Set this define to 1 to enable SLAVE code You must have purchased the UC Modbus S package in order to set this define to 1 4 02 Configuring UC Modbus MODBUS MASTER EN This define constant specifies whether your product will support Modbus master or client mode Set this define to 1 to enable MASTER code You must have purchased the UC Modbus M package in order to set this define to 1 4 03 Configuring uC Modbus MODBUS ASCII EN This define constant specifies whether your product will support the Modbus ASCII protocol Setting this value to 1 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 ASCII EN to 1 allows the code in MBS ASCI
5. IX10 65535 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 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 70 uC Modbus Returned Value None Notes Warnings Code is enabled when either MODBUS FCO06 EN is set to 1 or MODBUS FC16 EN is set to 1 in your product s MB CFG H file Called By MBS_FC06_HoldingRegWr and MBS FC16 HoldingRegWr IN MBS 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 bi
6. InRegRdFP CPU_INT16U reg Arguments reg perr Returned Value MB InRegRdFP CPU INT16U perr Is the desired input register to read and can be a number between MODBUS 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_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 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 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_FC04_EN is set to 1 and MODBUS FP ENis set to 1 in your product s MB_CFG H file 64 Called By MBS_FC04_InRegRd IN MBS_CORE C Example In this example our product has 4 floating point variables that we want to assign to input registers
7. This pointer is returned to your application when you call MB_CfgCh is the physical port number associated with the pWC Modbus communication channel For example uC 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 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 79 uC 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 SR_x_Handler where the x would write a function called MBS CommRxTx indicates the p
8. 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 uC Modbus MB Init 1000 Initialize uC Modbus RTU timer at 1000 Hz MB CfgCh Ly 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 Parity NONE ODD or _EVEN ioe 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 ty Number of stop bits 1 or 2 ODBUS_WR_EN Enable _EN or disable _DIS writes IMPORTANT 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 t
9. 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 accordingly MBS_FC _ The response is placed in the TxFrameData buffer in binary format 93 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 doin
10. 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 48 uC Modbus 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 in MB CfgCh void MB WrEnSet MODBUS CH pch CPU INTOSQU 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 49 uC Modbus 4 00
11. 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 99 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 IN MB C MBM RxReply MBM CORE C MBM RxReply determines whether the channel is set for ASCII or RTU and calls MB ASCII Rx Of MB RTU Rx to receive the packet MB RTU Rx MB C MB RTU Rx determines if the packet received contains the proper format 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 100 uC Modbus 9 00 Acronyms Abbreviations and Mnemonics uC Modbus includes a number of acronyms abbreviations and mnemonics and some are listed in Table 9 1
12. 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 uC 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 1 Your Application MB CFG H 2 Data Access CPU Interface MB DATA C CPU uC Modbus MB C and MB H MB DEF H MB UTIL C MBS CORE C MBM CORE C MBS ASCII C MBS RTU C mn F Py m F py UART Driver MB BSP C UART RS 232C or RS 485 Figure 1 5 Relationship between modules uC Modbus F1 5 5 F1 5 6 uC Modbus assumes the presence of an RTOS Real Time Operating System However you can use just about any RTOS and the RTOS specifics are actually isolated in a file called MB OS C The code for uC OS II is provided so you don t have to change this code if you use UC OS I 1 in your product uC Modbus is independent of the CPU and the com
13. contains the Modbus slave implementation of UC Modbus and UC Modbus M indicates that your product contains the Modbus master implementation of uC Modbus You should note that a product can contain both uC Modbus S and uC Modbus M at the same time However the master and the slave would be on separate ports uC 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 uUC Modbus S Running uC Modbus S Running uC 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 uC Modbus M You will note that only one master can be present on each RS 485 network Modbus Master Your Product Running UC Modbus M RS 485 Bus Your Product Your Product Running uC Modbus S Running uC 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 each master needs to have a direct connection to each slave UC Modbus supports this topology since each product can have multiple communication
14. 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 66 uC Modbus Notes Warnings Code is enabled when MODBUS FC03 EN is set to 1 in your product s MB CFG H file Called By MBS_FC03_HoldingRegRd IN MBS 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 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 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 7 return val case 1002 CPU CRITICAL ENTER val CPU INT16U AppPres CPU CRITICAL EXIT return val case 1003 CPU CRITICAL ENTER val AppRxPktCtr CPU CRITICAL EXIT return val default perr MODBUS ERR RANGE retur
15. numbers MO MODBUS_FP_START_ IX 2 and Mol IX 0 uC Modbus Your systems Engineer decided to assign MODBUS input register DBUS FP START MODBUS FP START IX 1 DBUS 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 MB InRegRdFP CPU_INT16U reg CPU INT16U perr CPU FP32 AppT CPU FP32 AppT CPU FP32 AppP CPU FP32 AppP CPU FP32 CPU FP32 empAir empFuel resAir resFuel val perr MODBUS_ERR_NONE switch reg case MODBUS_FP_START_IX 0 CPU_CRITICA l AppTempAir va CPU_CRITICA return val case MO CP U_CRITICA L_ENTER L_EXIT DBUS_FP_START_IX 1 L_ENTER val AppTempFuel CP return val case MO CPU_CRITICA va CP return val case MO CPU_CRITICA val AppPresFuel CPU_CRITICA return val U_CRITICA U_CRITICA default perr MODBUS_ERR_RANGE return CPU_FP32 0 L_EXIT DBUS_FP_START_IX 2 L_ENTER l AppPresAir L_EXIT DBUS FP START IX 3 L_ENTER L_EXIT i 65 uC Modbus 5 06 uC Modbus S MB_HoldingRegRd MB HoldingRegRd is called when a Modbus master sends a Function Code 3 command MB HoldingRegRd read the value of a single holding register Integer holding r
16. 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 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 slave_addr specifies the start address of the floating point holding registers that you want to change This can be from 0 to 65535 p_reg_tbl is an array of IEEE 754 floating point values corresponding to the desired values of the holding registers in the slave 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 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 applica
17. slave rx timeout modbus mode port nbr 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 node 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_ASC or MODBUS 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 nu
18. 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 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 to the number of bytes received Notes Warnings None Called By Your Modbus master application Example 37 uC Modbus 3 12 Using uC 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 pch 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 pe
19. the character received needs to be passed to the ASCII or RTU handler If ASCII the character is passed to MB ASCII RxByte 0 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 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 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
20. to receive doesn t correspond to the number of bytes received Notes Warnings MODBUS FP START IX corresponds to that of the slave Called By Your Modbus master application Example 31 uC 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 FC05 CoilWr MODBUS_CH pch Arguments pch slave node slave addr coil val CPU INTO8U slave node CPU INT16U 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 pch 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 Of MODBUS_COIL_ON 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 rece
21. 1 1631 1 954 217 2036 1 785 841 2624 FAX 1 954 217 2037 FAX WEB http www rdbooks com e mail uC Modbus Micrium com e mail rdorders rdbooks com WEB www Micrium com IAR Systems Inc MODICON Inc Century Plaza Industrial Automation Systems 1065 E Hillsdale Blvd One High Street Foster City CA 94404 North Andover Massachusetts 01845 USA USA 1 650 287 4250 1 650 287 4253 FAX WEB http www lAR com e mail info IAR com 103
22. 32 AppCircumference CPU FP32 AppTempDegC Modbus Holding Register MODBUS FP START IX 1 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 FP START IX 0 temp val reg val CPU FP32 3 141592654 Compute circumference EV CPU CRITICAL ENTER AppDiameter reg val AppCircumference temp val CPU CRITICAL EXIT Break case MODBUS FP START IX 1 temp val reg val CPU FP32 1 8 CPU FP32 32 0 C gt F Conversion I CPU CRITICAL ENTER AppTempDegC reg val AppTempDegF temp_val CPU_CRITICAL_EXIT break default perr MODBUS_ERR_RANGE break As shown in the example above computations are performed when a value is changed 73 uC Modbus 5 10 uC Modbus S MB FileRd MB FilerRd 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 uC Modbus Prototype CPU INT16U MB FileRd CPU INT16U file nbr CPU INT16U record nbr CPU INT16U ix CPU INTO08U record len CPU INT16U perr Arguments file nbr Is the n
23. C Modbus MODBUS_FCO01_EN c0ee 52 Configuring UC Modbus MODBUS _FC02_EN ccc0e 52 Configuring uC Modbus MODBUS _FC03_EN 0000 52 Configuring uC Modbus MODBUS_FC04_EN cc0ee 52 Configuring uC Modbus MODBUS FC05 EN cccee 52 Configuring uC Modbus MODBUS _FCO6_EN 00008 52 Configuring uC Modbus MODBUS_FC08_EN c0e 53 Configuring uC Modbus MODBUS_FC15_EN eee 53 Configuring uC Modbus MODBUS_FC16_EN ee 53 Configuring UC Modbus S MODBUS _FC20_EN 008 53 Configuring uC Modbus S MODBUS_FC21_EN 00 53 Configuring uC Modbus RAM Memory Requirements 54 UC Modbus S Accessing application data errrrnnrrnrrnrnnnn 55 CIM GDS SMB GARA Luunmummmunmmemnmmdnnmdenendendiv 56 uC Modbus S TE MN Aenean nee anaes 58 uC Modbus S MB_DIRd cisiectccstvcnicanhiastiunetinsercutcankocaiseasinctientis 60 uC Modbus S MB_InRegRd wcccsecccesccesccncstesiecnstatcrecstascnecetonsee 62 uC Modbus S ME INREPFOAPP iiicssisscvessescsancvesiseacsuiaenwrercve 64 uC Modbus S MB_HoldingRegRd cceccssceessseeeseeeeees 66 uC Modbus S MB_HoldingRegRdFP csssccccssseessseeeees 68 uC Modbus S MB_HoldingRegwr sccccsseeeessseeeseeeees 70 uC Modbus S MB HoldingRegWrFP cccceeeseeeeees 72 CMOS SME FER uaseseimanuumeinmienvvj
24. I C to be compiled In other words setting MODBUS_ASCII_EN to 1 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 uC Modbus 4 04 Configuring UC Modbus MODBUS RTU EN This define constant specifies whether your product will support the MODBUS RTU protocol Setting this value to 1 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 mode at run time Setting MODBUS RTU EN to 1 allows the code in MBS RTU C to be compiled In other words setting MODBUS RTU EN to 1 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 UC Modbus MODBUS MAX CH uC 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 MAX CHO 1 4 06 Configuring UC Modbus MODBUS BUF SIZE MODBUS protocol packets can contain up to 256 bytes of data To hold this data each uC 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 recom
25. Micrium Copyright 2006 Micrium All Rights reserved uC Modbus V2 10 uC Modbus S and uC Modbus M User s Manual www Micrium com uC Modbus Table of Contents Revision eg seisein iaeei ea 5 1 00 MOT 6 1 01 MONS EE 8 1 02 uC Modbus Architecture eossnnnnoeesnnteoeernneertrnnnernnnneernnnneenne 10 2 00 Diretores and FES ve 13 2 01 Directories and Files Target Independent Source Code 13 2 02 Directories and Files RTOS Interface mrrrrnnnnnnnnnnnnnnnnnnnnnnnnnn 13 2 03 Directories and Files Product Specific Files cceeeeeeeeeees 14 2 04 Directories and Files CPU and Compiler Specific Files 14 3 00 Using PE MODUS iicisissisnsutenavensamnatusesennbavstneidadivedulanetavsdabanedabedsas 16 3 01 Using C Modbus MB OIgtuummneraennmmansienssn died 18 3 02 Using uC Modbus MB_ChToPortMap ccceeseeeeeeeeees 20 3 03 Using uC Modbus MB_EXit cccceeceeeeeceeeeeeeeeeeeeeeseneeeeeeees 21 3 04 Using uC Modbus M MBM FC01 CoilRd rrernnrnnrnnennnnnnn 22 3 05 Using uC Modbus M MBM_FC02_DIRd cceeeeeeeeeeees 24 3 06 Using uC Modbus M MBM FC03 HoldingRegR 26 3 07 Using uC Modbus M MBM_FC03_HoldingRegRdFP 28 3 08 Using uC Modbus M MBM FC04 InRegRd rrrrrrnerrennnn 30 3 09 Using uC Modbus M MBM_FCO5_CoilWr eeeeeeeeeeees 32 3 10 Using uC Modbus M MBM FC06 Hol
26. PU SR Save and CPU SR Restore directly in C and call this file cpu c instead of cpu a asm 15 uC Modbus 3 00 Using UC Modbus In order to use UC Modbus in your product you need to make sure you have the following elements Setup the uC 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 uC Modbus includes a mb bsp c file for the Sharp LH79520 ARM7 running on a LogicPD Card Engine and an mb bsp c for the Philips LPC2000 family of microcontrollers You can use these files as examples on how to write the BSP Setup the RTOS Interface for the RTOS YOU are using uC Modbus includes an RTOS interface for UC OS II see section 6 If you are using a different RTOS you will need to provide an mb_os c file You can actually model your RTOS interface from the one provided for UC OS II For UC OS II 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 Initialize UC Modbus and configure your channels uC 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
27. R AppTempAir L_EXIT i case MODBUS FP START IX 1 case case CPU CRITICA L ENTER val AppTempFuel CPU CRITICA return val CPU CRITICA val CPU CRITICA return val CPU CRITICA L EXIT i MODBUS_FP_START_IX 2 L ENTER AppPresAir L_EXIT MODBUS_FP_START_IX 3 L_ENTER val AppPresFuel CPU_CRITICA return val default perr MODBUS_ERR_RANGE return L_EXIT i CPU FP32 0 69 uC Modbus 5 08 UC 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 single holding register value Integer holding registers are numbered from 0 through MODBUS FP START IX 1 MODBUS 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 uC Modbus Prototype void MB HoldingRegWr CPU_INT16U reg Arguments reg reg_val perr CPU_INT16U reg_val CPU INT16U perr Is the desired holding register to read and can be a number between 0 and MODBUS 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 FP START
28. S 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 41 uC 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 INTO08U 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 pch 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 c
29. 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 uC 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 RS 232C or RS 485 Running uC 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 uC Modbus can be made to look like having multiple master or slave ports In fact uC Modbus allows you to have a combination of up to 250 master or slave ports from a single target system uC Modbus S indicates that your product
30. 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 FP START IX of the slave if you intend to have floating point registers i e you set MODBUS FP EN to 1 in 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 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 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 FP START IX corresponds to that of the slave Ca
31. _di_tbl 0 8 7 1 p_di_tbl 1 16 15 9 specifies the number of discrete inputs 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 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 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
32. 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 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 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 85 uC Modbus F7 1 5 Wh
33. an 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 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 uC Modbus 3 15 Using UC 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 CPU INTO08U slave node CPU INT16U slave addr CPU FP32 p reg tbl CPU INT16U nbr reg Arguments
34. and the other discrete inputs to 103 through 118 CPU BOOLEAN CPU BOOLEAN CPU BOOLEAN CPU BOOLEAN CPU BOOLEAN AppDITbl 16 AppSwStart AppSwStop AppSwReset MB DIRd CPU INT16U di CPU INT16U perr perr MODBUS ERR NONE switch di case case case case case case case case case case LOO return AppSwStart LO1 return AppSwStop 102 return AppSwReset 103 104 LOS 106 LO7 L108 109 case Live case case il case 11 case il case li case case case AYADUPWNEFO 118 return AppDITbl di 1031 default perr MODBUS ERR RANGE return FALSE 61 uC Modbus 5 04 uC Modbus S MB InRegRd MB InRegrRd 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 FP START IX 1 MODBUS 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 UC Modbus Prototype CPU_INT16U 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 FP START IX 1 depending on your product It is up to you to decide what application variable is assigned to each input register numbe
35. as been transmitted MB_TxByte MB TxByte in turn calls MB CommTx1 which sends a byte to the UART and enables Tx interrupts 96 uC Modbus 8 03 UC 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 Or MB 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 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 slave MB ASCII Tx then calls 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 TxB
36. 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 uC Modbus S Running uC 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 UC 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 uC Modbus 1 02 UC Modbus Architecture Figure 1 5 shows how the UC Modbus communications stack fits in your product and also shows which source files are associated with each layer MB stands for 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 independe
37. dingRegWr 34 3 11 Using uC Modbus M MBM_FC06_HoldingRegWrFP 36 3 12 Using uC Modbus M MBM_FC08_Diag esceceeeeeees 38 3 13 Using uC Modbus M MBM_FC15_CoilWr ceceeceeeeeees 40 3 14 Using uC Modbus M MBM FC16 HoldingRegWrN 42 3 15 Using uC Modbus M MBM FC16 HoldingRegWrNFP 44 3 16 Using PL MOdbus MB JON Lu uuumearsmemnemnmmsnimememsinie 46 3 17 Using C Modbus MB M deSeLuasesaunemnunmsnokssjene 47 3 18 Using uC Modbus S MB_NodeAddrSet 2 cceeeeeeeeeees 48 3 19 Using uC Modbus S MB_WrEnSet ccsccccceeeeeeeeeeeeeees 49 4 00 4 01 4 02 4 03 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 05 5 06 5 07 5 08 5 09 5 10 5 11 uC Modbus Configuring UC MOdbUs svciscirccecesssnsercdsvssevedecsdencsenederestndassianeeeite 50 Configuring UC Modbus MODBUS SLAVE EN 006 50 Configuring UC Modbus MODBUS_MASTER_EN 06 50 Configuring UC Modbus MODBUS_ASCII_EN ceeeee 50 Configuring uC Modbus MODBUS_RTU_EN cece 51 Configuring uC Modbus MODBUS_MAX_CH cceeees 51 Configuring uC Modbus MODBUS_BUF_SIZE 0006 51 Configuring uC Modbus MODBUS_FP_EN cssceeeees 51 Configuring uC Modbus MODBUS_FP_START_IX 51 Configuring u
38. e 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 95 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 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 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 h
39. e 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 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 f 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 record nbr ix CPU CRITICAL EXIT return val 75 uC 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 uC Modbus Prototype void MB FileWr CPU INT16U file nbr CPU INT16U record nbr CPU INT16U ix CPU INTO08U record len CPU INT16U val CPU INT16U perr Arguments file nbr Is the number of the desired file record nbr Is the desired record within
40. ed 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 72 uC Modbus Returned Value None Notes Warnings Code is enabled when either MODBUS FCO06 EN is set to 1 or MODBUS FC16 EN is set to 1 in your product s MB CFG H file Called By MBS_FC06_HoldingRegWr and MBS FC16 HoldingRegWr IN MBS 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 FP START IX 0 and MODBUS 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 FP START IX 0 ef CPU FP
41. ef double CPU_FP 64 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 shown below typedef CPU INT32U CPU SR define CPU CRITICAL ENTER fcpu 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 C
42. egisters are numbered from 0 through MODBUS 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 uUC Modbus Prototype CPU_INT16U MB HoldingRegRd CPU INT16U reg CPU INT16U perr Arguments reg Is the desired holding register to read and can be a number between 0 and MODBUS 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 FP START IX10 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 HoldingRegrRd 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
43. en 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 four functions in a file called MB OS C MB OS Init MB OS Exit MB OS RxSignal 86 uC Modbus 7 01 RTOS Interface MB OS Init This function is called by UC 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 IN MB C Example 87 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 I 1 we would simply delete the Modbus Rx Task and the message queue Prototype void MB OS Exit void Arguments None Returned Value None Notes Warnings None Called By MB Exit IN MB C Example 88 uC Modbus 7 03 RTOS Int
44. erface 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 packet Returned Value None Notes Warnings None Called By MB RTU TmrUpdate Of MB ASCII RxByte in MB C Example 89 uC Modbus 7 04 RTOS Interface MB OS RxWiait 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 pch 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
45. 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 90 7 05 RTOS Interface Configuration uC Modbus If you use C OS II you need to configure the following define constants MB OS RX TASK PRIO MB OS RX TASK ID MB OS RX ASK STK SIZE These constants need to be defines in you application Ps uC Modbus 8 00 uC 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 8 1 along with variables used to manage these buffers UART RxCtr RxBufPtr RxFrameNDataByte RxFrameData RxFrameCRC TxFrameNDataByte TxFrame Datal TxFrameCRC TXCEr TxBufByteCtr TxBuff rete UART Figure 8 1 uC Modbus Buffer Management 92 uC Modbus 8 01 uC 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
46. g a twos complement In other words the LRC consist only of the node address function code and data sent to the Modbus masier MB ASCII Tx then calls 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 94 uC Modbus 8 02 uC 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 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
47. g 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 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 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 puceil_tbli fo 8 7 1 p goil thi fis 16 15 9 specifies the number of coils you want to read from the slave 22 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
48. he 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 AppLEl 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 INTO08U 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_ break case 201 AppRunning coil_ break case 202 AppLED coil_ break default perr MODBUS_ERR_RANGE break Val Val D respectively uC Modbus 5 03 uC 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 uC Modbus Prototype CPU BOOLEAN MB DIRd CPU_INT16U diy CPU_INT16U perr Arguments di Is the discrete input number that you want to read and can be a number between 0 and 65535 dependi
49. his 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 FALSE Notes Warnings Code is enabled when MODBUS_FC01_EN is set to 1 in your product s MB_CFG H file Called By MBS FCO1 CoilRd IN MBS_CORE C 56 Example In this example our product has 163 coils AppCoilTbl uC Modbus 160 coils are placed in a table called 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 AppLEI CPU INTO08U AppCoilTb1 20 CPU BOOLEAN AppStatus CPU BOOLEAN AppRunning CPU BOOLEAN AppLED CPU BOOLEAN MB CoilRd CPU INT16U coil CPU INT16U perr CPU INTOS8U ix CPU INTO8U bit nbr perr MODBUS ERR NONE if coil lt 20 sizeof CPU INTO8U 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
50. hysical 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 CPU_INTO8U MODBUS_CH Cy ch pch amp MB_ChTb1 0 0 ch lt MODBUS_MAX_CH pch gt PortNbr port nbr if Rx Interrupt G Read byte from UART Clear Rx Interrupt pch gt RxCtr MB RxByte pch ch Cc Pass if Tx Interrupt pch gt TxCtr MB TxByte pch Clear Tx Interrupt Send break else pcht Clear supious interrupts 80 byte to Modbus to process next byte in respons uC Modbus 6 04 BSP MB CommRxIntEn This function is called by UC 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 uC 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 CommTxl MODBUS_CH pch CPU INTO08U C switch pch gt PortNbr Write byte c to specified UART uC Modbus 6 07 BSP MB CommrT
51. in MB DATA C Specifically functions that YOU provided in this file are called by uUC 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 section 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 MB CoilRd MB CoilWwr MB DIRd MB InRegRd MB InRegRdFP MB HoldingRegRd MB HoldingRegRdFP MB HoldingReguWr MB HoldingRegWrFP MB FileRd MB FileWr 55 uC 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 uC 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 t
52. ived 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 33 uC Modbus 3 10 Using uC Modbus M MBM FCO06 HoldingRegWr This function is called from YOUR application code to write to a single 16 bit holding registers on a Modbus slave Prototype CPU INT16U MBM FC06 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 pch 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 value
53. lled By Your Modbus master application Example 27 uC Modbus 3 07 Using UC 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 FC03 HoldingRegRdFP MODBUS_CH pch Arguments pch slave node start addr p reg tbl nbr regs CPU INTO8U slave node CPU INT16U start addr CPU FP32 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 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 FP START IX to 65535 of the slave and assumes that you enabled floating point support by setting MODBUS_FP_EN to 1 in MB CFG H in the 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 28 uC Modbus Returned Value MODBUS ERR NONE if the call was s
54. mbering 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 18 uC Modbus parity 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 stops 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 wr_en 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 Returned Value The function returns a pointer to the created channel which you can use when calling other functions Notes Warnings None Called By Your Modbus mas
55. mend that you leave MODBUS 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 uC Modbus MODBUS FP EN When set to 1 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 0 4 08 Configuring UC Modbus MODBUS 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 from address or index 0 to MODBUS FP START IX 1 and floating point input registers and holding registers from MODBUS_FP_START_IX to 65535 51 uC Modbus 4 09 Configuring UC Modbus MODBUS FC01 EN When set to 1 this define determines whether UC Modbus will support Coil Read commands Function Code 1 When set to 0 code will not be generated for this command 4 10 Configuring UC Modbus MODBUS FC02 EN When set to 1 this define determines whether UC Modbus will support Discrete Input Read commands Function Code 2 When set to 0 code will not be generated for this command 4 11 Configuring UC Modbus MODBUS FC03 EN When set to 1 this define determines whether UC Modbus will support Holding register Read commands Function Code 3 When set to 0 code will not be generated for this command 4 12 Configuri
56. mines whether UC Modbus will support the File Read command Function Code 20 When set to 0 code will not be generated for this command 4 19 Configuring UC Modbus S MODBUS FC21 EN When set to 1 this define determines whether UC Modbus will support the File Write command Function Code 21 When set to 0 code will not be generated for this command 53 uC Modbus 4 20 Configuring UC Modbus RAM Memory Requirements The amount of RAM required by each UC 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 CPU_INTO8U J 8 1028 4 MODBUS_BUF_SIZE CPU_INT16U 2 13 30 2 MODBUS RTU EN CPU INT32U 4 4 16 CPU INTO8U 4 2 8 Total per uC Modbus channel 1083 see MB_ChSize The global variable MB_TotalRAMSize contains the total amount of RAM in bytes needed by uC 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 54 uC Modbus 5 00 UC Modbus S Accessing application data uC Modbus S accesses your application data via interface functions that are defined
57. n 0 67 uC 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 FP START IX to 65535 or less if you doesn t have a lot of floating point registers MODBUS FP START IX allows you to specify the start of floating point MB_HoldingRegRdFP should only be called by uC Modbus 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 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 FP START IX10 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 a
58. ng UC Modbus MODBUS FC04 EN When set to 1 this define determines whether UC Modbus will support Input register Read commands Function Code 4 When set to 0 code will not be generated for this command 4 13 Configuring UC Modbus MODBUS FC05 EN When set to 1 this define determines whether UC Modbus will support Coil Write commands Function Code 5 When set to 0 code will not be generated for this command 4 14 Configuring UC Modbus MODBUS FC06 EN When set to 1 this define determines whether UC Modbus will support writing to a single Holding Register commands Function Code 6 When set to 0 code will not be generated for this command 52 uC Modbus 4 15 Configuring UC Modbus MODBUS FC08 EN When set to 1 this define determines whether UC Modbus will support diagnostic loopback commands Function Code 8 When set to 0 code will not be generated for this command 4 16 Configuring UC Modbus MODBUS FC15 EN When set to 1 this def ine determines whether UC Modbus will support the Multiple Coil Write command Function Code 15 When set to 0 code will not be generated for this command 4 17 Configuring UC Modbus MODBUS FC16 EN When set to 1 this def ine determines whether UC Modbus will support the Multiple Holding Register Write command Function Code 16 When set to 0 code will not be generated for this command 4 18 Configuring UC Modbus S MODBUS FC20 EN When set to 1 this define deter
59. ng UC Modbus if you are also using UC Modbus in your product or use this interface layer as an example for your own RTOS if you are not using UC OS II Figure 7 1 shows a flow diagram of receive model RTU Ti ae gt 0 i 4 OE EE ee eee TLE Pend Modbus Rx Task 1 UART Rx MB_OS_RxSignal pch ISR UART Rx Figure 7 1 uC Modbus Rx Tasking Model F7 1 1 uUC Modbus uses a single task to receive packets from any channel The Modbus Rx Task simply waits for a Modbus packet from any channel A UC OS Il message queue is used for this purpose 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 84 F7 1 2 F7 1 3 F7 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
60. ng on your product Itis 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 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 FC02 EN is set to 1 in your product s MB_CFG H file Called By MBS FC02 DIRA in MBS CORE C 60 Example uC Modbus 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 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
61. nnnnnn 101 Be 271 EE EE EE 103 SE EE 103 COMAIS EE EE ne et eee 103 uC Modbus Revision History Version Date Description V1 00 2004 09 08 Initial Release V1 61 2005 07 29 Converted code to use uC CPU files Removed dependencies on stdlib functions Improved the 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 uC Modbus 1 00 Introduction This document describes uC Modbus which implements the Modicon Modbus Protocol referred to as Modbus along with the Daniels 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 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
62. 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 39 uC 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 INTI6U MBM FC15 CoilWr MODBUS CH pch Arguments pch slave node slave addr p coil tbl nbr coils CPU INTO8U slave node CPU INT16U slave 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 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 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 peoill tbig 8 7 1 pPovil tbl fi 16 15 9 specifies the number of coils you are changing Of course the array pointed to by p_coil_tb1 must contain the corresponding number of entries 40 uC Modbus Returned Value MODBU
63. nt 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 UC 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 uC 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 MD 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 surface 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
64. o add any additional code in your application tasks 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 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 Once a uC 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 Your application interfaces to uC 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 UC Modbus S applies only to Slave channels and uC Modbus M applies only to Master channels 17 uC Modbus 3 01 Using UC Modbus MB CfgCh This function is used to configure each Modbus channel in your product MB_CfgCh MUST be called AFTER calling MB_Tnit The function prototype is Prototype MODBUS CH MB CfgCh Arguments node addr master
65. opies 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 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_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
66. our 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_CRITICAL_ENTER val CPU_INT16U AppTemp CPU_CRITICAL_EXIT return val case 1001 CPU CRITICAL ENTER val AppCtr CPU CRITICAL EXIT 7 return val case 1002 CPU CRITICAL ENTER val CPU INT16U AppPres CPU CRITICAL EXIT return val case 1003 CPU CRITICAL ENTER val AppRxPktCtr CPU CRITICAL EXIT return val default perr MODBUS ERR RANGE return 0 63 uC Modbus 5 05 UC Modbus S MB InRegRadFP MB InRegRdFP is called when a Modbus master sends a Function Code 4 command MB 1 nRegRdFP 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 MO DBUS FP START IX to 65535 or less if you don t have a lot of floating point registers MODBUS FP START IX allows you to specify the start of floating point Prototype MB_ InRegRdFP should only be called by uC Modbus CPU_FP32 MB
67. piler 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 UC 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 uC Modbus 2 00 Directories and Files The code for uC 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 MB H MB DEF H MB UTIL C MBS CORE C Slave specific code MBM CORE C Master specific code 2 02 Directories and Files RTOS Interface Micrium Software uC Modbus 0S uCOS II This directory contains the code to interface to the UC OS I RTOS and contains the following file MB_OS C See Section 7 00 If you interface UC Modbus to a different RTOS you would place an MB OS C file in a separate directory In other words all RTOS interface file
68. r Note that if your product doesn t have any floating point registers but a large number of input registers you can set MODBUS FP START IX10 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 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 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 62 uC Modbus Notes Warnings Code is enabled when MODBUS FC04 EN is set to 1 in your product s MB CFG H file Called By MBS_FC04_InRegRd IN MBS 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 f
69. rforma a diagnostic function to This can be a number between 1 and 255 but needs to match the number assigned to the slave node fnet 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 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 38 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 fnet corresponds to a sub function argument for the function At this time UC 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
70. rgument 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 68 Notes Warnings Code is enabled when both mo 1 in your product s MB_CFG H file uC Modbus DBUS FC03 ENis set to 1 and MODBUS FP ENi is set to Holding registers and input registers are completely different and can be assigned to different variables Called By MBS FC03 HoldingRegRd IN MBS 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 MO DBUS FP START IX 0 MO DBUS FP START MO DBUS FP START IX 1 IX 2 and MODBUS 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 FP32 val CPU INT16U reg CPU INT16U err perr MODBUS ERR NONE switch reg case MODBUS FP START IX 0 CPU CRITICA val CPU CRITICA return val L ENTE
71. s and doesn t really care about the sign 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 Notes Warnings None Called By Your Modbus master application Example 35 uC Modbus 3 11 Using uC Modbus M MBM FCO6 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
72. s 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 uC Modbus 2 03 Directories and Files Product Specific Files YVProduct 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 existing files than create new ones from scratch so that you don t forget anything An example of these files is provided with uC Modbus cpu h This file defines CPU compiler specific data types The code below shows an example of the data types needed by UC 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_INTO8S typedef unsigned short CPU INT16U typedef signed short CPU INT16S typedef unsigned int CPU_INT32U typedef signed int CPU_INT32S typedef float CPU_FP32 typed
73. t values are not atomic CPU INT16U AppCtrl 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 71 uC Modbus 5 09 UC 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 numbered from MODBUS_FP_START_1IX to 65535 In other words MODBUS FP START IX allows you to specify the start of floating point holding register addresses MB_HoldingRegWrFP should only be called by uC 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 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_FP_START_IX to 65535 reg_val Is the desir
74. ter 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 98 uC Modbus 8 04 uC Modbus M RTU 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 Or MB 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 c
75. ter or slave application Example 19 uC 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 UC 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 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 Returned Value None Notes Warnings None Called By Your Modbus master or slave application Example 20 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 21 uC Modbus 3 04 Usin
76. 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 in MB 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 or MBS RTU Task MBS CORE C respectively to do the actual processing of the message received MBS RTU 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_RTU_Task calls MB_RTU_Rx MB C which copies the frame received from the RxBuf tothe RxFrameData buffer wu 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 th
77. 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 val Is the value to write to the file 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 specified ix is not a valid index into the specified record 76 uC Modbus Returned Value None Notes Warnings Code is enabled when MODBUS FC21 EN is set to 1 in your product s MB CFG H file Called By MBS_FC21_FileWr IN MBS 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 INTO08U record len CPU INT16U val CPU INT16U perr perr MODBUS ERR NONE if file nbr gt APP MAX FILES perr MODBUS ERR FILE return
78. tion Example 45 uC Modbus 3 16 Using UC Modbus MB Init As mentioned in the previous section MB Init needs to be called to initialize uC Modbus When called m B 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 Example 46 uC 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 channels mode would have been set in MB 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 47 uC Modbus 3 18 Using UC Modbus S MB NodeAddrSet This function allows you to change the node
79. 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 23 uC Modbus 3 05 Using uC Modbus M MBM FC02 DIRd This function is called from YOUR application code to read discrete inputs from a Modbus slave Prototype CPU INT16U MBM FC02 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 pch 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 MSB LSB B7 B6 BS B4 B3 B2 B1 BO p
80. uccessful 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 corresponds 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 FP START IX corresponds to that of the slave Called By Your Modbus master application Example 29 uC Modbus 3 08 Using uC Modbus M MBM FC04 InRegRa This function is called from YOUR application code to read 16 bit input registers registers from a Modbus slave Prototype CPU_INT16U MBM FC04 InRegRd MODBUS CH pch Arguments pch slave node start addr p reg tbl nbr regs CPU INTO08U 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 pch m
81. uf 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 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 97 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 mas
82. umber 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 specified ix is not a valid index into the specified record 74 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 FC20 EN is set to 1 in your product s MB CFG H file Called By MBS_FC20_FileRd in MBS_CORE C Example In this example we have two files that w
83. une 74 uC Modbus S ME PGW sicccccccrscnsccctaccuciricetecdteaceisdiastensdindhts 76 uC Modbus 7 00 7 05 8 00 8 01 8 02 8 03 8 04 9 00 Board Support Package BSP L uursnmivomimnvieinsnmjnmdndvinektnsen 78 BSP MB AAO EE 78 BSP MB OI OPT acccresicenmenvtenbseudeedeveyieaammuineddceueueyvmerbrens 79 BSP MB CommRxTxISR x Handler cccccceeeeeeeeereeeeeees 80 BSP MB CommPdMlEh LLuuvnmnsmemnsnnmmnmmminstjehesmnime 81 BSP MB_CommRxlntDis 2 s esrmnemnminesinetvoamebinmendnentns dnne 81 BSP EGG ER 81 BSP ME AOI Tx MEM asssresssreenes banene 82 BSP MB CommTXIMDIS neestas asana 82 BSP MB RATU TMEINI ERE 83 BSP MB RUTE 1 seoce eserita a ia 83 BSP MB_RTU_TmrlSR_Handler cccceeeeeeeeeeeeeeeeeeeeees 83 ee EEE 84 RTOS Interface MB OS MAN Lumsesemeneirrvdesasenbannseskaes ne 87 RTOS Interface MB_OS_Exit uasassrsrdeedeedhekteg 88 RTOS Interface MB OS RxSignal mrnnnnnnnnnnnvnnnnnnnnnnnnnnnnnnn 89 RTOS Interface MB OS FRxWaltQ mesemiminsnissosetnseenseendskannke 90 RTOS Interface CGomfiguahlbn u uuun miin um 91 uC Modbus Program FOM svu sssv4srsonsseensegesnGnGee 92 UC Modbus S ASCII Rx and Tr em sesusissssmeeinsseusissssresn 93 uC Modbus S RTU Ae BAG Tunes 95 UC Modbus M ASCII Rx and Tx rnnnnnnrnnrrnennnrnnrrnnnnnrnnrnnennernnn 97 uC Modbus M RTU Rx and TX sureeesereimsunuseinninvvee 99 Acronyms Abbreviations and Mnemonics rrrrrvvvvvvrvvvvrvnnn
84. ust 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 FP START IX of the slave if you intend to have floating point registers i e you set MODBUS_FP_EN to 1 in 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 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
85. xIntEn 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 CommrTxIntDis 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 uC 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 stop 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 uC Modbus 7 00 RTOS Interface uC Modbus assumes the presence of an RTOS but UC Modbus 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 uC Modbus is provided with a UC Modbus RTOS interface layer so you can start usi

Download Pdf Manuals

image

Related Search

Related Contents

Guide technique des produits distribués par le SEA EDITION 2014  ControlGuard Endpoint Access Manager v3.1 Installation  Samsung WF8714ADV Manuel de l'utilisateur  RETINOL C ANTIWRINKLE  Tyco MX4428 Smoke Alarm User Manual  Samsung ES73 User Manual  Life Fitness LS-9100C User's Manual  Pioneer VSX-LX70 User's Manual  

Copyright © All rights reserved.
Failed to retrieve file