Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 #include "mbed.h" 00002 00003 /* ----------------------- Modbus includes ----------------------------------*/ 00004 #include "mb.h" 00005 #include "mbport.h" 00006 00007 /* ----------------------- Defines ------------------------------------------*/ 00008 #define REG_INPUT_START 1000 00009 #define REG_INPUT_NREGS 4 00010 #define SLAVE_ID 0x0A 00011 00012 //------------------------------------ 00013 // Hyperterminal configuration 00014 // 9600 bauds, 8-bit data, no parity 00015 //------------------------------------ 00016 00017 Serial pc_port(SERIAL_TX, SERIAL_RX); 00018 00019 DigitalOut myled(LED1); 00020 /* 00021 int main() 00022 { 00023 int i = 1; 00024 pc_port.printf("Hello World !\n"); 00025 while(1) { 00026 wait(1); 00027 pc_port.printf("This program runs since %d seconds.\n", i++); 00028 myled = !myled; 00029 } 00030 } 00031 */ 00032 00033 /* ----------------------- Static variables ---------------------------------*/ 00034 static USHORT usRegInputStart = REG_INPUT_START; 00035 static USHORT usRegInputBuf[REG_INPUT_NREGS]; 00036 00037 /* ----------------------- Start implementation -----------------------------*/ 00038 int 00039 main( void ) 00040 { 00041 eMBErrorCode eStatus; 00042 00043 eStatus = eMBInit( MB_RTU, SLAVE_ID, 0, 9600, MB_PAR_NONE ); 00044 00045 /* Enable the Modbus Protocol Stack. */ 00046 eStatus = eMBEnable( ); 00047 00048 // Initialise some registers 00049 usRegInputBuf[1] = 0x1234; 00050 usRegInputBuf[2] = 0x5678; 00051 usRegInputBuf[3] = 0x9abc; 00052 00053 for( ;; ) 00054 { 00055 ( void )eMBPoll( ); 00056 00057 /* Here we simply count the number of poll cycles. */ 00058 usRegInputBuf[0]++; 00059 } 00060 } 00061 00062 eMBErrorCode 00063 eMBRegInputCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs ) 00064 { 00065 eMBErrorCode eStatus = MB_ENOERR; 00066 int iRegIndex; 00067 00068 if( ( usAddress >= REG_INPUT_START ) 00069 && ( usAddress + usNRegs <= REG_INPUT_START + REG_INPUT_NREGS ) ) 00070 { 00071 iRegIndex = ( int )( usAddress - usRegInputStart ); 00072 while( usNRegs > 0 ) 00073 { 00074 *pucRegBuffer++ = 00075 ( unsigned char )( usRegInputBuf[iRegIndex] >> 8 ); 00076 *pucRegBuffer++ = 00077 ( unsigned char )( usRegInputBuf[iRegIndex] & 0xFF ); 00078 iRegIndex++; 00079 usNRegs--; 00080 } 00081 } 00082 else 00083 { 00084 eStatus = MB_ENOREG; 00085 } 00086 00087 return eStatus; 00088 } 00089 00090 eMBErrorCode 00091 eMBRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs, eMBRegisterMode eMode ) 00092 { 00093 eMBErrorCode eStatus = MB_ENOERR; 00094 int iRegIndex; 00095 00096 if (eMode == MB_REG_READ) 00097 { 00098 if( ( usAddress >= REG_INPUT_START ) 00099 && ( usAddress + usNRegs <= REG_INPUT_START + REG_INPUT_NREGS ) ) 00100 { 00101 iRegIndex = ( int )( usAddress - usRegInputStart ); 00102 while( usNRegs > 0 ) 00103 { 00104 *pucRegBuffer++ = 00105 ( unsigned char )( usRegInputBuf[iRegIndex] >> 8 ); 00106 *pucRegBuffer++ = 00107 ( unsigned char )( usRegInputBuf[iRegIndex] & 0xFF ); 00108 iRegIndex++; 00109 usNRegs--; 00110 } 00111 } 00112 } 00113 00114 if (eMode == MB_REG_WRITE) 00115 { 00116 if( ( usAddress >= REG_INPUT_START ) 00117 && ( usAddress + usNRegs <= REG_INPUT_START + REG_INPUT_NREGS ) ) 00118 { 00119 iRegIndex = ( int )( usAddress - usRegInputStart ); 00120 while( usNRegs > 0 ) 00121 { 00122 usRegInputBuf[iRegIndex] = ((unsigned int) *pucRegBuffer << 8) | ((unsigned int) *(pucRegBuffer+1)); 00123 pucRegBuffer+=2; 00124 iRegIndex++; 00125 usNRegs--; 00126 } 00127 } 00128 } 00129 00130 return eStatus; 00131 } 00132 00133 00134 eMBErrorCode 00135 eMBRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNCoils, 00136 eMBRegisterMode eMode ) 00137 { 00138 return MB_ENOREG; 00139 } 00140 00141 eMBErrorCode 00142 eMBRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNDiscrete ) 00143 { 00144 return MB_ENOREG; 00145 } 00146
Generated on Mon Jul 18 2022 08:01:55 by
1.7.2