From: Thomas Pietrzak Date: Wed, 7 Mar 2012 10:43:08 +0000 (+0000) Subject: Check if the serial port is open, arduino serial communication in a separate standalo... X-Git-Url: https://git.thomaspietrzak.com/?a=commitdiff_plain;h=471ba7f4f2f5899e5e789eef1c1c32cbeb15acab;p=tactonlibrary.git Check if the serial port is open, arduino serial communication in a separate standalone static library git-svn-id: svn+ssh://thomaspietrzak.com/var/svn/rep@53 47cf9a05-e0a8-4ed5-9e9b-101a649bc004 --- diff --git a/TactonLibrary.suo b/TactonLibrary.suo index 5927214..3e62d93 100644 Binary files a/TactonLibrary.suo and b/TactonLibrary.suo differ diff --git a/TactonPlayer/Serial.cpp b/TactonPlayer/Serial.cpp deleted file mode 100644 index 6474450..0000000 --- a/TactonPlayer/Serial.cpp +++ /dev/null @@ -1,10 +0,0 @@ -#include "Serial.hpp" - -Serial::Serial(const char *) -:_connected(false) -{ -} - -Serial::~Serial() -{ -} diff --git a/TactonPlayer/Serial.hpp b/TactonPlayer/Serial.hpp deleted file mode 100644 index 4ee686d..0000000 --- a/TactonPlayer/Serial.hpp +++ /dev/null @@ -1,35 +0,0 @@ -#ifndef _SERIAL_ -#define _SERIAL_ - -#define ARDUINO_WAIT_TIME 500 - -class Serial -{ - public: - //Initialize Serial communication with the given COM port - Serial(const char *portName); - - //Close the connection - //NOTA: for some reason you can't connect again before exiting - //the program and running it again - virtual ~Serial(); - - //Read data in a buffer, if nbChar is greater than the - //maximum number of bytes available, it will return only the - //bytes available. The function return -1 when nothing could - //be read, the number of bytes actually read. - virtual int ReadData(void *buffer, unsigned int nbChar)=0; - - //Writes data from a buffer through the Serial connection - //return true on success. - virtual bool WriteData(void *buffer, unsigned int nbChar)=0; - - //Check if we are actually connected - bool IsConnected() { return _connected; } - - protected: - //Connection status - bool _connected; -}; - -#endif diff --git a/TactonPlayer/SerialWindows.cpp b/TactonPlayer/SerialWindows.cpp deleted file mode 100644 index 251f9bd..0000000 --- a/TactonPlayer/SerialWindows.cpp +++ /dev/null @@ -1,142 +0,0 @@ -#include "SerialWindows.hpp" - -#include -using namespace std; - -#ifdef WIN32 -#include -#endif - -SerialWindows::SerialWindows(const char *portName) -:Serial(portName) -{ - //Try to connect to the given port throuh CreateFile - _hSerial = CreateFileA(portName, - GENERIC_READ | GENERIC_WRITE, - 0, - NULL, - OPEN_EXISTING, - FILE_FLAG_NO_BUFFERING | FILE_FLAG_WRITE_THROUGH, //FILE_ATTRIBUTE_NORMAL, - NULL); - - //Check if the connection was successfull - if(_hSerial == INVALID_HANDLE_VALUE) - { - //If not success full display an Error - if(GetLastError() == ERROR_FILE_NOT_FOUND) - cerr << "ERROR: Handle was not attached. Reason: " << portName << " not available." << endl; - else - cerr << "ERROR unknown" << endl; - char buffer[256]; - sprintf(buffer, "Port %s does not exist or not reachable", portName); - throw buffer; - } - else - { - /* - COMMTIMEOUTS commTimeouts; - commTimeouts.ReadIntervalTimeout = 1; - commTimeouts.ReadTotalTimeoutMultiplier = 10; - commTimeouts.ReadTotalTimeoutConstant = 100; - commTimeouts.WriteTotalTimeoutMultiplier = 10; - commTimeouts.WriteTotalTimeoutConstant = 100; - if (SetCommTimeouts(_comport, &commTimeouts) == 0) - throw "Cannot set COM port timeouts";*/ - - - //If connected we try to set the comm parameters - DCB dcbSerialParams = {0}; - - //Try to get the current - if (!GetCommState(_hSerial, &dcbSerialParams)) - //If impossible, show an error - cerr << "ERROR: failed to get current serial parameters!" << endl; - else - { - //Define serial connection parameters for the arduino board - dcbSerialParams.BaudRate=CBR_57600; - dcbSerialParams.ByteSize=8; - dcbSerialParams.StopBits=ONESTOPBIT; - dcbSerialParams.Parity=NOPARITY; - - //Set the parameters and check for their proper application - if(!SetCommState(_hSerial, &dcbSerialParams)) - cerr << "ERROR: Could not set Serial Port parameters" << endl; - else - { - //If everything went fine we're connected - _connected = true; - //We wait 2s as the arduino board will be reseting - Sleep(2000); - //SDL_Delay(ARDUINO_WAIT_TIME); - } - } - } -} - -SerialWindows::~SerialWindows() -{ - //Check if we are connected before trying to disconnect - if(_connected) - { - //We're no longer connected - _connected = false; - //Close the serial handler - CloseHandle(_hSerial); - } -} - -int SerialWindows::ReadData(void *buffer, unsigned int nbChar) -{ - //Number of bytes we'll have read - DWORD bytesRead = 0; - //Number of bytes we'll really ask to read - unsigned int toRead = 0; - - //Use the ClearCommError function to get status info on the Serial port - ClearCommError(_hSerial, &_errors, &_status); - - //Check if there is something to read - if(_status.cbInQue > 0) - { - //If there is we check if there is enough data to read the required number - //of characters, if not we'll read only the available characters to prevent - //locking of the application. - if(_status.cbInQue > nbChar) - toRead = nbChar; - else - toRead = _status.cbInQue; - - //Try to read the require number of chars, and return the number of read bytes on success - if(ReadFile(_hSerial, buffer, toRead, &bytesRead, NULL) && bytesRead != 0) - return bytesRead; - } - - //If nothing has been read, or that an error was detected return -1 - return -1; -} - - -bool SerialWindows::WriteData(void *buffer, unsigned int nbChar) -{ - DWORD bytesSend; - - //Try to write the buffer on the Serial port - if(!WriteFile(_hSerial, buffer, nbChar, &bytesSend, 0)) - { - //In case it don't work get comm error and return false - ClearCommError(_hSerial, &_errors, &_status); - - return false; - } - else - { - ClearCommError(_hSerial, &_errors, &_status); -/* if(!FlushFileBuffers(_hSerial)) - cout << "ERROR while flushing" << endl;*/ -/* stringstream s; - s << bytesSend << " SENT" << endl; - OutputDebugString(s.str().c_str());*/ - return true; - } -} diff --git a/TactonPlayer/SerialWindows.hpp b/TactonPlayer/SerialWindows.hpp deleted file mode 100644 index a7e5c16..0000000 --- a/TactonPlayer/SerialWindows.hpp +++ /dev/null @@ -1,35 +0,0 @@ -#ifndef _SERIALWINDOWS_ -#define _SERIALWINDOWS_ - -#include "Serial.hpp" - -#include - -class SerialWindows : public Serial -{ - public: - //Initialize Serial communication with the given COM port - SerialWindows(const char *portName); - //Close the connection - //NOTA: for some reason you can't connect again before exiting - //the program and running it again - ~SerialWindows(); - //Read data in a buffer, if nbChar is greater than the - //maximum number of bytes available, it will return only the - //bytes available. The function return -1 when nothing could - //be read, the number of bytes actually read. - int ReadData(void *buffer, unsigned int nbChar); - //Writes data from a buffer through the Serial connection - //return true on success. - bool WriteData(void *buffer, unsigned int nbChar); - - - private: - //Serial comm handler - HANDLE _hSerial; - //Get various information about the connection - COMSTAT _status; - //Keep track of last error - DWORD _errors; -}; -#endif diff --git a/TactonPlayer/TactonPlayer.cpp b/TactonPlayer/TactonPlayer.cpp index 35eab85..e57595c 100644 --- a/TactonPlayer/TactonPlayer.cpp +++ b/TactonPlayer/TactonPlayer.cpp @@ -1,6 +1,6 @@ #include "TactonPlayer.hpp" -#include "SerialWindows.hpp" +#include #include diff --git a/TactonPlayer/TactonPlayer.hpp b/TactonPlayer/TactonPlayer.hpp index bb77e14..3671db2 100644 --- a/TactonPlayer/TactonPlayer.hpp +++ b/TactonPlayer/TactonPlayer.hpp @@ -1,6 +1,6 @@ #include "Tacton.hpp" -#include "Serial.hpp" +#include class TactonPlayer { diff --git a/TactonPlayer/TactonPlayer.vcxproj b/TactonPlayer/TactonPlayer.vcxproj index 7902274..86019d3 100644 --- a/TactonPlayer/TactonPlayer.vcxproj +++ b/TactonPlayer/TactonPlayer.vcxproj @@ -56,7 +56,7 @@ Windows true - arduinoserial.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies) + arduinoseriald.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies) copy /y $(CodeAnalysisInputAssembly) C:\Windows\system\