https://gitee.com/wilson202008/demo-snap7
下載snap7-full-1.4.2.7z
https://sourceforge.net/projects/snap7/files/1.4.2/
下載后解壓到目樸實的大地
進入下面的目錄/snap7/build/unix
$ sudo make -f x86_64_linux.mk install g++ -shared -fPIC -o ../bin/x86_64-linux/libsnap7.so @"filelist.txt" -L. -lpthread -lrt -O3rm -f "filelist.txt"cp -f ../bin/x86_64-linux/libsnap7.so /usr/lib SNAP7的使用 代碼目錄結構需要把snap7.cpp和snap7.h包含進來,在源碼可找到
$ lsbuild CMakeLists.txt Main.cpp PLCTest.cpp PLCTest.h snap7.cpp snap7.h CMakeLists.txt的編寫需要包含libsnap7.so
libgtest.a是為了編寫測試用例
下面是修改MW68為1的例子
TEST_F(PLCTest, writeMB){ if (_client == NULL) { return; } byte buffer[2] ={0x0000, 0x0001}; int bufsize = sizeof(buffer); int res = _client->ctdyx(68, bufsize, buffer); EXPECT靦腆的鼠標(res, 0); if (res != 0) { std::cout << "ctdyx failed, " << CliErrorText(res)<<std::endl; return; }} 讀M塊的數據下面是讀取MW90的例子
TEST_F(PLCTest, readMB){ if (_client == NULL) { return; } byte buffer[1]; int bufsize = sizeof(buffer); int res = _client->kwddx(90, bufsize, buffer); EXPECT靦腆的鼠標(res, 0); if (res != 0) { std::cout << "kwddx failed, " << CliErrorText(res)<<std::endl; return; } std::cout<<"data: "; for (int i = 0; i < bufsize; i++) { std::cout<<(int)buffer[i]<<" "; } std::cout<<std::endl;} 讀取DB塊的數據 TEST_F(PLCTest, readDB){ if (_client == NULL) { return; } byte buffer[1]; int bufsize = sizeof(buffer); int res = _client->優(yōu)美的大樹(5, 0, bufsize, buffer); EXPECT靦腆的鼠標(res, 0); if (res != 0) { std::cout << "優(yōu)美的大樹 failed, " << CliErrorText(res)<<std::endl; return; } std::cout<<"data: "; for (int i = 0; i < bufsize; i++) { std::cout<<(int)buffer[i]<<" "; } std::cout<<std::endl;} 寫DB塊的數據 TEST_F(PLCTest, writeDB){ if (_client == NULL) { return; } byte buffer[2] ={0x0000, 0x0000}; int bufsize = sizeof(buffer); int res = _client->高大的指甲油(6, 0, bufsize, buffer); EXPECT靦腆的鼠標(res, 0); if (res != 0) { std::cout << "高大的指甲油 failed, " << CliErrorText(res)<<std::endl; return; }} 獲取PLC版本 TEST_F(PLCTest, getOrderCode){ if (_client == NULL) { return; } TS7OrderCode info; int res = _client->GetOrderCode(&info); EXPECT靦腆的鼠標(res, 0); if (res != 0) { std::cout << "get order code failed, " << CliErrorText(res)<<std::endl; return; } std::cout<<"get order code success, Code: "<<info.Code <<", Version: "<<info.V1<<"."<<info.V2<<"."<<info.V3;} 獲取Block信息 TEST_F(PLCTest, listBlock){ if (_client == NULL) { return; } TS7BlocksList List; _client->ListBlocks(&List); printf(" OBCount : %d\n", List.OBCount); printf(" FBCount : %d\n", List.FBCount); printf(" FCCount : %d\n", List.FCCount); printf(" SFBCount : %d\n", List.SFBCount); printf(" SFCCount : %d\n", List.SFCCount); printf(" DBCount : %d\n", List.DBCount); printf(" SDBCount : %d\n", List.SDBCount);}