22
33Arduino::~Arduino ()
44{
5- CloseHandle (m_hArduino);
5+ if (m_arduinoHandle != INVALID_HANDLE_VALUE)
6+ {
7+ CloseHandle (m_arduinoHandle);
8+ }
69}
710
8- Arduino::Arduino (LPCSTR device_name)
11+ Arduino::Arduino (LPCSTR device_name) : m_arduinoHandle(INVALID_HANDLE_VALUE)
912{
10- char port[100 ] = " \\ .\\ " ;
11-
12- while (!Arduino::get_device (device_name, port))
13- {
14- Sleep (1000 );
15- }
16-
17- m_hArduino = CreateFile (port, GENERIC_READ | GENERIC_WRITE, 0 , nullptr , OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr );
18-
19- if (m_hArduino == INVALID_HANDLE_VALUE)
20- {
21- printf (" Error opening port: %d\n " , GetLastError ());
22- return ;
23- }
24-
25- DCB dcb = { };
26- dcb.DCBlength = sizeof (dcb);
27- if (!GetCommState (m_hArduino, &dcb))
28- return ;
29-
30- dcb.BaudRate = CBR_9600;
31- dcb.ByteSize = 8 ;
32- dcb.StopBits = ONESTOPBIT;
33- dcb.Parity = NOPARITY;
34-
35- if (!SetCommState (m_hArduino, &dcb))
36- return ;
37-
38- COMMTIMEOUTS cto = { };
39- cto.ReadIntervalTimeout = 50 ;
40- cto.ReadTotalTimeoutConstant = 50 ;
41- cto.ReadTotalTimeoutMultiplier = 10 ;
42- cto.WriteTotalTimeoutConstant = 50 ;
43- cto.WriteTotalTimeoutMultiplier = 10 ;
44- if (!SetCommTimeouts (m_hArduino, &cto))
45- return ;
46-
47- printf (" Successfully connected!\n " );
13+ char port[100 ] = " \\ .\\ " ;
14+
15+ while (!this ->GetDevice (device_name, port))
16+ {
17+ sleep_for (milliseconds (1000 ));
18+ }
19+
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)
23+ {
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 ;
48+
49+ if (!SetCommTimeouts (m_arduinoHandle, &cto))
50+ return ;
51+
52+ cout << " Successfully connected!" << endl;
4853}
4954
50- bool Arduino::get_device (LPCSTR friendly_name, LPSTR com_port)
55+ bool Arduino::IsAvailable () const
5156{
52- const char com[] = " COM" ;
53-
54- HDEVINFO device_info = SetupDiGetClassDevs (&GUID_DEVCLASS_PORTS, nullptr , nullptr , DIGCF_PRESENT);
55- if (device_info == INVALID_HANDLE_VALUE)
56- return false ;
57-
58- SP_DEVINFO_DATA dev_info_data = { };
59- dev_info_data.cbSize = sizeof (dev_info_data);
60-
61- DWORD device_count = 0 ;
62- while (SetupDiEnumDeviceInfo (device_info, device_count++, &dev_info_data))
63- {
64- BYTE buff[256 ] = { 0 };
65- if (SetupDiGetDeviceRegistryProperty (device_info, &dev_info_data, SPDRP_FRIENDLYNAME, nullptr , buff, sizeof (buff), nullptr ))
66- {
67- DWORD out_buff_len = strlen (com_port);
68- LPCSTR port_name_pos = strstr (reinterpret_cast <LPCSTR>(buff), com);
69- if (port_name_pos == nullptr )
70- continue ;
71-
72- DWORD len = out_buff_len + strlen (port_name_pos);
73- if (strstr (reinterpret_cast <LPCSTR>(buff), friendly_name))
74- {
75- for (DWORD i = 0 ; i < len; i++, out_buff_len++)
76- com_port[out_buff_len] = port_name_pos[i];
77-
78- com_port[strlen (com_port) - 1 ] = 0 ;
79- return true ;
80- }
81- }
82- }
83-
84- return false ;
57+ DWORD errors;
58+ COMSTAT status;
59+
60+ if (!ClearCommError (m_arduinoHandle, &errors, &status))
61+ {
62+ cerr << " Error checking available data: " << GetLastError () << endl;
63+ return false ;
64+ }
65+
66+ return status.cbInQue > 0 ;
8567}
8668
87- bool Arduino::send_data ( const string &message) const
69+ bool Arduino::GetDevice (LPCSTR friendly_name, LPSTR com_port)
8870{
89- DWORD bw = 0 ;
90- BOOL result = WriteFile (m_hArduino, message. c_str (), message. size () + 1 , &bw , nullptr );
71+ const char com[] = " COM " ;
72+ HDEVINFO device_info = SetupDiGetClassDevs (&GUID_DEVCLASS_PORTS, nullptr , nullptr , DIGCF_PRESENT );
9173
92- if (result == 0 || bw != message.size () + 1 )
93- {
94- printf (" Failed to send message!\n " );
95- exit (1 );
96- }
74+ if (device_info == INVALID_HANDLE_VALUE)
75+ return false ;
9776
98- return true ;
77+ SP_DEVINFO_DATA dev_info_data = {};
78+ dev_info_data.cbSize = sizeof (dev_info_data);
79+
80+ DWORD device_count = 0 ;
81+
82+ while (SetupDiEnumDeviceInfo (device_info, device_count++, &dev_info_data))
83+ {
84+ BYTE buff[256 ] = { 0 };
85+
86+ if (SetupDiGetDeviceRegistryProperty (device_info, &dev_info_data, SPDRP_FRIENDLYNAME, nullptr , buff, sizeof (buff), nullptr ))
87+ {
88+ LPCSTR port_name_pos = strstr (reinterpret_cast <LPCSTR>(buff), com);
89+
90+ if (port_name_pos == nullptr )
91+ continue ;
92+
93+ if (strstr (reinterpret_cast <LPCSTR>(buff), friendly_name))
94+ {
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 ;
100+ }
101+ }
102+ }
103+
104+ SetupDiDestroyDeviceInfoList (device_info);
105+ return false ;
99106}
100107
101- bool Arduino::available ( ) const
108+ bool Arduino::WriteMessage ( const string& message ) const
102109{
103- DWORD errors ;
104- COMSTAT status ;
110+ DWORD bw = 0 ;
111+ BOOL result = WriteFile (m_arduinoHandle, message. c_str (), message. size () + 1 , &bw, nullptr ) ;
105112
106- if (! ClearCommError (m_hArduino, &errors, &status) )
107- {
108- printf ( " Error checking available data: %d \n " , GetLastError ()) ;
109- return false ;
110- }
113+ if (result == 0 || bw != message. size () + 1 )
114+ {
115+ cerr << " Failed to send message! " << endl ;
116+ return false ;
117+ }
111118
112- return status. cbInQue > 0 ;
119+ return true ;
113120}
114121
115- string Arduino::readStringUntil (char delimiter) const
122+ string Arduino::ReceiveMessage (char delimiter) const
116123{
117- string result;
118- char buffer[256 ];
119- DWORD bytesRead = 0 ;
120-
121- while (this ->available ())
122- {
123- memset (buffer, 0 , sizeof (buffer));
124-
125- if (!ReadFile (m_hArduino, buffer, sizeof (buffer), &bytesRead, nullptr ))
126- break ;
127-
128- for (DWORD i = 0 ; i < bytesRead; i++)
129- {
130- if (buffer[i] == delimiter)
131- {
132- return result;
133- }
134-
135- result += buffer[i];
136- }
137- }
138- return result;
124+ string result;
125+ char buffer[256 ];
126+ DWORD bytesRead = 0 ;
127+
128+ while (this ->IsAvailable ())
129+ {
130+ memset (buffer, 0 , sizeof (buffer));
131+
132+ if (!ReadFile (m_arduinoHandle, buffer, sizeof (buffer), &bytesRead, nullptr ))
133+ break ;
134+
135+ for (DWORD i = 0 ; i < bytesRead; i++)
136+ {
137+ if (buffer[i] == delimiter)
138+ {
139+ return result;
140+ }
141+
142+ result += buffer[i];
143+ }
144+ }
145+
146+ return result;
139147}
0 commit comments