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

📄 comm_api.cpp

📁 free sources for gsm
💻 CPP
📖 第 1 页 / 共 3 页
字号:
//---------------------------------------------------------------------------

#pragma hdrstop

#include <stdio>
#include "comm_api.h"

//---------------------------------------------------------------------------

#pragma package(smart_init)

CCommApi::CCommApi() {
   Errors=true;
}

CCommApi::~CCommApi() {
}

void CCommApi::SetErrors(BOOL Status) {
   Errors=Status;
}

BOOL CCommApi::OpenCom(char *ComName) {
// open com port

   // overlapped structures
   FOsRead.Offset=0;
	FOsRead.OffsetHigh=0;

   FOsRead.hEvent=CreateEvent(NULL, true, false, NULL);

	if (!FOsRead.hEvent) {
      ErrReport("Cannot create reading overlapped event.");
		return false;
   }

	FOsWrite.Offset=0;
	FOsWrite.OffsetHigh=0;

   FOsWrite.hEvent=CreateEvent(NULL, true, false, NULL);

	if (!FOsWrite.hEvent) {
      ErrReport("Cannot create writing overlapped event.");
		return false;
   }

   // open comport
	FComHandle=CreateFile(ComName, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED, NULL);

	if (FComHandle==INVALID_HANDLE_VALUE) {
      ErrReport("Cannot open COM port.");
      return false;
   }

   return true;
}

BOOL CCommApi::CloseCom() {
// close com port

   if ((FComHandle!=NULL) && (FComHandle!=INVALID_HANDLE_VALUE)) {
//      if (!SetMask(0))
//         return false;

      if (!PurgeAll())
         return false;

      if (!CloseHandle(FComHandle)) {
         ErrReport("Cannot close COM port.");
         return false;
      }
   }

   return true;
}

BOOL CCommApi::SetMask(DWORD dwEvtMask) {
// specifies a set of events to be monitored

// if the function succeeds, returns true

// function specifies the set of events that can be monitored for a particular communications resource
// a handle to the communications resource can be specified in a call to the WaitEvent function, which waits for one of the events to occur
// to get the current event mask of a communications resource, use the GetMask function

// events to be enabled:
// ---------------------

// EV_BREAK
// 0x0040 	a break was detected on input

// EV_CTS
// 0x0008 	the CTS (clear-to-send) signal changed state

// EV_DSR
// 0x0010 	the DSR (data-set-ready) signal changed state

// EV_ERR
// 0x0080 	a line-status error occurred; line-status errors are CE_FRAME, CE_OVERRUN, and CE_RXPARITY

// EV_RING
// 0x0100 	a ring indicator was detected

// EV_RLSD
// 0x0020 	the RLSD (receive-line-signal-detect) signal changed state

// EV_RXCHAR
// 0x0001 	a character was received and placed in the input buffer

// EV_RXFLAG
// 0x0002 	the event character was received and placed in the input buffer;
//         the event character is specified in the device's DCB structure, which is applied to a serial port by using the SetState function

// EV_TXEMPTY
// 0x0004 	the last character in the output buffer was sent

   if (!SetCommMask(FComHandle, dwEvtMask)) {
      ErrReport("Cannot set COM port events.");
      return false;
   }

   return true;
}

BOOL CCommApi::Purge(DWORD dwFlags) {
// discards all characters from the output or input buffer
// it can also terminate pending read or write operations on the resource

// if the function succeeds, returns true

// NOTE: if a thread uses Purge to flush an output buffer, the deleted characters are not transmitted;
// to empty the output buffer while ensuring that the contents are transmitted, call the FlushFileBuffers function (a synchronous operation);
// note, however, that FlushFileBuffers is subject to flow control but not to write time-outs, and it will not return until all pending write operations have been transmitted

// possible parameters:
// --------------------

// PURGE_RXABORT
// 0x0002 	terminates all outstanding overlapped read operations and returns immediately, even if the read operations have not been completed

// PURGE_RXCLEAR
// 0x0008 	clears the input buffer (if the device driver has one)

// PURGE_TXABORT
// 0x0001 	terminates all outstanding overlapped write operations and returns immediately, even if the write operations have not been completed

// PURGE_TXCLEAR
// 0x0004 	clears the output buffer (if the device driver has one)

   if (!PurgeComm(FComHandle, dwFlags)) {
      ErrReport("Cannot flush COM port buffers.");
      return false;
   }

   return true;
}

BOOL CCommApi::PurgeAll() {
// discards all characters from the output and input buffer

   return Purge(PURGE_TXABORT|PURGE_TXCLEAR|PURGE_RXABORT|PURGE_RXCLEAR);
}

BOOL CCommApi::SetBuffers(DWORD dwInQueue, DWORD dwOutQueue) {
// initializes the communications parameters

// if the function succeeds, returns true

// parameters:
// -----------

// dwInQueue
//         recommended size of the device's internal input buffer, in bytes

// dwOutQueue
//         recommended size of the device's internal output buffer, in bytes

   if (!SetupComm(FComHandle, dwInQueue, dwOutQueue)) {
      ErrReport("Cannot setup COM port buffers.");
      return false;
   }

   return true;
}

BOOL CCommApi::SetTimeouts(DWORD ReadIntervalTimeout, DWORD ReadTotalTimeoutMultiplier, DWORD ReadTotalTimeoutConstant, DWORD WriteTotalTimeoutMultiplier, DWORD WriteTotalTimeoutConstant) {
// sets the time-out parameters for all read and write operations

// if the function succeeds, returns true

// parameters:
// -----------

// ReadIntervalTimeout
//         maximum time allowed to elapse between the arrival of two bytes on the communications line, in milliseconds
//         during a ReadFile operation, the time period begins when the first byte is received
//         if the interval between the arrival of any two bytes exceeds this amount, the ReadFile operation is completed and any buffered data is returned
//         a value of zero indicates that interval time-outs are not used

//         a value of MAXDWORD, combined with zero values for both the ReadTotalTimeoutConstant and ReadTotalTimeoutMultiplier members, specifies that the read operation is to return immediately with the bytes that have already been received, even if no bytes have been received

// ReadTotalTimeoutMultiplier
//         multiplier used to calculate the total time-out period for read operations, in milliseconds
//         for each read operation, this value is multiplied by the requested number of bytes to be read

// ReadTotalTimeoutConstant
//         constant used to calculate the total time-out period for read operations, in milliseconds
//         for each read operation, this value is added to the product of the ReadTotalTimeoutMultiplier member and the requested number of bytes

//         a value of zero for both the ReadTotalTimeoutMultiplier and ReadTotalTimeoutConstant members indicates that total time-outs are not used for read operations

// WriteTotalTimeoutMultiplier
//         multiplier used to calculate the total time-out period for write operations, in milliseconds
//         for each write operation, this value is multiplied by the number of bytes to be written

// WriteTotalTimeoutConstant
//         constant used to calculate the total time-out period for write operations, in milliseconds
//         for each write operation, this value is added to the product of the WriteTotalTimeoutMultiplier member and the number of bytes to be written

//         a value of zero for both the WriteTotalTimeoutMultiplier and WriteTotalTimeoutConstant members indicates that total time-outs are not used for write operations

// if an application sets ReadIntervalTimeout and ReadTotalTimeoutMultiplier to MAXDWORD and sets ReadTotalTimeoutConstant to a value greater than zero and less than MAXDWORD, one of the following occurs when the ReadFile function is called:
//      - if there are any bytes in the input buffer, ReadFile returns immediately with the bytes in the buffer
//      - if there are no bytes in the input buffer, ReadFile waits until a byte arrives and then returns immediately
//      - if no bytes arrive within the time specified by ReadTotalTimeoutConstant, ReadFile times out

   COMMTIMEOUTS CommTimeouts;

   CommTimeouts.ReadIntervalTimeout=ReadIntervalTimeout;
   CommTimeouts.ReadTotalTimeoutMultiplier=ReadTotalTimeoutMultiplier;
   CommTimeouts.ReadTotalTimeoutConstant=ReadTotalTimeoutConstant;
   CommTimeouts.WriteTotalTimeoutMultiplier=WriteTotalTimeoutMultiplier;
   CommTimeouts.WriteTotalTimeoutConstant=WriteTotalTimeoutConstant;

   if (!SetCommTimeouts(FComHandle, &CommTimeouts)) {
      ErrReport("Cannot set COM port timeouts.");
      return false;
   }

   return true;
}

BOOL CCommApi::GetState(LPDCB lpDCB) {
// retrieves the current control settings

// if the function succeeds, returns true

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

   return true;
}

BOOL CCommApi::SetState(LPDCB lpDCB) {
// configures a communications device according to the specifications in a device-control block (a DCB structure)
// the function reinitializes all hardware and control settings, but it does not empty output or input queues

// if the function succeeds, returns true

// remarks:
// the SetState function uses a DCB structure to specify the desired configuration
// the GetState function returns the current configuration
// to set only a few members of the DCB structure, you should modify a DCB structure that has been filled in by a call to GetState
// this ensures that the other members of the DCB structure have appropriate values

// the SetState function fails if the XonChar member of the DCB structure is equal to the XoffChar member

// when SetState is used to configure the 8250, the following restrictions apply to the values for the DCB structure's ByteSize and StopBits members:
// the number of data bits must be 5 to 8 bits

// DCB members:

// DCBlength
//      length of the structure, in bytes

// BaudRate
//      baud rate at which the communications device operates
//      this member can be an actual baud rate value, or one of the following indexes

// fBinary
//      if this member is TRUE, binary mode is enabled
//      Windows does not support nonbinary mode transfers, so this member must be TRUE

// fParity
//      if this member is TRUE, parity checking is performed and errors are reported

// fOutxCtsFlow
//      if this member is TRUE, the CTS (clear-to-send) signal is monitored for output flow control
//      if this member is TRUE and CTS is turned off, output is suspended until CTS is sent again

// fOutxDsrFlow
//      if this member is TRUE, the DSR (data-set-ready) signal is monitored for output flow control
//      if this member is TRUE and DSR is turned off, output is suspended until DSR is sent again

// fDtrControl
//      DTR (data-terminal-ready) flow control
//      this member can be one of the following values:

// value 	meaning
// --------------------
// DTR_CONTROL_DISABLE
// 0x00 	disables the DTR line when the device is opened and leaves it disabled

// DTR_CONTROL_ENABLE
// 0x01 	enables the DTR line when the device is opened and leaves it on

// DTR_CONTROL_HANDSHAKE
// 0x02 	enables DTR handshaking;
//         if handshaking is enabled, it is an error for the application to adjust the line by using the EscapeCommFunction function

// fDsrSensitivity
//      if this member is TRUE, the communications driver is sensitive to the state of the DSR signal;
//      the driver ignores any bytes received, unless the DSR modem input line is high

// fTXContinueOnXoff
//      if this member is TRUE, transmission continues after the input buffer has come within XoffLim bytes of being full and the driver has transmitted the XoffChar character to stop receiving bytes;
//      if this member is FALSE, transmission does not continue until the input buffer is within XonLim bytes of being empty and the driver has transmitted the XonChar character to resume reception

// fOutX
//      indicates whether XON/XOFF flow control is used during transmission;
//      if this member is TRUE, transmission stops when the XoffChar character is received and starts again when the XonChar character is received

// fInX
//      indicates whether XON/XOFF flow control is used during reception;
//      if this member is TRUE, the XoffChar character is sent when the input buffer comes within XoffLim bytes of being full, and the XonChar character is sent when the input buffer comes within XonLim bytes of being empty

// fErrorChar
//      indicates whether bytes received with parity errors are replaced with the character specified by the ErrorChar member;
//      if this member is TRUE and the fParity member is TRUE, replacement occurs

// fNull
//      if this member is TRUE, null bytes are discarded when received

// fRtsControl
//      RTS (request-to-send) flow control
//      this member can be one of the following values:

// value 	meaning
// --------------------
// RTS_CONTROL_DISABLE
// 0x00 	disables the RTS line when the device is opened and leaves it disabled

// RTS_CONTROL_ENABLE
// 0x01 	enables the RTS line when the device is opened and leaves it on

// RTS_CONTROL_HANDSHAKE
// 0x02 	enables RTS handshaking;
//         the driver raises the RTS line when the "type-ahead" (input) buffer is less than one-half full and lowers the RTS line when the buffer is more than three-quarters full;
//         if handshaking is enabled, it is an error for the application to adjust the line by using the EscapeCommFunction function

// RTS_CONTROL_TOGGLE

⌨️ 快捷键说明

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