@@ -12,44 +12,45 @@ Arduino::Arduino(LPCSTR device_name) : m_arduinoHandle(INVALID_HANDLE_VALUE)
1212{
1313 char port[100 ] = " \\ .\\ " ;
1414
15- while (!this -> GetDevice (device_name, port))
15+ while (!GetDevice (device_name, port))
1616 {
1717 sleep_for (milliseconds (1000 ));
1818 }
1919
20- m_arduinoHandle = CreateFile (port, GENERIC_READ | GENERIC_WRITE, 0 , nullptr , OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr );
21-
22- if (m_arduinoHandle == INVALID_HANDLE_VALUE)
20+ this ->m_arduinoHandle = CreateFile (port, GENERIC_READ | GENERIC_WRITE, 0 , NULL , OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
21+ if (this ->m_arduinoHandle )
2322 {
24- cerr << " Error opening port: " << GetLastError () << endl;
25- return ;
26- }
27-
28- DCB dcb = {};
29- dcb.DCBlength = sizeof (dcb);
30-
31- if (!GetCommState (m_arduinoHandle, &dcb))
32- return ;
33-
34- dcb.BaudRate = CBR_9600;
35- dcb.ByteSize = 8 ;
36- dcb.StopBits = ONESTOPBIT;
37- dcb.Parity = NOPARITY;
38-
39- if (!SetCommState (m_arduinoHandle, &dcb))
40- return ;
41-
42- COMMTIMEOUTS cto = {};
43- cto.ReadIntervalTimeout = 50 ;
44- cto.ReadTotalTimeoutConstant = 50 ;
45- cto.ReadTotalTimeoutMultiplier = 10 ;
46- cto.WriteTotalTimeoutConstant = 50 ;
47- cto.WriteTotalTimeoutMultiplier = 10 ;
23+ DCB dcb = { 0 };
24+ dcb.DCBlength = sizeof (dcb);
25+ if (!GetCommState (this ->m_arduinoHandle , &dcb))
26+ {
27+ printf (" GetCommState() failed\n " );
28+ CloseHandle (this ->m_arduinoHandle );
29+ }
4830
49- if (!SetCommTimeouts (m_arduinoHandle, &cto))
50- return ;
31+ dcb.BaudRate = CBR_9600;
32+ dcb.ByteSize = 8 ;
33+ dcb.StopBits = ONESTOPBIT;
34+ dcb.Parity = NOPARITY;
35+ if (!SetCommState (this ->m_arduinoHandle , &dcb))
36+ {
37+ printf (" SetCommState() failed\n " );
38+ CloseHandle (this ->m_arduinoHandle );
39+ }
5140
52- cout << " Successfully connected!" << endl;
41+ COMMTIMEOUTS cto = { 0 };
42+ cto.ReadIntervalTimeout = 50 ;
43+ cto.ReadTotalTimeoutConstant = 50 ;
44+ cto.ReadTotalTimeoutMultiplier = 10 ;
45+ cto.WriteTotalTimeoutConstant = 50 ;
46+ cto.WriteTotalTimeoutMultiplier = 10 ;
47+ if (!SetCommTimeouts (this ->m_arduinoHandle , &cto))
48+ {
49+ printf (" SetCommTimeouts() failed\n " );
50+ CloseHandle (this ->m_arduinoHandle );
51+ }
52+ cout << " Successfully connected!" << endl;
53+ }
5354}
5455
5556bool Arduino::IsAvailable () const
@@ -68,41 +69,42 @@ bool Arduino::IsAvailable() const
6869
6970bool Arduino::GetDevice (LPCSTR friendly_name, LPSTR com_port)
7071{
71- const char com[] = " COM" ;
72- HDEVINFO device_info = SetupDiGetClassDevs (&GUID_DEVCLASS_PORTS, nullptr , nullptr , DIGCF_PRESENT) ;
72+ char com[] = " COM" ;
73+ bool status = false ;
7374
74- if ( device_info == INVALID_HANDLE_VALUE)
75- return false ;
75+ HDEVINFO device_info = SetupDiGetClassDevs (&GUID_DEVCLASS_PORTS, NULL , NULL , DIGCF_PRESENT);
76+ if (device_info == INVALID_HANDLE_VALUE) return false ;
7677
77- SP_DEVINFO_DATA dev_info_data = {} ;
78+ SP_DEVINFO_DATA dev_info_data;
7879 dev_info_data.cbSize = sizeof (dev_info_data);
7980
80- DWORD device_count = 0 ;
81+ DWORD count = 0 ;
8182
82- while (SetupDiEnumDeviceInfo (device_info, device_count ++, &dev_info_data))
83+ while (SetupDiEnumDeviceInfo (device_info, count ++, &dev_info_data))
8384 {
84- BYTE buff[256 ] = { 0 };
85-
86- if (SetupDiGetDeviceRegistryProperty (device_info, &dev_info_data, SPDRP_FRIENDLYNAME, nullptr , buff, sizeof (buff), nullptr ))
85+ BYTE buffer[256 ];
86+ if (SetupDiGetDeviceRegistryProperty (device_info, &dev_info_data, SPDRP_FRIENDLYNAME, NULL , buffer, sizeof (buffer), NULL ))
8787 {
88- LPCSTR port_name_pos = strstr (reinterpret_cast <LPCSTR>(buff), com);
88+ DWORD i = strlen (com_port);
89+ LPCSTR lp_pos = strstr ((LPCSTR)buffer, com);
90+ DWORD len = i + (lp_pos ? strlen (lp_pos) : 0 );
8991
90- if (port_name_pos == nullptr )
91- continue ;
92-
93- if (strstr (reinterpret_cast <LPCSTR>(buff), friendly_name))
92+ if (strstr ((LPCSTR)buffer, friendly_name) && lp_pos)
9493 {
95- strncpy_s (com_port, 100 , port_name_pos, strlen (port_name_pos) - 1 );
96- com_port[strlen (port_name_pos) - 1 ] = ' \0 ' ;
97- SetupDiDestroyDeviceInfoList (device_info);
98-
99- return true ;
94+ for (DWORD j = 0 ; i < len; i++, j++)
95+ {
96+ com_port[i] = lp_pos[j];
97+ }
98+
99+ com_port[i - 1 ] = ' \0 ' ;
100+ status = true ;
101+ break ;
100102 }
101103 }
102104 }
103105
104106 SetupDiDestroyDeviceInfoList (device_info);
105- return false ;
107+ return status ;
106108}
107109
108110bool Arduino::WriteMessage (const string& message) const
0 commit comments