From 471ba7f4f2f5899e5e789eef1c1c32cbeb15acab Mon Sep 17 00:00:00 2001 From: Thomas Pietrzak Date: Wed, 7 Mar 2012 10:43:08 +0000 Subject: [PATCH] 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 --- TactonLibrary.suo | Bin 24576 -> 25600 bytes TactonPlayer/Serial.cpp | 10 --- TactonPlayer/Serial.hpp | 35 -------- TactonPlayer/SerialWindows.cpp | 142 ------------------------------ TactonPlayer/SerialWindows.hpp | 35 -------- TactonPlayer/TactonPlayer.cpp | 2 +- TactonPlayer/TactonPlayer.hpp | 2 +- TactonPlayer/TactonPlayer.vcxproj | 2 +- 8 files changed, 3 insertions(+), 225 deletions(-) delete mode 100644 TactonPlayer/Serial.cpp delete mode 100644 TactonPlayer/Serial.hpp delete mode 100644 TactonPlayer/SerialWindows.cpp delete mode 100644 TactonPlayer/SerialWindows.hpp diff --git a/TactonLibrary.suo b/TactonLibrary.suo index 592721446cd606d7c54e95e53ac64722c231c1a2..3e62d939c917d3dd768a3f197e13a7b07ea5fb2f 100644 GIT binary patch delta 1149 zcmcIiOH30{6rDGnek@Zh1quR68KfCjP)$EB<2Au#xyl zq{oFZYLK`v(U`)xGDd53;V)5)D>v%aM8$-KF~)iah$*^ul9Tt&efOO^_nn(av&2)D zO7OZ0j^iW(5_}Ox_@a5$MBIc@l$Yg=^*UKnIG`E1($g6EDaV9+ys%%%c5Cg1&9vD< z*h=_?-P#t<0owHu`UwXKQNkg@uS_O0Kx>?EWR@SL^%y3!CGvOe3*NxZCvGE9Y6cU+ z5_wAhN-!{m=FbT)=-|aXisWWR5jTYl(^nT{N2g5PIz>SFrZwYvubHT@lB_h%LLwy^ zoG55DupHW(vJNp-5L6dvI8G(Em{0R612zA(u!Q+gE)>eb!Ul%Nf=69USNi4S#UY;U z!Gu8WM4USh?`P?rn%epHboE`gQx%gp-xso}GMQC%>qH)HK&r#dmFxx{RW%^JJPoO<3(-8gss#9LXV^`228NOH7iq1eae>=~39%2UY9qFJ1LO6NRWp{0a|$$wJdEBT z($!wLd~cA7?1a%@gcD89@pS>F^JHZszW!XYUc%@a2~PxfPK3^066iz5R{^P^+`yCp zCnx_5n>t41IXZRi)6+52D9uc#E(*6G{2g?o%Wr})vH_{OQdsg#p>hHiwG2ZD=0hxvk&uYRAw8?a^`H;E z8*67@PmkBF0wx1J^4r!246$|#uC0)mU1mWC`}eKo{Eo{4-uM*JIE+3Ev&px*kNn^N h>gCx;R(lfFcTIalHk%YDlboOPay?v_JIw9oegn6`o@xL9 delta 892 zcmb`DUr3Wt7{<@}{5Ge4bL!^&)68jO{-xR+Yr4d>&qz(&9P>|NGL6b4y08VAMIS}b z#Sq=oMM*`42tkzX+(b96b|Yw+5MA4K5ETaDh3kApu`asnzzgqt&hL57!>cS)B}tbQ zL9?Asr?=;Njk+2_NG&hi6G~Z9@X@)5E-s$dZX7;3>Hf$j#pi;k!q}T#I~Oj%cy{|- z*qRgNl5@Pc$1e6gdqsd;!JFwzlfflHspHTOlkCq^cY45D>377F;n;v zMbepsv)PRIeG_rlvM!&y{@LZGOUh!teuYA5Rxq|_1kb!33I7mH>`!IsiH5U9fAMXS z0Drh2OL7oa%Lod*7OcuTTToEHOX(7X;kDO&`xs&Sj88(O>j3M4} z9tGX`NOaHB>*(#fmNU)~HQjC8AG@VdClNGMg4v!J76QXq2zlazZal6u;ch_7g%gO_ z>e;)cA|4$r!jtYh|0S9FkF}{Dk(PS+yG-!OF*JI0RKlIHhgq{c -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\ -- 2.30.2