c++ - How to determine if Arduino is disconnected on Windows? -


i using c++ code found on https://playground.arduino.cc/interfacing/cppwindows communicate arduino. have connected , received serial output, unsure of how determine if arduino has been disconnected computer. looked code , promising thing discovered variable of type 'comstat' windows.h library, conveniently came comment, "get various info connection," difficult interpret in windows documentation. can keep counter variable , predict arduino has been disconnected if no data has been read in x amount of loops (the arduino should output data multiple times per second), there ought surefire way determine when arduino has been disconnected. first time working c++ there chance may missing obvious.

here code arduino site:

serial.h:

#ifndef serialclass_h_included #define serialclass_h_included  #define arduino_wait_time 2000  #include <windows.h> #include <stdio.h> #include <stdlib.h>  class serial {     private:         //serial comm handler         handle hserial;         //connection status         bool connected;         //get various information connection         comstat status;  // *** think lead         //keep track of last error         dword errors;      public:         //initialize serial communication given com port         serial(const char *portname);         //close connection         ~serial();         //read data in buffer, if nbchar greater         //maximum number of bytes available, return         //bytes available. function return -1 when nothing         //be read, number of bytes read.         int readdata(char *buffer, unsigned int nbchar);         //writes data buffer through serial connection         //return true on success.         bool writedata(const char *buffer, unsigned int nbchar);         //check if connected         bool isconnected();   };  #endif // serialclass_h_included 

serial.cpp

#include "serialclass.h"  serial::serial(const char *portname) {     //we're not yet connected     this->connected = false;      //try connect given port throuh createfile     this->hserial = createfile(portname,             generic_read | generic_write,             0,             null,             open_existing,             file_attribute_normal,             null);      //check if connection successfull     if(this->hserial==invalid_handle_value)     {         //if not success full display error         if(getlasterror()==error_file_not_found){              //print error if neccessary             printf("error: handle not attached. reason: %s not available.\n", portname);          }         else         {             printf("error!!!");         }     }     else     {         //if connected try set comm parameters         dcb dcbserialparams = {0};          //try current         if (!getcommstate(this->hserial, &dcbserialparams))         {             //if impossible, show error             printf("failed current serial parameters!");         }         else         {             //define serial connection parameters arduino board             dcbserialparams.baudrate=cbr_9600;             dcbserialparams.bytesize=8;             dcbserialparams.stopbits=onestopbit;             dcbserialparams.parity=noparity;             //setting dtr control_enable ensures arduino             //reset upon establishing connection             dcbserialparams.fdtrcontrol = dtr_control_enable;               //set parameters , check proper application              if(!setcommstate(hserial, &dcbserialparams))              {                 printf("alert: not set serial port parameters");              }              else              {                  //if went fine we're connected                  this->connected = true;                  //flush remaining characters in buffers                   purgecomm(this->hserial, purge_rxclear | purge_txclear);                  //we wait 2s arduino board reseting                  sleep(arduino_wait_time);              }         }     }  }  serial::~serial() {     //check if connected before trying disconnect     if(this->connected)     {         //we're no longer connected         this->connected = false;         //close serial handler         closehandle(this->hserial);     } }  int serial::readdata(char *buffer, unsigned int nbchar) {     //number of bytes we'll have read     dword bytesread;     //number of bytes we'll ask read     unsigned int toread;      //use clearcommerror function status info on serial port     clearcommerror(this->hserial, &this->errors, &this->status);      //check if there read     if(this->status.cbinque>0)     {         //if there check if there enough data read required number         //of characters, if not we'll read available characters prevent         //locking of application.         if(this->status.cbinque>nbchar)         {             toread = nbchar;         }         else         {             toread = this->status.cbinque;         }          //try read require number of chars, , return number of read bytes on success         if(readfile(this->hserial, buffer, toread, &bytesread, null) )         {             return bytesread;         }      }      //if nothing has been read, or error detected return 0     return 0;  }   bool serial::writedata(const char *buffer, unsigned int nbchar) {     dword bytessend;      //try write buffer on serial port     if(!writefile(this->hserial, (void *)buffer, nbchar, &bytessend, 0))     {         //in case don't work comm error , return false         clearcommerror(this->hserial, &this->errors, &this->status);          return false;     }     else         return true; }  bool serial::isconnected() {     //simply return connection status     return this->connected; } 


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -