My cpp code :
//#pragma GCC diagnostic ignored "-Wwrite-strings"
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
#include "main.h"
#include "AGKLibraryCommands.h"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <Windows.h>
#include "SerialPort.h"
using namespace std;
//String for getting the output from arduino
char output[MAX_DATA_LENGTH];
/*Portname must contain these backslashes, and remember to
replace the following com port*/
//char *port_name = "\\\\.\\COM7";
//char *port_name;
std::string port_name;
//String for incoming data
char incomingData[MAX_DATA_LENGTH];
// a sample exported function
void DLL_EXPORT SomeFunction(const LPCSTR sometext)
{
MessageBoxA(0, sometext, "DLL Message", MB_OK | MB_ICONINFORMATION);
}
extern "C" DLL_EXPORT BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
switch (fdwReason)
{
case DLL_PROCESS_ATTACH:
// attach to process
// return FALSE to fail DLL load
break;
case DLL_PROCESS_DETACH:
// detach from process
break;
case DLL_THREAD_ATTACH:
// attach to thread
break;
case DLL_THREAD_DETACH:
// detach from thread
break;
}
return TRUE; // succesful
}
SerialPort::SerialPort(char *portName)
{
this->connected = false;
this->handler = CreateFileA(static_cast<LPCSTR>(portName),
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (this->handler == INVALID_HANDLE_VALUE)
{
if (GetLastError() == ERROR_FILE_NOT_FOUND)
{
printf("ERROR: Handle was not attached. Reason: %s not available\n", portName);
}
else
{
printf("ERROR!!!");
}
}
else
{
DCB dcbSerialParameters = {0};
if (!GetCommState(this->handler, &dcbSerialParameters))
{
printf("failed to get current serial parameters");
}
else
{
dcbSerialParameters.BaudRate = CBR_9600;
dcbSerialParameters.ByteSize = 8;
dcbSerialParameters.StopBits = ONESTOPBIT;
dcbSerialParameters.Parity = NOPARITY;
dcbSerialParameters.fDtrControl = DTR_CONTROL_ENABLE;
if (!SetCommState(handler, &dcbSerialParameters))
{
printf("ALERT: could not set Serial port parameters\n");
}
else
{
this->connected = true;
PurgeComm(this->handler, PURGE_RXCLEAR | PURGE_TXCLEAR);
Sleep(ARDUINO_WAIT_TIME);
}
}
}
}
SerialPort::~SerialPort()
{
if (this->connected)
{
this->connected = false;
CloseHandle(this->handler);
}
}
int SerialPort::readSerialPort(char *buffer, unsigned int buf_size)
{
DWORD bytesRead;
unsigned int toRead = 0;
ClearCommError(this->handler, &this->errors, &this->status);
if (this->status.cbInQue > 0)
{
if (this->status.cbInQue > buf_size)
{
toRead = buf_size;
}
else
toRead = this->status.cbInQue;
}
memset(buffer, 0, buf_size);
if (ReadFile(this->handler, buffer, toRead, &bytesRead, NULL))
return bytesRead;
return 0;
}
bool SerialPort::writeSerialPort(char *buffer, unsigned int buf_size)
{
DWORD bytesSend;
if (!WriteFile(this->handler, (void*) buffer, buf_size, &bytesSend, 0))
{
ClearCommError(this->handler, &this->errors, &this->status);
return false;
}
else
return true;
}
bool SerialPort::isConnected()
{
if (!ClearCommError(this->handler, &this->errors, &this->status))
this->connected = false;
return this->connected;
}
void Stealth()
{
HWND Stealth;
AllocConsole();
Stealth = FindWindowA("ConsoleWindowClass", NULL);
ShowWindow(Stealth,0);
}
DLL_EXPORT void Travaille(const char *numero_port,const char *Commande)
{
std::string Action;
port_name = "\\\\.\\COM";
port_name = port_name + numero_port;
char port_name_char[port_name.size() + 1];
strcpy(port_name_char, port_name.c_str()); // or pass &s[0]
//Stealth();
SerialPort arduino(port_name_char);
//Action = argv[2];
Action = Commande;
if (arduino.isConnected())
{
//Writing to arduino
cout << "CONNECTEE " << Action << " -> ";
if ( Action == "ouvre")
{
arduino.writeSerialPort("ouvre\n", MAX_DATA_LENGTH);
printf ("ouvre");
}
if ( Action == "ferme")
{
arduino.writeSerialPort("ferme\n", MAX_DATA_LENGTH);
printf ("ferme");
}
if ( Action == "init")
{
arduino.writeSerialPort("init\n", MAX_DATA_LENGTH);
printf ("initialise");
}
if ( Action == "perdu")
{
arduino.writeSerialPort("perdu\n", MAX_DATA_LENGTH);
printf ("perdu");
}
if ( Action == "gagne")
{
arduino.writeSerialPort("gagne\n", MAX_DATA_LENGTH);
printf ("gagne");
}
//Sleep(50);
}
else
{
cout << " NON connectee !!! ";
}
//*/
}
h file :
/*
* Author: Manash Kumar Mandal
* Modified Library introduced in Arduino Playground which does not work
* This works perfectly
* LICENSE: MIT
*/
#ifndef SERIALPORT_H
#define SERIALPORT_H
#define ARDUINO_WAIT_TIME 2000
#define MAX_DATA_LENGTH 255
//#include <windows.h>
//#include <stdio.h>
//#include <stdlib.h>
class SerialPort
{
private:
HANDLE handler;
bool connected;
COMSTAT status;
DWORD errors;
public:
SerialPort(char *portName);
~SerialPort();
int readSerialPort(char *buffer, unsigned int buf_size);
bool writeSerialPort(char *buffer, unsigned int buf_size);
bool isConnected();
};
#endif // SERIALPORT_H