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

📄 tdlportio.cpp

📁 这里介绍的一款多功能编程器
💻 CPP
📖 第 1 页 / 共 4 页
字号:
      ValueCount++;
   }

   // Close the key
   RegCloseKey(CurKey);

   for (DWORD index=0; index<ValueCount; index++)
   {
      char DosDev[MAX_PATH];    // Key value for \DosDevices\LPT
      DWORD DataType, DataSize; // Type and size of data read from the registry

      // Is it a \DosDevices\LPT key?
      strcpy(KeyName, BASE_KEY);
      if (RegOpenKeyEx(
            HKEY_LOCAL_MACHINE, KeyName, 0, KEY_PERMISSIONS, &CurKey
                        ) == ERROR_SUCCESS)
      {
         DataSize = MAX_PATH;
         RegQueryValueEx(
            CurKey, ValueList[index], NULL, &DataType, DosDev, &DataSize
                         );
         RegCloseKey(CurKey);

         // Make sure it was a string
         if (DataType != REG_SZ)
            strcpy(DosDev, "");
      }
      else
         strcpy(DosDev, "");

      if (strstr(DosDev, DOS_DEVICES) != NULL)
      {
         int PortNumber;                  // The nubmer of the port
         char PortNumberStr[MAX_PATH];    // String version of PortNumber
         char PortIDStr[MAX_PATH];        // PortID

         memset(PortNumberStr, '\0', MAX_PATH);
         strncpy(PortNumberStr,
                 strstr(DosDev, DOS_DEVICES) + strlen(DOS_DEVICES),
                 strlen(DosDev) - (strstr(DosDev, DOS_DEVICES)-DosDev)
                                - strlen(DOS_DEVICES) + 1
                 );

         // Get the Port ID
         memset(PortIDStr, '\0', MAX_PATH);
         strncpy(PortIDStr,
                 strstr(ValueList[index], DEVICE_PARALLEL) + strlen(DEVICE_PARALLEL),
                 strlen(ValueList[index])
                     - (strstr(ValueList[index], DEVICE_PARALLEL)-ValueList[index])
                     - strlen(DEVICE_PARALLEL) + 1
                 );

         // Get the port number
         PortNumber = atoi(PortNumberStr);

         // Get the port address
         RegOpenKeyEx(HKEY_LOCAL_MACHINE, LOADED_KEY, 0, KEY_PERMISSIONS, &CurKey);

         strcpy(DosDev, "\\Device\\ParallelPort");
         strcat(DosDev, PortIDStr);
         strcat(DosDev, ".Raw");

         if (RegQueryValueEx(
               CurKey, DosDev, NULL, &DataType, NULL, NULL
                             ) == ERROR_SUCCESS &&
             DataType == REG_RESOURCE_LIST)
         {
            WORD Allocation[64]; // Binary data with port number inside

            // Read in the binary data
            DataSize = sizeof(Allocation);
            RegQueryValueEx(
               CurKey, DosDev, NULL, NULL,
               (unsigned char*)Allocation, &DataSize
                            );

            // Found a port; add it to the list
            if (DataSize>0 && PortNumber<=MAX_LPT_PORTS)
            {
               FLPTAddress[PortNumber] = Allocation[12];
               FLPTCount++;
            }
         }

         RegCloseKey(CurKey);
      }
   }

   // Destroy our key value list
   // Destroy our key list
   for (DWORD index=0; index<=ValueCount; index++)
      delete[] ValueList[index];
   delete ValueList;
}


//---------------------------------------------------------------------------
// DetectPorts()
//    Detects the number of printer ports and their addresses
//---------------------------------------------------------------------------
void _export __stdcall DetectPorts()
{
   // Detect the printer ports available
   if (FRunningWinNT)
      DetectPortsNT(); // WinNT version
   else
      DetectPorts9x(); // Win9x version
}


//---------------------------------------------------------------------------
// LPTStrobe()
//    Sends STROBE signal to the printer
//---------------------------------------------------------------------------
void _export __stdcall LPTStrobe(void)
{
   // Set to strobe pin to 0V
   SetPin(STROBE_PIN, false);
   // Wait one millisecond
   Sleep(1);
   // Set strobe pin back to 5V
   SetPin(STROBE_PIN, true);
}


//---------------------------------------------------------------------------
// LPTAutofd()
//    Sends AUTOFD (auto line feed) signal to the printer
//---------------------------------------------------------------------------
void _export __stdcall LPTAutofd(bool Flag)
{
   // Set the auto line feed pin
   SetPin(AUTOFD_PIN, Flag);
}


//---------------------------------------------------------------------------
// LPTInit()
//    Resets printer by sending INIT signal
//---------------------------------------------------------------------------
void _export __stdcall LPTInit(void)
{
   // Set pin to a 0V
   SetPin(INIT_PIN, false);
   // Wait 1 ms
   Sleep(1);
   // Set pin back to 5V
   SetPin(INIT_PIN, true);
}


//---------------------------------------------------------------------------
// LPTSlctIn()
//    Sends SLCTIN signal to the printer
//---------------------------------------------------------------------------
void _export __stdcall LPTSlctIn(void)
{
   // Send the signal (0V)
   SetPin(SELECTIN_PIN, false);
}


//---------------------------------------------------------------------------
// LPTPrintChar()
//    Sends a character to the printer.
//    Returns true on success. Repeat as neccessary.
//---------------------------------------------------------------------------
bool _export __stdcall LPTPrintChar(char Ch)
{
    // Write data to Base+0
    WritePort(FLPTBase, (BYTE)Ch);
    // Write 0Dh to Base+2.
    WritePort(FLPTBase+2, 0x0D);
    // Make sure there's a delay of at least one microsecond
    Sleep(1);
    // Write 0Ch to Base+2.
    WritePort(FLPTBase+2, 0x0C);
    // Input from Base+1 and check if Bit 7 is 1.
    // Return this status as whether the character was printed
    return (ReadPort(FLPTBase+1)&BIT7)!=0;
}


//---------------------------------------------------------------------------
// LPTNumPorts()
//    Shows how many LPT ports are installed on your PC.
//    Always returns 3...
//---------------------------------------------------------------------------
BYTE _export __stdcall LPTNumPorts(void)
{
   return FLPTCount;
}


//---------------------------------------------------------------------------
// GetLPTNumber()
//    Selects the LPT port to use for all LPT operations
//---------------------------------------------------------------------------
BYTE _export __stdcall GetLPTNumber(void)
{
   return FLPTNumber;
}


//---------------------------------------------------------------------------
// SetLPTNumber()
//    Selects the LPT port to use for all LPT operations
//---------------------------------------------------------------------------
void _export __stdcall SetLPTNumber(BYTE Number)
{
   // Note that we don't make sure it is within the range 1..FLPTCount
   // because there _might_ (can someone claify this?) be a port numbered
   // as #2, where it may be the _only_ port installed on the system.
   if (Number>0 && Number<=MAX_LPT_PORTS)
   {
      FLPTNumber=Number;
      FLPTBase=FLPTAddress[Number];
   }
}


//---------------------------------------------------------------------------
// LPTBasePort()
//    Returns a base address of the current LPT port.
//---------------------------------------------------------------------------
WORD _export __stdcall LPTBasePort(void)
{
   return FLPTBase;
}


//---------------------------------------------------------------------------
// GetPin()
//    Index valid is in the range 1-25 only (other values return false)
//    Reading the pin returns true when it is 5V, or false when it at 0V.
//---------------------------------------------------------------------------
bool _export __stdcall GetPin(BYTE Pin)
{
   switch (Pin)
   {
      case 1:  return (ReadPort(FLPTBase+2)&BIT0)==0;  // Inverted
      case 2:  return (ReadPort(FLPTBase)&BIT0)!=0;
      case 3:  return (ReadPort(FLPTBase)&BIT1)!=0;
      case 4:  return (ReadPort(FLPTBase)&BIT2)!=0;
      case 5:  return (ReadPort(FLPTBase)&BIT3)!=0;
      case 6:  return (ReadPort(FLPTBase)&BIT4)!=0;
      case 7:  return (ReadPort(FLPTBase)&BIT5)!=0;
      case 8:  return (ReadPort(FLPTBase)&BIT6)!=0;
      case 9:  return (ReadPort(FLPTBase)&BIT7)!=0;
      case 10: return (ReadPort(FLPTBase+1)&BIT6)!=0;
      case 11: return (ReadPort(FLPTBase+1)&BIT7)==0;  // Inverted
      case 12: return (ReadPort(FLPTBase+1)&BIT5)!=0;
      case 13: return (ReadPort(FLPTBase+1)&BIT4)!=0;
      case 14: return (ReadPort(FLPTBase+2)&BIT1)==0;  // Inverted
      case 15: return (ReadPort(FLPTBase+1)&BIT3)!=0;
      case 16: return (ReadPort(FLPTBase+2)&BIT2)!=0;
      case 17: return (ReadPort(FLPTBase+2)&BIT3)==0;  // Inverted
      default: return false; // pins 18-25 (GND), and other invalid pins
   }
}


//---------------------------------------------------------------------------
// SetPin()
//    Index valid is in the range 1-25 only (other values return false)
//    Writing true sets the pin to 5V, or 0V when false.
//---------------------------------------------------------------------------
void _export __stdcall SetPin(BYTE Pin, bool State)
{
   // Pins marked with "//" are inverted by the
   // hardware, so we'll do the inverting here too.
   if (State)
      switch (Pin)
      {
         case 1:  WritePort(FLPTBase+2, ReadPort(FLPTBase+2)&~BIT0); break; //
         case 2:  WritePort(FLPTBase, ReadPort(FLPTBase)|BIT0);      break;
         case 3:  WritePort(FLPTBase, ReadPort(FLPTBase)|BIT1);      break;
         case 4:  WritePort(FLPTBase, ReadPort(FLPTBase)|BIT2);      break;
         case 5:  WritePort(FLPTBase, ReadPort(FLPTBase)|BIT3);      break;
         case 6:  WritePort(FLPTBase, ReadPort(FLPTBase)|BIT4);      break;
         case 7:  WritePort(FLPTBase, ReadPort(FLPTBase)|BIT5);      break;
         case 8:  WritePort(FLPTBase, ReadPort(FLPTBase)|BIT6);      break;
         case 9:  WritePort(FLPTBase, ReadPort(FLPTBase)|BIT7);      break;
         /*
         case 10: WritePort(FLPTBase+1, ReadPort(FLPTBase+1)|BIT6);  break;
         case 11: WritePort(FLPTBase+1, ReadPort(FLPTBase+1)&~BIT7); break; //
         case 12: WritePort(FLPTBase+1, ReadPort(FLPTBase+1)|BIT5);  break;
         case 13: WritePort(FLPTBase+1, ReadPort(FLPTBase+1)|BIT4);  break;
         */
         case 14: WritePort(FLPTBase+2, ReadPort(FLPTBase+2)&~BIT1); break; //
         /*
         case 15: WritePort(FLPTBase+1, ReadPort(FLPTBase+1)|BIT3);  break;
         */
         case 16: WritePort(FLPTBase+2, ReadPort(FLPTBase+2)|BIT2);  break;
         case 17: WritePort(FLPTBase+2, ReadPort(FLPTBase+2)&~BIT3); break; //
         default: break; // pins 18-25 (GND), and other invalid pins
      }
   else
      switch (Pin)
      {
         case 1:  WritePort(FLPTBase+2, ReadPort(FLPTBase+2)|BIT0);   break; //
         case 2:  WritePort(FLPTBase, ReadPort(FLPTBase)&~BIT0);      break;
         case 3:  WritePort(FLPTBase, ReadPort(FLPTBase)&~BIT1);      break;
         case 4:  WritePort(FLPTBase, ReadPort(FLPTBase)&~BIT2);      break;
         case 5:  WritePort(FLPTBase, ReadPort(FLPTBase)&~BIT3);      break;
         case 6:  WritePort(FLPTBase, ReadPort(FLPTBase)&~BIT4);      break;
         case 7:  WritePort(FLPTBase, ReadPort(FLPTBase)&~BIT5);      break;
         case 8:  WritePort(FLPTBase, ReadPort(FLPTBase)&~BIT6);      break;
         case 9:  WritePort(FLPTBase, ReadPort(FLPTBase)&~BIT7);      break;
         /*
         case 10: WritePort(FLPTBase+1, ReadPort(FLPTBase+1)&~BIT6);  break;
         case 11: WritePort(FLPTBase+1, ReadPort(FLPTBase+1)|BIT7);   break; //
         case 12: WritePort(FLPTBase+1, ReadPort(FLPTBase+1)&~BIT5);  break;
         case 13: WritePort(FLPTBase+1, ReadPort(FLPTBase+1)&~BIT4);  break;
         */
         case 14: WritePort(FLPTBase+2, ReadPort(FLPTBase+2)|BIT1);   break; //
         /*
         case 15: WritePort(FLPTBase+1, ReadPort(FLPTBase+1)&~BIT3);  break;
         */
         case 16: WritePort(FLPTBase+2, ReadPort(FLPTBase+2)&~BIT2);  break;
         case 17: WritePort(FLPTBase+2, ReadPort(FLPTBase+2)|BIT3);   break; //
         default: break; // pins 18-25 (GND), and other invalid pins
      }
}


//---------------------------------------------------------------------------
// LPTAckwl()
//    Returns ACKWL state from the printer
//---------------------------------------------------------------------------
bool _export __stdcall LPTAckwl(void)
{
   return GetPin(ACK_PIN);
}


//---------------------------------------------------------------------------
// LPTBusy()
//    Returns BUSY state from the printer
//---------------------------------------------------------------------------
bool _export __stdcall LPTBusy(void)
{
   return GetPin(BUSY_PIN);
}


//---------------------------------------------------------------------------
// LPTPaperEnd()
//    Returns PAPER END state from the printer
//---------------------------------------------------------------------------
bool _export __stdcall LPTPaperEnd(void)
{
   return GetPin(PAPEREND_PIN);
}


//---------------------------------------------------------------------------
// LPTSlct()
//    Returns SLCT state from the printer
//---------------------------------------------------------------------------
bool _export __stdcall LPTSlct(void)
{
   return GetPin(SELECTOUT_PIN);
}


//---------------------------------------------------------------------------
// LPTError()
//    Returns ERROR state from the printer
//---------------------------------------------------------------------------
bool _export __stdcall LPTError(void)
{
   return GetPin(ERROR_PIN);
}

⌨️ 快捷键说明

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