FreeModbus的具体介绍就不提了。至于为什么要移植,大概就是因为移植比较快,而且比较稳定,可以减少因为自己编写出现的漏洞。
但是FreeModbus 1.5版本是没有主机的,因此移植的时候只可以做从机。网上有几个关于Modbus主机的源代码,回头等我弄好了再更新。
==================================
理论上来说,此处我移植了全部,但是只调试了RTU部分,因此其他部分不做赘述。
移植过程:
1.将modbus目录下所有文件拷贝加入工程。
2.对modbus中的include下的mbconfig.h进行编辑,裁剪其中需要的模块。(此处我没有进行裁剪,因此选项都是默认)
3.将demo中的合适的port文件夹下的文件加入工程。
4.修改port文件夹下的代码,移植UART驱动。
5.使用modbus poll调试。
因为没有配置mbconfig.h,因此直接从port的移植开始说。
port.c
#include "stm32f10x.h"
void EnterCriticalSection( )
{
__set_PRIMASK(1);
}
void ExitCriticalSection( )
{
__set_PRIMASK(0);
}
port.c中的两个函数作用是开关总中断并保存。
port.h
#ifndef _PORT_H
#define _PORT_H
#include "stm32f10x.h"
#include <assert.h>
#include <inttypes.h>
#define INLINE
#define PR_BEGIN_EXTERN_C extern "C" {
#define PR_END_EXTERN_C }
#define ENTER_CRITICAL_SECTION( ) EnterCriticalSection( )
#define EXIT_CRITICAL_SECTION( ) ExitCriticalSection( )
void EnterCriticalSection( void );
void ExitCriticalSection( void );
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif
typedef u8 UCHAR;
typedef u16 USHORT;
typedef u8 BOOL;
typedef u32 ULONG;
typedef char CHAR;
typedef long LONG;
typedef short SHORT;
typedef int INT;
#endif
port.h只要是针对一些类型的跨平台支持。
portevent.c
#include "mb.h"
#include "mbport.h"
/* ----------------------- Variables ----------------------------------------*/
static eMBEventType eQ