ParallelPort.h
上传用户:kathy7909
上传日期:2007-03-27
资源大小:15k
文件大小:6k
源码类别:

并口编程

开发平台:

Visual C++

  1. /*
  2. Module : PARALLEL.H
  3. Purpose: Declaration for an MFC class to encapsulate parallel ports
  4. Created: PJN / 28-12-1999
  5. History: None
  6. Copyright (c) 1999 - 2000 by PJ Naughter.  
  7. All rights reserved.
  8. */
  9. ///////////////////// Macros / Structs etc //////////////////////////
  10. #ifndef __PARALLEL_H__
  11. #define __PARALLEL_H__
  12. #ifndef __AFXTEMPL_H__
  13. #pragma message("To avoid this message, put afxtempl.h in your pre compiled header (normally stdafx.h)")
  14. #include <afxtempl.h> 
  15. #endif
  16. /////////////////////////// Classes ///////////////////////////////////////////
  17. ////// Parallel port exception class ////////////////////////////////////////////
  18. void AfxThrowParallelException(DWORD dwError = 0);
  19. class CParallelException : public CException
  20. {
  21. public:
  22. //Constructors / Destructors
  23. CParallelException(DWORD dwError);
  24. ~CParallelException();
  25. //Methods
  26. #ifdef _DEBUG
  27. virtual void Dump(CDumpContext& dc) const;
  28. #endif
  29. virtual BOOL GetErrorMessage(LPTSTR lpstrError, UINT nMaxError, PUINT pnHelpContext = NULL);
  30. CString GetErrorMessage();
  31. //Data members
  32. DWORD m_dwError;
  33. protected:
  34. DECLARE_DYNAMIC(CParallelException)
  35. };
  36. ////// The class used to hold a parallel ports settings /////////////////////////
  37. class CParallelPortSettings
  38. {
  39. public:
  40. //Typedefs / Defines
  41.   enum ParallelPortType
  42.   {
  43.     ParallelTypeUndefined,
  44.     ParallelTypeSPP,
  45.     ParallelTypePS2,
  46.     ParallelTypeEPP,
  47.     ParallelTypeECP,
  48.   };
  49.   enum ECPPortMode
  50.   {
  51.     ECPModeUndefined,
  52.     ECPModeSPP,
  53.     ECPModePS2,
  54.     ECPModeFastCentronics,
  55.     ECPModeECP,
  56.     ECPModeEPP,
  57.     ECPModeTest,
  58.     ECPModeConfiguration
  59.   };
  60. //Constructors / Destructors
  61.   CParallelPortSettings();
  62.   CParallelPortSettings(const CParallelPortSettings& state);
  63. //Standard methods
  64.   CParallelPortSettings& operator=(const CParallelPortSettings& state);
  65. //Member variables
  66.   unsigned short   m_nBaseAddress; //The base address of the parallel port e.g. 0x278
  67.   ParallelPortType m_Type;         //The port type
  68.   ECPPortMode      m_ECPMode;      //The ECP mode if the type is ECP 
  69. };
  70.                                   
  71. //// The actual parallel port class /////////////////////////////////////////////
  72. class CParallelPort : public CObject
  73. {
  74. public:  
  75. //Constructors / Destructors
  76.   CParallelPort();
  77.   ~CParallelPort();
  78. //Methods
  79.   void                               Open(int nPort=1);
  80.   inline BOOL                        IsOpen() const;
  81.   void                               Close();
  82.   void                               SetECPMode(CParallelPortSettings::ECPPortMode mode);
  83.   CParallelPortSettings::ECPPortMode GetECPMode();
  84.   void                               WriteControl(int nData);
  85.   int                                ReadControl();
  86.   void                               WriteData(int nData);
  87.   int                                ReadData();
  88.   int                                ReadStatus();
  89.   BOOL                               ReadByteUsingNibbleMode(BYTE& byData);
  90.   BOOL                               WriteByteUsingNibbleMode(BYTE byData);
  91.   inline BOOL                        ReadUsingNibbleMode(void* lpBuf, DWORD dwBytes);
  92.   inline BOOL                        WriteUsingNibbleMode(const void* lpBuf, DWORD dwBytes);
  93.   void                               SetTimeout(DWORD dwTimeout) { m_dwTimeout = dwTimeout; };
  94.   DWORD                              GetTimeout() const { return m_dwTimeout; };
  95.   CParallelPortSettings              GetSettings() const;
  96. //Static methods
  97.   static BOOL PortPresent(int nPort);
  98. //MFC Diagnostics
  99. #ifdef _DEBUG
  100.   void Dump(CDumpContext& dc) const;
  101. #endif
  102. protected:
  103. //typedefs
  104.   typedef UCHAR (_stdcall DLPORTREADPORTUCHAR)(ULONG Port);
  105.   typedef DLPORTREADPORTUCHAR* LPDLPORTREADPORTUCHAR;
  106.   typedef VOID (_stdcall DLPORTWRITEPORTUCHAR)(ULONG Port, UCHAR Value);
  107.   typedef DLPORTWRITEPORTUCHAR* LPDLPORTWRITEPORTUCHAR;
  108. //Methods
  109.   static BOOL        InitializeDriverLINX();
  110.   static void        DeInitializeDriverLINX();
  111.   static BOOL        RunningOnNT();
  112.   static BOOL        GetPort(unsigned short nBaseAddress, CParallelPortSettings& settings);
  113.   static BOOL        GetECPPort(unsigned short nBaseAddress);
  114.   static BOOL        GetEPPPort(unsigned short nBaseAddrsss);
  115.   static BOOL        GetPS2Port(unsigned short nBaseAddress);
  116.   static BOOL        GetSPPPort(unsigned short nBaseAddress);
  117.   static int         GetEPPTimeoutBit(unsigned short nBaseAddress);
  118.   static CParallelPortSettings::ECPPortMode ReadECPMode(unsigned short nBaseAddress);
  119.   inline static int  _inp(unsigned short port);
  120.   inline static int  _outp(unsigned short port, int databyte);
  121.   inline static void WriteControl(unsigned short nBaseAddress, int nData);
  122.   inline static int  ReadControl(unsigned short nBaseAddress);
  123.   inline static void WriteData(unsigned short nBaseAddress, int nData);
  124.   inline static int  ReadData(unsigned short nBaseAddress);
  125.   inline static int  ReadStatus(unsigned short nBaseAddress);
  126. //Member variables
  127.   HANDLE         m_hPort;         //SDK handle to lock Win32 access to the port
  128.   int            m_nPortIndex;    //index of the current port into m_Ports
  129.   DWORD          m_dwTimeout;     //Timeout in Milliseconds for reads and writes
  130.   unsigned short m_nBaseAddress;  //The base address of the port
  131. //Static memmber variables
  132.   static BOOL                                                  sm_bRunningOnNT;         //Are we running on NT / Windows 2000
  133.   static int                                                   sm_nRefCount;            //Reference count for the class
  134.   static HINSTANCE                                             sm_hDLINX;               //handle to the DriverLINX dll.
  135.   static LPDLPORTREADPORTUCHAR                                 sm_lpfnDlPortReadUchar;  //Function pointer for writing via DriverLINX
  136.   static LPDLPORTWRITEPORTUCHAR                                sm_lpfnDlPortWriteUchar; //Function pointer for rading via DriverLINX
  137.   static CArray<CParallelPortSettings, CParallelPortSettings&> sm_Ports;                //All the parallel ports available on this machine
  138.   DECLARE_DYNAMIC(CParallelPort)
  139. };
  140. //// A Simple class to transfer and receive files using nibble mode
  141. class CParallelPortFileTransfer
  142. {
  143. public:
  144. //Constructors / Destructors
  145.   CParallelPortFileTransfer(CParallelPort* pPort);
  146.   ~CParallelPortFileTransfer();
  147. //Methods
  148.   void SendFile(const CString& sLocalFile);
  149.   void ReceiveFile(const CString& sLocalFile);
  150. protected:
  151.   CParallelPort* m_pPort;
  152. };
  153. #endif //__PARALLEL_H__