NuMaker Modbus-RS485
Dependencies: Modbus nvt_rs485
Fork of modbus-over-rs485-sample by
main.cpp
00001 /* 00002 * The program is a sample code. 00003 * It needs run with some NuMaker-PFM-NUC472 boards. 00004 * 00005 * Please remeber to modify global definition to enable RS485 port on board. 00006 * Modify '//#define DEF_RS485_PORT 1' to '#define DEF_RS485_PORT 1' 00007 */ 00008 00009 /* ----------------------- System includes --------------------------------*/ 00010 #include "mbed.h" 00011 #include "rtos.h" 00012 /*----------------------- Modbus includes ----------------------------------*/ 00013 #include "mb.h" 00014 #include "mbport.h" 00015 00016 /* ----------------------- Defines ------------------------------------------*/ 00017 // Sharing buffer index 00018 enum { 00019 eData_MBInCounter, 00020 eData_MBOutCounter, 00021 eData_MBError, 00022 eData_DI, 00023 eData_DATA, 00024 eData_Cnt 00025 } E_DATA_TYPE; 00026 00027 #define REG_INPUT_START 1 00028 #define REG_INPUT_NREGS eData_Cnt 00029 /* ----------------------- Static variables ---------------------------------*/ 00030 static USHORT usRegInputStart = REG_INPUT_START; 00031 static USHORT usRegInputBuf[REG_INPUT_NREGS]; 00032 00033 DigitalOut led1(LED1); // For temperature worker. 00034 DigitalOut led2(LED2); // For Modbus worker. 00035 DigitalOut led3(LED3); // For Holder CB 00036 00037 #define DEF_PIN_NUM 6 00038 DigitalIn DipSwitch[DEF_PIN_NUM] = { D2, D3, D4, D5, D6, D7 } ; 00039 00040 unsigned short GetValueOnDipSwitch() 00041 { 00042 int i=0; 00043 unsigned short usDipValue = 0x0; 00044 for ( i=0; i<DEF_PIN_NUM ; i++) 00045 usDipValue |= DipSwitch[i].read() << i; 00046 usDipValue = (~usDipValue) & 0x003F; 00047 return usDipValue; 00048 } 00049 00050 void worker_uart(void const *args) 00051 { 00052 int counter=0; 00053 // For UART-SERIAL Tx/Rx Service. 00054 while (true) 00055 { 00056 //xMBPortSerialPolling(); 00057 if ( counter > 10000 ) 00058 { 00059 led2 = !led2; 00060 counter=0; 00061 } 00062 counter++; 00063 } 00064 } 00065 00066 /* ----------------------- Start implementation -----------------------------*/ 00067 int 00068 main( void ) 00069 { 00070 eMBErrorCode eStatus; 00071 //Thread uart_thread(worker_uart); 00072 unsigned short usSlaveID=GetValueOnDipSwitch(); 00073 #ifdef MBED_MAJOR_VERSION 00074 printf("Mbed OS version %d.%d.%d\r\n\n", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION); 00075 #endif 00076 // Initialise some registers 00077 for (int i=0; i<REG_INPUT_NREGS; i++) 00078 usRegInputBuf[i] = 0x0; 00079 00080 printf("We will set modbus slave ID-%d(0x%x) for the device.\r\n", usSlaveID, usSlaveID ); 00081 00082 /* Enable the Modbus Protocol Stack. */ 00083 if ( (eStatus = eMBInit( MB_RTU, usSlaveID, 0, 115200, MB_PAR_NONE )) != MB_ENOERR ) 00084 goto FAIL_MB; 00085 else if ( (eStatus = eMBEnable( ) ) != MB_ENOERR ) 00086 goto FAIL_MB_1; 00087 else { 00088 for( ;; ) 00089 { 00090 xMBPortSerialPolling(); 00091 if ( eMBPoll( ) != MB_ENOERR ) break; 00092 } 00093 } 00094 00095 FAIL_MB_1: 00096 eMBClose(); 00097 00098 FAIL_MB: 00099 for( ;; ) 00100 { 00101 led2 = !led2; 00102 #if MBED_MAJOR_VERSION >= 6 00103 ThisThread::sleep_for(200); 00104 #else 00105 Thread::wait(200); 00106 #endif 00107 } 00108 } 00109 00110 00111 00112 00113 eMBErrorCode 00114 eMBRegInputCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs ) 00115 { 00116 eMBErrorCode eStatus = MB_ENOERR; 00117 int iRegIndex; 00118 00119 if( ( usAddress >= REG_INPUT_START ) 00120 && ( usAddress + usNRegs <= REG_INPUT_START + REG_INPUT_NREGS ) ) 00121 { 00122 iRegIndex = ( int )( usAddress - usRegInputStart ); 00123 while( usNRegs > 0 ) 00124 { 00125 *pucRegBuffer++ = 00126 ( unsigned char )( usRegInputBuf[iRegIndex] >> 8 ); 00127 *pucRegBuffer++ = 00128 ( unsigned char )( usRegInputBuf[iRegIndex] & 0xFF ); 00129 iRegIndex++; 00130 usNRegs--; 00131 } 00132 } 00133 else 00134 { 00135 eStatus = MB_ENOREG; 00136 } 00137 00138 return eStatus; 00139 } 00140 00141 eMBErrorCode 00142 eMBRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs, eMBRegisterMode eMode ) 00143 { 00144 eMBErrorCode eStatus = MB_ENOERR; 00145 int iRegIndex; 00146 00147 usRegInputBuf[eData_MBInCounter]++; 00148 usRegInputBuf[eData_MBOutCounter]++; 00149 00150 if (eMode == MB_REG_READ) 00151 { 00152 usRegInputBuf[eData_DI] = GetValueOnDipSwitch(); 00153 00154 if( ( usAddress >= REG_INPUT_START ) 00155 && ( usAddress + usNRegs <= REG_INPUT_START + REG_INPUT_NREGS ) ) 00156 { 00157 iRegIndex = ( int )( usAddress - usRegInputStart ); 00158 while( usNRegs > 0 ) 00159 { 00160 *pucRegBuffer++ = 00161 ( unsigned char )( usRegInputBuf[iRegIndex] >> 8 ); 00162 *pucRegBuffer++ = 00163 ( unsigned char )( usRegInputBuf[iRegIndex] & 0xFF ); 00164 iRegIndex++; 00165 usNRegs--; 00166 } 00167 } 00168 } 00169 00170 if (eMode == MB_REG_WRITE) 00171 { 00172 if( ( usAddress >= REG_INPUT_START ) 00173 && ( usAddress + usNRegs <= REG_INPUT_START + REG_INPUT_NREGS ) ) 00174 { 00175 iRegIndex = ( int )( usAddress - usRegInputStart ); 00176 while( usNRegs > 0 ) 00177 { 00178 usRegInputBuf[iRegIndex] = ((unsigned int) *pucRegBuffer << 8) | ((unsigned int) *(pucRegBuffer+1)); 00179 pucRegBuffer+=2; 00180 iRegIndex++; 00181 usNRegs--; 00182 } 00183 } 00184 } 00185 00186 led3=!led3; 00187 00188 return eStatus; 00189 } 00190 00191 00192 eMBErrorCode 00193 eMBRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNCoils, 00194 eMBRegisterMode eMode ) 00195 { 00196 return MB_ENOREG; 00197 } 00198 00199 eMBErrorCode 00200 eMBRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNDiscrete ) 00201 { 00202 return MB_ENOREG; 00203 }
Generated on Wed May 3 2023 05:08:39 by 1.7.2