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

📄 comm_api.cpp

📁 free sources for gsm
💻 CPP
📖 第 1 页 / 共 3 页
字号:
// 0x03 	specifies that the RTS line will be high if bytes are available for transmission;
//         after all buffered bytes have been sent, the RTS line will be low
//         Windows Me/98/95: this value is not supported

// fAbortOnError
//      if this member is TRUE, the driver terminates all read and write operations with an error status if an error occurs;
//      the driver will not accept any further communications operations until the application has acknowledged the error by calling the ClearCommError function

// fDummy2
//      reserved; do not use

// wReserved
//      reserved; must be zero

// XonLim
//      minimum number of bytes allowed in the input buffer before flow control is activated to inhibit the sender;
//      note that the sender may transmit characters after the flow control signal has been activated, so this value should never be zero;
//      this assumes that either XON/XOFF, RTS, or DTR input flow control is specified in fInX, fRtsControl, or fDtrControl

// XoffLim
//      maximum number of bytes allowed in the input buffer before flow control is activated to allow transmission by the sender;
//      this assumes that either XON/XOFF, RTS, or DTR input flow control is specified in fInX, fRtsControl, or fDtrControl;
//      the maximum number of bytes allowed is calculated by subtracting this value from the size, in bytes, of the input buffer

// ByteSize
//      number of bits in the bytes transmitted and received

// Parity
//      parity scheme to be used;
//      this member can be one of the following values

// value 	meaning
// --------------------
// EVENPARITY
// 2 	   even parity

// MARKPARITY
// 3 	   mark parity

// NOPARITY
// 0 	   no parity

// ODDPARITY
// 1 	   odd parity

// SPACEPARITY
// 4 	   space parity

// StopBits
//      number of stop bits to be used;
//      this member can be one of the following values:

// value 	meaning
// --------------------
// ONESTOPBIT
// 0 	   1 stop bit

// ONE5STOPBITS
// 1 	   1.5 stop bits

// TWOSTOPBITS
// 2 	   2 stop bits

// XonChar
//      value of the XON character for both transmission and reception

// XoffChar
//      value of the XOFF character for both transmission and reception

// ErrorChar
//      value of the character used to replace bytes received with a parity error

// EofChar
//      value of the character used to signal the end of data

// EvtChar
//      Value of the character used to signal an event

// wReserved1
//      reserved; do not use

   if (!SetCommState(FComHandle, lpDCB)) {
      ErrReport("Cannot set COM port state.");
      return false;
   }

   return true;
}

BOOL CCommApi::SetChars(char XonChar, char XoffChar, char ErrorChar, char EofChar, char EvtChar) {
// sets special characters

// if the function succeeds, returns true

   DCB dcb;

   dcb.DCBlength=sizeof(DCB);

   // get current state
   if (!GetState(&dcb))
      return false;

   // prepare chars
   dcb.XonChar=XonChar;
   dcb.XoffChar=XoffChar;
   dcb.ErrorChar=ErrorChar;
   dcb.EofChar=EofChar;
   dcb.EvtChar=EvtChar;

   // set new state
   return SetState(&dcb);
}

BOOL CCommApi::SetBaudRate(DWORD Bd) {
// sets baudrate

// if the function succeeds, returns true

   DCB dcb;

   dcb.DCBlength=sizeof(DCB);

   // get current state
   if (!GetState(&dcb))
      return false;

   // prepare baudrate
   dcb.BaudRate=Bd;

   // set new state
   return SetState(&dcb);
}

BOOL CCommApi::SetLineControl(unsigned char DataBits, unsigned char Parity, unsigned char StopBits) {
// sets ByteSize (number of data bits), parity and number of stop bits

// if the function succeeds, returns true

   DCB dcb;

   dcb.DCBlength=sizeof(DCB);

   // get current state
   if (!GetState(&dcb))
      return false;

   // prepare data
   dcb.ByteSize=DataBits;
   dcb.Parity=Parity;
   dcb.StopBits=StopBits;

   // set new state
   return SetState(&dcb);
}

BOOL CCommApi::SetRtsDtr(DWORD fRtsControl, DWORD fDtrControl) {
// sets RTS and DTR flow control

// if the function succeeds, returns true

   DCB dcb;

   dcb.DCBlength=sizeof(DCB);

   // get current state
   if (!GetState(&dcb))
      return false;

   // prepare data
   dcb.fRtsControl=fRtsControl;
   dcb.fDtrControl=fDtrControl;

   // set new state
   return SetState(&dcb);
}

BOOL CCommApi::SetLimits(unsigned short XonLim, unsigned short XoffLim) {
// sets XonLim and XoffLim

// if the function succeeds, returns true

   DCB dcb;

   dcb.DCBlength=sizeof(DCB);

   // get current state
   if (!GetState(&dcb))
      return false;

   // prepare data
   dcb.XonLim=XonLim;
   dcb.XoffLim=XoffLim;

   // set new state
   return SetState(&dcb);
}

BOOL CCommApi::Escape(DWORD dwFunc) {
// directs communications device to perform an extended function

// if the function succeeds, returns true

// parameter:
// ----------

// value 	meaning
// CLRBREAK
// 9 	   restores character transmission and places the transmission line in a nonbreak state;
//         the CLRBREAK extended function code is identical to the ClearCommBreak function

// CLRDTR
// 6 	   clears the DTR (data-terminal-ready) signal

// CLRRTS
// 4 	   clears the RTS (request-to-send) signal

// SETBREAK
// 8 	   suspends character transmission and places the transmission line in a break state until the ClearCommBreak function is called (or EscapeCommFunction is called with the CLRBREAK extended function code);
//         the SETBREAK extended function code is identical to the SetCommBreak function;
//         note that this extended function does not flush data that has not been transmitted

// SETDTR
// 5 	   sends the DTR (data-terminal-ready) signal

// SETRTS
// 3 	   sends the RTS (request-to-send) signal

// SETXOFF
// 1 	   causes transmission to act as if an XOFF character has been received

// SETXON
// 2 	   causes transmission to act as if an XON character has been received

   if (!EscapeCommFunction(FComHandle, dwFunc)) {
      ErrReport("Cannot perform extended COM port function.");
      return false;
   }

   return true;
}

BOOL CCommApi::ClearError(LPDWORD lpErrors, LPCOMSTAT lpStat) {
// retrieves information about a communications error and reports the current status of a communications device;
// the function is called when a communications error occurs, and it clears the device's error flag to enable additional input and output (I/O) operations

// if the function succeeds, returns true

// lpErrors parameter:
// -------------------

// value 	meaning
// CE_BREAK
// 0x0010 	the hardware detected a break condition

// CE_DNS
// 0x0800       Windows Me/98/95: a parallel device is not selected

// CE_FRAME
// 0x0008 	the hardware detected a framing error

// CE_IOE
// 0x0400 	an I/O error occurred during communications with the device

// CE_MODE
// 0x8000 	the requested mode is not supported, or the hFile parameter is invalid;
//         if this value is specified, it is the only valid error

// CE_OOP
// 0x1000       Windows Me/98/95: a parallel device signaled that it is out of paper

// CE_OVERRUN
// 0x0002 	a character-buffer overrun has occurred;
//         the next character is lost

// CE_PTO
// 0x0200       Windows Me/98/95: a time-out occurred on a parallel device

// CE_RXOVER
// 0x0001 	an input buffer overflow has occurred;
//         there is either no room in the input buffer, or a character was received after the end-of-file (EOF) character

// CE_RXPARITY
// 0x0004 	the hardware detected a parity error

// CE_TXFULL
// 0x0100 	the application tried to transmit a character, but the output buffer was full

// lpStat is pointer to a COMSTAT structure in which the device's status information is returned;
// if this parameter is NULL, no status information is returned

// lpStat members:

// fCtsHold
//      if this member is TRUE, transmission is waiting for the CTS (clear-to-send) signal to be sent

// fDsrHold
//      if this member is TRUE, transmission is waiting for the DSR (data-set-ready) signal to be sent

// fRlsdHold
//      if this member is TRUE, transmission is waiting for the RLSD (receive-line-signal-detect) signal to be sent

// fXoffHold
//      if this member is TRUE, transmission is waiting because the XOFF character was received

// fXoffSent
//      if this member is TRUE, transmission is waiting because the XOFF character was transmitted;
//      transmission halts when the XOFF character is transmitted to a system that takes the next character as XON, regardless of the actual character

// fEof
//      if this member is TRUE, the end-of-file (EOF) character has been received

// fTxim
//      if this member is TRUE, there is a character queued for transmission that has come to the communications device by way of the TransmitCommChar function;
//      the communications device transmits such a character ahead of other characters in the device's output buffer

// fReserved
//      reserved; do not use

// cbInQue
//      number of bytes received by the serial provider but not yet read by a ReadFile operation

// cbOutQue
//      number of bytes of user data remaining to be transmitted for all write operations;
//      this value will be zero for a nonoverlapped write

   if (!ClearCommError(FComHandle, lpErrors, lpStat)) {
      ErrReport("Cannot retrieve information about COM port status.");
      return false;
   }

   return true;
}

BOOL CCommApi::WriteBlock(unsigned char *Buffer, DWORD BytesToWrite) {
// writes block to device

// if the function succeeds, returns true

	DWORD BytesWritten, Error, ErrorFlags;
	COMSTAT	Stat;

	if (!WriteFile(FComHandle, Buffer, BytesToWrite, &BytesWritten, &FOsWrite)) {
		if (GetLastError()==ERROR_IO_PENDING) {
			while (!GetOverlappedResult(FComHandle, &FOsWrite, &BytesWritten, true)) {
				Sleep(0);
				Error=GetLastError();

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

⌨️ 快捷键说明

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