当前位置:网站首页>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 .
边栏推荐
- Global and Chinese markets for modular storage area network (SAN) solutions 2022-2028: Research Report on technology, participants, trends, market size and share
- 英雄联盟轮播图自动轮播
- Vs All comments and uncomments
- CANoe的数据回放(Replay Block),还是要结合CAPL脚本才能说的明白
- 在CANoe中通过Panel面板控制Test Module 运行(初级)
- Basic concepts of libuv
- June brush question 02 - string
- Redis connection redis service command
- 为什么大学单片机课上51+汇编,为什么不直接来STM32
- Detailed explanation of cookies and sessions
猜你喜欢

Use of activiti7 workflow

【深度学习】语义分割:论文阅读:(2021-12)Mask2Former

Learning SCM is of great help to society

MapReduce instance (IV): natural sorting

Listen to my advice and learn according to this embedded curriculum content and curriculum system

五月集训总结——来自阿光

CANoe仿真功能之自动化序列(Automation Sequences )

Mapreduce实例(十):ChainMapReduce

Minio distributed file storage cluster for full stack development

Regular expressions are actually very simple
随机推荐
I2C summary (single host and multi host)
max-flow min-cut
There are software load balancing and hardware load balancing. Which one to choose?
How can I take a shortcut to learn C language in college
Mapreduce实例(七):单表join
CANoe的数据回放(Replay Block),还是要结合CAPL脚本才能说的明白
MapReduce instance (VII): single table join
CAPL 脚本打印函数 write ,writeEx ,writeLineEx ,writeToLog ,writeToLogEx ,writeDbgLevel 你真的分的清楚什么情况下用哪个吗?
[Chongqing Guangdong education] reference materials for nine lectures on the essence of Marxist Philosophy in Wuhan University
How does the single chip microcomputer execute the main function from power on reset?
Use of activiti7 workflow
What are the models of data modeling
C#/. Net phase VI 01C Foundation_ 01: running environment, process of creating new C program, strict case sensitivity, meaning of class library
英雄联盟轮播图自动轮播
Cap theory
在CANoe中通过Panel面板控制Test Module 运行(高级)
单片机实现模块化编程:思维+实例+系统教程(实用程度令人发指)
Basic concepts of libuv
[Yu Yue education] reference materials of power electronics technology of Jiangxi University of science and technology
听哥一句劝,按这套嵌入式的课程内容和课程体系去学习
