并口定义及其编程基础之二:并口驱动加载程序段

      在一般的使用并口的软件中都需要在主程序之前加载并验证并口驱动的完整性。比如,在使用并口开发JTAG和IIC等调试烧录软件时,就需要提前加载GIVEIO驱动,普遍的程序段如下。

(1)打开并口

int        validPpt;

BOOL OpenPpt( HWND hwnd )
{
      if( !InstallIoDriver() )     //如果没有安装GIVEIO并口驱动
      {
              MessageBox( hwnd, "无法加载并口驱动程序", "错误", MB_ICONERROR );
              return 0;//Open ppt fail
      }

      validPpt = GetValidPpt();     //获取有效并口端口
      if( !validPpt )
      {
              MessageBox( hwnd, "无法识别并口", "错误", MB_ICONERROR );
              return 0;//Open ppt fail
      }
      SetPptCompMode();            //设置并口工作状态和模式
      return 1;//Open ppt successful
}

(2)是否安装了并口驱动

int InstallIoDriver(void)
{
       OSVERSIONINFO osvi;           //OS版本信息
       osvi.dwOSVersionInfoSize = sizeof( OSVERSIONINFO );
       GetVersionEx( &osvi ); 
       if( osvi.dwPlatformId == VER_PLATFORM_WIN32_NT )      //NT操作系统
       {  
                hParallel = CreateFile( "////.//giveio", GENERIC_READ, 0, NULL,
                         OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );

                if( hParallel == INVALID_HANDLE_VALUE )

                         return 0;
                else

                         return 0x2000;//OS=NT/2000        //打开GIVEIO有效
        }
        else
        { 
                return 0x0098;//OS=WIN98       //WIN98系统不用并口驱动可以直接操作并口
        }
}

(3)获得有效并口(因为很多PC不只一个并口)

#define LPT1      0x378 
#define LPT2      0x278 
#define LPT3      0x3bc 

 

#define WritePptByte( ppt, value )     _outp( (unsigned short)ppt, value )
#define ReadPptByte( ppt )                _inp( (unsigned short)(ppt) )

int GetValidPpt(void)
{
 // search for valid parallel port
        WritePptByte( LPT1, 0x55 );
        if( (int)ReadPptByte( LPT1 ) == 0x55 )   //通过写入和读出来验证

               return LPT1;
 
        WritePptByte( LPT2, 0x55 );
        if( (int)ReadPptByte( LPT2 ) == 0x55 )   

               return LPT2;
 
        WritePptByte( LPT3, 0x55 );
        if( (int)ReadPptByte( LPT3 ) == 0x55 )

               return LPT3;
 
        return 0; 
}

(4)设置并口

#define ECP_ECR      (0x402)  


#define ECR_STANDARD    (0x0)
#define ECR_DISnERRORINT   (0x10)
#define ECR_DISDMA     (0x0)
#define ECR_DISSVCINT    (0x4)

 

void SetPptCompMode(void)
{
    //configure the parallel port at the compatibility mode.
      WritePptByte(validPpt + ECP_ECR, ECR_STANDARD | ECR_DISnERRORINT | ECR_DISDMA | ECR_DISSVCINT);
}

你可能感兴趣的:(编程,工作,File,null,search,parallel)