当前位置:网站首页>Canoe cannot automatically identify serial port number? Then encapsulate a DLL so that it must work
Canoe cannot automatically identify serial port number? Then encapsulate a DLL so that it must work
2022-07-06 09:48:00 【Ant soldier】
Preface
For example, program-controlled power supply , Every colleague needs , But the project is the same , So every colleague before running the project , Choose different serial ports , This is very troublesome , Can you make CANoe Automatically identify the serial port number of the program-controlled power supply connected to the computer
CAPL The built-in function of operating the serial port cannot automatically identify the serial port number , After searching for information , call Windows The equipment API It can be realized , Then I thought of packaging DLL, stay CANoe Call in DLL To achieve the goal .
Demonstrate hardware and software environment
Win10 x64;CANoe 11 SP2 x64;Visual Studio 2019


C++ Commissioning project
1️⃣ Old rules , encapsulation DLL Before that, let's create a new console project to debug the code

2️⃣ The figure below , It's connected to my computer COM
- The description of connecting the program-controlled power supply to the computer is
USB Serial Device (COMxx), that I can go through Key stringUSB Serial DeviceTo implement filtering , To return to COM Number

3️⃣ All the code , as follows :
// PrintDeviceInfo.cpp : Defines the entry point for the console application .
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <Windows.h>
#include <setupapi.h>
#pragma comment(lib, "setupapi.lib")
int getSpecialComNum(const char* s, const char* t)
{
char n = t[0];
int l ,i,j, sourlen, tarlen;
char temp[200] = {
0 };
char out[200] = {
0 };
sourlen = strlen(s);
tarlen = strlen(t);
for (i = 0; i < sourlen - tarlen + 1; i++)
{
for (j = 0; j < tarlen; j++)
{
if (s[i + j] != t[j])
{
break;
}
}
if (j == tarlen)
{
return i+7; //find it
}
}
return -1;
}
// From the device information Get the specific serial port number @return <=0 Invalid
int getSpecialComNumFromDevInfo(char ComList[][200] , const char* SpecialCom ,int *ComNum)
{
HDEVINFO hDevInfo = SetupDiGetClassDevs(NULL, NULL, NULL, DIGCF_PRESENT | DIGCF_ALLCLASSES);
if (hDevInfo == INVALID_HANDLE_VALUE)
{
printf("SetupDiGetClassDevs Err:%d", GetLastError());
return -2;
};
SP_CLASSIMAGELIST_DATA _spImageData = {
0 };
_spImageData.cbSize = sizeof(SP_CLASSIMAGELIST_DATA);
SetupDiGetClassImageList(&_spImageData);
SP_DEVINFO_DATA spDevInfoData = {
0 };
spDevInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
int k;
k = 0;
for (DWORD i = 0; SetupDiEnumDeviceInfo(hDevInfo, i, &spDevInfoData); i++)
{
char szBuf[MAX_PATH] = {
0 };
int wImageIdx = 0;
short wItem = 0;
char szBuf_byteToChar[256] = {
0 };
int j;
if (!SetupDiGetDeviceRegistryPropertyA(hDevInfo, &spDevInfoData, SPDRP_CLASS, NULL, (PBYTE)szBuf, MAX_PATH, 0))
{
continue;
};
if (strcmp(szBuf, "Ports") != 0) // Port only
{
continue;
}
//printf("Class1:%s\r\n", szBuf);
if (SetupDiGetClassImageIndex(&_spImageData, &spDevInfoData.ClassGuid, &wImageIdx))
{
char szName[MAX_PATH] = {
0 };
DWORD dwRequireSize;
if (!SetupDiGetClassDescription(&spDevInfoData.ClassGuid, (PWSTR)szBuf, MAX_PATH, &dwRequireSize))
{
continue;
};
//wprintf(L"Class:%s\r\n", szBuf);
if (SetupDiGetDeviceRegistryProperty(hDevInfo, &spDevInfoData, SPDRP_FRIENDLYNAME, NULL, (PBYTE)szName, MAX_PATH - 1, 0))
{
//wprintf(L"FriendlyName:%s\r\n\r\n", szName);
sprintf_s(szBuf_byteToChar, "");
for (j = 0; j < sizeof(szName); j++)
{
sprintf_s(szBuf_byteToChar, "%s%c", szBuf_byteToChar, szName[j]);
}
strcpy_s(ComList[k], szBuf_byteToChar);
int comNum = getSpecialComNum(szBuf_byteToChar, SpecialCom);
//wprintf(L"comNum:%s\r\n\r\n", comNum);
k++;
*ComNum = k;
if (comNum > 0)
{
return comNum;
}
}
else if (SetupDiGetDeviceRegistryProperty(hDevInfo, &spDevInfoData, SPDRP_DEVICEDESC, NULL, (PBYTE)szName, MAX_PATH - 1, 0))
{
wprintf(L"Device:%s\r\n\r\n", szName);
}
}
}
SetupDiDestroyClassImageList(&_spImageData);
return -1;
}
int main(int argc, char* argv[])
{
char comList[50][200];
char SpecialCom[100] = "USB Serial Device";
int serialPortsNum;
//printf("Locale is: %s\n", setlocale(LC_ALL, "chs"));
printf(" Based on the description %s, Find the serial port number :%d\n", SpecialCom,getSpecialComNumFromDevInfo(comList, SpecialCom, &serialPortsNum));
printf(" Serial port number : %d\n", serialPortsNum);
for (int i = 0; i < serialPortsNum; i++)
{
printf("%d: %s\n", i,comList[i]);
}
//PrintDevicesInfo2();
getchar();
return 0;
}
4️⃣ Debug See the results of the next project , Back to what we wanted

Encapsulated into DLL
- CANoe encapsulation DLL, I won't go into that here , Details can be found in CANoe DLL Programming series

CANoe call DLL demonstration
1️⃣ Create a new NetWork Module Node

2️⃣ can The code of the script is as follows ,:
Core function :dllGetSeriesPorts(comList,SpecialComDescribtion,serialPortsNum);
- comList : Return all the Port Description of mouth
- SpecialComDescribtion: Input parameters , Select to specify Port Description of mouth , such as Program controlled power supply COM The description is
USB Serial Device - serialPortsNum : Input parameters , Back to all Ports Statistics of population
/*@!Encoding:936*/
includes
{
#pragma library("autoSerialPort.dll")
}
int connectSerialPort(char SpecialComDescribtion[])
{
char comList[50][200];
long serialPortsNum;
long retCom;
int i ,RetVal;
retCom = dllGetSeriesPorts(comList,SpecialComDescribtion,serialPortsNum);
write("Return COM Port:%d",retCom);
write("Total COM Ports Number:%d",serialPortsNum);
for ( i = 0; i < serialPortsNum; i++)
{
write("%d: %s", i,comList[i]);
}
RetVal = RS232Open(retCom);
if (RetVal == 1 )
write("Open port %d Ok.",retCom);
else
write("Open port %d failed.",retCom);
RetVal = RS232Configure(retCom,9600,8,1,0 );
if (RetVal == 1 )
write("Configure port %d Ok.",retCom);
else
write("Configure port %d failed.",retCom);
return RetVal;
}
on key 'k'
{
connectSerialPort("USB Serial Device");
}
3️⃣ The test results are as follows , Achieve the goal


| End |
summary


- CSDN Source download , Can the boss in need give the blogger a Hua Zi ?
- It needs to be used in the demonstration demo Engineering and DLL Of documents , You can follow the official account online disk below to get it by yourself , Thank you for reading .

- Have the most simple life , The furthest dream , Even if it's freezing tomorrow , Lu Yao's horse died !
- Wechat partners can pay attention Langge on-board diagnosis , A small circle in the industry , In the group
SkyDrive data,Source code,There are all kinds of godsFree time communication technology , Talk about job opportunities .- If this blog is helpful to you , please “ give the thumbs-up ” “ Comment on ”“ Collection ” One key, three links Oh ! It's not easy to code words , Everyone's support is my driving force to stick to it .
边栏推荐
- Lua script of redis
- CANoe下载地址以及CAN Demo 16的下载与激活,并附录所有CANoe软件版本
- Listen to my advice and learn according to this embedded curriculum content and curriculum system
- [Chongqing Guangdong education] reference materials for nine lectures on the essence of Marxist Philosophy in Wuhan University
- 有软件负载均衡,也有硬件负载均衡,选择哪个?
- Sqlmap installation tutorial and problem explanation under Windows Environment -- "sqlmap installation | CSDN creation punch in"
- 竞赛vscode配置指南
- CAPL 脚本打印函数 write ,writeEx ,writeLineEx ,writeToLog ,writeToLogEx ,writeDbgLevel 你真的分的清楚什么情况下用哪个吗?
- leetcode-14. Longest common prefix JS longitudinal scanning method
- MapReduce instance (x): chainmapreduce
猜你喜欢

面渣逆袭:Redis连环五十二问,图文详解,这下面试稳了

基于B/S的网上零食销售系统的设计与实现(附:源码 论文 Sql文件)

Mapreduce实例(四):自然排序

嵌入式开发比单片机要难很多?谈谈单片机和嵌入式开发设计经历

CAPL脚本中关于相对路径/绝对路径操作的几个傻傻分不清的内置函数

51单片机进修的一些感悟

Design and implementation of online snack sales system based on b/s (attached: source code paper SQL file)

Activiti7工作流的使用

Une grande vague d'attaques à la source ouverte

max-flow min-cut
随机推荐
Single chip microcomputer realizes modular programming: Thinking + example + system tutorial (the degree of practicality is appalling)
Hero League rotation map automatic rotation
Nc29 search in two-dimensional array
工作流—activiti7环境搭建
Yarn organizational structure
Redis distributed lock implementation redison 15 questions
Global and Chinese markets for modular storage area network (SAN) solutions 2022-2028: Research Report on technology, participants, trends, market size and share
One article read, DDD landing database design practice
MapReduce instance (VII): single table join
DCDC power ripple test
MapReduce instance (VI): inverted index
一文读懂,DDD落地数据库设计实战
嵌入式开发比单片机要难很多?谈谈单片机和嵌入式开发设计经历
018.有效的回文
Global and Chinese markets for hardware based encryption 2022-2028: Research Report on technology, participants, trends, market size and share
为什么要数据分层
Teach you how to write the first MCU program hand in hand
美团二面:为什么 Redis 会有哨兵?
Learning SCM is of great help to society
Defensive C language programming in embedded development
