第一句子网 - 唯美句子、句子迷、好句子大全
第一句子网 > QT调用Saleae C#生成的dll文件

QT调用Saleae C#生成的dll文件

时间:2023-01-25 10:41:59

相关推荐

QT调用Saleae C#生成的dll文件

背景:设计开发PEPS的测试设备时,需要对manchester 等波形采集数据,实际使用时常出现波形解析不准确,设置参数多等原因导致数据采集出现问题。所以冒出个想法是否可以直接使用逻辑分析仪输出的数据进行分析,这样就避免了在嵌入式中编写过多的冗余代码来实现各种功能,将数据在上位机中处理。

在查找了各方面的资料后,发现saleae是支持该方法的使用。

Protocol Analyzer SDK - Saleae Support

该文档中介绍了该如何使用编译相关SDK包 ,example包的使用,下载路径。内容详细,不多介绍。

下载最新的逻辑分析仪软件:我使用的是Logic1.2.29.exe 这个版本

Logic 1.x Download - Saleae Support

编译环境:VS,安装C#库

实操:

下载相关代码完成后,首先使用自带的example进行编译,跑一下例程,熟悉SDK的各个接口

1)打开SaleaeSocketApi.sln 编译,可以直接编译通过,直接下载的代码库没有任何错误。设置SaleaeSocketApiExample”设为启动项目“

2)打开Logic1.2.29.exe 设置如下 options->Preferences->Developer选项卡。设置接口为10429,如下图所示

3)运行example,在console下直接点击回车键,完成example的运行。

关键API解读

在example的代码中发现我们比较关注的API接口主要有如下几种:

1)Capture():开启逻辑分析仪触发指令,相当于点击start按钮

2)StopCapture():相当于点击 stop 按钮

3)void SetCaptureSeconds(double seconds):设置采集时间

4)void ExportAnalyzers( int selected, String filename, bool mXmitFile ):导出解析出来的数据

其中ExportAnalyzers() 函数使用了较多的参数,并且分析出来的数据只能存储在文件中,并不方便我们使用。对其进行简单改写,满足使用要求:

public String ExportAnalyzers_QT(){String filename = System.IO.Directory.GetCurrentDirectory() + "\\xuhy.csv"; //固定文件名,写入程序目录下String export_command = export_analyzer_cmd + ", ";export_command += '1' + ", " + filename;export_command += ", mXmitFile";WriteString(export_command);​String response = "";GetResponse(ref response);return response; //将接收到的字符串返回}

以上4种API可以协助我们完成利用逻辑分析仪解析出数据的功能。

封装QT使用的dll库

由于本人并不熟悉C#的代码编写,所以还是决定使用C++/QT为基础完成后续的相关代码编写。这就需要将C#的API封装成可供QT使用的库。

以下内容参考该链接:Qt程序中调用C#编写的dll(推荐) / 张生荣

创建CLR工程,填写如下代码,将C#的dll再次打包可供QT使用的dll文件:

#pragma once#include <vcclr.h>#include "stdlib.h"#include "string.h"#include "msclr\marshal_cppstd.h"using namespace msclr::interop;using namespace System;using namespace System::Reflection;using namespace System::Runtime::InteropServices;​#using "F:\03_study\02_SaleaeSocketApi\CSaleaeSocketApi\CSaleaeSocketApi\CSaleaeSocketApi\SaleaeSocketApi.dll"using namespace SaleaeSocketApi;​extern "C" __declspec(dllexport) void SetCaptureSeconds(double seconds){SaleaeSocketApi::SaleaeClient^ obj = gcnew SaleaeClient("127.0.0.1", 10429);return obj->SetCaptureSeconds(seconds);}​extern "C" __declspec(dllexport) void Capture(){SaleaeSocketApi::SaleaeClient^ obj = gcnew SaleaeClient("127.0.0.1", 10429);obj->Capture();}​extern "C" __declspec(dllexport) void StopCapture(){SaleaeSocketApi::SaleaeClient^ obj = gcnew SaleaeClient("127.0.0.1", 10429);obj->StopCapture();}​extern "C" __declspec(dllexport) char* ExportAnalyzers(void){SaleaeSocketApi::SaleaeClient^ obj = gcnew SaleaeClient("127.0.0.1", 10429);String^ get_str = obj->ExportAnalyzers_QT();​IntPtr str = Marshal::StringToHGlobalAnsi(get_str);char* strData = reinterpret_cast<char*>(static_cast<void*>(str));return strData;}​namespace CSaleaeSocketApi {​public ref class Class1{// TODO: 在此处添加此类的方法。};}

编译完成后会生成*.dll , *.lib文件,供QT工程使用。

编写QT工程

该工程直接使用QT example目录下的assistant进行

初始化sdk中apitypedef void(*SetCaptureSeconds)(double seconds);typedef void(*Capture)();typedef char*(*ExportAnalyzers)();CLR工程与QT工程对应关系如下:

导入dll库并使用:

QLibrary library("./CSaleaeSocketApi.dll");if (library.load()) {SetCaptureSeconds m_SetCaptureSeconds = (SetCaptureSeconds)library.resolve("SetCaptureSeconds");Capture m_Capture = (Capture)library.resolve("Capture");ExportAnalyzers m_ExportAnalyzers = (ExportAnalyzers)library.resolve("ExportAnalyzers");​if (m_SetCaptureSeconds){m_SetCaptureSeconds(5);}if (m_Capture){m_Capture();}if (m_ExportAnalyzers){ file_name = m_ExportAnalyzers();qDebug() << file_name;}}

library.resolve("API名"):中API名的获取:使用dumpbin工具:

dumpbin -exports ***.dll

演示示例

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。