当前位置:   article > 正文

Snap7 在西门子PLC的使用_snap7 dbread c++

snap7 dbread c++

编译源码

参考代码

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  -O3
rm -f "filelist.txt"
cp -f ../bin/x86_64-linux/libsnap7.so /usr/lib
  • 1
  • 2
  • 3
  • 4

SNAP7的使用

代码目录结构

需要把snap7.cpp和snap7.h包含进来,在源码可找到

$ ls
build  CMakeLists.txt  Main.cpp  PLCTest.cpp  PLCTest.h  snap7.cpp  snap7.h
  • 1
  • 2

CMakeLists.txt的编写

需要包含libsnap7.so
libgtest.a是为了编写测试用例

cmake_minimum_required(VERSION 2.8)

project(demo)

SET(CMAKE_BUILD_TYPE "Debug")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -pthread -g -Wall")

SET(SNAP7LIB /usr/lib/libsnap7.so)
SET(GTESTLIB /usr/local/lib/libgtest.a)

aux_source_directory(. DIRSRCS)

add_executable(demo ${DIRSRCS})
target_link_libraries(demo ${SNAP7LIB} ${GTESTLIB})
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

PLCTest.h的编写

#pragma once

#include <memory>
#include <iostream>

#include "gtest/gtest.h"
#include "snap7.h"

class PLCTest : public testing::Test
{
private:
    void SetUp() override;
    void TearDown() override;  

public:
    std::shared_ptr<TS7Client> _client;
};
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

连接PLC

void PLCTest::SetUp()
{
    _client = std::make_shared<TS7Client>();
    int res = _client->ConnectTo("192.168.30.5", 0, 1);

    std::cout << "connect result: " << res << std::endl;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

断开连接

void PLCTest::TearDown()
{
    if (_client != NULL)
    {
        _client->Disconnect();
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

写M块的数据

下面是修改MW68为1的例子

TEST_F(PLCTest, writeMB)
{
    if (_client == NULL)
    {
        return;
    }
    byte buffer[2] ={0x0000, 0x0001};
    int bufsize = sizeof(buffer);

    int res = _client->MBWrite(68, bufsize, buffer);
    EXPECT_EQ(res, 0);
    if (res != 0)
    {
        std::cout << "MBWrite failed, " << CliErrorText(res)<<std::endl;
        return;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

读M块的数据

下面是读取MW90的例子

TEST_F(PLCTest, readMB)
{
    if (_client == NULL)
    {
        return;
    }

    byte buffer[1];
    int bufsize = sizeof(buffer);

    int res = _client->MBRead(90, bufsize, buffer);
    EXPECT_EQ(res, 0);
    if (res != 0)
    {
        std::cout << "MBRead 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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

读取DB块的数据

TEST_F(PLCTest, readDB)
{
    if (_client == NULL)
    {
        return;
    }

    byte buffer[1];
    int bufsize = sizeof(buffer);

    int res = _client->DBRead(5, 0, bufsize, buffer);
    EXPECT_EQ(res, 0);
    if (res != 0)
    {
        std::cout << "DBRead 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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

写DB块的数据

TEST_F(PLCTest, writeDB)
{
    if (_client == NULL)
    {
        return;
    }
    
    byte buffer[2] ={0x0000, 0x0000};
    int bufsize = sizeof(buffer);

    int res = _client->DBWrite(6, 0, bufsize, buffer);
    EXPECT_EQ(res, 0);
    if (res != 0)
    {
        std::cout << "DBWrite failed, " << CliErrorText(res)<<std::endl;
        return;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

获取PLC版本

TEST_F(PLCTest, getOrderCode)
{
    if (_client == NULL)
    {
        return;
    }
    TS7OrderCode info;
    int res = _client->GetOrderCode(&info);
    EXPECT_EQ(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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

获取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);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/空白诗007/article/detail/770366
推荐阅读
相关标签
  

闽ICP备14008679号