当前位置:网站首页>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 Device
To 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 gods
Free 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 .
边栏推荐
- [Yu Yue education] reference materials of power electronics technology of Jiangxi University of science and technology
- 一文读懂,DDD落地数据库设计实战
- Regular expressions are actually very simple
- Teach you how to write the first MCU program hand in hand
- Design and implementation of film and television creation forum based on b/s (attached: source code paper SQL file project deployment tutorial)
- Scoped in webrtc_ refptr
- 通过bat脚本配置系统环境变量
- 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)
- 大学想要选择学习自动化专业,可以看什么书去提前了解?
猜你喜欢
嵌入式开发比单片机要难很多?谈谈单片机和嵌入式开发设计经历
MapReduce instance (V): secondary sorting
Design and implementation of film and television creation forum based on b/s (attached: source code paper SQL file project deployment tutorial)
Listen to my advice and learn according to this embedded curriculum content and curriculum system
单片机实现模块化编程:思维+实例+系统教程(实用程度令人发指)
Workflow - activiti7 environment setup
MapReduce instance (VIII): Map end join
Regular expressions are actually very simple
Why can't TN-C use 2p circuit breaker?
O & M, let go of monitoring - let go of yourself
随机推荐
Une grande vague d'attaques à la source ouverte
Processes of libuv
五月刷题27——图
YARN组织架构
Hero League rotation chart manual rotation
Nc29 search in two-dimensional array
Mapreduce实例(八):Map端join
MapReduce instance (V): secondary sorting
DCDC power ripple test
Cooperative development in embedded -- function pointer
Redis connection redis service command
五月集训总结——来自阿光
零基础学习单片机切记这四点要求,少走弯路
May brush question 27 - figure
May brush question 26 - concurrent search
一文读懂,DDD落地数据库设计实战
Solve the problem of too many small files
《ASP.NET Core 6框架揭秘》样章发布[200页/5章]
May brush question 03 - sorting
CANoe CAPL文件操作目录合集