(MFC) 자동으로 시리얼 번호 스캔
레지스트리 값으로 시리얼 번호 스캔
[MFC]
- 최초 작성일: 2022년 2월 21일 (월)
##
컴퓨터에 USB를 통해 하드웨어 연결시 ‘COMXX’ 식의 시리얼 번호를 확인할 수 있는데,
프로그램 실행시 이 포트 번호를 자동으로 찾아 설정해주는 기능을 구현하고자 한다.
##
시리얼 포트를 스캔하기 위해서는 레지스트리의
HKEY_LOCALMACHINE\HARDWARE\DEVICEMAP\SERIALCOMM 항목을 찾아보면 된다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
int findComPortList(int *list)
{
HKEY hSerialCom;
TCHAR buffer[_MAX_PATH];
char data[_MAX_PATH];
DWORD len, type, dataSize;
long i;
char idata[3];
int port_list_count = 0;
if (::RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("HARDWARE\\DEVICEMAP\\SERIALCOMM"), 0, KEY_ALL_ACCESS, &hSerialCom) == ERROR_SUCCESS)
{
for (i = 0, len = dataSize = _MAX_PATH;
::RegEnumValue(hSerialCom, i, buffer, &len, NULL, &type, (BYTE *)data, &dataSize) == ERROR_SUCCESS;
i++, len = dataSize = _MAX_PATH)
{
RegQueryValueEx(hSerialCom, buffer, &len, NULL, (BYTE *)data, &dataSize);
int tmp = _tcslen(buffer);
if (tmp > 17)
{
buffer[17] = '\0';
tmp++;
}
if (_tcscmp(buffer, _T("\\Device\\USBSER000")) == 0)
{
//int l_size = dataSize - 4;
//int i;
//for (i = 0; i < l_size; i++)
//{
// idata[i] = data[i + 3];
//}
//idata[i] = '\0';
int cnt = 0;
for (i = 0; i < dataSize; i++)
{
if (data[i] >= 0x30 && data[i] <= 0x39)
{
idata[cnt] = data[i];
cnt++;
}
}
if(cnt == 2)
idata[cnt] = '\0';
else if (cnt == 1)
{
idata[cnt] = '\0';
idata[cnt+1] = '\0';
}
list[port_list_count] = atoi(idata);
port_list_count++;
}
}::RegCloseKey(hSerialCom);
}
return port_list_count;
}
char * byIndexComPort(int xPort)
{
static char PortName[30] = { 0, };
TCHAR PortName2[30];
// sprintf_s(PortName, 30, "\\\\.\\COM%d", xPort);
wsprintf(PortName2, TEXT("\\\\.\\COM%d"), xPort);
// MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, PortName, strlen(PortName), PortName2, 60);
return PortName;
}
BOOL open_comport()
{
int portList[10];
int candidateSize = 0;
candidateSize = findComPortList(portList);
if (candidateSize == 0)
{
m_portfound = FALSE;
m_Comportnum = 10;
}
else if (candidateSize == 1)
{
m_portfound = TRUE;
m_Comportnum = portList[0];
}
else
{
m_portfound = TRUE;
m_Comportnum = 10;
}
//serial.iBaudRate = 1; //9600
//serial.iDataBit = 3; //8
//serial.iStopBit = 1;//ONE5STOPBITS
//serial.iParity = 0; //NOPARITY
if (m_pCom1)
{
m_Connected = FALSE;
delete m_pCom1;
m_pCom1 = NULL;
}
TCHAR PortName2[30];
wsprintf(PortName2, _T("\\\\.\\COM%d"), m_Comportnum);
m_pCom1 = new CSerialPort(PortName2);
if (m_pCom1 == NULL)
return FALSE;
else
{
m_pCom1->SetBaudRate(CBR_9600);//테스트용
m_pCom1->SetBufferSizes(4096, 4096);
m_pCom1->SetParityDataStop(NOPARITY, EIGHTDATABITS, ONE5STOPBITS);
m_pCom1->SetFlowControl(PCF_XONXOFF);
m_pCom1->SetBufferSizes(SP_INBUFSIZE, SP_OUTBUFSIZE);
m_pCom1->SetReadTimeouts(MAXDWORD, 0, 0);
m_pCom1->SetWriteTimeouts(0, SP_WRITETIMEOUT);
m_pCom1->StartCommThread(Com1Reader, 0);
m_Connected = TRUE;
m_realtimestart = FALSE;
return m_pCom1->IsValid();
}
}
void close_comport()
{
Sleep(100);
if (m_pCom1)
{
if (m_pCom1->IsValid())
{
m_pCom1->PurgeComm(PURGE_RXCLEAR);
m_pCom1->StopCommThread();
}
delete m_pCom1;
m_pCom1 = NULL;
m_Connected = FALSE;
}
}
이 기사는 저작권자의 CC BY 4.0 라이센스를 따릅니다.
