⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 comm_api.cpp

📁 free sources for gsm
💻 CPP
📖 第 1 页 / 共 3 页
字号:
		else {
			BytesWritten=0;
			ClearError(&ErrorFlags, &Stat);
		}
	}

	Sleep(0);

	return (BytesWritten==BytesToWrite);
}

DWORD CCommApi::ReadBlock(unsigned char *Buffer, DWORD BytesToRead) {
// reads block from device

// function return number of really read bytes

	DWORD BytesRead, Error, ErrorFlags;
	COMSTAT	Stat;

	if (!ReadFile(FComHandle, Buffer, BytesToRead, &BytesRead, &FOsRead)) {
		if (GetLastError()==ERROR_IO_PENDING) {
			while (!GetOverlappedResult(FComHandle, &FOsRead, &BytesRead, true)) {
				Sleep(0);
				Error=GetLastError();

				if (Error!=ERROR_IO_INCOMPLETE)	{
					ClearError(&ErrorFlags, &Stat);
					break;
				}
			}
		}
		else {
			BytesRead=0;
			ClearError(&ErrorFlags, &Stat);
		}
	}

	Sleep(0);

   if (BytesRead<BytesToRead)
      if (Errors) {
         sprintf(TxtBuff, "Communication timeout (requested %d B, received %d B).", BytesToRead, BytesRead);
         ErrReport(TxtBuff);
      }

	return BytesRead;
}

BOOL CCommApi::SetDtr() {
// perform SETDTR function

// if the function succeeds, returns true

   return Escape(SETDTR);
}

BOOL CCommApi::SetRts() {
// perform SETRTS function

// if the function succeeds, returns true

   return Escape(SETRTS);
}

BOOL CCommApi::ClearDtr() {
// perform CLRDTR function

// if the function succeeds, returns true

   return Escape(CLRDTR);
}

BOOL CCommApi::ClearRts() {
// perform CLRRTS function

// if the function succeeds, returns true

   return Escape(CLRRTS);
}

unsigned long CCommApi::GetInQueueLn() {
// gets length of input queue

	DWORD ErrorFlags;
	COMSTAT	Stat;

	ClearError(&ErrorFlags, &Stat);

	return Stat.cbInQue;
}

unsigned long CCommApi::GetOutQueueLn() {
// gets length of output queue

	DWORD ErrorFlags;
	COMSTAT	Stat;

	ClearError(&ErrorFlags, &Stat);

	return Stat.cbOutQue;
}

BOOL CCommApi::WriteChar(unsigned char CharToWrite) {
// write char to com port

// if the function succeeds, returns true

   return WriteBlock(&CharToWrite, 1);
}

BOOL CCommApi::WriteChars(unsigned char *Buffer, DWORD BytesToWrite) {
// write block by chars to com port

// if the function succeeds, returns true

   DWORD i;

   for (i=0; i<BytesToWrite; i++)
      if (!WriteChar(Buffer[i]))
         return false;

   return true;
}

BOOL CCommApi::WaitOutQueueEmpty(int Timeout) {
// wait for desired char in specified timeout

   int FreqTimeout;
   unsigned long OutQueueLn;
   __int64 start, freq, stop, now;

   // get system frequency
   QueryPerformanceFrequency((_LARGE_INTEGER*)(&freq));

   // calc given miliseconds to count
   FreqTimeout=Timeout*freq/1000;

   // initial count
   QueryPerformanceCounter((_LARGE_INTEGER*)(&start));

   do {
      // get output queue length
      OutQueueLn=GetOutQueueLn();

      // current count
      QueryPerformanceCounter((_LARGE_INTEGER*)(&stop));

      // stop now?
      now=stop-start;

      // update progress
      Application->ProcessMessages();

   } while ((OutQueueLn>0) && (now<=FreqTimeout));

   if (OutQueueLn>0)
      return false;

   if (!PurgeAll())
      return false;

   return true;
}

BOOL CCommApi::RequestChar(int Timeout, unsigned char RequestedChar) {
// wait for desired char in specified timeout

   int FreqTimeout;
   unsigned char CharRead;
   __int64 start, freq, stop, now;

   // get system frequency
   QueryPerformanceFrequency((_LARGE_INTEGER*)(&freq));

   // calc given miliseconds to count
   FreqTimeout=Timeout*freq/1000;

   // initial count
   QueryPerformanceCounter((_LARGE_INTEGER*)(&start));

   do {
      if (GetInQueueLn()>0)
         ReadBlock(&CharRead, 1);

      // current count
      QueryPerformanceCounter((_LARGE_INTEGER*)(&stop));

      // stop now?
      now=stop-start;

      // update progress
      Application->ProcessMessages();

   } while ((CharRead!=RequestedChar) && (now<=FreqTimeout));

   if (CharRead!=RequestedChar) {
      if (Errors) {
         sprintf(TxtBuff, "Communication timeout (in %d ms, waiting for 0x%02X).", Timeout, RequestedChar);
         ErrReport(TxtBuff);
      }

      PurgeAll();

      return false;
   }

   return true;
}

BOOL CCommApi::RequestCharExcept(int Timeout, unsigned char ExceptedChar1, unsigned char ExceptedChar2) {
// wait for any char expception from ExceptedChar

   int FreqTimeout;
   unsigned char CharRead;
   __int64 start, freq, stop, now;

   // get system frequency
   QueryPerformanceFrequency((_LARGE_INTEGER*)(&freq));

   // calc given miliseconds to count
   FreqTimeout=Timeout*freq/1000;

   // initial count
   QueryPerformanceCounter((_LARGE_INTEGER*)(&start));

   do {
      if (GetInQueueLn()>0)
         ReadBlock(&CharRead, 1);

      // current count
      QueryPerformanceCounter((_LARGE_INTEGER*)(&stop));

      // stop now?
      now=stop-start;

      // update progress
      Application->ProcessMessages();

   } while (((CharRead==ExceptedChar1) || (CharRead==ExceptedChar2)) && (now<=FreqTimeout));

   if (now>=FreqTimeout) {
      if (Errors) {
         sprintf(TxtBuff, "Communication timeout (in %d ms, waiting for char).", Timeout);
         ErrReport(TxtBuff);
      }

      PurgeAll();

      return false;
   }

   return true;
}

BOOL CCommApi::SendAndRequestChar(int Timeout, unsigned char SendChar, unsigned char RequestedChar) {
// send char in a cycle and wait for desired char in specified timeout

   int FreqTimeout;
   unsigned char CharRead;
   __int64 start, freq, stop, now;

   // get system frequency
   QueryPerformanceFrequency((_LARGE_INTEGER*)(&freq));

   // calc given miliseconds to count
   FreqTimeout=Timeout*freq/1000;

   // initial count
   QueryPerformanceCounter((_LARGE_INTEGER*)(&start));

   do {
      if (!WriteChar(SendChar))
         return false;

      if (GetInQueueLn()>0)
         ReadBlock(&CharRead, 1);

      // current count
      QueryPerformanceCounter((_LARGE_INTEGER*)(&stop));

      // stop now?
      now=stop-start;

      // update progress
      Application->ProcessMessages();

   } while ((CharRead!=RequestedChar) && (now<=FreqTimeout));

   if (CharRead!=RequestedChar) {
      if (Errors) {
         sprintf(TxtBuff, "Communication timeout (in %d ms, waiting for 0x%02X).", Timeout, RequestedChar);
         ErrReport(TxtBuff);
      }

      PurgeAll();

      return false;
   }

   return true;
}

BOOL CCommApi::SendBlockAndRequestChar(int Timeout, unsigned char *SendBlock, int SendBlockLn, unsigned char RequestedChar) {
// send block in a cycle and wait for desired char in specified timeout

   int FreqTimeout;
   unsigned char CharRead;
   __int64 start, freq, stop, now;

   // get system frequency
   QueryPerformanceFrequency((_LARGE_INTEGER*)(&freq));

   // calc given miliseconds to count
   FreqTimeout=Timeout*freq/1000;

   // initial count
   QueryPerformanceCounter((_LARGE_INTEGER*)(&start));

   do {
      if (!WriteBlock(SendBlock, SendBlockLn))
         return false;

      Sleep(30);

      if (GetInQueueLn()>0)
         ReadBlock(&CharRead, 1);

      // current count
      QueryPerformanceCounter((_LARGE_INTEGER*)(&stop));

      // stop now?
      now=stop-start;

      // update progress
      Application->ProcessMessages();

   } while ((CharRead!=RequestedChar) && (now<=FreqTimeout));

   if (CharRead!=RequestedChar) {
      if (Errors) {
         sprintf(TxtBuff, "Communication timeout (in %d ms, waiting for 0x%02X).", Timeout, RequestedChar);
         ErrReport(TxtBuff);
      }

      PurgeAll();

      return false;
   }

   return true;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -