当前位置:网站首页>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 .
边栏推荐
- 《ASP.NET Core 6框架揭秘》样章发布[200页/5章]
- Activiti7工作流的使用
- YARN组织架构
- 068. Find the insertion position -- binary search
- How does the single chip microcomputer execute the main function from power on reset?
- Selection of software load balancing and hardware load balancing
- 硬件工程师的真实前途我说出来可能你们不信
- How can I take a shortcut to learn C language in college
- Mapreduce实例(五):二次排序
- May brush question 27 - figure
猜你喜欢

Mapreduce实例(十):ChainMapReduce

How can I take a shortcut to learn C language in college

Mapreduce实例(五):二次排序

【深度学习】语义分割:论文阅读(NeurIPS 2021)MaskFormer: per-pixel classification is not all you need

零基础学习单片机切记这四点要求,少走弯路

【深度學習】語義分割-源代碼匯總

Single chip microcomputer realizes modular programming: Thinking + example + system tutorial (the degree of practicality is appalling)

Solve the problem of too many small files

MapReduce instance (VII): single table join

学习单片机对社会的帮助是很大的
随机推荐
CAP理论
工作流—activiti7环境搭建
Oom happened. Do you know the reason and how to solve it?
Several ways of MySQL database optimization (pen interview must ask)
Summary of May training - from a Guang
通过bat脚本配置系统环境变量
CANoe不能自动识别串口号?那就封装个DLL让它必须行
[deep learning] semantic segmentation: thesis reading (neurips 2021) maskformer: per pixel classification is not all you need
单片机如何从上电复位执行到main函数?
018.有效的回文
068.查找插入位置--二分查找
018. Valid palindromes
MapReduce instance (x): chainmapreduce
Sqlmap installation tutorial and problem explanation under Windows Environment -- "sqlmap installation | CSDN creation punch in"
Segmentation sémantique de l'apprentissage profond - résumé du code source
Counter attack of noodles: redis asked 52 questions in a series, with detailed pictures and pictures. Now the interview is stable
Research and implementation of hospital management inpatient system based on b/s (attached: source code paper SQL file)
一文读懂,DDD落地数据库设计实战
Redis分布式锁实现Redisson 15问
数据建模有哪些模型
