Browse Source

change constructor with proto input, restructure.

youchen 5 years ago
parent
commit
6ab8e3b762

+ 2 - 0
.gitignore

@@ -28,6 +28,8 @@ obj/
 _ReSharper*/
 [Tt]est[Rr]esult*
 .vs/
+.vscode/
 #Nuget packages folder
 packages/
 build/
+plc/plc_test.cpp

+ 0 - 18
.vscode/c_cpp_properties.json

@@ -1,18 +0,0 @@
-{
-    "configurations": [
-        {
-            "name": "Linux",
-            "includePath": [
-                "${workspaceFolder}/**",
-                "/usr/local/include/modbus",
-                "${workspaceFolder}/task"
-            ],
-            "defines": [],
-            "compilerPath": "/usr/bin/clang",
-            "cStandard": "c11",
-            "cppStandard": "c++17",
-            "intelliSenseMode": "clang-x64"
-        }
-    ],
-    "version": 4
-}

+ 0 - 56
.vscode/settings.json

@@ -1,56 +0,0 @@
-{
-    "files.associations": {
-        "thread": "cpp",
-        "cctype": "cpp",
-        "clocale": "cpp",
-        "cmath": "cpp",
-        "cstdarg": "cpp",
-        "cstddef": "cpp",
-        "cstdio": "cpp",
-        "cstdlib": "cpp",
-        "cstring": "cpp",
-        "ctime": "cpp",
-        "cwchar": "cpp",
-        "cwctype": "cpp",
-        "array": "cpp",
-        "atomic": "cpp",
-        "strstream": "cpp",
-        "*.tcc": "cpp",
-        "bitset": "cpp",
-        "chrono": "cpp",
-        "cinttypes": "cpp",
-        "complex": "cpp",
-        "condition_variable": "cpp",
-        "cstdint": "cpp",
-        "deque": "cpp",
-        "list": "cpp",
-        "unordered_map": "cpp",
-        "vector": "cpp",
-        "exception": "cpp",
-        "algorithm": "cpp",
-        "functional": "cpp",
-        "ratio": "cpp",
-        "system_error": "cpp",
-        "tuple": "cpp",
-        "type_traits": "cpp",
-        "fstream": "cpp",
-        "initializer_list": "cpp",
-        "iomanip": "cpp",
-        "iosfwd": "cpp",
-        "iostream": "cpp",
-        "istream": "cpp",
-        "limits": "cpp",
-        "memory": "cpp",
-        "mutex": "cpp",
-        "new": "cpp",
-        "ostream": "cpp",
-        "numeric": "cpp",
-        "sstream": "cpp",
-        "stdexcept": "cpp",
-        "streambuf": "cpp",
-        "cfenv": "cpp",
-        "utility": "cpp",
-        "typeindex": "cpp",
-        "typeinfo": "cpp"
-    }
-}

+ 10 - 2
CMakeLists.txt

@@ -11,6 +11,7 @@ FIND_PACKAGE(PCL REQUIRED)
 FIND_PACKAGE(Protobuf REQUIRED)
 FIND_PACKAGE(Glog REQUIRED)
 set(CMAKE_MODULE_PATH "/usr/local/share/")
+set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wl,--no-as-needed")
 
 set(CMAKE_CXX_FLAGS "-std=c++11 -msse -msse2 -msse3 -msse4 -msse4.1 -msse4.2")
 set(CMAKE_BUILD_TYPE "RELEASE")
@@ -21,6 +22,7 @@ set(CMAKE_BUILD_TYPE "RELEASE")
 include_directories(
         plc
         task
+        /usr/local/include
         /usr/local/include/modbus
   ${PCL_INCLUDE_DIRS}
   ${OpenCV_INCLUDE_DIRS}
@@ -32,10 +34,16 @@ aux_source_directory(${CMAKE_CURRENT_LIST_DIR}/plc PLC_SRC )
 #aux_source_directory(${CMAKE_CURRENT_LIST_DIR}/Locate LOCATE_SRC )
 #aux_source_directory(${CMAKE_CURRENT_LIST_DIR}/terminor TERMINOR_SRC )
 aux_source_directory(${CMAKE_CURRENT_LIST_DIR}/task TASK_MANAGER_SRC )
+aux_source_directory(${CMAKE_CURRENT_LIST_DIR}/tool TOOL_SRC )
 
-add_executable(LidarMeasure ./main.cpp ${PLC_SRC} ${TASK_MANAGER_SRC})
+add_executable(LidarMeasure ./main.cpp ${PLC_SRC} ${TASK_MANAGER_SRC} ${TOOL_SRC})
 target_link_libraries(LidarMeasure ${OpenCV_LIBS}
-        ${GLOG_LIBRARIES} ${PCL_LIBRARIES} ${PROTOBUF_LIBRARIES} ipopt modbus)
+        ${GLOG_LIBRARIES} ${PCL_LIBRARIES} ${PROTOBUF_LIBRARIES} ipopt modbus libnanomsg.so libnnxx.a)
+
+# plc test module
+add_executable(plc_test  ./test/plc_test.cpp ${PLC_SRC} ${TASK_MANAGER_SRC} ${TOOL_SRC})
+target_link_libraries(plc_test ${OpenCV_LIBS}
+        ${GLOG_LIBRARIES} ${PCL_LIBRARIES} ${PROTOBUF_LIBRARIES} ipopt modbus libnanomsg.so libnnxx.a )
 
 
 #add_executable(LidarMeasure ./main.cpp ${LASER_SRC} ${PLC_SRC} ${TERMINOR_SRC} ${LOCATE_SRC} ${TASK_MANAGER_SRC})

+ 0 - 73
main.cpp

@@ -3,81 +3,8 @@
 //
 #include <iostream>
 #include "plc/plc_communicator.h"
-class Test{
-public:
-    Test(Plc_Communicator* p){
-        mp_pc = p;
-    }
-    static ERROR_CODE callback(int terminal_id, void *p_owner)
-    {
-        Test* t = (Test*)p_owner;
-        t->m_term_id = terminal_id;
-        std::cout<<"callback "<<t->m_term_id<<" called."<<std::endl;
-        usleep(1000 * 1000);
-        if(t->mp_pc!=0){
-            // Plc_Task task;
-            struct measure_result result;
-            result.terminal_id = t->m_term_id;
-            result.correctness = terminal_id % 2 == 0?true:false;
-            result.x = 3501.2f;
-            result.angle = 91.35f;
-            result.wheel_base = 2651.6f;
-            ERROR_CODE ec = t->m_task.set_result(result);
-            std::cout<<"callback set result "<<ec<<std::endl;
-            std::thread* th = new std::thread(execute_thread, t);
-            th->detach();
-        }
-        return ERROR_CODE::SUCCESS;
-    }
-
-    static void execute_thread(Test* t){
-        usleep(1000 * 2000);
-        if(t->mp_pc!= 0){
-            ERROR_CODE ec = t->mp_pc->execute_task(&t->m_task);
-            std::cout << "thread execute task " << ec << std::endl;
-
-            int count = 5;
-            while (count-- > 0)
-            {
-                std::cout << "---------- "<<count<<" --------" << std::endl;
-                std::vector<uint16_t> vec;
-                ec = t->mp_pc->get_plc_data(vec, t->m_term_id);
-                std::cout << "get data " << ec << std::endl;
-                std::cout << "[ ";
-                for (size_t i = 0; i < vec.size(); i++)
-                {
-                    std::cout << vec[i] << " ";
-                }
-                std::cout << "]" << std::endl;
-
-                ec = t->mp_pc->set_status_update_timeout(15000);
-                std::cout << "set status " << ec << std::endl;
-
-                ec = t->mp_pc->get_error();
-                std::cout << "get error " << ec << std::endl;
-
-                bool init_state = t->mp_pc->get_initialize_status();
-                std::cout << "get initialize status " << init_state << std::endl;
-
-                bool conn = t->mp_pc->get_connection();
-                std::cout << "get connection status " << conn << std::endl;
-                usleep(1000 * 1500);
-            }
-        }
-    }
-private:
-    int m_term_id;
-    Plc_Communicator* mp_pc;
-    Plc_Task    m_task;
-};
 
 int main(int argc,char* argv[])
 {
-    Plc_Communicator pc("192.168.2.131", 502, 1);
-    Test test(&pc);
-    ERROR_CODE ec = pc.set_plc_callback(test.callback, &test);
-    std::cout<<"set callback "<<ec<<std::endl;
 
-    getchar();
-    return 0;
 }

+ 4 - 4
plc/plc_communicator.cpp

@@ -1,15 +1,15 @@
 #include "plc_communicator.h"
 
 // ××××××××××× 构造与析构 ×××××××××××
-Plc_Communicator::Plc_Communicator(const char *ip, int port, int slave_id) : mb_plc_initialized(false),
+Plc_Communicator::Plc_Communicator(plc_module::plc_connection_params connection_params) : mb_plc_initialized(false),
                                                                              mb_plc_is_connected(false),
                                                                              mb_plc_is_updating(false),
                                                                              mp_plc_owner(0),
                                                                              m_plc_thread(0)
 {
-    m_plc_ip_str = ip;
-    m_plc_port = port;
-    m_plc_slave_id = slave_id;
+    m_plc_ip_str = connection_params.ip();
+    m_plc_port = connection_params.port();
+    m_plc_slave_id = connection_params.slave_id();
     m_plc_current_error = ERROR_CODE::SUCCESS;
     m_plc_status_update_timeout = 10000;
     for (size_t i = 0; i < PLC_REGION_NUM; i++)

+ 24 - 2
plc/plc_communicator.h

@@ -14,7 +14,29 @@
 #include "../task/task_command_manager.h"
 #include "plc_task.h"
 #include "LibmodbusWrapper.h"
-#include "StdCondition.h"
+#include "../tool/StdCondition.h"
+#include "plc_module.pb.h"
+
+#include <google/protobuf/io/coded_stream.h>
+#include <google/protobuf/io/zero_copy_stream_impl.h>
+#include <google/protobuf/text_format.h>
+using google::protobuf::io::FileInputStream;
+using google::protobuf::io::FileOutputStream;
+using google::protobuf::io::ZeroCopyInputStream;
+using google::protobuf::io::CodedInputStream;
+using google::protobuf::io::ZeroCopyOutputStream;
+using google::protobuf::io::CodedOutputStream;
+using google::protobuf::Message;
+
+#include <nnxx/message.h>
+#include <nnxx/message_control.h>
+#include <nnxx/socket.h>
+#include <nnxx/reqrep.h>
+//#include <nnxx/unittest.h>
+#include <nnxx/timeout.h>
+#include <nnxx/error.h>
+#include <cstring>
+#include <nnxx/message>
 
 const int PLC_SIGNAL_BEGIN_OFFSET=2;
 const int PLC_REGION_NUM=6;
@@ -39,7 +61,7 @@ class Plc_Communicator
 {
     
 public:
-    Plc_Communicator(const char *ip, int port, int slave_id);
+    Plc_Communicator(plc_module::plc_connection_params connection_params);
     ~Plc_Communicator();
 
     // get set 方法

+ 500 - 0
plc/plc_module.pb.cc

@@ -0,0 +1,500 @@
+// Generated by the protocol buffer compiler.  DO NOT EDIT!
+// source: plc_module.proto
+
+#include "plc_module.pb.h"
+
+#include <algorithm>
+
+#include <google/protobuf/stubs/common.h>
+#include <google/protobuf/stubs/port.h>
+#include <google/protobuf/stubs/once.h>
+#include <google/protobuf/io/coded_stream.h>
+#include <google/protobuf/wire_format_lite_inl.h>
+#include <google/protobuf/descriptor.h>
+#include <google/protobuf/generated_message_reflection.h>
+#include <google/protobuf/reflection_ops.h>
+#include <google/protobuf/wire_format.h>
+// This is a temporary google only hack
+#ifdef GOOGLE_PROTOBUF_ENFORCE_UNIQUENESS
+#include "third_party/protobuf/version.h"
+#endif
+// @@protoc_insertion_point(includes)
+namespace plc_module {
+class plc_connection_paramsDefaultTypeInternal {
+ public:
+  ::google::protobuf::internal::ExplicitlyConstructed<plc_connection_params>
+      _instance;
+} _plc_connection_params_default_instance_;
+}  // namespace plc_module
+namespace protobuf_plc_5fmodule_2eproto {
+void InitDefaultsplc_connection_paramsImpl() {
+  GOOGLE_PROTOBUF_VERIFY_VERSION;
+
+#ifdef GOOGLE_PROTOBUF_ENFORCE_UNIQUENESS
+  ::google::protobuf::internal::InitProtobufDefaultsForceUnique();
+#else
+  ::google::protobuf::internal::InitProtobufDefaults();
+#endif  // GOOGLE_PROTOBUF_ENFORCE_UNIQUENESS
+  {
+    void* ptr = &::plc_module::_plc_connection_params_default_instance_;
+    new (ptr) ::plc_module::plc_connection_params();
+    ::google::protobuf::internal::OnShutdownDestroyMessage(ptr);
+  }
+  ::plc_module::plc_connection_params::InitAsDefaultInstance();
+}
+
+void InitDefaultsplc_connection_params() {
+  static GOOGLE_PROTOBUF_DECLARE_ONCE(once);
+  ::google::protobuf::GoogleOnceInit(&once, &InitDefaultsplc_connection_paramsImpl);
+}
+
+::google::protobuf::Metadata file_level_metadata[1];
+
+const ::google::protobuf::uint32 TableStruct::offsets[] GOOGLE_PROTOBUF_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold) = {
+  GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::plc_module::plc_connection_params, _has_bits_),
+  GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::plc_module::plc_connection_params, _internal_metadata_),
+  ~0u,  // no _extensions_
+  ~0u,  // no _oneof_case_
+  ~0u,  // no _weak_field_map_
+  GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::plc_module::plc_connection_params, ip_),
+  GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::plc_module::plc_connection_params, port_),
+  GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::plc_module::plc_connection_params, slave_id_),
+  0,
+  1,
+  2,
+};
+static const ::google::protobuf::internal::MigrationSchema schemas[] GOOGLE_PROTOBUF_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold) = {
+  { 0, 8, sizeof(::plc_module::plc_connection_params)},
+};
+
+static ::google::protobuf::Message const * const file_default_instances[] = {
+  reinterpret_cast<const ::google::protobuf::Message*>(&::plc_module::_plc_connection_params_default_instance_),
+};
+
+void protobuf_AssignDescriptors() {
+  AddDescriptors();
+  ::google::protobuf::MessageFactory* factory = NULL;
+  AssignDescriptors(
+      "plc_module.proto", schemas, file_default_instances, TableStruct::offsets, factory,
+      file_level_metadata, NULL, NULL);
+}
+
+void protobuf_AssignDescriptorsOnce() {
+  static GOOGLE_PROTOBUF_DECLARE_ONCE(once);
+  ::google::protobuf::GoogleOnceInit(&once, &protobuf_AssignDescriptors);
+}
+
+void protobuf_RegisterTypes(const ::std::string&) GOOGLE_PROTOBUF_ATTRIBUTE_COLD;
+void protobuf_RegisterTypes(const ::std::string&) {
+  protobuf_AssignDescriptorsOnce();
+  ::google::protobuf::internal::RegisterAllTypes(file_level_metadata, 1);
+}
+
+void AddDescriptorsImpl() {
+  InitDefaults();
+  static const char descriptor[] GOOGLE_PROTOBUF_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold) = {
+      "\n\020plc_module.proto\022\nplc_module\"C\n\025plc_co"
+      "nnection_params\022\n\n\002ip\030\001 \002(\t\022\014\n\004port\030\002 \002("
+      "\005\022\020\n\010slave_id\030\003 \002(\005"
+  };
+  ::google::protobuf::DescriptorPool::InternalAddGeneratedFile(
+      descriptor, 99);
+  ::google::protobuf::MessageFactory::InternalRegisterGeneratedFile(
+    "plc_module.proto", &protobuf_RegisterTypes);
+}
+
+void AddDescriptors() {
+  static GOOGLE_PROTOBUF_DECLARE_ONCE(once);
+  ::google::protobuf::GoogleOnceInit(&once, &AddDescriptorsImpl);
+}
+// Force AddDescriptors() to be called at dynamic initialization time.
+struct StaticDescriptorInitializer {
+  StaticDescriptorInitializer() {
+    AddDescriptors();
+  }
+} static_descriptor_initializer;
+}  // namespace protobuf_plc_5fmodule_2eproto
+namespace plc_module {
+
+// ===================================================================
+
+void plc_connection_params::InitAsDefaultInstance() {
+}
+#if !defined(_MSC_VER) || _MSC_VER >= 1900
+const int plc_connection_params::kIpFieldNumber;
+const int plc_connection_params::kPortFieldNumber;
+const int plc_connection_params::kSlaveIdFieldNumber;
+#endif  // !defined(_MSC_VER) || _MSC_VER >= 1900
+
+plc_connection_params::plc_connection_params()
+  : ::google::protobuf::Message(), _internal_metadata_(NULL) {
+  if (GOOGLE_PREDICT_TRUE(this != internal_default_instance())) {
+    ::protobuf_plc_5fmodule_2eproto::InitDefaultsplc_connection_params();
+  }
+  SharedCtor();
+  // @@protoc_insertion_point(constructor:plc_module.plc_connection_params)
+}
+plc_connection_params::plc_connection_params(const plc_connection_params& from)
+  : ::google::protobuf::Message(),
+      _internal_metadata_(NULL),
+      _has_bits_(from._has_bits_),
+      _cached_size_(0) {
+  _internal_metadata_.MergeFrom(from._internal_metadata_);
+  ip_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
+  if (from.has_ip()) {
+    ip_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.ip_);
+  }
+  ::memcpy(&port_, &from.port_,
+    static_cast<size_t>(reinterpret_cast<char*>(&slave_id_) -
+    reinterpret_cast<char*>(&port_)) + sizeof(slave_id_));
+  // @@protoc_insertion_point(copy_constructor:plc_module.plc_connection_params)
+}
+
+void plc_connection_params::SharedCtor() {
+  _cached_size_ = 0;
+  ip_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
+  ::memset(&port_, 0, static_cast<size_t>(
+      reinterpret_cast<char*>(&slave_id_) -
+      reinterpret_cast<char*>(&port_)) + sizeof(slave_id_));
+}
+
+plc_connection_params::~plc_connection_params() {
+  // @@protoc_insertion_point(destructor:plc_module.plc_connection_params)
+  SharedDtor();
+}
+
+void plc_connection_params::SharedDtor() {
+  ip_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
+}
+
+void plc_connection_params::SetCachedSize(int size) const {
+  GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN();
+  _cached_size_ = size;
+  GOOGLE_SAFE_CONCURRENT_WRITES_END();
+}
+const ::google::protobuf::Descriptor* plc_connection_params::descriptor() {
+  ::protobuf_plc_5fmodule_2eproto::protobuf_AssignDescriptorsOnce();
+  return ::protobuf_plc_5fmodule_2eproto::file_level_metadata[kIndexInFileMessages].descriptor;
+}
+
+const plc_connection_params& plc_connection_params::default_instance() {
+  ::protobuf_plc_5fmodule_2eproto::InitDefaultsplc_connection_params();
+  return *internal_default_instance();
+}
+
+plc_connection_params* plc_connection_params::New(::google::protobuf::Arena* arena) const {
+  plc_connection_params* n = new plc_connection_params;
+  if (arena != NULL) {
+    arena->Own(n);
+  }
+  return n;
+}
+
+void plc_connection_params::Clear() {
+// @@protoc_insertion_point(message_clear_start:plc_module.plc_connection_params)
+  ::google::protobuf::uint32 cached_has_bits = 0;
+  // Prevent compiler warnings about cached_has_bits being unused
+  (void) cached_has_bits;
+
+  cached_has_bits = _has_bits_[0];
+  if (cached_has_bits & 0x00000001u) {
+    GOOGLE_DCHECK(!ip_.IsDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()));
+    (*ip_.UnsafeRawStringPointer())->clear();
+  }
+  if (cached_has_bits & 6u) {
+    ::memset(&port_, 0, static_cast<size_t>(
+        reinterpret_cast<char*>(&slave_id_) -
+        reinterpret_cast<char*>(&port_)) + sizeof(slave_id_));
+  }
+  _has_bits_.Clear();
+  _internal_metadata_.Clear();
+}
+
+bool plc_connection_params::MergePartialFromCodedStream(
+    ::google::protobuf::io::CodedInputStream* input) {
+#define DO_(EXPRESSION) if (!GOOGLE_PREDICT_TRUE(EXPRESSION)) goto failure
+  ::google::protobuf::uint32 tag;
+  // @@protoc_insertion_point(parse_start:plc_module.plc_connection_params)
+  for (;;) {
+    ::std::pair< ::google::protobuf::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u);
+    tag = p.first;
+    if (!p.second) goto handle_unusual;
+    switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) {
+      // required string ip = 1;
+      case 1: {
+        if (static_cast< ::google::protobuf::uint8>(tag) ==
+            static_cast< ::google::protobuf::uint8>(10u /* 10 & 0xFF */)) {
+          DO_(::google::protobuf::internal::WireFormatLite::ReadString(
+                input, this->mutable_ip()));
+          ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
+            this->ip().data(), static_cast<int>(this->ip().length()),
+            ::google::protobuf::internal::WireFormat::PARSE,
+            "plc_module.plc_connection_params.ip");
+        } else {
+          goto handle_unusual;
+        }
+        break;
+      }
+
+      // required int32 port = 2;
+      case 2: {
+        if (static_cast< ::google::protobuf::uint8>(tag) ==
+            static_cast< ::google::protobuf::uint8>(16u /* 16 & 0xFF */)) {
+          set_has_port();
+          DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive<
+                   ::google::protobuf::int32, ::google::protobuf::internal::WireFormatLite::TYPE_INT32>(
+                 input, &port_)));
+        } else {
+          goto handle_unusual;
+        }
+        break;
+      }
+
+      // required int32 slave_id = 3;
+      case 3: {
+        if (static_cast< ::google::protobuf::uint8>(tag) ==
+            static_cast< ::google::protobuf::uint8>(24u /* 24 & 0xFF */)) {
+          set_has_slave_id();
+          DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive<
+                   ::google::protobuf::int32, ::google::protobuf::internal::WireFormatLite::TYPE_INT32>(
+                 input, &slave_id_)));
+        } else {
+          goto handle_unusual;
+        }
+        break;
+      }
+
+      default: {
+      handle_unusual:
+        if (tag == 0) {
+          goto success;
+        }
+        DO_(::google::protobuf::internal::WireFormat::SkipField(
+              input, tag, _internal_metadata_.mutable_unknown_fields()));
+        break;
+      }
+    }
+  }
+success:
+  // @@protoc_insertion_point(parse_success:plc_module.plc_connection_params)
+  return true;
+failure:
+  // @@protoc_insertion_point(parse_failure:plc_module.plc_connection_params)
+  return false;
+#undef DO_
+}
+
+void plc_connection_params::SerializeWithCachedSizes(
+    ::google::protobuf::io::CodedOutputStream* output) const {
+  // @@protoc_insertion_point(serialize_start:plc_module.plc_connection_params)
+  ::google::protobuf::uint32 cached_has_bits = 0;
+  (void) cached_has_bits;
+
+  cached_has_bits = _has_bits_[0];
+  // required string ip = 1;
+  if (cached_has_bits & 0x00000001u) {
+    ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
+      this->ip().data(), static_cast<int>(this->ip().length()),
+      ::google::protobuf::internal::WireFormat::SERIALIZE,
+      "plc_module.plc_connection_params.ip");
+    ::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased(
+      1, this->ip(), output);
+  }
+
+  // required int32 port = 2;
+  if (cached_has_bits & 0x00000002u) {
+    ::google::protobuf::internal::WireFormatLite::WriteInt32(2, this->port(), output);
+  }
+
+  // required int32 slave_id = 3;
+  if (cached_has_bits & 0x00000004u) {
+    ::google::protobuf::internal::WireFormatLite::WriteInt32(3, this->slave_id(), output);
+  }
+
+  if (_internal_metadata_.have_unknown_fields()) {
+    ::google::protobuf::internal::WireFormat::SerializeUnknownFields(
+        _internal_metadata_.unknown_fields(), output);
+  }
+  // @@protoc_insertion_point(serialize_end:plc_module.plc_connection_params)
+}
+
+::google::protobuf::uint8* plc_connection_params::InternalSerializeWithCachedSizesToArray(
+    bool deterministic, ::google::protobuf::uint8* target) const {
+  (void)deterministic; // Unused
+  // @@protoc_insertion_point(serialize_to_array_start:plc_module.plc_connection_params)
+  ::google::protobuf::uint32 cached_has_bits = 0;
+  (void) cached_has_bits;
+
+  cached_has_bits = _has_bits_[0];
+  // required string ip = 1;
+  if (cached_has_bits & 0x00000001u) {
+    ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
+      this->ip().data(), static_cast<int>(this->ip().length()),
+      ::google::protobuf::internal::WireFormat::SERIALIZE,
+      "plc_module.plc_connection_params.ip");
+    target =
+      ::google::protobuf::internal::WireFormatLite::WriteStringToArray(
+        1, this->ip(), target);
+  }
+
+  // required int32 port = 2;
+  if (cached_has_bits & 0x00000002u) {
+    target = ::google::protobuf::internal::WireFormatLite::WriteInt32ToArray(2, this->port(), target);
+  }
+
+  // required int32 slave_id = 3;
+  if (cached_has_bits & 0x00000004u) {
+    target = ::google::protobuf::internal::WireFormatLite::WriteInt32ToArray(3, this->slave_id(), target);
+  }
+
+  if (_internal_metadata_.have_unknown_fields()) {
+    target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray(
+        _internal_metadata_.unknown_fields(), target);
+  }
+  // @@protoc_insertion_point(serialize_to_array_end:plc_module.plc_connection_params)
+  return target;
+}
+
+size_t plc_connection_params::RequiredFieldsByteSizeFallback() const {
+// @@protoc_insertion_point(required_fields_byte_size_fallback_start:plc_module.plc_connection_params)
+  size_t total_size = 0;
+
+  if (has_ip()) {
+    // required string ip = 1;
+    total_size += 1 +
+      ::google::protobuf::internal::WireFormatLite::StringSize(
+        this->ip());
+  }
+
+  if (has_port()) {
+    // required int32 port = 2;
+    total_size += 1 +
+      ::google::protobuf::internal::WireFormatLite::Int32Size(
+        this->port());
+  }
+
+  if (has_slave_id()) {
+    // required int32 slave_id = 3;
+    total_size += 1 +
+      ::google::protobuf::internal::WireFormatLite::Int32Size(
+        this->slave_id());
+  }
+
+  return total_size;
+}
+size_t plc_connection_params::ByteSizeLong() const {
+// @@protoc_insertion_point(message_byte_size_start:plc_module.plc_connection_params)
+  size_t total_size = 0;
+
+  if (_internal_metadata_.have_unknown_fields()) {
+    total_size +=
+      ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize(
+        _internal_metadata_.unknown_fields());
+  }
+  if (((_has_bits_[0] & 0x00000007) ^ 0x00000007) == 0) {  // All required fields are present.
+    // required string ip = 1;
+    total_size += 1 +
+      ::google::protobuf::internal::WireFormatLite::StringSize(
+        this->ip());
+
+    // required int32 port = 2;
+    total_size += 1 +
+      ::google::protobuf::internal::WireFormatLite::Int32Size(
+        this->port());
+
+    // required int32 slave_id = 3;
+    total_size += 1 +
+      ::google::protobuf::internal::WireFormatLite::Int32Size(
+        this->slave_id());
+
+  } else {
+    total_size += RequiredFieldsByteSizeFallback();
+  }
+  int cached_size = ::google::protobuf::internal::ToCachedSize(total_size);
+  GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN();
+  _cached_size_ = cached_size;
+  GOOGLE_SAFE_CONCURRENT_WRITES_END();
+  return total_size;
+}
+
+void plc_connection_params::MergeFrom(const ::google::protobuf::Message& from) {
+// @@protoc_insertion_point(generalized_merge_from_start:plc_module.plc_connection_params)
+  GOOGLE_DCHECK_NE(&from, this);
+  const plc_connection_params* source =
+      ::google::protobuf::internal::DynamicCastToGenerated<const plc_connection_params>(
+          &from);
+  if (source == NULL) {
+  // @@protoc_insertion_point(generalized_merge_from_cast_fail:plc_module.plc_connection_params)
+    ::google::protobuf::internal::ReflectionOps::Merge(from, this);
+  } else {
+  // @@protoc_insertion_point(generalized_merge_from_cast_success:plc_module.plc_connection_params)
+    MergeFrom(*source);
+  }
+}
+
+void plc_connection_params::MergeFrom(const plc_connection_params& from) {
+// @@protoc_insertion_point(class_specific_merge_from_start:plc_module.plc_connection_params)
+  GOOGLE_DCHECK_NE(&from, this);
+  _internal_metadata_.MergeFrom(from._internal_metadata_);
+  ::google::protobuf::uint32 cached_has_bits = 0;
+  (void) cached_has_bits;
+
+  cached_has_bits = from._has_bits_[0];
+  if (cached_has_bits & 7u) {
+    if (cached_has_bits & 0x00000001u) {
+      set_has_ip();
+      ip_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.ip_);
+    }
+    if (cached_has_bits & 0x00000002u) {
+      port_ = from.port_;
+    }
+    if (cached_has_bits & 0x00000004u) {
+      slave_id_ = from.slave_id_;
+    }
+    _has_bits_[0] |= cached_has_bits;
+  }
+}
+
+void plc_connection_params::CopyFrom(const ::google::protobuf::Message& from) {
+// @@protoc_insertion_point(generalized_copy_from_start:plc_module.plc_connection_params)
+  if (&from == this) return;
+  Clear();
+  MergeFrom(from);
+}
+
+void plc_connection_params::CopyFrom(const plc_connection_params& from) {
+// @@protoc_insertion_point(class_specific_copy_from_start:plc_module.plc_connection_params)
+  if (&from == this) return;
+  Clear();
+  MergeFrom(from);
+}
+
+bool plc_connection_params::IsInitialized() const {
+  if ((_has_bits_[0] & 0x00000007) != 0x00000007) return false;
+  return true;
+}
+
+void plc_connection_params::Swap(plc_connection_params* other) {
+  if (other == this) return;
+  InternalSwap(other);
+}
+void plc_connection_params::InternalSwap(plc_connection_params* other) {
+  using std::swap;
+  ip_.Swap(&other->ip_);
+  swap(port_, other->port_);
+  swap(slave_id_, other->slave_id_);
+  swap(_has_bits_[0], other->_has_bits_[0]);
+  _internal_metadata_.Swap(&other->_internal_metadata_);
+  swap(_cached_size_, other->_cached_size_);
+}
+
+::google::protobuf::Metadata plc_connection_params::GetMetadata() const {
+  protobuf_plc_5fmodule_2eproto::protobuf_AssignDescriptorsOnce();
+  return ::protobuf_plc_5fmodule_2eproto::file_level_metadata[kIndexInFileMessages];
+}
+
+
+// @@protoc_insertion_point(namespace_scope)
+}  // namespace plc_module
+
+// @@protoc_insertion_point(global_scope)

+ 331 - 0
plc/plc_module.pb.h

@@ -0,0 +1,331 @@
+// Generated by the protocol buffer compiler.  DO NOT EDIT!
+// source: plc_module.proto
+
+#ifndef PROTOBUF_plc_5fmodule_2eproto__INCLUDED
+#define PROTOBUF_plc_5fmodule_2eproto__INCLUDED
+
+#include <string>
+
+#include <google/protobuf/stubs/common.h>
+
+#if GOOGLE_PROTOBUF_VERSION < 3005000
+#error This file was generated by a newer version of protoc which is
+#error incompatible with your Protocol Buffer headers.  Please update
+#error your headers.
+#endif
+#if 3005000 < GOOGLE_PROTOBUF_MIN_PROTOC_VERSION
+#error This file was generated by an older version of protoc which is
+#error incompatible with your Protocol Buffer headers.  Please
+#error regenerate this file with a newer version of protoc.
+#endif
+
+#include <google/protobuf/io/coded_stream.h>
+#include <google/protobuf/arena.h>
+#include <google/protobuf/arenastring.h>
+#include <google/protobuf/generated_message_table_driven.h>
+#include <google/protobuf/generated_message_util.h>
+#include <google/protobuf/metadata.h>
+#include <google/protobuf/message.h>
+#include <google/protobuf/repeated_field.h>  // IWYU pragma: export
+#include <google/protobuf/extension_set.h>  // IWYU pragma: export
+#include <google/protobuf/unknown_field_set.h>
+// @@protoc_insertion_point(includes)
+
+namespace protobuf_plc_5fmodule_2eproto {
+// Internal implementation detail -- do not use these members.
+struct TableStruct {
+  static const ::google::protobuf::internal::ParseTableField entries[];
+  static const ::google::protobuf::internal::AuxillaryParseTableField aux[];
+  static const ::google::protobuf::internal::ParseTable schema[1];
+  static const ::google::protobuf::internal::FieldMetadata field_metadata[];
+  static const ::google::protobuf::internal::SerializationTable serialization_table[];
+  static const ::google::protobuf::uint32 offsets[];
+};
+void AddDescriptors();
+void InitDefaultsplc_connection_paramsImpl();
+void InitDefaultsplc_connection_params();
+inline void InitDefaults() {
+  InitDefaultsplc_connection_params();
+}
+}  // namespace protobuf_plc_5fmodule_2eproto
+namespace plc_module {
+class plc_connection_params;
+class plc_connection_paramsDefaultTypeInternal;
+extern plc_connection_paramsDefaultTypeInternal _plc_connection_params_default_instance_;
+}  // namespace plc_module
+namespace plc_module {
+
+// ===================================================================
+
+class plc_connection_params : public ::google::protobuf::Message /* @@protoc_insertion_point(class_definition:plc_module.plc_connection_params) */ {
+ public:
+  plc_connection_params();
+  virtual ~plc_connection_params();
+
+  plc_connection_params(const plc_connection_params& from);
+
+  inline plc_connection_params& operator=(const plc_connection_params& from) {
+    CopyFrom(from);
+    return *this;
+  }
+  #if LANG_CXX11
+  plc_connection_params(plc_connection_params&& from) noexcept
+    : plc_connection_params() {
+    *this = ::std::move(from);
+  }
+
+  inline plc_connection_params& operator=(plc_connection_params&& from) noexcept {
+    if (GetArenaNoVirtual() == from.GetArenaNoVirtual()) {
+      if (this != &from) InternalSwap(&from);
+    } else {
+      CopyFrom(from);
+    }
+    return *this;
+  }
+  #endif
+  inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const {
+    return _internal_metadata_.unknown_fields();
+  }
+  inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() {
+    return _internal_metadata_.mutable_unknown_fields();
+  }
+
+  static const ::google::protobuf::Descriptor* descriptor();
+  static const plc_connection_params& default_instance();
+
+  static void InitAsDefaultInstance();  // FOR INTERNAL USE ONLY
+  static inline const plc_connection_params* internal_default_instance() {
+    return reinterpret_cast<const plc_connection_params*>(
+               &_plc_connection_params_default_instance_);
+  }
+  static PROTOBUF_CONSTEXPR int const kIndexInFileMessages =
+    0;
+
+  void Swap(plc_connection_params* other);
+  friend void swap(plc_connection_params& a, plc_connection_params& b) {
+    a.Swap(&b);
+  }
+
+  // implements Message ----------------------------------------------
+
+  inline plc_connection_params* New() const PROTOBUF_FINAL { return New(NULL); }
+
+  plc_connection_params* New(::google::protobuf::Arena* arena) const PROTOBUF_FINAL;
+  void CopyFrom(const ::google::protobuf::Message& from) PROTOBUF_FINAL;
+  void MergeFrom(const ::google::protobuf::Message& from) PROTOBUF_FINAL;
+  void CopyFrom(const plc_connection_params& from);
+  void MergeFrom(const plc_connection_params& from);
+  void Clear() PROTOBUF_FINAL;
+  bool IsInitialized() const PROTOBUF_FINAL;
+
+  size_t ByteSizeLong() const PROTOBUF_FINAL;
+  bool MergePartialFromCodedStream(
+      ::google::protobuf::io::CodedInputStream* input) PROTOBUF_FINAL;
+  void SerializeWithCachedSizes(
+      ::google::protobuf::io::CodedOutputStream* output) const PROTOBUF_FINAL;
+  ::google::protobuf::uint8* InternalSerializeWithCachedSizesToArray(
+      bool deterministic, ::google::protobuf::uint8* target) const PROTOBUF_FINAL;
+  int GetCachedSize() const PROTOBUF_FINAL { return _cached_size_; }
+  private:
+  void SharedCtor();
+  void SharedDtor();
+  void SetCachedSize(int size) const PROTOBUF_FINAL;
+  void InternalSwap(plc_connection_params* other);
+  private:
+  inline ::google::protobuf::Arena* GetArenaNoVirtual() const {
+    return NULL;
+  }
+  inline void* MaybeArenaPtr() const {
+    return NULL;
+  }
+  public:
+
+  ::google::protobuf::Metadata GetMetadata() const PROTOBUF_FINAL;
+
+  // nested types ----------------------------------------------------
+
+  // accessors -------------------------------------------------------
+
+  // required string ip = 1;
+  bool has_ip() const;
+  void clear_ip();
+  static const int kIpFieldNumber = 1;
+  const ::std::string& ip() const;
+  void set_ip(const ::std::string& value);
+  #if LANG_CXX11
+  void set_ip(::std::string&& value);
+  #endif
+  void set_ip(const char* value);
+  void set_ip(const char* value, size_t size);
+  ::std::string* mutable_ip();
+  ::std::string* release_ip();
+  void set_allocated_ip(::std::string* ip);
+
+  // required int32 port = 2;
+  bool has_port() const;
+  void clear_port();
+  static const int kPortFieldNumber = 2;
+  ::google::protobuf::int32 port() const;
+  void set_port(::google::protobuf::int32 value);
+
+  // required int32 slave_id = 3;
+  bool has_slave_id() const;
+  void clear_slave_id();
+  static const int kSlaveIdFieldNumber = 3;
+  ::google::protobuf::int32 slave_id() const;
+  void set_slave_id(::google::protobuf::int32 value);
+
+  // @@protoc_insertion_point(class_scope:plc_module.plc_connection_params)
+ private:
+  void set_has_ip();
+  void clear_has_ip();
+  void set_has_port();
+  void clear_has_port();
+  void set_has_slave_id();
+  void clear_has_slave_id();
+
+  // helper for ByteSizeLong()
+  size_t RequiredFieldsByteSizeFallback() const;
+
+  ::google::protobuf::internal::InternalMetadataWithArena _internal_metadata_;
+  ::google::protobuf::internal::HasBits<1> _has_bits_;
+  mutable int _cached_size_;
+  ::google::protobuf::internal::ArenaStringPtr ip_;
+  ::google::protobuf::int32 port_;
+  ::google::protobuf::int32 slave_id_;
+  friend struct ::protobuf_plc_5fmodule_2eproto::TableStruct;
+  friend void ::protobuf_plc_5fmodule_2eproto::InitDefaultsplc_connection_paramsImpl();
+};
+// ===================================================================
+
+
+// ===================================================================
+
+#ifdef __GNUC__
+  #pragma GCC diagnostic push
+  #pragma GCC diagnostic ignored "-Wstrict-aliasing"
+#endif  // __GNUC__
+// plc_connection_params
+
+// required string ip = 1;
+inline bool plc_connection_params::has_ip() const {
+  return (_has_bits_[0] & 0x00000001u) != 0;
+}
+inline void plc_connection_params::set_has_ip() {
+  _has_bits_[0] |= 0x00000001u;
+}
+inline void plc_connection_params::clear_has_ip() {
+  _has_bits_[0] &= ~0x00000001u;
+}
+inline void plc_connection_params::clear_ip() {
+  ip_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
+  clear_has_ip();
+}
+inline const ::std::string& plc_connection_params::ip() const {
+  // @@protoc_insertion_point(field_get:plc_module.plc_connection_params.ip)
+  return ip_.GetNoArena();
+}
+inline void plc_connection_params::set_ip(const ::std::string& value) {
+  set_has_ip();
+  ip_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), value);
+  // @@protoc_insertion_point(field_set:plc_module.plc_connection_params.ip)
+}
+#if LANG_CXX11
+inline void plc_connection_params::set_ip(::std::string&& value) {
+  set_has_ip();
+  ip_.SetNoArena(
+    &::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::move(value));
+  // @@protoc_insertion_point(field_set_rvalue:plc_module.plc_connection_params.ip)
+}
+#endif
+inline void plc_connection_params::set_ip(const char* value) {
+  GOOGLE_DCHECK(value != NULL);
+  set_has_ip();
+  ip_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string(value));
+  // @@protoc_insertion_point(field_set_char:plc_module.plc_connection_params.ip)
+}
+inline void plc_connection_params::set_ip(const char* value, size_t size) {
+  set_has_ip();
+  ip_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(),
+      ::std::string(reinterpret_cast<const char*>(value), size));
+  // @@protoc_insertion_point(field_set_pointer:plc_module.plc_connection_params.ip)
+}
+inline ::std::string* plc_connection_params::mutable_ip() {
+  set_has_ip();
+  // @@protoc_insertion_point(field_mutable:plc_module.plc_connection_params.ip)
+  return ip_.MutableNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
+}
+inline ::std::string* plc_connection_params::release_ip() {
+  // @@protoc_insertion_point(field_release:plc_module.plc_connection_params.ip)
+  clear_has_ip();
+  return ip_.ReleaseNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
+}
+inline void plc_connection_params::set_allocated_ip(::std::string* ip) {
+  if (ip != NULL) {
+    set_has_ip();
+  } else {
+    clear_has_ip();
+  }
+  ip_.SetAllocatedNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ip);
+  // @@protoc_insertion_point(field_set_allocated:plc_module.plc_connection_params.ip)
+}
+
+// required int32 port = 2;
+inline bool plc_connection_params::has_port() const {
+  return (_has_bits_[0] & 0x00000002u) != 0;
+}
+inline void plc_connection_params::set_has_port() {
+  _has_bits_[0] |= 0x00000002u;
+}
+inline void plc_connection_params::clear_has_port() {
+  _has_bits_[0] &= ~0x00000002u;
+}
+inline void plc_connection_params::clear_port() {
+  port_ = 0;
+  clear_has_port();
+}
+inline ::google::protobuf::int32 plc_connection_params::port() const {
+  // @@protoc_insertion_point(field_get:plc_module.plc_connection_params.port)
+  return port_;
+}
+inline void plc_connection_params::set_port(::google::protobuf::int32 value) {
+  set_has_port();
+  port_ = value;
+  // @@protoc_insertion_point(field_set:plc_module.plc_connection_params.port)
+}
+
+// required int32 slave_id = 3;
+inline bool plc_connection_params::has_slave_id() const {
+  return (_has_bits_[0] & 0x00000004u) != 0;
+}
+inline void plc_connection_params::set_has_slave_id() {
+  _has_bits_[0] |= 0x00000004u;
+}
+inline void plc_connection_params::clear_has_slave_id() {
+  _has_bits_[0] &= ~0x00000004u;
+}
+inline void plc_connection_params::clear_slave_id() {
+  slave_id_ = 0;
+  clear_has_slave_id();
+}
+inline ::google::protobuf::int32 plc_connection_params::slave_id() const {
+  // @@protoc_insertion_point(field_get:plc_module.plc_connection_params.slave_id)
+  return slave_id_;
+}
+inline void plc_connection_params::set_slave_id(::google::protobuf::int32 value) {
+  set_has_slave_id();
+  slave_id_ = value;
+  // @@protoc_insertion_point(field_set:plc_module.plc_connection_params.slave_id)
+}
+
+#ifdef __GNUC__
+  #pragma GCC diagnostic pop
+#endif  // __GNUC__
+
+// @@protoc_insertion_point(namespace_scope)
+
+}  // namespace plc_module
+
+// @@protoc_insertion_point(global_scope)
+
+#endif  // PROTOBUF_plc_5fmodule_2eproto__INCLUDED

+ 9 - 0
plc/plc_module.proto

@@ -0,0 +1,9 @@
+syntax = "proto2";
+package plc_module;
+
+message plc_connection_params{
+    required string ip=1;
+    required int32 port=2;
+    required int32 slave_id=3;
+}
+

+ 88 - 0
test/plc_test.cpp

@@ -0,0 +1,88 @@
+//
+// Created by zx on 2019/12/28.
+//
+#include <iostream>
+#include "plc_communicator.h"
+
+class Test{
+public:
+    Test(Plc_Communicator* p){
+        mp_pc = p;
+    }
+    static ERROR_CODE callback(int terminal_id, void *p_owner)
+    {
+        Test* t = (Test*)p_owner;
+        t->m_term_id = terminal_id;
+        std::cout<<"callback "<<t->m_term_id<<" called."<<std::endl;
+        usleep(1000 * 1000);
+        if(t->mp_pc!=0){
+            // Plc_Task task;
+            struct measure_result result;
+            result.terminal_id = t->m_term_id;
+            result.correctness = terminal_id % 2 == 0?true:false;
+            result.x = 3501.2f;
+            result.angle = 91.35f;
+            result.wheel_base = 2651.6f;
+            ERROR_CODE ec = t->m_task.set_result(result);
+            std::cout<<"callback set result "<<ec<<std::endl;
+            std::thread* th = new std::thread(execute_thread, t);
+            th->detach();
+        }
+        return ERROR_CODE::SUCCESS;
+    }
+
+    static void execute_thread(Test* t){
+        usleep(1000 * 2000);
+        if(t->mp_pc!= 0){
+            ERROR_CODE ec = t->mp_pc->execute_task(&t->m_task);
+            std::cout << "thread execute task " << ec << std::endl;
+
+            int count = 5;
+            while (count-- > 0)
+            {
+                std::cout << "---------- "<<count<<" --------" << std::endl;
+                std::vector<uint16_t> vec;
+                ec = t->mp_pc->get_plc_data(vec, t->m_term_id);
+                std::cout << "get data " << ec << std::endl;
+                std::cout << "[ ";
+                for (size_t i = 0; i < vec.size(); i++)
+                {
+                    std::cout << vec[i] << " ";
+                }
+                std::cout << "]" << std::endl;
+
+                ec = t->mp_pc->set_status_update_timeout(15000);
+                std::cout << "set status " << ec << std::endl;
+
+                ec = t->mp_pc->get_error();
+                std::cout << "get error " << ec << std::endl;
+
+                bool init_state = t->mp_pc->get_initialize_status();
+                std::cout << "get initialize status " << init_state << std::endl;
+
+                bool conn = t->mp_pc->get_connection();
+                std::cout << "get connection status " << conn << std::endl;
+                usleep(1000 * 1500);
+            }
+        }
+    }
+private:
+    int m_term_id;
+    Plc_Communicator* mp_pc;
+    Plc_Task    m_task;
+};
+
+int main(int argc,char* argv[])
+{
+    plc_module::plc_connection_params params;
+    params.set_ip("192.168.2.131");
+    params.set_port(502);
+    params.set_slave_id(1);
+    Plc_Communicator pc(params);
+    Test test(&pc);
+    ERROR_CODE ec = pc.set_plc_callback(test.callback, &test);
+    std::cout<<"set callback "<<ec<<std::endl;
+
+    getchar();
+    return 0;
+}

+ 36 - 0
tool/MeasureTopicPublisher.cpp

@@ -0,0 +1,36 @@
+//
+// Created by zx on 2019/11/25.
+//
+
+#include "MeasureTopicPublisher.h"
+#include "sys/time.h"
+#include "unistd.h"
+#include "math.h"
+MeasureTopicPublisher* MeasureTopicPublisher::g_publisher=0;
+MeasureTopicPublisher* MeasureTopicPublisher::GetInstance()
+{
+    if(g_publisher==0)
+    {
+        g_publisher=new MeasureTopicPublisher(CONNECTSTRING);
+    }
+    return g_publisher;
+}
+MeasureTopicPublisher::MeasureTopicPublisher(std::string connectStr)
+{
+    m_sock.bind(connectStr);
+    gettimeofday(&m_last_time,0);
+}
+
+bool MeasureTopicPublisher::Publish(std::string data)
+{
+    std::lock_guard<std::mutex> lk(m_lock);
+    /*double tick=0;
+    do{
+        struct timeval tm;
+        gettimeofday(&tm, 0);
+        tick=fabs((tm.tv_sec-m_last_time.tv_sec)*1000.0+(tm.tv_usec-m_last_time.tv_usec)/1000.0);
+        m_last_time=tm;
+    }while(tick<10);*/
+    usleep(100);
+    return m_sock.send(data)==data.length();
+}

+ 37 - 0
tool/MeasureTopicPublisher.h

@@ -0,0 +1,37 @@
+//
+// Created by zx on 2019/11/25.
+//
+
+#ifndef MEASURETOPICSERVER_H
+#define MEASURETOPICSERVER_H
+
+#include <nnxx/message.h>
+#include <nnxx/message_control.h>
+#include <nnxx/socket.h>
+#include <nnxx/pubsub.h>
+#include <nnxx/timeout.h>
+#include <nnxx/error.h>
+#include <string>
+#include <iostream>
+#include <mutex>
+
+#define CONNECTSTRING "tcp://127.0.0.1:10080"
+
+class MeasureTopicPublisher
+{
+public:
+    static MeasureTopicPublisher* GetInstance();
+    bool Publish(std::string data);
+private:
+    MeasureTopicPublisher(std::string connectStr);
+
+private:
+    struct timeval         m_last_time;
+    nnxx::socket m_sock{nnxx::SP, nnxx::PUB};
+
+    std::mutex  m_lock;
+    static MeasureTopicPublisher* g_publisher;
+};
+
+
+#endif //MEASURETOPICSERVER_H

plc/StdCondition.cpp → tool/StdCondition.cpp


plc/StdCondition.h → tool/StdCondition.h