基于Windows的SOEM上位机编程步骤

一、检测SOEM运行环境

1、准备事项

  • 从 http://openethercatsociety.github.io/ 下载SOEM 1.3.1 源代码
  • 下载wincap https://www.winpcap.org/archive/4.1beta3-WinPcap.exe 并安装
  • VS2015(个人使用VS2015,其他类似)

2、编译SOEM库

2.1 打开VS2015 x64 x86兼容工具命令提示符

2.2 命令行转到SOEM-1.3.1目录下

cd E:\EtherCAT\上位机\SOEM-1.3.1\SOEM-1.3.1

2.3 编译生成lib库

make_libsoem_lib.bat "D:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE" x86

其中“D:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE" 为VS目录

上述指令生成lib文件

过程指令如下:

E:\EtherCAT\上位机\SOEM-1.3.1\SOEM-1.3.1>make_libsoem_lib.bat "D:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE" x86E:\EtherCAT\上位机\SOEM-1.3.1\SOEM-1.3.1>REM @echo offE:\EtherCAT\上位机\SOEM-1.3.1\SOEM-1.3.1>rem give path as argE:\EtherCAT\上位机\SOEM-1.3.1\SOEM-1.3.1>if "D:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE" == "" goto exit_err_argE:\EtherCAT\上位机\SOEM-1.3.1\SOEM-1.3.1>if NOT EXIST "D:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE" goto exit_err_argE:\EtherCAT\上位机\SOEM-1.3.1\SOEM-1.3.1>rem call cvarsall to load the envE:\EtherCAT\上位机\SOEM-1.3.1\SOEM-1.3.1>call "D:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\vcvarsall.bat" x86'"D:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\vcvarsall.bat"' 不是内部或外部命令,也不是可运行的程序或批处理文件。E:\EtherCAT\上位机\SOEM-1.3.1\SOEM-1.3.1>rem cd to folder containing .bat fileE:\EtherCAT\上位机\SOEM-1.3.1\SOEM-1.3.1>cd /d "make_libsoem_lib.bat\.."E:\EtherCAT\上位机\SOEM-1.3.1\SOEM-1.3.1>rem compile and build libraryE:\EtherCAT\上位机\SOEM-1.3.1\SOEM-1.3.1>if EXIST obj goto skip_objE:\EtherCAT\上位机\SOEM-1.3.1\SOEM-1.3.1>MKDIR objE:\EtherCAT\上位机\SOEM-1.3.1\SOEM-1.3.1>cl.exe @make\cl_libsoem.rsp /errorReport:prompt用于 x86 的 Microsoft (R) C/C++ 优化编译器 19.00.24210 版版权所有(C) Microsoft Corporation。保留所有权利。cl /c /Zi /W3 /WX- /Od /Oy- /D "_CRT_SECURE_NO_WARNINGS" /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /Gm /EHsc /RTC1 /MTd /GS /fp:precise /Zc:wchar_t /Zc:forScope /Fo"obj\\" /Fd"obj\vc100.pdb" /Gd /TC /analyze-  /Odi /I "oshw\win32\wpcap\include" /I "osal" /I "osal\win32" /I "oshw\win32" /I "soem"   oshw\win32\nicdrv.c   oshw\win32\oshw.c   soem\ethercatbase.c   soem\ethercatcoe.c   soem\ethercatconfig.c   soem\ethercatdc.c   soem\ethercatfoe.c   soem\ethercatmain.c   soem\ethercatprint.c   soem\ethercatsoe.c   osal\win32\osal.c    ......nicdrv.cE:\EtherCAT\上位机\SOEM-1.3.1\SOEM-1.3.1\soem\ethercattype.h: warning C4819: 该文件包含不能在当前代码页(936)中表示的字符。请将该文件保存为 Unicode 格式以防止数据丢失E:\EtherCAT\上位机\SOEM-1.3.1\SOEM-1.3.1\osal\win32\osal_defs.h: warning C4819: 该文件包含不能在当前代码页(936)中表示的 字符。请将该文件保存为 Unicode 格式以防止数据丢失e:\ethercat\上位机\soem-1.3.1\soem-1.3.1\oshw\win32\nicdrv.h: warning C4819: 该文件包含不能在当前代码页(936)中表示的字符。请将该文件保存为 Unicode 格式以防止数据丢失E:\EtherCAT\上位机\SOEM-1.3.1\SOEM-1.3.1\osal\win32\osal_win32.h: warning C4819: 该文件包含不能在当前代码页(936)中表示的字符。请将该文件保存为 Unicode 格式以防止数据丢失正在生成代码...E:\EtherCAT\上位机\SOEM-1.3.1\SOEM-1.3.1>if EXIST lib\win32 goto skip_libE:\EtherCAT\上位机\SOEM-1.3.1\SOEM-1.3.1>MKDIR lib\win32E:\EtherCAT\上位机\SOEM-1.3.1\SOEM-1.3.1>lib.exe @make\lib_libsoem.rsp /nologo /errorReport:promptE:\EtherCAT\上位机\SOEM-1.3.1\SOEM-1.3.1>echo make donemake doneE:\EtherCAT\上位机\SOEM-1.3.1\SOEM-1.3.1>goto :eofE:\EtherCAT\上位机\SOEM-1.3.1\SOEM-1.3.1>

3、生成测试文件

3.1 编译生成测试文件(非必须,但可用于测试)

部分测试文件无法在Windows下生成

make_test_win32_all.bat "D:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE" x86

过程编译代码

E:\EtherCAT\上位机\SOEM-1.3.1\SOEM-1.3.1>make_test_win32_all.bat "D:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE" x86用于 x86 的 Microsoft (R) C/C++ 优化编译器 19.00.24210 版版权所有(C) Microsoft Corporation。保留所有权利。cl /c /Zi /W3 /WX- /Od /Oy- /D "_CRT_SECURE_NO_WARNINGS" /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /Gm /EHsc /RTC1 /MTd /GS /fp:precise /Zc:wchar_t /Zc:forScope /Fo"obj\\" /Fd"obj\vc100.pdb" /Gd /TC /analyze-  /Odi /I "oshw\win32\wpcap\include" /I "osal" /I "osal\win32" /I "oshw\win32" /I "soem"   oshw\win32\nicdrv.c   oshw\win32\oshw.c   soem\ethercatbase.c   soem\ethercatcoe.c   soem\ethercatconfig.c   soem\ethercatdc.c   soem\ethercatfoe.c   soem\ethercatmain.c   soem\ethercatprint.c   soem\ethercatsoe.c   osal\win32\osal.c    ......eepromtool.ce:\ethercat\上位机\soem-1.3.1\soem-1.3.1\soem\ethercattype.h: warning C4819: 该文件包含不能在当前代码页(936)中表示的字符。请将该文件保存为 Unicode 格式以防止数据丢失e:\ethercat\上位机\soem-1.3.1\soem-1.3.1\osal\win32\osal_defs.h: warning C4819: 该文件包含不能在当前代码页(936)中表示的字符。请将该文件保存为 Unicode 格式以防止数据丢失e:\ethercat\上位机\soem-1.3.1\soem-1.3.1\oshw\win32\nicdrv.h: warning C4819: 该文件包含不能在当前代码页(936)中表示的字符。请将该文件保存为 Unicode 格式以防止数据丢失e:\ethercat\上位机\soem-1.3.1\soem-1.3.1\soem\ethercatbase.h: warning C4819: 该文件包含不能在当前代码页(936)中表示的字符。请将该文件保存为 Unicode 格式以防止数据丢失e:\ethercat\上位机\soem-1.3.1\soem-1.3.1\soem\ethercatmain.h: warning C4819: 该文件包含不能在当前代码页(936)中表示的字符。请将该文件保存为 Unicode 格式以防止数据丢失e:\ethercat\上位机\soem-1.3.1\soem-1.3.1\soem\ethercatcoe.h: warning C4819: 该文件包含不能在当前代码页(936)中表示的字符。请将该文件保存为 Unicode 格式以防止数据丢失make doneE:\EtherCAT\上位机\SOEM-1.3.1\SOEM-1.3.1>

3.2 使用simple_test测试

转到simple_test目录下

cd E:\EtherCAT\上位机\SOEM-1.3.1\SOEM-1.3.1\test\win32\simple_test

获取设备驱动

simple_test.exe

运行测试文件

二、在Windows下创建上位机工程

目标是在C++中生成动态链接库(.dll)后可以被C#调用。

1、创建C# winform工程,用于调用C++的dll

如果不熟悉,建议看C++创建步骤,现在C++控制台上完成调试。

2、创建C++ 库工程

项目大概基本布局如图:

3、部署C++运行环境

3.1 拷贝SOEM-1.3.1到项目文件中

3.2 添加附加包含目录

SOEM-1.3.1\osal;SOEM-1.3.1\osal\win32;SOEM-1.3.1\oshw\win32\wpcap\Include\pcap;SOEM-1.3.1\oshw\win32\wpcap\Include;SOEM-1.3.1\oshw\win32;SOEM-1.3.1\soem;

3.3 添加附加库目录

SOEM-1.3.1\lib\win32SOEM-1.3.1\oshw\win32\wpcap\Lib

3.4 添加附加依赖项

libsoem.libPacket.libwpcap.lib

注意事项

事项1:
LINK : warning LNK4098: 默认库“LIBCMTD”与其他库的使用冲突;请使用
/NODEFAULTLIB:library
解决方案1:
【连接器】->【输入】->【忽略指定库】,输入:msvcrt.lib
解决方案2:
【连接器】->【命令行】,输入:/NODEFAULTLIB:msvcrt.lib


事项2:
warning C4819: 该文件包含不能在当前代码页(936)中表示的字符。请将该文件保存为 Unicode
格式以防止数据丢失
解决方案:
点击报警,进入触发报警的文件,点击菜单栏中“文件->高级保存选项”,选择“Unicode 1200”即可。

事项3:
调试C#时,无法在C++中打断点


解决方案:
属性->调试 中启用“本机代码调试”

事项4:
C4996 'strcpy': This function or variable may be unsafe.
在使用vs编写c++程序时使用strcpy函数会报错
解决方案:
“属性”->“c/c++”->“预处理器”,在“预处理定义”中加入下面语句:_CRT_SECURE_NO_WARNINGS

  • 2
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值