sprint 1-alpha
|
00001 #ifndef _SPRINT_SERIAL_H 00002 #define _SPRINT_SERIAL_H 00003 00006 00007 #define OUTPUTDEBUGSTRING(a) 00008 00009 #ifdef WIN32 00010 # include <windows.h> 00011 #endif 00012 00013 #include <vector> 00014 #include <string> 00015 00016 namespace sprint { 00017 00018 namespace io { 00019 /* 00020 For instance, if one thread were waiting for a ReadFile function to return, any other thread that issued a WriteFile function would be blocked. 00021 */ 00022 00023 #ifdef WIN32 00024 00025 BOOL Win32_Serial_Write(HANDLE hComm, const char * lpBuf, DWORD dwToWrite); 00027 DWORD Win32_Serial_Read(HANDLE hComm, char *buffer, DWORD buffer_lenght); 00028 #endif 00029 00030 00032 class serial { 00033 LPCOMMPROP lpCommProp; 00034 00035 public: 00036 HANDLE hComm; 00037 00038 public: 00039 serial(); 00040 00043 serial(LPCTSTR lpszComName); 00044 00045 ~serial(); 00046 00048 bool open(LPCTSTR lpszInput, bool read, bool write, bool async); 00049 00051 LPCOMMPROP GetProp(); 00052 00054 bool good() const { 00055 return hComm!=INVALID_HANDLE_VALUE; 00056 } 00057 00058 static bool IsAvaiable(LPCTSTR lpszCommName); 00059 static BOOL Configure(HWND hWnd, LPCTSTR lpszComName); 00060 static bool Test(LPCTSTR lpszCommName, LPTSTR lpszMsgBuffer); 00061 static BOOL TestDCB(LPCTSTR lpszCommName, DCB *test); 00062 static void DCBString(LPSTR lpszBuffer, UINT uBufferLen, DCB *test); 00063 00064 // static bool ComboBox_EnumPort(HWND hCombo); 00065 00066 inline void SetTimeouts(COMMTIMEOUTS *pcto) 00067 { 00068 SetCommTimeouts(hComm, pcto); 00069 } 00070 00071 inline BOOL SetState(DCB *dcb) 00072 { 00073 return SetCommState(hComm, dcb); 00074 } 00075 00076 inline BOOL GetState(DCB *dcb) const 00077 { 00078 return GetCommState(hComm, dcb); 00079 } 00080 00081 bool SetBaudRate(int rate) 00082 { 00083 DCB dcb; 00084 memset(&dcb, 0, sizeof(DCB)); 00085 00086 dcb.DCBlength = sizeof(DCB); 00087 if(::GetCommState(hComm, &dcb) == FALSE) 00088 return false; 00089 00090 dcb.BaudRate = rate; 00091 00092 00093 return ::SetCommState(hComm, &dcb) == TRUE; 00094 } 00095 00096 bool SetParity(int parity) 00097 { 00098 00099 } 00100 00101 bool SetFlowControl(int mode) 00102 { 00103 DCB dcb; 00104 memset(&dcb, 0, sizeof(DCB)); 00105 00106 dcb.DCBlength = sizeof(DCB); 00107 if(::GetCommState(hComm, &dcb) == FALSE) 00108 return false; 00109 00110 dcb.fOutxCtsFlow = FALSE; // No CTS output flow control 00111 dcb.fOutxDsrFlow = FALSE; // No DSR output flow control 00112 dcb.fTXContinueOnXoff = TRUE; // XOFF continues Tx 00113 dcb.fDtrControl = DTR_CONTROL_ENABLE; // DTR flow control type 00114 dcb.fDsrSensitivity = FALSE; // DSR sensitivity 00115 dcb.fOutX = FALSE; // No XON/XOFF out flow control 00116 dcb.fInX = FALSE; // No XON/XOFF in flow control 00117 dcb.fRtsControl = RTS_CONTROL_ENABLE; // RTS flow control: RTS active 00118 00119 if(mode == 0) 00120 { 00121 /* 00122 dcb.fDtrControl = DTR_CONTROL_DISABLE; // DTR flow control type 00123 dcb.fRtsControl = RTS_CONTROL_DISABLE; // RTS flow control 00124 */ 00125 00126 // dcb.c_iflag &= ~(IXOFF | IXON); 00127 } 00128 else 00129 if(mode == 1) 00130 { 00131 dcb.fOutX = TRUE; 00132 dcb.fInX = TRUE; 00133 // dcb.c_iflag |= IXOFF | IXON; 00134 // dcb.c_cflag |= CRTSCTS; 00135 } 00136 else 00137 if(mode == 2) 00138 { 00139 dcb.fOutxCtsFlow = TRUE; 00140 dcb.fRtsControl = RTS_CONTROL_HANDSHAKE; 00141 // dcb.c_iflag |= IXOFF | IXON; 00142 // dcb.c_cflag |= CRTSCTS; 00143 } 00144 00145 00146 return ::SetCommState(hComm, &dcb) == TRUE; 00147 } 00148 00149 inline BOOL EscapeFunction(DWORD dwFunc) 00150 { 00151 return EscapeCommFunction(hComm, dwFunc); 00152 } 00153 00154 inline BOOL GetModemStatus(LPDWORD lpModemStat) 00155 { 00156 return GetCommModemStatus(hComm, lpModemStat); 00157 } 00158 00160 inline void close() 00161 { 00162 CloseHandle(hComm); 00163 hComm = INVALID_HANDLE_VALUE; 00164 } 00165 00167 BOOL Read(char *buffer, UINT uBufferSize, LPDWORD lpdwNumberOfBytesRead, LPDWORD lpdwStatus); 00169 BOOL Write(const char *buffer, UINT uBufferSize, LPDWORD lpdwNumberOfBytesWritten, LPDWORD lpdwStatus); 00170 00172 int read(char *buf, int size) 00173 { 00174 DWORD dwSize; 00175 BOOL ret = Read(buf, size, &dwSize, NULL); 00176 return (ret) ? (int) dwSize : -1; 00177 } 00178 00180 int get() 00181 { 00182 DWORD dwSize; 00183 char buf; 00184 do { 00185 BOOL ret = Read(&buf, 1, &dwSize, NULL); 00186 if(!ret) 00187 return -1; 00188 } while(dwSize == 0); 00189 00190 return buf; 00191 } 00192 00194 int write(const char *buf, int size) 00195 { 00196 DWORD dwSize; 00197 BOOL ret = Write(buf, size, &dwSize, NULL); 00198 return (ret) ? dwSize : -1; 00199 } 00200 00201 }; 00202 00203 } // io 00204 00205 } // sprint 00206 00207 #endif