Browse Source

change modulecomm, add python interface, not complete.

yuchuli 11 months ago
parent
commit
fd1e676cfa

+ 28 - 48
src/common/modulecomm/CMakeLists.txt

@@ -1,7 +1,7 @@
-cmake_minimum_required(VERSION 3.10)  
+cmake_minimum_required(VERSION 3.14)  
   
 # 设置项目名称  
-project(modulecomm)  
+project(example)  
   
 # 设置C++标准(如果需要)  
 set(CMAKE_CXX_STANDARD 11)  
@@ -10,30 +10,34 @@ set(CMAKE_CXX_STANDARD 11)
 set(CMAKE_SHARED_LIBRARY_PREFIX "")
 set(CMAKE_SHARED_LIBRARY_SUFFIX ".so")
 
-add_custom_command(  
-    OUTPUT modulecomm_wrap.cxx
-    COMMAND swig -c++ -python  -outcurrentdir ./../modulecomm.i  # 这是要运行的命令  
-    COMMENT "Running script to generate files" # 这条信息将在构建时显示  
-    VERBATIM # 这使得命令中的参数被直接传递,而不是被CMake进一步处理  
+# 首先,我们尝试使用find_package找到Python库  
+find_package(PythonLibs REQUIRED)  
+# 然后,我们尝试使用find_path找到Python.h  
+find_path(PYTHON_INCLUDE_DIRS  
+    NAMES Python.h  
+    PATHS ${PYTHON_LIBRARIES_DIR}  
+    NO_DEFAULT_PATH  
 )  
-# 假设你已经找到了PYTHON_EXECUTABLE  
-find_path(PYTHON_INCLUDE_DIRS NAMES Python.h PATHS "/usr/include/python3.10" PATH_SUFFIXES include  NO_DEFAULT_PATH)  
-   
-  
-if(PYTHON_INCLUDE_DIRS)  
-    message(STATUS "Found Python include dirs at ${PYTHON_INCLUDE_DIRS}")  
-    include_directories(${PYTHON_INCLUDE_DIRS})  
-else()  
-    message(FATAL_ERROR "Could not find Python include dirs")  
-endif()
+# 如果在PYTHON_LIBRARIES_DIR中找不到Python.h,那么我们将搜索默认的路径  
+if(NOT PYTHON_INCLUDE_DIRS)  
+    find_path(PYTHON_INCLUDE_DIRS  
+        NAMES Python.h  
+    )  
+endif()  
+
 
-# 查找Qt组件  
+#set(BP "310")
 
+#find_package(Boost REQUIRED COMPONENTS python${BP})
+
+find_package(Boost REQUIRED COMPONENTS python numpy system)
 find_package(Qt5 COMPONENTS Core Xml REQUIRED) 
+  
 
 # 添加你的源文件  
 
-set(SOURCE_FILES  
+set(SOURCE_FILES
+    modulecommpython.cpp  
     modulecomm.cpp
     modulecomm_base.cpp
     shm/modulecomm_shm.cpp
@@ -69,37 +73,13 @@ qt5_wrap_cpp(MOC_SOURCES ${HEADER_FILES})
 # 将moc生成的文件添加到源文件中  
 
 list(APPEND SOURCE_FILES ${MOC_SOURCES})  
-  
-# 添加源文件  
-add_library(libmodulecomm SHARED ${SOURCE_FILES}) 
-
-#add_library(adcmodulecomm SHARED ${SOURCE_FILES}) 
-  
-# 添加头文件路径  
-target_include_directories(libmodulecomm PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/shm ${CMAKE_CURRENT_SOURCE_DIR}/inter)  
+# 现在MY_LATEST_SO_LIBRARY变量包含了找到的最新库的完整路径  
 
-# 链接Qt库  
+# 你可以将它链接到你的目标中  
 
-target_link_libraries(libmodulecomm Qt5::Core Qt5::Xml)  
+add_library(modulecommpython SHARED ${SOURCE_FILES}) 
 
-# 添加源文件  
-add_library(_modulecomm SHARED ${SOURCE_FILES} modulecomm_wrap.cxx) 
-
-# 设置OUTPUT_NAME属性来去掉lib前缀  
-
-#set_target_properties(_modulecomm PROPERTIES OUTPUT_NAME adcmodulecomm) 
-
-#add_library(adcmodulecomm SHARED ${SOURCE_FILES}) 
-  
 # 添加头文件路径  
-target_include_directories(_modulecomm PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/shm ${CMAKE_CURRENT_SOURCE_DIR}/inter)  
+target_include_directories(modulecommpython PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} ${PYTHON_INCLUDE_DIRS}  ${CMAKE_CURRENT_SOURCE_DIR}/shm ${CMAKE_CURRENT_SOURCE_DIR}/inter)  
 
-# 链接Qt库  
-
-target_link_libraries(_modulecomm Qt5::Core Qt5::Xml)  
-  
-# 安装目标(如果需要)  
-#install(TARGETS MyLibrary DESTINATION lib)  
-  
-# 如果库依赖于其他库,可以使用 target_link_libraries 添加依赖  
-# 例如:target_link_libraries(MyLibrary PRIVATE SomeOtherLibrary)
+target_link_libraries(modulecommpython Boost::python Boost::numpy Boost::system  Qt5::Core Qt5::Xml)

+ 126 - 0
src/common/modulecomm/CMakeListsSwig.txt

@@ -0,0 +1,126 @@
+cmake_minimum_required(VERSION 3.10)  
+  
+# 设置项目名称  
+project(modulecomm)  
+  
+# 设置C++标准(如果需要)  
+set(CMAKE_CXX_STANDARD 11)  
+#set(CMAKE_CXX_STANDARD_REQUIRED ON)  
+
+set(CMAKE_SHARED_LIBRARY_PREFIX "")
+set(CMAKE_SHARED_LIBRARY_SUFFIX ".so")
+
+add_custom_command(  
+    OUTPUT modulecomm_wrap.cxx
+    COMMAND swig -c++ -python  -outcurrentdir ./../modulecomm.i  # 这是要运行的命令  
+    COMMENT "Running script to generate files" # 这条信息将在构建时显示  
+    VERBATIM # 这使得命令中的参数被直接传递,而不是被CMake进一步处理  
+)
+
+# 首先,我们尝试使用find_package找到Python库  
+find_package(PythonLibs REQUIRED)  
+# 然后,我们尝试使用find_path找到Python.h  
+find_path(PYTHON_INCLUDE_DIRS  
+    NAMES Python.h  
+    PATHS ${PYTHON_LIBRARIES_DIR}  
+    NO_DEFAULT_PATH  
+)  
+# 如果在PYTHON_LIBRARIES_DIR中找不到Python.h,那么我们将搜索默认的路径  
+if(NOT PYTHON_INCLUDE_DIRS)  
+    find_path(PYTHON_INCLUDE_DIRS  
+        NAMES Python.h  
+    )  
+endif()  
+
+  
+
+# 打印找到的路径  
+
+message(STATUS "Python include dirs: ${PYTHON_INCLUDE_DIRS}")  
+# 假设你已经找到了PYTHON_EXECUTABLE  
+#find_path(PYTHON_INCLUDE_DIRS NAMES Python.h PATHS "/usr/include/python3.10" PATH_SUFFIXES include  NO_DEFAULT_PATH)  
+   
+  
+#if(PYTHON_INCLUDE_DIRS)  
+#    message(STATUS "Found Python include dirs at ${PYTHON_INCLUDE_DIRS}")  
+#    include_directories(${PYTHON_INCLUDE_DIRS})  
+#else()  
+#    message(FATAL_ERROR "Could not find Python include dirs")  
+#endif()
+
+# 查找Qt组件  
+
+find_package(Qt5 COMPONENTS Core Xml REQUIRED) 
+
+# 添加你的源文件  
+
+set(SOURCE_FILES  
+    modulecomm.cpp
+    modulecomm_base.cpp
+    shm/modulecomm_shm.cpp
+    shm/procsm.cpp
+    shm/procsm_if.cpp
+    inter/intercomm.cpp
+    inter/modulecomm_inter.cpp
+)  
+
+  
+
+# 添加你的头文件,moc会处理这些文件  
+
+set(HEADER_FILES  
+   ivmodulemsg_type.h
+   modulecomm.h
+   modulecomm_base.h
+   ivstdcolorout.h
+   shm/modulecomm_shm.h
+   shm/procsm.h 
+   shm/procsm_if.h
+
+)  
+
+  
+
+# 使用qt5_wrap_cpp来处理moc文件  
+
+qt5_wrap_cpp(MOC_SOURCES ${HEADER_FILES})  
+
+  
+
+# 将moc生成的文件添加到源文件中  
+
+list(APPEND SOURCE_FILES ${MOC_SOURCES})  
+  
+# 添加源文件  
+add_library(libmodulecomm SHARED ${SOURCE_FILES}) 
+
+#add_library(adcmodulecomm SHARED ${SOURCE_FILES}) 
+  
+# 添加头文件路径  
+target_include_directories(libmodulecomm PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/shm ${CMAKE_CURRENT_SOURCE_DIR}/inter)  
+
+# 链接Qt库  
+
+target_link_libraries(libmodulecomm Qt5::Core Qt5::Xml)  
+
+# 添加源文件  
+add_library(_modulecomm SHARED ${SOURCE_FILES} modulecomm_wrap.cxx) 
+
+# 设置OUTPUT_NAME属性来去掉lib前缀  
+
+#set_target_properties(_modulecomm PROPERTIES OUTPUT_NAME adcmodulecomm) 
+
+#add_library(adcmodulecomm SHARED ${SOURCE_FILES}) 
+  
+# 添加头文件路径  
+target_include_directories(_modulecomm PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/shm ${CMAKE_CURRENT_SOURCE_DIR}/inter)  
+
+# 链接Qt库  
+
+target_link_libraries(_modulecomm Qt5::Core Qt5::Xml)  
+  
+# 安装目标(如果需要)  
+#install(TARGETS MyLibrary DESTINATION lib)  
+  
+# 如果库依赖于其他库,可以使用 target_link_libraries 添加依赖  
+# 例如:target_link_libraries(MyLibrary PRIVATE SomeOtherLibrary)

+ 73 - 0
src/common/modulecomm/PyModuleComm.py

@@ -0,0 +1,73 @@
+
+import threading  
+import time  
+
+import modulecommpython
+import numpy as np  
+  
+class PyModuleComm:  
+    def __init__(self,strname):  
+        # 初始化代码...  
+        print("name: ",strname)
+        self.strmemname = strname
+        self.mbRegister = False
+        self.mnMode = 0
+        self.obj = modulecommpython.get_ca_object()
+        pass  
+
+    def RegisterRecv(self,call):
+        if self.mbRegister:
+            print(" Have register, can't register other.")
+            return
+        print("Register: ",self.strmemname)
+        self.mpcall = call
+        self.mbRegister = True
+        self.mbRun = True
+        self.mpthread = threading.Thread(target=self.threadrecvdata, args=(self.strmemname,))  
+        self.mnMode = 1
+
+    def RegiseterSend(self,nSize,nPacCount):
+        if self.mbRegister:
+            print(" Have register, can't register other.")
+            return
+        print("Register: ",self.strmemname)
+        self.mnsize = nSize
+        self.mnPacCount = nPacCount
+        self.mbRegister = True
+        self.mnMode = 2
+        self.obj.RegisterSend(self.strmemname)
+    
+    def SendData(self,arr):      
+        nsendsize = 1000
+        nrealsize = 0
+        nrealsize = np.zeros(1, dtype=np.int32)  
+        nrtn = self.obj.RecvData(arr,nsendsize,nrealsize)
+        print("senddata, nrtn: ",nrtn)
+        print("senddata, realsize: ",nrealsize)
+        pass
+  
+    def threadrecvdata(self, arg):  
+        # 这个函数将被线程执行  
+        print(f"线程开始执行,参数是 {arg}")  
+        while(self.mbRun):
+            print("thread name: ",self.strmemname)
+            time.sleep(0.1)
+            self.mpcall(self.strmemname)
+
+    def stop_thread(self):
+        self.mbRun = False
+        self.mpthread.join()
+  
+    def start_thread(self, arg):  
+        # 创建线程对象,target参数指向要在线程中运行的函数  
+        self.mbRun = True
+        self.mpthread = threading.Thread(target=self.my_function, args=(arg,))  
+          
+        # 启动线程  
+        self.mpthread.start()  
+          
+        # 可以在这里添加其他代码,主线程会继续执行  
+        print("主线程继续执行...")  
+  
+        # 如果需要等待线程结束,可以调用 join() 方法  
+        # thread.join()  

+ 3 - 0
src/common/modulecomm/modulecomm.cpp

@@ -180,6 +180,7 @@ void  *  RegisterRecv(const char * strcommname,SMCallBack pCall,ModuleComm_TYPE
     return pmi;
 }
 
+
 void *  RegisterRecvPlus(const char * strcommname,ModuleFun xFun,
                                                 ModuleComm_TYPE xmctype,const char * strip,
                          const unsigned short nPort)
@@ -290,5 +291,7 @@ void * RegisterRecvPlus(const char * strcommname,ModuleFun xFun)
     return RegisterRecvPlus(strcommname,xFun,iv::modulecomm::ModuleComm_UNDEFINE,0,5100);
 }
 
+
+
 }
 }

+ 1 - 0
src/common/modulecomm/modulecomm_base.h

@@ -10,6 +10,7 @@
 
 typedef std::function<void(const char * ,const unsigned int , const unsigned int , QDateTime * ,const char *)> ModuleFun;
 typedef void (* SMCallBack)(const char * strdata,const unsigned int nSize,const unsigned int index,const QDateTime * dt,const char * strmemname);
+typedef void (* SMPyCallBack)(const char * strdata,const unsigned int nSize,const unsigned int index,const int64_t * dt,const char * strmemname);
 #define IV_MODULE_FUN
 #endif
 

+ 1 - 0
src/common/modulecomm/shm/modulecomm_shm.cpp

@@ -26,6 +26,7 @@ void modulecomm_shm::RegisterRecv(const char *strcommname, SMCallBack pCall)
     mpif = pif;
 }
 
+
 void modulecomm_shm::RegisterRecvPlus(const char *strcommname, ModuleFun xFun)
 {
     procsm_if * pif = new procsm_if(strcommname,0,0,procsm::ModeRead);

+ 42 - 0
src/common/modulecomm/test.py

@@ -0,0 +1,42 @@
+import modulecommpython
+import numpy as np  
+import time
+
+from PyModuleComm import PyModuleComm
+  
+
+def my_python_callback(value):  
+    print("Python callback function called from C++!. value: ",value)  
+
+def main():  
+    arr = np.array([1, 2, 3, 4, 5], dtype=np.uint8) 
+    obj = modulecommpython.get_ca_object()
+    obj.SetCall(my_python_callback)
+    print("obj")
+
+    result = obj.Add(arr) 
+
+    print(result)  # 输出: 5
+
+    print("np2 : ",arr[2])
+    # 初始化一个变量  
+    count = 0  
+
+    mc = PyModuleComm("hcp2_gpsimu")
+    mc.RegiseterSend(1000,1)
+
+  
+    # 使用while循环,只要count小于10,就继续循环  
+    while count < 10:  
+        time.sleep(1.0)
+        obj.TestCall()
+        arr = np.zeros(1000, dtype=np.uint8)  
+        mc.SendData(arr)
+ #       print(f"当前计数是: {count}")  
+ #       count += 1  # 每次循环,增加count的值  
+  
+    print("循环结束!")  
+  
+# 调用main函数  
+if __name__ == "__main__":  
+    main()