Skip to main content
added 1 character in body
Source Link
Bhoke
  • 321
  • 3
  • 11
#include <windows.h>
#include <string>
#include <stdio.h>
#define ARDUINO_WAIT_TIME 2000
using namespace std;

string portNumber;
HANDLE hSerial;
bool connected;
COMSTAT status;

bool connectPrinter(char *portName){

    //Connects to the port.
    hSerial = CreateFileA(portName,
    GENERIC_READ | GENERIC_WRITE,
    0,
    NULL,
    OPEN_EXISTING,
    FILE_ATTRIBUTE_NORMAL,
    NULL);

    if(hSerial==INVALID_HANDLE_VALUE)
    {
        if(GetLastError()==ERROR_FILE_NOT_FOUND){
            printf("ERROR: Handle was not attached. Reason: %s not available.\n", portName);
            return false;
        }
        else
        {
            //If port is in use by another program or did not closed previous process.
            printf("ERROR!!! \n");
            CloseHandle(hSerial);
            return false;
        }
    }
    else
    {

        DCB dcbSerialParams = {0};

        if (!GetCommState(hSerial, &dcbSerialParams))
        {
            printf("failed to get current serial parameters!");
            return false;
        }
        else
        {
            // Set Serial Port specifications.
            dcbSerialParams.BaudRate=CBR_115200;
            dcbSerialParams.ByteSize=8;
            dcbSerialParams.StopBits=ONESTOPBIT;
            dcbSerialParams.Parity=NOPARITY;

             if(!SetCommState(hSerial, &dcbSerialParams))
             {
                printf("ALERT: Could not set Serial Port parameters");
                return false;
             }
             else
             {
                 connected = true;
                 printf("Connection Successful for :%s !!! \n" ,portName);
                 //Wait 2s as the arduino board will be reseting
                 return true;
                 Sleep(ARDUINO_WAIT_TIME);
             }
        }
    }

}


int readData(char *buffer,unsigned int nbChar){

    DWORD bytesRead;
    DWORD dwCommModemStatus;

        //Set the type of data to be caught.(RXCHAR -> Data available on RX pin.)
        SetCommMask (hSerial, EV_RXCHAR );

        while (hSerial != INVALID_HANDLE_VALUE)
        {
          // Wait for an event to occur for the port.
          WaitCommEvent (hSerial, &dwCommModemStatus, 0);

          if (dwCommModemStatus & EV_RXCHAR )
          {
                //unsigned int toRead;
                //ClearCommError(this->hSerial, &this->errors, &this->status);
                do
                {
                  // Read the data from the serial port.
                    ReadFile (hSerial, buffer, 1, &bytesRead, 0);
                  // Display the data read.
                      printf ("%s" ,buffer);

                } while (bytesRead > 0);
          }
        }
    //}
    //If nothing has been read, or that an error was detected return -1
    return -1;
}

    int main()
    {

        char* buffer = new char[32768];
        int nbChar= 32768;
        string str="COM4";str="COMx"; // Use your port number to connect

        char * writable = new char[str.size() + 1];
        std::copy(str.begin(), str.end(), writable);
        writable[str.size()] = '\0';

        connectPrinter(writable); // Use your port number to connect
        delete []writable;
        readData(buffer,nbChar);
        return 0;
    }
#include <windows.h>
#include <string>
#include <stdio.h>
#define ARDUINO_WAIT_TIME 2000
using namespace std;

string portNumber;
HANDLE hSerial;
bool connected;
COMSTAT status;

bool connectPrinter(char *portName){

    //Connects to the port.
    hSerial = CreateFileA(portName,
    GENERIC_READ | GENERIC_WRITE,
    0,
    NULL,
    OPEN_EXISTING,
    FILE_ATTRIBUTE_NORMAL,
    NULL);

    if(hSerial==INVALID_HANDLE_VALUE)
    {
        if(GetLastError()==ERROR_FILE_NOT_FOUND){
            printf("ERROR: Handle was not attached. Reason: %s not available.\n", portName);
            return false;
        }
        else
        {
            //If port is in use by another program or did not closed previous process.
            printf("ERROR!!! \n");
            CloseHandle(hSerial);
            return false;
        }
    }
    else
    {

        DCB dcbSerialParams = {0};

        if (!GetCommState(hSerial, &dcbSerialParams))
        {
            printf("failed to get current serial parameters!");
            return false;
        }
        else
        {
            // Set Serial Port specifications.
            dcbSerialParams.BaudRate=CBR_115200;
            dcbSerialParams.ByteSize=8;
            dcbSerialParams.StopBits=ONESTOPBIT;
            dcbSerialParams.Parity=NOPARITY;

             if(!SetCommState(hSerial, &dcbSerialParams))
             {
                printf("ALERT: Could not set Serial Port parameters");
                return false;
             }
             else
             {
                 connected = true;
                 printf("Connection Successful for :%s !!! \n" ,portName);
                 //Wait 2s as the arduino board will be reseting
                 return true;
                 Sleep(ARDUINO_WAIT_TIME);
             }
        }
    }

}


int readData(char *buffer,unsigned int nbChar){

    DWORD bytesRead;
    DWORD dwCommModemStatus;

        //Set the type of data to be caught.(RXCHAR -> Data available on RX pin.)
        SetCommMask (hSerial, EV_RXCHAR );

        while (hSerial != INVALID_HANDLE_VALUE)
        {
          // Wait for an event to occur for the port.
          WaitCommEvent (hSerial, &dwCommModemStatus, 0);

          if (dwCommModemStatus & EV_RXCHAR )
          {
                //unsigned int toRead;
                //ClearCommError(this->hSerial, &this->errors, &this->status);
                do
                {
                  // Read the data from the serial port.
                    ReadFile (hSerial, buffer, 1, &bytesRead, 0);
                  // Display the data read.
                      printf ("%s" ,buffer);

                } while (bytesRead > 0);
          }
        }
    //}
    //If nothing has been read, or that an error was detected return -1
    return -1;
}

    int main()
    {

        char* buffer = new char[32768];
        int nbChar= 32768;
        string str="COM4";

        char * writable = new char[str.size() + 1];
        std::copy(str.begin(), str.end(), writable);
        writable[str.size()] = '\0';

        connectPrinter(writable); // Use your port number to connect
        delete []writable;
        readData(buffer,nbChar);
        return 0;
    }
#include <windows.h>
#include <string>
#include <stdio.h>
#define ARDUINO_WAIT_TIME 2000
using namespace std;

string portNumber;
HANDLE hSerial;
bool connected;
COMSTAT status;

bool connectPrinter(char *portName){

    //Connects to the port.
    hSerial = CreateFileA(portName,
    GENERIC_READ | GENERIC_WRITE,
    0,
    NULL,
    OPEN_EXISTING,
    FILE_ATTRIBUTE_NORMAL,
    NULL);

    if(hSerial==INVALID_HANDLE_VALUE)
    {
        if(GetLastError()==ERROR_FILE_NOT_FOUND){
            printf("ERROR: Handle was not attached. Reason: %s not available.\n", portName);
            return false;
        }
        else
        {
            //If port is in use by another program or did not closed previous process.
            printf("ERROR!!! \n");
            CloseHandle(hSerial);
            return false;
        }
    }
    else
    {

        DCB dcbSerialParams = {0};

        if (!GetCommState(hSerial, &dcbSerialParams))
        {
            printf("failed to get current serial parameters!");
            return false;
        }
        else
        {
            // Set Serial Port specifications.
            dcbSerialParams.BaudRate=CBR_115200;
            dcbSerialParams.ByteSize=8;
            dcbSerialParams.StopBits=ONESTOPBIT;
            dcbSerialParams.Parity=NOPARITY;

             if(!SetCommState(hSerial, &dcbSerialParams))
             {
                printf("ALERT: Could not set Serial Port parameters");
                return false;
             }
             else
             {
                 connected = true;
                 printf("Connection Successful for :%s !!! \n" ,portName);
                 //Wait 2s as the arduino board will be reseting
                 return true;
                 Sleep(ARDUINO_WAIT_TIME);
             }
        }
    }

}


int readData(char *buffer,unsigned int nbChar){

    DWORD bytesRead;
    DWORD dwCommModemStatus;

        //Set the type of data to be caught.(RXCHAR -> Data available on RX pin.)
        SetCommMask (hSerial, EV_RXCHAR );

        while (hSerial != INVALID_HANDLE_VALUE)
        {
          // Wait for an event to occur for the port.
          WaitCommEvent (hSerial, &dwCommModemStatus, 0);

          if (dwCommModemStatus & EV_RXCHAR )
          {
                //unsigned int toRead;
                //ClearCommError(this->hSerial, &this->errors, &this->status);
                do
                {
                  // Read the data from the serial port.
                    ReadFile (hSerial, buffer, 1, &bytesRead, 0);
                  // Display the data read.
                      printf ("%s" ,buffer);

                } while (bytesRead > 0);
          }
        }
    //}
    //If nothing has been read, or that an error was detected return -1
    return -1;
}

    int main()
    {

        char* buffer = new char[32768];
        int nbChar= 32768;
        string str="COMx"; // Use your port number to connect

        char * writable = new char[str.size() + 1];
        std::copy(str.begin(), str.end(), writable);
        writable[str.size()] = '\0';

        connectPrinter(writable); 
        delete []writable;
        readData(buffer,nbChar);
        return 0;
    }
modifying for GCC Compiler
Source Link
Bhoke
  • 321
  • 3
  • 11
#include <windows.h>
#include <string>
std::#include <stdio.h>
#define ARDUINO_WAIT_TIME 2000
using namespace std;

string portNumber;
HANDLE hSerial;
bool connected;
COMSTAT status;
DWORD errors;

bool connectPrinter(char *portName){

    //Connects to the port.
    this->hSerialhSerial = CreateFileA(portName,
    GENERIC_READ | GENERIC_WRITE,
    0,
    NULL,
    OPEN_EXISTING,
    FILE_ATTRIBUTE_NORMAL,
    NULL);

    if(this->hSerial==INVALID_HANDLE_VALUEhSerial==INVALID_HANDLE_VALUE)
    {
        if(GetLastError()==ERROR_FILE_NOT_FOUND){
            printf("ERROR: Handle was not attached. Reason: %s not available.\n", portName);
            return false;
        }
        else
        {   
            //If port is in use by another program or did not closed previous process.
            printf("ERROR!!! \n");
            CloseHandle(hSerial);
            return false;
        }
    }
    else
    {
        
        DCB dcbSerialParams = {0};

        if (!GetCommState(this->hSerialhSerial, &dcbSerialParams))
        {
            printf("failed to get current serial parameters!");
            return false;
        }
        else
        {
            // Set Serial Port specifications.
            //TODO:Different Baudrates will be added with respect to Combobox baudRateList selection.
            dcbSerialParams.BaudRate=CBR_115200;
            dcbSerialParams.ByteSize=8;
            dcbSerialParams.StopBits=ONESTOPBIT;
            dcbSerialParams.Parity=NOPARITY;

             if(!SetCommState(hSerial, &dcbSerialParams))
             {
                printf("ALERT: Could not set Serial Port parameters");
                return false;
             }
             else
             {
                 this->connectedconnected = true;
                 printf("Connection Successful for :%s !!! \n" ,portName);
                 //Wait 2s as the arduino board will be reseting
                 return true;
                 Sleep(ARDUINO_WAIT_TIME);
             }
        }
    }

}


int readData(char *buffer,unsigned int nbChar){

    DWORD bytesRead;
    DWORD dwCommModemStatus;
    
        //Set the type of data to be caught.(RXCHAR -> Data available on RX pin.)
        SetCommMask (this->hSerialhSerial, EV_RXCHAR );

        while (this->hSerialhSerial != INVALID_HANDLE_VALUE) 
        {
          // Wait for an event to occur for the port.
          WaitCommEvent (this->hSerialhSerial, &dwCommModemStatus, 0); 

          if (dwCommModemStatus & EV_RXCHAR ) 
          {
                //unsigned int toRead;
                //ClearCommError(this->hSerial, &this->errors, &this->status);
                if(this->status.cbInQue>0)
                do 
                {
                  // Read the data from the serial port.
                    ReadFile (this->hSerialhSerial, buffer, 1, &bytesRead, 0);
                  // Display the data read.
                  if (bytesRead == 1)
                      printf ("%s" ,buffer);

                } while (bytesRead > 0);
          }
 
        }
 
    //}
    
    //If nothing has been read, or that an error was detected return -1
    return -1;
}
 

    voidint main()
    { 

    
     char* buffer = new char[32768];
        int nbChar= 32768;
        string str="COM4";

        char * writable = new char[str.size(32768) + 1];
        std::copy(str.begin(), str.end(), writable);
    int nbChar= 32768;  writable[str.size()] = '\0';

        connectPrinter("COMx"writable); // Use your port number to connect
        delete []writable;
        readData(buffer,nbChar);
        return 0;
    }

try this one, it should work. Comment below if it does not work. You have tomight reset Arduino to read data in method void setup() in Arduino code. I recommend you to use C#, Java or Python for efficiency.

#include <windows.h>

std::string portNumber;
HANDLE hSerial;
bool connected;
COMSTAT status;
DWORD errors;

bool connectPrinter(char *portName){

    //Connects to the port.
    this->hSerial = CreateFileA(portName,
    GENERIC_READ | GENERIC_WRITE,
    0,
    NULL,
    OPEN_EXISTING,
    FILE_ATTRIBUTE_NORMAL,
    NULL);

    if(this->hSerial==INVALID_HANDLE_VALUE)
    {
        if(GetLastError()==ERROR_FILE_NOT_FOUND){
            printf("ERROR: Handle was not attached. Reason: %s not available.\n", portName);
            return false;
        }
        else
        {   
            //If port is in use by another program or did not closed previous process.
            printf("ERROR!!! \n");
            CloseHandle(hSerial);
            return false;
        }
    }
    else
    {
        
        DCB dcbSerialParams = {0};

        if (!GetCommState(this->hSerial, &dcbSerialParams))
        {
            printf("failed to get current serial parameters!");
            return false;
        }
        else
        {
            // Set Serial Port specifications.
            //TODO:Different Baudrates will be added with respect to Combobox baudRateList selection.
            dcbSerialParams.BaudRate=CBR_115200;
            dcbSerialParams.ByteSize=8;
            dcbSerialParams.StopBits=ONESTOPBIT;
            dcbSerialParams.Parity=NOPARITY;

             if(!SetCommState(hSerial, &dcbSerialParams))
             {
                printf("ALERT: Could not set Serial Port parameters");
                return false;
             }
             else
             {
                 this->connected = true;
                 printf("Connection Successful for :%s !!! \n" ,portName);
                 //Wait 2s as the arduino board will be reseting
                 return true;
                 Sleep(ARDUINO_WAIT_TIME);
             }
        }
    }

}


int readData(char *buffer,unsigned int nbChar){

    DWORD bytesRead;
    DWORD dwCommModemStatus;
    
        //Set the type of data to be caught.(RXCHAR -> Data available on RX pin.)
        SetCommMask (this->hSerial, EV_RXCHAR );

        while (this->hSerial != INVALID_HANDLE_VALUE) 
        {
          // Wait for an event to occur for the port.
          WaitCommEvent (this->hSerial, &dwCommModemStatus, 0);
          if (dwCommModemStatus & EV_RXCHAR ) 
          {
                //unsigned int toRead;
                //ClearCommError(this->hSerial, &this->errors, &this->status);
                if(this->status.cbInQue>0)
                do 
                {
                  // Read the data from the serial port.
                    ReadFile (this->hSerial, buffer, 1, &bytesRead, 0);
                  // Display the data read.
                  if (bytesRead == 1)
                      printf ("%s" ,buffer);

                } while (bytesRead > 0);
          }
 
        }
 
    //}
    
    //If nothing has been read, or that an error was detected return -1
    return -1;
}
 

    void main() {
    
     char* buffer = new char (32768);
    int nbChar= 32768;

    connectPrinter("COMx"); // Use your port number to connect
    readData(buffer,nbChar);
    
    }

try this one, it should work. Comment below if it does not work. You have to reset Arduino to read data in method void setup() in Arduino code. I recommend you to use C#, Java or Python for efficiency.

#include <windows.h>
#include <string>
#include <stdio.h>
#define ARDUINO_WAIT_TIME 2000
using namespace std;

string portNumber;
HANDLE hSerial;
bool connected;
COMSTAT status;

bool connectPrinter(char *portName){

    //Connects to the port.
    hSerial = CreateFileA(portName,
    GENERIC_READ | GENERIC_WRITE,
    0,
    NULL,
    OPEN_EXISTING,
    FILE_ATTRIBUTE_NORMAL,
    NULL);

    if(hSerial==INVALID_HANDLE_VALUE)
    {
        if(GetLastError()==ERROR_FILE_NOT_FOUND){
            printf("ERROR: Handle was not attached. Reason: %s not available.\n", portName);
            return false;
        }
        else
        {
            //If port is in use by another program or did not closed previous process.
            printf("ERROR!!! \n");
            CloseHandle(hSerial);
            return false;
        }
    }
    else
    {

        DCB dcbSerialParams = {0};

        if (!GetCommState(hSerial, &dcbSerialParams))
        {
            printf("failed to get current serial parameters!");
            return false;
        }
        else
        {
            // Set Serial Port specifications.
            dcbSerialParams.BaudRate=CBR_115200;
            dcbSerialParams.ByteSize=8;
            dcbSerialParams.StopBits=ONESTOPBIT;
            dcbSerialParams.Parity=NOPARITY;

             if(!SetCommState(hSerial, &dcbSerialParams))
             {
                printf("ALERT: Could not set Serial Port parameters");
                return false;
             }
             else
             {
                 connected = true;
                 printf("Connection Successful for :%s !!! \n" ,portName);
                 //Wait 2s as the arduino board will be reseting
                 return true;
                 Sleep(ARDUINO_WAIT_TIME);
             }
        }
    }

}


int readData(char *buffer,unsigned int nbChar){

    DWORD bytesRead;
    DWORD dwCommModemStatus;

        //Set the type of data to be caught.(RXCHAR -> Data available on RX pin.)
        SetCommMask (hSerial, EV_RXCHAR );

        while (hSerial != INVALID_HANDLE_VALUE)
        {
          // Wait for an event to occur for the port.
          WaitCommEvent (hSerial, &dwCommModemStatus, 0); 

          if (dwCommModemStatus & EV_RXCHAR )
          {
                //unsigned int toRead;
                //ClearCommError(this->hSerial, &this->errors, &this->status);
                do
                {
                  // Read the data from the serial port.
                    ReadFile (hSerial, buffer, 1, &bytesRead, 0);
                  // Display the data read.
                      printf ("%s" ,buffer);

                } while (bytesRead > 0);
          }
        }
    //}
    //If nothing has been read, or that an error was detected return -1
    return -1;
}

    int main()
    { 

        char* buffer = new char[32768];
        int nbChar= 32768;
        string str="COM4";

        char * writable = new char[str.size() + 1];
        std::copy(str.begin(), str.end(), writable);
        writable[str.size()] = '\0';

        connectPrinter(writable); // Use your port number to connect
        delete []writable;
        readData(buffer,nbChar);
        return 0;
    }

try this one, it should work. Comment below if it does not work. You might reset Arduino to read data in method void setup() in Arduino code. I recommend you to use C#, Java or Python for efficiency.

added 43 characters in body
Source Link
Bhoke
  • 321
  • 3
  • 11
#include <windows.h>

std::string portNumber;
HANDLE hSerial;
bool connected;
COMSTAT status;
DWORD errors;

bool connectPrinter(char *portName){

    //Connects to the port.
    this->hSerial = CreateFileA(portName,
    GENERIC_READ | GENERIC_WRITE,
    0,
    NULL,
    OPEN_EXISTING,
    FILE_ATTRIBUTE_NORMAL,
    NULL);

    if(this->hSerial==INVALID_HANDLE_VALUE)
    {
        if(GetLastError()==ERROR_FILE_NOT_FOUND){
            printf("ERROR: Handle was not attached. Reason: %s not available.\n", portName);
            return false;
        }
        else
        {   
            //If port is in use by another program or did not closed previous process.
            printf("ERROR!!! \n");
            CloseHandle(hSerial);
            return false;
        }
    }
    else
    {
        
        DCB dcbSerialParams = {0};

        if (!GetCommState(this->hSerial, &dcbSerialParams))
        {
            printf("failed to get current serial parameters!");
            return false;
        }
        else
        {
            // Set Serial Port specifications.
            //TODO:Different Baudrates will be added with respect to Combobox baudRateList selection.
            dcbSerialParams.BaudRate=CBR_115200;
            dcbSerialParams.ByteSize=8;
            dcbSerialParams.StopBits=ONESTOPBIT;
            dcbSerialParams.Parity=NOPARITY;

             if(!SetCommState(hSerial, &dcbSerialParams))
             {
                printf("ALERT: Could not set Serial Port parameters");
                return false;
             }
             else
             {
                 this->connected = true;
                 printf("Connection Successful for :%s !!! \n" ,portName);
                 //Wait 2s as the arduino board will be reseting
                 return true;
                 Sleep(ARDUINO_WAIT_TIME);
             }
        }
    }

}


int readData(char *buffer,unsigned int nbChar){

    DWORD bytesRead;
    DWORD dwCommModemStatus;
    
        //Set the type of data to be caught.(RXCHAR -> Data available on RX pin.)
        SetCommMask (this->hSerial, EV_RXCHAR );

        while (this->hSerial != INVALID_HANDLE_VALUE) 
        {
          // Wait for an event to occur for the port.
          WaitCommEvent (this->hSerial, &dwCommModemStatus, 0);
          if (dwCommModemStatus & EV_RXCHAR ) 
          {
                //unsigned int toRead;
                //ClearCommError(this->hSerial, &this->errors, &this->status);
                if(this->status.cbInQue>0)
                do 
                {
                  // Read the data from the serial port.
                    ReadFile (this->hSerial, buffer, 1, &bytesRead, 0);
                  // Display the data read.
                  if (bytesRead == 1)
                      printf ("%s" ,buffer);

                } while (bytesRead > 0);
          }

        }

    //}
    
    //If nothing has been read, or that an error was detected return -1
    return -1;
}


    void main() {
    
    char* buffer = new char (32768);
    int nbChar= 32768;

    connectPrinter("COMx"); // Use your port number to connect
    readData(char *bufferbuffer,unsigned int nbChar);
    
    }
#include <windows.h>

std::string portNumber;
HANDLE hSerial;
bool connected;
COMSTAT status;
DWORD errors;

bool connectPrinter(char *portName){

    //Connects to the port.
    this->hSerial = CreateFileA(portName,
    GENERIC_READ | GENERIC_WRITE,
    0,
    NULL,
    OPEN_EXISTING,
    FILE_ATTRIBUTE_NORMAL,
    NULL);

    if(this->hSerial==INVALID_HANDLE_VALUE)
    {
        if(GetLastError()==ERROR_FILE_NOT_FOUND){
            printf("ERROR: Handle was not attached. Reason: %s not available.\n", portName);
            return false;
        }
        else
        {   
            //If port is in use by another program or did not closed previous process.
            printf("ERROR!!! \n");
            CloseHandle(hSerial);
            return false;
        }
    }
    else
    {
        
        DCB dcbSerialParams = {0};

        if (!GetCommState(this->hSerial, &dcbSerialParams))
        {
            printf("failed to get current serial parameters!");
            return false;
        }
        else
        {
            // Set Serial Port specifications.
            //TODO:Different Baudrates will be added with respect to Combobox baudRateList selection.
            dcbSerialParams.BaudRate=CBR_115200;
            dcbSerialParams.ByteSize=8;
            dcbSerialParams.StopBits=ONESTOPBIT;
            dcbSerialParams.Parity=NOPARITY;

             if(!SetCommState(hSerial, &dcbSerialParams))
             {
                printf("ALERT: Could not set Serial Port parameters");
                return false;
             }
             else
             {
                 this->connected = true;
                 printf("Connection Successful for :%s !!! \n" ,portName);
                 //Wait 2s as the arduino board will be reseting
                 return true;
                 Sleep(ARDUINO_WAIT_TIME);
             }
        }
    }

}


int readData(char *buffer,unsigned int nbChar){

    DWORD bytesRead;
    DWORD dwCommModemStatus;
    
        //Set the type of data to be caught.(RXCHAR -> Data available on RX pin.)
        SetCommMask (this->hSerial, EV_RXCHAR );

        while (this->hSerial != INVALID_HANDLE_VALUE) 
        {
          // Wait for an event to occur for the port.
          WaitCommEvent (this->hSerial, &dwCommModemStatus, 0);
          if (dwCommModemStatus & EV_RXCHAR ) 
          {
                //unsigned int toRead;
                //ClearCommError(this->hSerial, &this->errors, &this->status);
                if(this->status.cbInQue>0)
                do 
                {
                  // Read the data from the serial port.
                    ReadFile (this->hSerial, buffer, 1, &bytesRead, 0);
                  // Display the data read.
                  if (bytesRead == 1)
                      printf ("%s" ,buffer);

                } while (bytesRead > 0);
          }

        }

    //}
    
    //If nothing has been read, or that an error was detected return -1
    return -1;
}


    void main() {
    
    
    connectPrinter("COMx"); // Use your port number to connect
    readData(char *buffer,unsigned int nbChar);
    
    }
#include <windows.h>

std::string portNumber;
HANDLE hSerial;
bool connected;
COMSTAT status;
DWORD errors;

bool connectPrinter(char *portName){

    //Connects to the port.
    this->hSerial = CreateFileA(portName,
    GENERIC_READ | GENERIC_WRITE,
    0,
    NULL,
    OPEN_EXISTING,
    FILE_ATTRIBUTE_NORMAL,
    NULL);

    if(this->hSerial==INVALID_HANDLE_VALUE)
    {
        if(GetLastError()==ERROR_FILE_NOT_FOUND){
            printf("ERROR: Handle was not attached. Reason: %s not available.\n", portName);
            return false;
        }
        else
        {   
            //If port is in use by another program or did not closed previous process.
            printf("ERROR!!! \n");
            CloseHandle(hSerial);
            return false;
        }
    }
    else
    {
        
        DCB dcbSerialParams = {0};

        if (!GetCommState(this->hSerial, &dcbSerialParams))
        {
            printf("failed to get current serial parameters!");
            return false;
        }
        else
        {
            // Set Serial Port specifications.
            //TODO:Different Baudrates will be added with respect to Combobox baudRateList selection.
            dcbSerialParams.BaudRate=CBR_115200;
            dcbSerialParams.ByteSize=8;
            dcbSerialParams.StopBits=ONESTOPBIT;
            dcbSerialParams.Parity=NOPARITY;

             if(!SetCommState(hSerial, &dcbSerialParams))
             {
                printf("ALERT: Could not set Serial Port parameters");
                return false;
             }
             else
             {
                 this->connected = true;
                 printf("Connection Successful for :%s !!! \n" ,portName);
                 //Wait 2s as the arduino board will be reseting
                 return true;
                 Sleep(ARDUINO_WAIT_TIME);
             }
        }
    }

}


int readData(char *buffer,unsigned int nbChar){

    DWORD bytesRead;
    DWORD dwCommModemStatus;
    
        //Set the type of data to be caught.(RXCHAR -> Data available on RX pin.)
        SetCommMask (this->hSerial, EV_RXCHAR );

        while (this->hSerial != INVALID_HANDLE_VALUE) 
        {
          // Wait for an event to occur for the port.
          WaitCommEvent (this->hSerial, &dwCommModemStatus, 0);
          if (dwCommModemStatus & EV_RXCHAR ) 
          {
                //unsigned int toRead;
                //ClearCommError(this->hSerial, &this->errors, &this->status);
                if(this->status.cbInQue>0)
                do 
                {
                  // Read the data from the serial port.
                    ReadFile (this->hSerial, buffer, 1, &bytesRead, 0);
                  // Display the data read.
                  if (bytesRead == 1)
                      printf ("%s" ,buffer);

                } while (bytesRead > 0);
          }

        }

    //}
    
    //If nothing has been read, or that an error was detected return -1
    return -1;
}


    void main() {
    
    char* buffer = new char (32768);
    int nbChar= 32768;

    connectPrinter("COMx"); // Use your port number to connect
    readData(buffer,nbChar);
    
    }
Source Link
Bhoke
  • 321
  • 3
  • 11
Loading