NuMaker Modbus-RS485

Dependencies:   nvt_rs485 Modbus

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

Committer:
wclin
Date:
Tue Sep 20 06:50:29 2016 +0000
Revision:
0:1e78831318df
Child:
1:f9d748875768
This is sample for NuMaker-PFM-NUC472.

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