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

📄 parallelport.cpp

📁 并口操作的封装类
💻 CPP
📖 第 1 页 / 共 3 页
字号:
    if (!WriteByteUsingNibbleMode(lpByteBuf[i]))
      return FALSE;
  }
  return TRUE;
}

int CParallelPort::_inp(unsigned short port)
{
  if (sm_bRunningOnNT)
  {
    ASSERT(sm_lpfnDlPortReadUchar);
    return sm_lpfnDlPortReadUchar(port);
  }
  else
    return ::_inp(port);
}

int CParallelPort::_outp(unsigned short port, int databyte)
{
  if (sm_bRunningOnNT)
  {
    ASSERT(sm_lpfnDlPortWriteUchar);
    sm_lpfnDlPortWriteUchar(port, (UCHAR) databyte);
    return databyte;
  }
  else
    return ::_outp(port, databyte);
}

BOOL CParallelPort::InitializeDriverLINX()
{
  //Load up the DriverLINX dll and get the function pointers we are interested in
  ASSERT(sm_hDLINX == NULL);
  BOOL bSuccess = FALSE;
  sm_hDLINX = LoadLibrary(_T("DLPORTIO.DLL"));
  if (sm_hDLINX)
  {
    //Get the function pointers
    sm_lpfnDlPortReadUchar = (LPDLPORTREADPORTUCHAR) GetProcAddress(sm_hDLINX, "DlPortReadPortUchar");
    sm_lpfnDlPortWriteUchar = (LPDLPORTWRITEPORTUCHAR) GetProcAddress(sm_hDLINX, "DlPortWritePortUchar");

    //If any of the functions are not installed then fail the load
    if (sm_lpfnDlPortReadUchar == NULL ||
        sm_lpfnDlPortWriteUchar == NULL)
    {
      TRACE(_T("Failed to get one of the function pointer`s in the DriverLINX dll\n"));
      DeInitializeDriverLINX();
    }
    else
      bSuccess = TRUE;
  }
  else
    TRACE(_T("Could not find the load the DriverLINX dll, please ensure DriverLINX driver is installed\n"));

  return bSuccess;
}

void CParallelPort::DeInitializeDriverLINX()
{
  if (sm_hDLINX)
  {
    //Unload the dll and reset the function pointers to NULL    
    FreeLibrary(sm_hDLINX);
    sm_hDLINX = NULL;
    sm_lpfnDlPortReadUchar = NULL;
    sm_lpfnDlPortWriteUchar = NULL;
  }
}

BOOL CParallelPort::PortPresent(int nPort)
{
  BOOL bSuccess = FALSE;

  //Try to open the port 
  CString sPort;
  sPort.Format(_T("\\\\.\\LPT%d"), nPort);
  HANDLE hPort = ::CreateFile(sPort, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
  if (hPort != INVALID_HANDLE_VALUE)
  {
    bSuccess = TRUE;

    //Close the port, now that we don't need it anymore
    ::CloseHandle(hPort);
  }
  else
  {
    //Determine if the port exists based on the error code.
    DWORD dwError = GetLastError();

    //Check to see if the error was because some other app had the port open or a general failure
    if (dwError == ERROR_ACCESS_DENIED || dwError == ERROR_GEN_FAILURE)
      bSuccess = TRUE;
  }

  return bSuccess;
}

BOOL CParallelPort::RunningOnNT()
{
  OSVERSIONINFO osvi;
  memset(&osvi, 0, sizeof(OSVERSIONINFO));
  osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  VERIFY(GetVersionEx(&osvi));
  return (osvi.dwPlatformId == VER_PLATFORM_WIN32_NT);
}

CParallelPortSettings CParallelPort::GetSettings() const
{
  ASSERT(IsOpen()); //Port must be open 
  return sm_Ports.GetAt(m_nPortIndex);
}



////////// The file transfer class

CParallelPortFileTransfer::CParallelPortFileTransfer(CParallelPort* pPort)
{
  m_pPort = NULL;
  ASSERT(pPort != NULL);
  ASSERT(pPort->IsOpen());

  //Ensure the Port is set to SPP mode if it is an ECP port
  CParallelPortSettings settings = pPort->GetSettings();
  if (settings.m_Type == CParallelPortSettings::ParallelTypeECP)
    pPort->SetECPMode(CParallelPortSettings::ECPModeSPP);

  //Initialize D3 (strobe) to 1
  pPort->WriteData(0x8);

  //Wait for the opposite end to set D3=1 (not busy)
  int S6;
  DWORD dwStartTicks = GetTickCount();
  do
  {
    S6 = (pPort->ReadStatus() & 0x40) >> 6;

    //Check the timeout has not elapsed
    if ((GetTickCount() - dwStartTicks) > pPort->GetTimeout())
    {
      TRACE(_T("Could not setup file transfer, other end busy\n"));
      AfxThrowParallelException(ERROR_TIMEOUT);
    }
  }
  while (S6 == 0);

  //Store away the port pointer for use in other class methods
  m_pPort = pPort;
}

CParallelPortFileTransfer::~CParallelPortFileTransfer()
{
  m_pPort = NULL;
}

void CParallelPortFileTransfer::SendFile(const CString& sLocalFile)
{
  ASSERT(m_pPort); //Initialize failed

	//For correct operation of the T2A macro, see MFC Tech Note 59
	USES_CONVERSION;

  //First try to open the file to send
  CFile file;
  if (file.Open(sLocalFile, CFile::modeRead | CFile::shareDenyWrite))
  {
    //first send the filename (excluding the path) (size if _MAX_PATH)
    TCHAR pszPath[_MAX_PATH];
    TCHAR pszFname[_MAX_FNAME];
    TCHAR pszExt[_MAX_EXT];
    _tsplitpath(sLocalFile, NULL, NULL, pszFname, pszExt);
    _tmakepath(pszPath, NULL, NULL, pszFname, pszExt);

    //Now need to convert the filename to ASCII so that a UNICODE
    //client can take to an ASCII server
    char pszAsciiPath[_MAX_PATH];
    char* pszTemp = T2A(pszPath);
    ZeroMemory(pszAsciiPath, _MAX_PATH);
    strcpy(pszAsciiPath, pszTemp);

    //Do the actual send of the filename
    if (!m_pPort->WriteUsingNibbleMode(pszAsciiPath, _MAX_PATH))
    {
      TRACE(_T("Failed to send the filename\n"));
      AfxThrowParallelException(ERROR_TIMEOUT);
    }

    try
    {
      //Then send the size of the file (as 4 bytes).
      DWORD dwLength = file.GetLength();
      if (!m_pPort->WriteUsingNibbleMode(&dwLength, sizeof(DWORD)))
      {
        TRACE(_T("Failed to send the file length\n"));
        AfxThrowParallelException(ERROR_TIMEOUT);
      }

      //Then the actual file contents ifself (4k at a time)
      BYTE byData[4096];
      int nBytesRead;
      do
      {
        nBytesRead = file.Read(byData, 4096);
        if (!m_pPort->WriteUsingNibbleMode(byData, nBytesRead))
        {
          TRACE(_T("Failed to send the file contents\n"));
          AfxThrowParallelException(ERROR_TIMEOUT);
        }
      }
      while (nBytesRead);
    }
    catch(CFileException* pEx)
    {
      TRACE(_T("An error occurred reading from the file to send\n"));
      delete pEx;
      AfxThrowParallelException();
    }
  }
  else
  {
    TRACE(_T("Could not open the file to send, %s\n"), sLocalFile);
    AfxThrowParallelException();
  }
}

void CParallelPortFileTransfer::ReceiveFile(const CString& sLocalFile)
{
	//For correct operation of the T2A macro, see MFC Tech Note 59
	USES_CONVERSION;

  //First try to open the file to send
  CFile file;
  if (file.Open(sLocalFile, CFile::modeCreate | CFile::modeWrite | CFile::shareDenyWrite))
  {
    //Receive the filename
    char pszAsciiPath[_MAX_PATH];
    if (!m_pPort->ReadUsingNibbleMode(pszAsciiPath, _MAX_PATH))
    {
      TRACE(_T("Failed to receive the filename\n"));

      //Delete the local file
      file.Close();
      DeleteFile(sLocalFile);

      //Throw the exception
      AfxThrowParallelException(ERROR_TIMEOUT);
    }

    try
    {
      //Receive the size of the file (as 4 bytes).
      DWORD dwLength;
      if (!m_pPort->ReadUsingNibbleMode(&dwLength, sizeof(DWORD)))
      {
        TRACE(_T("Failed to receive the file length\n"));
  
        //Delete the local file
        file.Close();
        DeleteFile(sLocalFile);

        //Throw the exception
        AfxThrowParallelException(ERROR_TIMEOUT);
      }

      //Then the actual file contents ifself (4k at a time)
      BYTE byData[4096];
      DWORD dwBytesLeft = dwLength;
      do
      {
        DWORD dwBytesToRead;
        if (dwBytesLeft > 4096)
          dwBytesToRead = 4096;
        else
          dwBytesToRead = dwBytesLeft;
          
        if (!m_pPort->ReadUsingNibbleMode(byData, dwBytesToRead))
        {
          TRACE(_T("Failed to receive the file contents\n"));

          //Delete the local file
          file.Close();
          DeleteFile(sLocalFile);

          //Throw the exception
          AfxThrowParallelException(ERROR_TIMEOUT);
        }

        file.Write(byData, dwBytesToRead);
        dwBytesLeft -= dwBytesToRead;
      }
      while (dwBytesLeft);
    }
    catch(CFileException* pEx)
    {
      TRACE(_T("An error occurred writing the file which was being received\n"));
      delete pEx;
      AfxThrowParallelException();
    }
  }
  else
  {
    TRACE(_T("Could not open the file to receive into, %s\n"), sLocalFile);
    AfxThrowParallelException();
  }
}


//The file transfer class

TansferDataTLV5610::TansferDataTLV5610(CParallelPort* pPort)
{
	m_pPort = NULL;
	ASSERT(pPort != NULL);
	ASSERT(pPort->IsOpen());
	//Store away the port pointer for use in other class methods
	m_pPort = pPort;
}

TansferDataTLV5610::~TansferDataTLV5610()
{
	m_pPort = NULL;
}

DWORD TansferDataTLV5610::StartWorking()
{
    WORD wSendData = 0;
    
}

DWORD TansferDataTLV5610::WriteData(WORD wWriteData)
{
	WORD wSendData = 0;

	try
	{
		wSendData = wSendData | wFsHigh | wSclkLow;  //Start Tag
		m_pPort->WriteData(wSendData);               
		Sleep(dwDelayTm);
        wSendData = wSendData | wFsLow;
        m_pPort->WriteData(wSendData);

		for(int i=0; i<16; i++)     //start Send Data
		{
            wSendData = wSendData | wSclkHigh | (wWriteData & (2 ^ i));  //Send bit
            m_pPort->WriteData(wSendData);  
			Sleep(dwDelayTm);
            wSendData = wSendData | wSclkLow;
            m_pPort->WriteData(wSendData);
		}

		wSendData = wSendData | wFsHigh | wSclkHigh;    //end Tag
        m_pPort->WriteData(wSendData); 
        
		return 0x0000;
	}
	catch(...)
	{
		return 0x0001;
	}
}

DWORD TansferDataTLV5610::SetDelayTm(WORD wDelay)
{
	try
	{
		dwDelayTm = wDelay;
	    return 0x0000;
	}
	catch(...)
	{
		return 0x0001;
	}
}

DWORD TansferDataTLV5610::GetDelayTm(WORD *pwDelayTm)
{
	try
	{
		*pwDelayTm = dwDelayTm;
		return 0x0000;
	}
    catch(...)
	{
		return 0x0001;
	}
	
}


⌨️ 快捷键说明

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