📄 demo.c
字号:
/* * FreeModbus Libary: STR71x Demo Application * Copyright (C) 2006 Christian Walter <wolti@sil.at> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * * File: $Id: demo.c,v 1.1 2006/11/02 23:14:43 wolti Exp $ *//* ----------------------- System includes ----------------------------------*/#include <assert.h>/* ----------------------- Platform includes --------------------------------*/#include "FreeRTOS.h"#include "task.h"#include "queue.h"/* ----------------------- STR71X includes ----------------------------------*/#include "rccu.h"#include "eic.h"/* ----------------------- Modbus includes ----------------------------------*/#include "mb.h"/* ----------------------- Defines ------------------------------------------*/#define REG_INPUT_START 1000#define REG_INPUT_NREGS 4/* ----------------------- Static variables ---------------------------------*/static unsigned short usRegInputStart = REG_INPUT_START;static unsigned short usRegInputBuf[REG_INPUT_NREGS];/* ----------------------- Static functions ---------------------------------*/static void vInitTask( void *pvParameters );static void vMeasureTask( void *pvParameters );/* ----------------------- Start implementation -----------------------------*/intmain( void ){ RCCU_Div2Config( ENABLE ); RCCU_PLL1Config( RCCU_PLL1_Mul_16, RCCU_Div_1 ); RCCU_RCLKSourceConfig( RCCU_PLL1_Output ); RCCU_MCLKConfig( RCCU_DEFAULT ); RCCU_PCLKConfig( RCCU_RCLK_2 ); RCCU_FCLKConfig( RCCU_RCLK_2 ); EIC_Init( ); EIC_IRQConfig( ENABLE ); ( void )xTaskCreate( vInitTask, NULL, configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL ); vTaskStartScheduler( ); return 0;}static voidvInitTask( void *pvParameters ){ const unsigned char ucSlaveIDAdditonal[] = { 0xAA, 0xBB, 0xCC }; eMBErrorCode eStatus; /* Select either ASCII or RTU Mode. */ eStatus = eMBInit( MB_RTU, 0x0A, 0, 38400, MB_PAR_EVEN ); assert( eStatus == MB_ENOERR ); /* Configure the slave id of the device. */ eStatus = eMBSetSlaveID( 44, TRUE, ucSlaveIDAdditonal, 3 ); assert( eStatus == MB_ENOERR ); /* Enable the Modbus Protocol Stack. */ eStatus = eMBEnable( ); for( ;; ) { /* Call the main polling loop of the Modbus protocol stack. Internally * the polling loop waits for a new event by calling the port * dependent function xMBPortEventGet( ). In the FreeRTOS port the * event layer is built with queues. */ ( void )eMBPoll( ); /* Here we simply count the number of poll cycles. */ usRegInputBuf[0]++; }}eMBErrorCodeeMBRegInputCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs ){ eMBErrorCode eStatus = MB_ENOERR; int iRegIndex; if( ( usAddress >= REG_INPUT_START ) && ( usAddress + usNRegs <= REG_INPUT_START + REG_INPUT_NREGS ) ) { iRegIndex = ( int )( usAddress - usRegInputStart ); while( usNRegs > 0 ) { *pucRegBuffer++ = ( unsigned char )( usRegInputBuf[iRegIndex] >> 8 ); *pucRegBuffer++ = ( unsigned char )( usRegInputBuf[iRegIndex] & 0xFF ); iRegIndex++; usNRegs--; } } else { eStatus = MB_ENOREG; } return eStatus;}eMBErrorCodeeMBRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs, eMBRegisterMode eMode ){ return MB_ENOREG;}eMBErrorCodeeMBRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNCoils, eMBRegisterMode eMode ){ return MB_ENOREG;}eMBErrorCodeeMBRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNDiscrete ){ return MB_ENOREG;}void__assert( const char *pcFile, int iLineNumber, const char *pcLine ){ portENTER_CRITICAL( ); for( ;; );}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -