NuMaker Modbus-RS485

Dependencies:   nvt_rs485 Modbus

Fork of modbus-over-rs485-sample by Wayne Lin

Committer:
shliu1
Date:
Thu Dec 22 15:10:51 2016 +0800
Revision:
4:f69e05cb6714
Parent:
1:f9d748875768
Child:
9:32ff3d56f070
Support M453, target is numaker_pfm_m453

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wclin 0:1e78831318df 1 /*
wclin 0:1e78831318df 2 * The program is a sample code.
wclin 0:1e78831318df 3 * It needs run with some NuMaker-PFM-NUC472 boards.
wclin 1:f9d748875768 4 *
wclin 1:f9d748875768 5 * Please remeber to modify global definition to enable RS485 port on board.
wclin 1:f9d748875768 6 * Modify '//#define DEF_RS485_PORT 1' to '#define DEF_RS485_PORT 1'
wclin 0:1e78831318df 7 */
wclin 0:1e78831318df 8
wclin 0:1e78831318df 9 /* ----------------------- System includes --------------------------------*/
wclin 0:1e78831318df 10 #include "mbed.h"
wclin 0:1e78831318df 11 #include "rtos.h"
wclin 0:1e78831318df 12 /*----------------------- Modbus includes ----------------------------------*/
wclin 0:1e78831318df 13 #include "mb.h"
wclin 0:1e78831318df 14 #include "mbport.h"
wclin 0:1e78831318df 15
wclin 0:1e78831318df 16 /* ----------------------- Defines ------------------------------------------*/
wclin 0:1e78831318df 17 // Sharing buffer index
wclin 0:1e78831318df 18 enum {
wclin 0:1e78831318df 19 eData_MBInCounter,
wclin 0:1e78831318df 20 eData_MBOutCounter,
wclin 0:1e78831318df 21 eData_MBError,
wclin 0:1e78831318df 22 eData_DI,
wclin 0:1e78831318df 23 eData_DATA,
wclin 0:1e78831318df 24 eData_Cnt
wclin 0:1e78831318df 25 } E_DATA_TYPE;
wclin 0:1e78831318df 26
wclin 0:1e78831318df 27 #define REG_INPUT_START 1
wclin 0:1e78831318df 28 #define REG_INPUT_NREGS eData_Cnt
wclin 0:1e78831318df 29 /* ----------------------- Static variables ---------------------------------*/
wclin 0:1e78831318df 30 static USHORT usRegInputStart = REG_INPUT_START;
wclin 0:1e78831318df 31 static USHORT usRegInputBuf[REG_INPUT_NREGS];
wclin 0:1e78831318df 32
wclin 0:1e78831318df 33 DigitalOut led1(LED1); // For temperature worker.
wclin 0:1e78831318df 34 DigitalOut led2(LED2); // For Modbus worker.
wclin 0:1e78831318df 35 DigitalOut led3(LED3); // For Holder CB
wclin 0:1e78831318df 36
wclin 0:1e78831318df 37 #define DEF_PIN_NUM 6
shliu1 4:f69e05cb6714 38 #if defined(TARGET_NUMAKER_PFM_NUC472)
wclin 0:1e78831318df 39 DigitalIn DipSwitch[DEF_PIN_NUM] = { PG_1, PG_2, PF_9, PF_10, PC_10, PC_11 } ;
shliu1 4:f69e05cb6714 40 #elif defined(TARGET_NUMAKER_PFM_M453)
shliu1 4:f69e05cb6714 41 DigitalIn DipSwitch[DEF_PIN_NUM] = { PD_6, PD_1, PC_6, PC_7, PC_11, PC_12 };
shliu1 4:f69e05cb6714 42 #endif
wclin 0:1e78831318df 43
wclin 0:1e78831318df 44 unsigned short GetValueOnDipSwitch()
wclin 0:1e78831318df 45 {
wclin 0:1e78831318df 46 int i=0;
wclin 0:1e78831318df 47 unsigned short usDipValue = 0x0;
wclin 0:1e78831318df 48 for ( i=0; i<DEF_PIN_NUM ; i++)
wclin 0:1e78831318df 49 usDipValue |= DipSwitch[i].read() << i;
wclin 0:1e78831318df 50 usDipValue = (~usDipValue) & 0x003F;
wclin 0:1e78831318df 51 return usDipValue;
wclin 0:1e78831318df 52 }
wclin 0:1e78831318df 53
wclin 0:1e78831318df 54 void worker_uart(void const *args)
wclin 0:1e78831318df 55 {
wclin 0:1e78831318df 56 int counter=0;
wclin 0:1e78831318df 57 // For UART-SERIAL Tx/Rx Service.
wclin 0:1e78831318df 58 while (true)
wclin 0:1e78831318df 59 {
wclin 0:1e78831318df 60 //xMBPortSerialPolling();
wclin 0:1e78831318df 61 if ( counter > 10000 )
wclin 0:1e78831318df 62 {
wclin 0:1e78831318df 63 led2 = !led2;
wclin 0:1e78831318df 64 counter=0;
wclin 0:1e78831318df 65 }
wclin 0:1e78831318df 66 counter++;
wclin 0:1e78831318df 67 }
wclin 0:1e78831318df 68 }
wclin 0:1e78831318df 69
wclin 0:1e78831318df 70 /* ----------------------- Start implementation -----------------------------*/
wclin 0:1e78831318df 71 int
wclin 0:1e78831318df 72 main( void )
wclin 0:1e78831318df 73 {
wclin 0:1e78831318df 74 eMBErrorCode eStatus;
wclin 0:1e78831318df 75 //Thread uart_thread(worker_uart);
wclin 0:1e78831318df 76 unsigned short usSlaveID=GetValueOnDipSwitch();
wclin 0:1e78831318df 77
wclin 0:1e78831318df 78 // Initialise some registers
wclin 0:1e78831318df 79 for (int i=0; i<REG_INPUT_NREGS; i++)
wclin 0:1e78831318df 80 usRegInputBuf[i] = 0x0;
wclin 0:1e78831318df 81
wclin 0:1e78831318df 82 printf("We will set modbus slave ID-%d(0x%x) for the device.\r\n", usSlaveID, usSlaveID );
wclin 0:1e78831318df 83
wclin 0:1e78831318df 84 /* Enable the Modbus Protocol Stack. */
wclin 0:1e78831318df 85 if ( (eStatus = eMBInit( MB_RTU, usSlaveID, 0, 115200, MB_PAR_NONE )) != MB_ENOERR )
wclin 0:1e78831318df 86 goto FAIL_MB;
wclin 0:1e78831318df 87 else if ( (eStatus = eMBEnable( ) ) != MB_ENOERR )
wclin 0:1e78831318df 88 goto FAIL_MB_1;
wclin 0:1e78831318df 89 else {
wclin 0:1e78831318df 90 for( ;; )
wclin 0:1e78831318df 91 {
wclin 0:1e78831318df 92 xMBPortSerialPolling();
wclin 0:1e78831318df 93 if ( eMBPoll( ) != MB_ENOERR ) break;
wclin 0:1e78831318df 94 }
wclin 0:1e78831318df 95 }
wclin 0:1e78831318df 96
wclin 0:1e78831318df 97 FAIL_MB_1:
wclin 0:1e78831318df 98 eMBClose();
wclin 0:1e78831318df 99
wclin 0:1e78831318df 100 FAIL_MB:
wclin 0:1e78831318df 101 for( ;; )
wclin 0:1e78831318df 102 {
wclin 0:1e78831318df 103 led2 = !led2;
wclin 0:1e78831318df 104 Thread::wait(200);
wclin 0:1e78831318df 105 }
wclin 0:1e78831318df 106 }
wclin 0:1e78831318df 107
wclin 0:1e78831318df 108
wclin 0:1e78831318df 109
wclin 0:1e78831318df 110
wclin 0:1e78831318df 111 eMBErrorCode
wclin 0:1e78831318df 112 eMBRegInputCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs )
wclin 0:1e78831318df 113 {
wclin 0:1e78831318df 114 eMBErrorCode eStatus = MB_ENOERR;
wclin 0:1e78831318df 115 int iRegIndex;
wclin 0:1e78831318df 116
wclin 0:1e78831318df 117 if( ( usAddress >= REG_INPUT_START )
wclin 0:1e78831318df 118 && ( usAddress + usNRegs <= REG_INPUT_START + REG_INPUT_NREGS ) )
wclin 0:1e78831318df 119 {
wclin 0:1e78831318df 120 iRegIndex = ( int )( usAddress - usRegInputStart );
wclin 0:1e78831318df 121 while( usNRegs > 0 )
wclin 0:1e78831318df 122 {
wclin 0:1e78831318df 123 *pucRegBuffer++ =
wclin 0:1e78831318df 124 ( unsigned char )( usRegInputBuf[iRegIndex] >> 8 );
wclin 0:1e78831318df 125 *pucRegBuffer++ =
wclin 0:1e78831318df 126 ( unsigned char )( usRegInputBuf[iRegIndex] & 0xFF );
wclin 0:1e78831318df 127 iRegIndex++;
wclin 0:1e78831318df 128 usNRegs--;
wclin 0:1e78831318df 129 }
wclin 0:1e78831318df 130 }
wclin 0:1e78831318df 131 else
wclin 0:1e78831318df 132 {
wclin 0:1e78831318df 133 eStatus = MB_ENOREG;
wclin 0:1e78831318df 134 }
wclin 0:1e78831318df 135
wclin 0:1e78831318df 136 return eStatus;
wclin 0:1e78831318df 137 }
wclin 0:1e78831318df 138
wclin 0:1e78831318df 139 eMBErrorCode
wclin 0:1e78831318df 140 eMBRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs, eMBRegisterMode eMode )
wclin 0:1e78831318df 141 {
wclin 0:1e78831318df 142 eMBErrorCode eStatus = MB_ENOERR;
wclin 0:1e78831318df 143 int iRegIndex;
wclin 0:1e78831318df 144
wclin 0:1e78831318df 145 usRegInputBuf[eData_MBInCounter]++;
wclin 0:1e78831318df 146 usRegInputBuf[eData_MBOutCounter]++;
wclin 0:1e78831318df 147
wclin 0:1e78831318df 148 if (eMode == MB_REG_READ)
wclin 0:1e78831318df 149 {
wclin 0:1e78831318df 150 usRegInputBuf[eData_DI] = GetValueOnDipSwitch();
wclin 0:1e78831318df 151
wclin 0:1e78831318df 152 if( ( usAddress >= REG_INPUT_START )
wclin 0:1e78831318df 153 && ( usAddress + usNRegs <= REG_INPUT_START + REG_INPUT_NREGS ) )
wclin 0:1e78831318df 154 {
wclin 0:1e78831318df 155 iRegIndex = ( int )( usAddress - usRegInputStart );
wclin 0:1e78831318df 156 while( usNRegs > 0 )
wclin 0:1e78831318df 157 {
wclin 0:1e78831318df 158 *pucRegBuffer++ =
wclin 0:1e78831318df 159 ( unsigned char )( usRegInputBuf[iRegIndex] >> 8 );
wclin 0:1e78831318df 160 *pucRegBuffer++ =
wclin 0:1e78831318df 161 ( unsigned char )( usRegInputBuf[iRegIndex] & 0xFF );
wclin 0:1e78831318df 162 iRegIndex++;
wclin 0:1e78831318df 163 usNRegs--;
wclin 0:1e78831318df 164 }
wclin 0:1e78831318df 165 }
wclin 0:1e78831318df 166 }
wclin 0:1e78831318df 167
wclin 0:1e78831318df 168 if (eMode == MB_REG_WRITE)
wclin 0:1e78831318df 169 {
wclin 0:1e78831318df 170 if( ( usAddress >= REG_INPUT_START )
wclin 0:1e78831318df 171 && ( usAddress + usNRegs <= REG_INPUT_START + REG_INPUT_NREGS ) )
wclin 0:1e78831318df 172 {
wclin 0:1e78831318df 173 iRegIndex = ( int )( usAddress - usRegInputStart );
wclin 0:1e78831318df 174 while( usNRegs > 0 )
wclin 0:1e78831318df 175 {
wclin 0:1e78831318df 176 usRegInputBuf[iRegIndex] = ((unsigned int) *pucRegBuffer << 8) | ((unsigned int) *(pucRegBuffer+1));
wclin 0:1e78831318df 177 pucRegBuffer+=2;
wclin 0:1e78831318df 178 iRegIndex++;
wclin 0:1e78831318df 179 usNRegs--;
wclin 0:1e78831318df 180 }
wclin 0:1e78831318df 181 }
wclin 0:1e78831318df 182 }
wclin 0:1e78831318df 183
wclin 0:1e78831318df 184 led3=!led3;
wclin 0:1e78831318df 185
wclin 0:1e78831318df 186 return eStatus;
wclin 0:1e78831318df 187 }
wclin 0:1e78831318df 188
wclin 0:1e78831318df 189
wclin 0:1e78831318df 190 eMBErrorCode
wclin 0:1e78831318df 191 eMBRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNCoils,
wclin 0:1e78831318df 192 eMBRegisterMode eMode )
wclin 0:1e78831318df 193 {
wclin 0:1e78831318df 194 return MB_ENOREG;
wclin 0:1e78831318df 195 }
wclin 0:1e78831318df 196
wclin 0:1e78831318df 197 eMBErrorCode
wclin 0:1e78831318df 198 eMBRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNDiscrete )
wclin 0:1e78831318df 199 {
wclin 0:1e78831318df 200 return MB_ENOREG;
wclin 0:1e78831318df 201 }