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

📄 serialport.h

📁 串口程序。dos底下的编程源码
💻 H
字号:
/*
Module : SERIALPORT.H
Purpose: Declaration for an MFC wrapper class for serial ports
Created: PJN / 31-05-1999

Copyright (c) 1999 - 2001 by PJ Naughter.  
All rights reserved.

*/



///////////////////// Macros / Structs etc //////////////////////////

#ifndef __SERIALPORT_H__
#define __SERIALPORT_H__



//Enums
enum FlowControl
{
    NoFlowControl,
    CtsRtsFlowControl,
    CtsDtrFlowControl,
    DsrRtsFlowControl,
    DsrDtrFlowControl,
    XonXoffFlowControl
};

enum Parity
{    
    EvenParity,
    MarkParity,
    NoParity,
    OddParity,
    SpaceParity
};

enum StopBits
{
    OneStopBit,
    OnePointFiveStopBits,
    TwoStopBits
};

//// The actual serial port class /////////////////////////////////////////////

/*
Module : SERIALPORT.CPP
Purpose: Implementation for an MFC wrapper class for serial ports
Created: PJN / 31-05-1999
History: PJN / 03-06-1999 1. Fixed problem with code using CancelIo which does not exist on 95.
                          2. Fixed leaks which can occur in sample app when an exception is thrown
         PJN / 16-06-1999 1. Fixed a bug whereby CString::ReleaseBuffer was not being called in 
                             CSerialException::GetErrorMessage
         PJN / 29-09-1999 1. Fixed a simple copy and paste bug in SetDTR
         PJN / 08-05-2000 1. Fixed an unreferrenced variable in GetOverlappedResult in VC 6
         PJN / 10-12-2000 1. Made class destructor virtual
         PJN / 15-01-2001 1. Attach method now also allows you to specify whether the serial port
                          is being attached to in overlapped mode
                          2. Removed some ASSERT's which were unnecessary in some of the functions
                          3. Updated the Read method which uses OVERLAPPED IO to also return the bytes
                          read. This allows calls to WriteFile with a 0'ized overlapped structure (This
                          is required when dealing with TAPI and serial communications)
                          4. Now includes copyright message in the source code and documentation.
         PJN / 24-03-2001 1. Added a BytesWaiting method
         PJN / 04-04-2001 1. Provided an overriden version of BytesWaiting which specifies a timeout
         PJN / 23-04-2001 1. Fixed a memory leak in DataWaiting method
		 LDB / 24-04-2002 1. Added WriteABuffer and ReadABuffer method

Copyright (c) 1996 - 2001 by PJ Naughter.  (Web: www.naughter.com, Email: pjna@naughter.com)

All rights reserved.

Copyright / Usage Details:

You are allowed to include the source code in any product (commercial, shareware, freeware or otherwise) 
when your product is released in binary form. You are allowed to modify the source code in any way you want 
except you cannot modify the copyright details at the top of each module. If you want to distribute source 
code with your application, then you are only allowed to distribute versions released by the author. This is 
to maintain a single distribution point for the source code. 

*/

/////////////////////////////////  Includes  //////////////////////////////////
#include "stdafx.h"
#include "serialport.h"


///////////////////////////////// defines /////////////////////////////////////

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

//////////////////////////////// Implementation ///////////////////////////////
////////// The actual serial port code


extern "C" HANDLE FAR PASCAL RS_Open(int nPort, DWORD dwBaud, Parity parity, BYTE DataBits, StopBits stopbits, FlowControl fc, BOOL bOverlapped);
extern "C" BOOL FAR PASCAL RS_Close(HANDLE hComm);
extern "C" DWORD FAR PASCAL RS_Read(HANDLE hComm, void* lpBuf, DWORD dwCount);
extern "C" BOOL FAR PASCAL RS_ReadOv(HANDLE hComm, void* lpBuf, DWORD dwCount, OVERLAPPED& overlapped, DWORD* pBytesRead);
extern "C" DWORD FAR PASCAL RS_Write(HANDLE hComm, const void* lpBuf, DWORD dwCount);
extern "C" BOOL FAR PASCAL RS_WriteOv(HANDLE hComm, const void* lpBuf, DWORD dwCount, OVERLAPPED& overlapped);
extern "C" BOOL FAR PASCAL RS_GetOverlappedResult(HANDLE hComm, OVERLAPPED& overlapped, DWORD& dwBytesTransferred, BOOL bWait);
extern "C" DWORD FAR PASCAL RS_BytesWaiting(HANDLE hComm);
extern "C" BOOL FAR PASCAL RS_TransmitChar(HANDLE hComm, char cChar);
extern "C" BOOL FAR PASCAL RS_GetConfig(HANDLE hComm, COMMCONFIG& config);
extern "C" BOOL FAR PASCAL RS_SetConfig(HANDLE hComm, COMMCONFIG& config);
extern "C" BOOL FAR PASCAL RS_SetBreak(HANDLE hComm);
extern "C" BOOL FAR PASCAL RS_ClearBreak(HANDLE hComm);
extern "C" BOOL FAR PASCAL RS_ClearError(HANDLE hComm, DWORD& dwErrors);
extern "C" BOOL FAR PASCAL RS_GetDefaultConfig(int nPort, COMMCONFIG& config);
extern "C" BOOL FAR PASCAL RS_SetDefaultConfig(int nPort, COMMCONFIG& config);
extern "C" BOOL FAR PASCAL RS_GetStatus(HANDLE hComm, COMSTAT& stat);
extern "C" BOOL FAR PASCAL RS_GetState(HANDLE hComm, DCB& dcb);
extern "C" BOOL FAR PASCAL RS_SetState(HANDLE hComm, DCB& dcb);
extern "C" BOOL FAR PASCAL RS_Escape(HANDLE hComm, DWORD dwFunc);
extern "C" BOOL FAR PASCAL RS_ClearDTR(HANDLE hComm);
extern "C" BOOL FAR PASCAL RS_ClearRTS(HANDLE hComm);
extern "C" BOOL FAR PASCAL RS_SetDTR(HANDLE hComm);
extern "C" BOOL FAR PASCAL RS_SetRTS(HANDLE hComm);
extern "C" BOOL FAR PASCAL RS_SetXOFF(HANDLE hComm);
extern "C" BOOL FAR PASCAL RS_SetXON(HANDLE hComm);
extern "C" BOOL FAR PASCAL RS_GetProperties(HANDLE hComm, COMMPROP& properties);
extern "C" BOOL FAR PASCAL RS_GetModemStatus(HANDLE hComm, DWORD& dwModemStatus);
extern "C" BOOL FAR PASCAL RS_SetMask(HANDLE hComm, DWORD dwMask);
extern "C" BOOL FAR PASCAL RS_GetMask(HANDLE hComm, DWORD& dwMask);
extern "C" BOOL FAR PASCAL RS_Flush(HANDLE hComm);
extern "C" BOOL FAR PASCAL RS_Purge(HANDLE hComm, DWORD dwFlags);
extern "C" BOOL FAR PASCAL RS_TerminateOutstandingWrites(HANDLE hComm);
extern "C" BOOL FAR PASCAL RS_TerminateOutstandingReads(HANDLE hComm);
extern "C" BOOL FAR PASCAL RS_ClearWriteBuffer(HANDLE hComm);
extern "C" BOOL FAR PASCAL RS_ClearReadBuffer(HANDLE hComm);
extern "C" BOOL FAR PASCAL RS_Setup(HANDLE hComm, DWORD dwInQueue, DWORD dwOutQueue);
extern "C" BOOL FAR PASCAL RS_SetTimeouts(HANDLE hComm, COMMTIMEOUTS& timeouts);
extern "C" BOOL FAR PASCAL RS_GetTimeouts(HANDLE hComm, COMMTIMEOUTS& timeouts);
extern "C" BOOL FAR PASCAL RS_Set0Timeout(HANDLE hComm);
extern "C" BOOL FAR PASCAL RS_Set0WriteTimeout(HANDLE hComm);
extern "C" BOOL FAR PASCAL RS_Set0ReadTimeout(HANDLE hComm);
extern "C" BOOL FAR PASCAL RS_WaitEvent(HANDLE hComm, DWORD& dwMask);
extern "C" BOOL FAR PASCAL RS_WaitEventOv(HANDLE hComm, DWORD& dwMask, OVERLAPPED& overlapped);
extern "C" BOOL FAR PASCAL RS_WriteBufferOv(HANDLE hComm, const void* lpBuf, DWORD dwCount, DWORD* pBytesWrite);
extern "C" BOOL FAR PASCAL RS_ReadBufferOv(HANDLE hComm, void* lpBuf, DWORD dwCount, DWORD* pBytesRead);


#endif //__SERIALPORT_H__

⌨️ 快捷键说明

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