dllbridge

Arduino UNO COM-Port C++ (2003)

Aug 22nd, 2025
485
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.98 KB | None | 0 0
  1.  
  2. #include <windows.h>
  3. #include  <iostream>
  4. #include    <string>
  5. #include   <sstream>
  6. #include    <vector>
  7. using namespace std;
  8.  
  9. /*  // Скетч для Arduino
  10. void setup()
  11. {
  12.   Serial.begin(9600);
  13. }
  14.  
  15. void loop()
  16. {
  17.   if (Serial.available() > 0)
  18.   {
  19.     String input = Serial.readString();
  20.     Serial.print("OK"); // Простой ответ
  21.   }
  22.   delay(100);
  23. }   */
  24.  
  25.  
  26. ///////////////////////
  27. class ArduinoFinder
  28. {
  29. private:
  30.     int baud_rate;
  31.  
  32.     bool isArduinoPort(const string& port_name)
  33.     {
  34.         HANDLE hSerial = CreateFile(port_name.c_str(),
  35.                                    GENERIC_READ | GENERIC_WRITE,
  36.                                    0, NULL, OPEN_EXISTING,
  37.                                    FILE_ATTRIBUTE_NORMAL, NULL);
  38.        
  39.         if (hSerial == INVALID_HANDLE_VALUE) {
  40.             return false;
  41.         }
  42.  
  43.  
  44.         DCB dcb       =         {0};                                                    // Настройка параметров порта
  45.         dcb.DCBlength = sizeof(dcb);
  46.        
  47.         if (!GetCommState(hSerial, &dcb))
  48.         {
  49.             CloseHandle(hSerial);
  50.             return false;
  51.         }
  52.  
  53.         dcb.BaudRate = baud_rate;
  54.         dcb.ByteSize = 8;
  55.         dcb.StopBits = ONESTOPBIT;
  56.         dcb.Parity = NOPARITY;
  57.  
  58.         if (!SetCommState(hSerial, &dcb))
  59.         {
  60.             CloseHandle(hSerial);
  61.             return false;
  62.         }
  63.  
  64.  
  65.         COMMTIMEOUTS         timeouts       = {0};                                             // Настройка таймаутов
  66.         timeouts.ReadIntervalTimeout        = 50 ;
  67.         timeouts.ReadTotalTimeoutConstant   = 50 ;
  68.         timeouts.ReadTotalTimeoutMultiplier = 10 ;
  69.         SetCommTimeouts(hSerial, &timeouts);
  70.  
  71.  
  72.         const char* test_cmd = "AT\r\n";                                                        // Тестирование порта
  73.         char buffer[256] = {0};
  74.         DWORD bytes_written, bytes_read;
  75.         bool is_arduino = false;
  76.  
  77.         if (WriteFile(hSerial, test_cmd, strlen(test_cmd), &bytes_written, NULL))
  78.         {
  79.             Sleep(100);
  80.             if (ReadFile(hSerial, buffer, sizeof(buffer)-1, &bytes_read, NULL))
  81.             {
  82.                 is_arduino = (bytes_read > 0);
  83.             }
  84.         }
  85.  
  86.         CloseHandle(hSerial);
  87.         return is_arduino;
  88.     }
  89.  
  90. public:
  91.     ArduinoFinder(int baud = 9600) : baud_rate(baud) {}
  92.    
  93.     ////////////////////
  94.     string findArduino()
  95.     {
  96.        
  97.         cout << "Поиск Arduino Uno (" << baud_rate << " бод)..." << endl << endl;
  98.  
  99.         for (int i = 1; i <= 30; ++i)
  100.         {
  101.            
  102.        
  103.             ostringstream oss;
  104.             oss << i;
  105.             string port = "COM" + oss.str();                                        // + to_string(i);  //  2011 STD
  106.             cout << "Проверка " << port << "... ";
  107.  
  108.             if(isArduinoPort(port))
  109.             {
  110.                 cout << "! НАЙДЕНО !" << endl;
  111.                 return port;
  112.             }
  113.             cout << "? не Arduino" << endl;
  114.         }
  115.  
  116.         return "";
  117.     }
  118. };
  119.  
  120.  
  121.  
  122.  
  123.  
  124. /////////////////////////////////////////////////////////////
  125. int main()                                                 //  
  126. {
  127.    
  128.     setlocale(LC_ALL, "rus");
  129.    
  130.     cout << "=== AUTOMATIC ARDUINO UNO FINDER ==="  << endl;
  131.     cout << "    Поиск на скорости 9600 бод"        << endl;
  132.     cout << "=====================================" << endl << endl;
  133.  
  134.     ArduinoFinder finder(9600);
  135.     string arduinoPort = finder.findArduino();
  136.  
  137.     cout << endl << "=====================================" << endl;
  138.    
  139.     if (!arduinoPort.empty()) {
  140.         cout << "?? Arduino найдено на: " << arduinoPort    << endl;
  141.     } else {
  142.         cout << "?? Arduino не найдено!"           << endl;
  143.         cout << "Проверьте подключение и драйверы" << endl;
  144.     }
  145.  
  146.     cout << endl << "Нажмите Enter для выхода...";
  147.     cin.get();
  148.  
  149.     return 0;
  150. }
  151.  
  152.  
  153.  
Advertisement
Add Comment
Please, Sign In to add comment