// ParallelPort.cpp: implementation of the CParallelPort class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"

#include "ParallelPort.h"
#include "conio.h"
#include "winio.h"

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

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CParallelPort::CParallelPort()
{
	m_CtrlByte=0;
	m_DataByte=0;
	m_StatByte=0;
	m_bWin98=FALSE;
}

CParallelPort::~CParallelPort()
{
	if(!m_bWin98)
		ShutdownWinIo();
}

extern	CString m_strAppPath;

BOOL CParallelPort::InitPort(int nPort)
{

	m_nDataPort=nPort;
	m_nStatPort=nPort+1;
	m_nCtrlPort=nPort+2;
	OSVERSIONINFO winInfo;
	winInfo.dwOSVersionInfoSize=sizeof(OSVERSIONINFO);
	GetVersionEx(&winInfo);
	TRACE("winInfo.dwMajorVersion:%d\n",winInfo.dwMajorVersion);
	if(winInfo.dwMajorVersion==4)//Win98或Win95
	{
		
		TRACE("m_bWin98=TRUE\n");
		m_bWin98=TRUE;
		return TRUE;
	}
	else if(winInfo.dwMajorVersion==5)//Win2000或WinXP要先装驱动
	{

		TRACE("m_bWin98=FALSE\n");
		m_bWin98=FALSE;
		m_strDrivePath=m_strAppPath+"winio.sys";
		char FileName[200];
		memset(FileName,0,200);
		strcpy(FileName,LPCTSTR(m_strDrivePath));
		if(InitializeWinIo())
		   return TRUE;
		if(!InstallWinIoDriver(FileName,FALSE))
		{
			AfxMessageBox("初始化驱动程序失败!");
		}
		if(!InitializeWinIo())
		{
			AfxMessageBox("初始化并口I/O失败!");
			return FALSE;
		}
		//TRACE("InstallRes:%d,InitRes:%d\n",InstallRes,InitRes);
		return TRUE;
	}
	else
	{
		return FALSE;
	}
}

BYTE CParallelPort::ReadData()
{
	if(m_bWin98)
		return (BYTE)_inp(m_nDataPort);
	else
	{
		DWORD	Val;
		GetPortVal((WORD)m_nDataPort,&Val,1);
		return (BYTE)Val;
	}
}

CParallelPort::WriteData(BYTE Data)
{
	if(m_bWin98)
		_outp(m_nDataPort,(int)Data);
	else
	{
		SetPortVal((WORD)m_nDataPort,(DWORD)Data,1);
	}
}

BYTE CParallelPort::ReadState()
{
	if(m_bWin98)
		return (BYTE)_inp(m_nStatPort);
	else
	{
		DWORD	Val;
		GetPortVal((WORD)m_nStatPort,&Val,1);
		return (BYTE)Val;
	}
}

BYTE CParallelPort::ReadCtrl()
{
	if(m_bWin98)
		return (BYTE)_inp(m_nCtrlPort);
	else
	{
		DWORD	Val;
		GetPortVal((WORD)m_nCtrlPort,&Val,1);
		return (BYTE)Val;
	}
}

CParallelPort::WriteCtrl(BYTE Ctrl)
{
	if(m_bWin98)
		_outp(m_nCtrlPort,(int)Ctrl);
	else
	{
		SetPortVal((WORD)m_nCtrlPort,(DWORD)Ctrl,1);
	}
}


BOOL CParallelPort::SetPinH(int nPin)
{
	if(nPin==0)
		return FALSE;
	switch(nPin)
	{
	case 1:
		m_CtrlByte&=0xfe;
		WriteCtrl(m_CtrlByte);
		return TRUE;
	case 2:
		m_DataByte|=0x01;
		WriteData(m_DataByte);
		return TRUE;
	case 3:
		m_DataByte|=0x02;
		WriteData(m_DataByte);
		return TRUE;
	case 4:
		m_DataByte|=0x04;
		WriteData(m_DataByte);
		return TRUE;
	case 5:
		m_DataByte|=0x08;
		WriteData(m_DataByte);
		return TRUE;
	case 6:
		m_DataByte|=0x10;
		WriteData(m_DataByte);
		return TRUE;
	case 7:
		m_DataByte|=0x20;
		WriteData(m_DataByte);
		return TRUE;
	case 8:
		m_DataByte|=0x40;
		WriteData(m_DataByte);
		return TRUE;
	case 9:
		m_DataByte|=0x80;
		WriteData(m_DataByte);
		return TRUE;
	case 14:
		m_CtrlByte&=0xfd;
		WriteCtrl(m_CtrlByte);
		return TRUE;
	case 16:
		m_CtrlByte|=0x04;
		WriteCtrl(m_CtrlByte);
		return TRUE;
	case 17:
		m_CtrlByte&=0xf7;
		WriteCtrl(m_CtrlByte);
		return TRUE;
	default:
		return FALSE;
	}
	return FALSE;
}

BOOL CParallelPort::SetPinL(int nPin)
{
	if(nPin==0)
		return FALSE;
	switch(nPin)
	{
	case 1:
		m_CtrlByte|=0x01;
		WriteCtrl(m_CtrlByte);
		return TRUE;
	case 2:
		m_DataByte&=0xfe;
		WriteData(m_DataByte);
		return TRUE;
	case 3:
		m_DataByte&=0xfd;
		WriteData(m_DataByte);
		return TRUE;
	case 4:
		m_DataByte&=0xfb;
		WriteData(m_DataByte);
		return TRUE;
	case 5:
		m_DataByte&=0xf7;
		WriteData(m_DataByte);
		return TRUE;
	case 6:
		m_DataByte&=0xef;
		WriteData(m_DataByte);
		return TRUE;
	case 7:
		m_DataByte&=0xdf;
		WriteData(m_DataByte);
		return TRUE;
	case 8:
		m_DataByte&=0xbf;
		WriteData(m_DataByte);
		return TRUE;
	case 9:
		m_DataByte&=0x7f;
		WriteData(m_DataByte);
		return TRUE;
	case 14:
		m_CtrlByte|=0x02;
		WriteCtrl(m_CtrlByte);
		return TRUE;
	case 16:
		m_CtrlByte&=0xfb;
		WriteCtrl(m_CtrlByte);
		return TRUE;
	case 17:
		m_CtrlByte|=0x08;
		WriteCtrl(m_CtrlByte);
		return TRUE;
	default:
		return FALSE;
	}
	return FALSE;

}

BOOL CParallelPort::SetPinLogic(int nPin,BOOL bLogic)
{
if(bLogic)
{
	if(nPin==0)
		return FALSE;
	switch(nPin)
	{
	case 1:
		m_CtrlByte&=0xfe;
		WriteCtrl(m_CtrlByte);
		return TRUE;
	case 2:
		m_DataByte|=0x01;
		WriteData(m_DataByte);
		return TRUE;
	case 3:
		m_DataByte|=0x02;
		WriteData(m_DataByte);
		return TRUE;
	case 4:
		m_DataByte|=0x04;
		WriteData(m_DataByte);
		return TRUE;
	case 5:
		m_DataByte|=0x08;
		WriteData(m_DataByte);
		return TRUE;
	case 6:
		m_DataByte|=0x10;
		WriteData(m_DataByte);
		return TRUE;
	case 7:
		m_DataByte|=0x20;
		WriteData(m_DataByte);
		return TRUE;
	case 8:
		m_DataByte|=0x40;
		WriteData(m_DataByte);
		return TRUE;
	case 9:
		m_DataByte|=0x80;
		WriteData(m_DataByte);
		return TRUE;
	case 14:
		m_CtrlByte&=0xfd;
		WriteCtrl(m_CtrlByte);
		return TRUE;
	case 16:
		m_CtrlByte|=0x04;
		WriteCtrl(m_CtrlByte);
		return TRUE;
	case 17:
		m_CtrlByte&=0xf7;
		WriteCtrl(m_CtrlByte);
		return TRUE;
	default:
		return FALSE;
	}
}
else
{
	if(nPin==0)
		return FALSE;
	switch(nPin)
	{
	case 1:
		m_CtrlByte|=0x01;
		WriteCtrl(m_CtrlByte);
		return TRUE;
	case 2:
		m_DataByte&=0xfe;
		WriteData(m_DataByte);
		return TRUE;
	case 3:
		m_DataByte&=0xfd;
		WriteData(m_DataByte);
		return TRUE;
	case 4:
		m_DataByte&=0xfb;
		WriteData(m_DataByte);
		return TRUE;
	case 5:
		m_DataByte&=0xf7;
		WriteData(m_DataByte);
		return TRUE;
	case 6:
		m_DataByte&=0xef;
		WriteData(m_DataByte);
		return TRUE;
	case 7:
		m_DataByte&=0xdf;
		WriteData(m_DataByte);
		return TRUE;
	case 8:
		m_DataByte&=0xbf;
		WriteData(m_DataByte);
		return TRUE;
	case 9:
		m_DataByte&=0x7f;
		WriteData(m_DataByte);
		return TRUE;
	case 14:
		m_CtrlByte|=0x02;
		WriteCtrl(m_CtrlByte);
		return TRUE;
	case 16:
		m_CtrlByte&=0xfb;
		WriteCtrl(m_CtrlByte);
		return TRUE;
	case 17:
		m_CtrlByte|=0x08;
		WriteCtrl(m_CtrlByte);
		return TRUE;
	default:
		return FALSE;
	}
}
}


BOOL CParallelPort::GetPinLogic(int nPin)
{
	switch(nPin)
	{
	case 1:
		m_CtrlByte=ReadCtrl();
		return !(m_CtrlByte & 0x01);
	case 2:
		m_DataByte=ReadData();
		return (m_DataByte & 0x01);
	case 3:
		m_DataByte=ReadData();
		return (m_DataByte & 0x02);
	case 4:
		m_DataByte=ReadData();
		return (m_DataByte & 0x04);
	case 5:
		m_DataByte=ReadData();
		return (m_DataByte & 0x08);
	case 6:
		m_DataByte=ReadData();
		return (m_DataByte & 0x10);
	case 7:
		m_DataByte=ReadData();
		return (m_DataByte & 0x20);
	case 8:
		m_DataByte=ReadData();
		return (m_DataByte & 0x40);
	case 9:
		m_DataByte=ReadData();
		return (m_DataByte & 0x80);
	case 10:
		m_StatByte=ReadState();
		return (m_StatByte & 0x40);
	case 11:
		m_StatByte=ReadState();
		return !(m_StatByte & 0x80);
	case 12:
		m_StatByte=ReadState();
		return (m_StatByte & 0x20);
	case 13:
		m_StatByte=ReadState();
		return (m_StatByte & 0x10);
	case 14:
		m_CtrlByte=ReadCtrl();
		return !(m_CtrlByte & 0x02);
	case 15:
		m_StatByte=ReadState();
		return (m_StatByte & 0x08);
	case 16:
		m_CtrlByte=ReadCtrl();
		return (m_CtrlByte & 0x04);
	case 17:
		m_CtrlByte=ReadCtrl();
		return !(m_CtrlByte & 0x08);
	default:
		return FALSE;
	}
	return FALSE;

}

