下面是Snap7移植步骤以及遇到的问题和解决方法:
-
工程项目中新建Snap7文件夹,添加Snap源文件,如图:
-
添加Snap7头文件路径到项目中
注:在MiscControls一栏加上 --exception -
点击运行工程,发现会报很多错误,有很多的数据类型未定义,比如u_short,u_int等,直接在相应位置重定义一下typedef unsigned short u_short。定位到错误位置,sockaddr_in在lwip/sockets.h,所以加上这个头文件
-
将snap_sysutils.cpp改为以下内容:
#include “snap_sysutils.h”
#include “stm32h7xx_hal.h”
#ifdef OS_OSX
int clock_gettime(int clk_id, struct timespec* t)
{
struct timeval now;
int rv = gettimeofday(&now, NULL);
if (rv) return rv;
t->tv_sec = now.tv_sec;
t->tv_nsec = now.tv_usec * 1000;
return 0;
}
#endif
//---------------------------------------------------------------------------
longword SysGetTick()
{
#ifdef OS_WINDOWS
return timeGetTime();
#else
#ifdef STM32H743xx
return HAL_GetTick();
#else
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (longword) (ts.tv_sec * 1000) + (ts.tv_nsec / 1000000);
#endif
#endif
}
//---------------------------------------------------------------------------
void SysSleep(longword Delay_ms)
{
#ifdef OS_WINDOWS
Sleep(Delay_ms);
#else
#ifdef STM32H743xx
HAL_Delay(Delay_ms);
#else
struct timespec ts;
ts.tv_sec = (time_t)(Delay_ms / 1000);
ts.tv_nsec =(long)((Delay_ms - ts.tv_sec) * 1000000);
nanosleep(&ts, (struct timespec *)0);
#endif
#endif
}
longword DeltaTime(longword &Elapsed)
{
longword TheTime;
TheTime=SysGetTick();
// Checks for rollover
if (TheTime<Elapsed)
Elapsed=0;
return TheTime-Elapsed;
}
5.
memcpy、memset、strncpy在string.h这个头文件里
6
这里将uint32_t全部改成u32_t
5.
这些未定义的变量都在snap_msgsock.h,这里需要修改一下预编译指令:
未改前:
改之后:
即将STM32H743平台加入,这样就可以引用下面的宏定义变量了
6.
Ioctl改成lwip_loctl
7.
自己在错误文件处宏定义一下:#define SOMAXCONN 128
10.
#define IPPROTO_ICMP 1
11.最后还有一个错误
在snap_msgsock.cpp加入头文件”errno.h”
之后编译工程,发现错误都消除了。