|
Программирование >> Перегруженные имена функций и идентификаторы
hCom = INVALID HANDLE VALUE; -------------------------------------------------------int fastcall TComPort::Open(int n) bool ierr; AnsiString ComName; ComName = \\\\.\\COM +IntToStr(n); if(hCom != INVALID HANDLE VALUE) Close(); hCom = CreateFile(ComName.c str(), GENERIC READGENERIC WRITE, 0, 0, OPEN EXISTING, FILE ATTRIBUTE NORMALFILE FLAG OVERLAPPED, if (hCom == INVALID HANDLE VALUE) throw Exception ( Невозможно открыть порт COM +IntToStr(n)); SetupComm(hCom, 2048, 2048); GetCommTimeouts(hCom, &Timeouts); Timeouts.ReadIntervalTimeout = MAXDWORD; Timeouts.ReadTotalTimeoutMultiplier = 0; Timeouts.ReadTotalTimeoutConstant = 0; Timeouts.WriteTotalTimeoutMultiplier = 0; Timeouts.WriteTotalTimeoutConstant = 0; ierr = SetCommTimeouts(hCom, &Timeouts); if(!ierr) throw Exception ( Ошибка инициализации порта COM +IntToStr(n)); GetCommState(hCom, &dcbBuf); dcbBuf.BaudRate = iBaudRate; dcbBuf.fBinary = true; dcbBuf.fParity = false; dcbBuf.ByteSize = 8; dcbBuf.Parity = 0; dcbBuf.StopBits = 0; ierr = SetCommState(hCom, &dcbBuf); if(!ierr) throw Exception ( Ошибка инициализации порта COM +IntToStr(n)); ierr = SetCommMask(hCom, EV RXCHAR); if(!ierr) throw Exception ( Ошибка инициализации порта COM +IntToStr(n)); return iComNumber = n; --------------------------------------------------int fastcall TComPort::Open(void) return Open(iComNumber); -------------------------------------------------void fastcall TComPort::Close(void) CloseHandle(hCom); hCom = INVALID HANDLE VALUE; -------------------------------------------------void fastcall TComPort::FlushBuffers(void) PurgeComm(hCom, PURGE TXABORTPURGE TXCLEARPURGE RXABORTPURGE RXCLEAR); --------------------------------------------------DWORD fastcall TComPort::WriteBlock(void *buf, int count) DWORD realCount; WriteFile(hCom, buf, count, &realCount, &OverlappedStructure); return realCount; ---------------------------------------------------DWORD fastcall TComPort::ReadBlock(void *buf, int count) DWORD realCount; bool bResult = ReadFile(hCom, buf, count, &realCount, &OverlappedStructure); if there was a problem, or the async. operations still pending ... if(!bResult) { deal with the error code switch(GetLastError()) case ERROR HANDLE EOF: were reached the end of the file during the call to ReadFile code to handle that throw Exception( 1 ); case ERROR IO PENDING: asynchronous i/o is still in progress do something else for a while Sleep(100); check on the results of the asynchronous read bResult = GetOverlappedResult(hCom, &OverlappedStructure, &realCount, false); if there was a problem ... if(!bResult) { deal with the error code switch(GetLastError()) case ERROR HANDLE EOF: were reached the end of the file during asynchronous operation throw Exception( 2 ); deal with other error cases default: throw Exception( 3 );
|
© 2006 - 2024 pmbk.ru. Генерация страницы: 0
При копировании материалов приветствуются ссылки. |