Advertisement

01.10.2006 at 01:45AM PST, ID: 21690541
[x]
Attachment Details

Writing a parallel port control program with C++ Builder

Asked by preddibear in C++ Builder

Tags: , ,

Hello,
I have rencently read the article "Programming Parallel Port in Win2k an XP Problem" and that's the same question I am facing right now.
Can anybody please tell me what to do to build a program that controls my device via parallel cable? I was just trying to write a small program and to see if my board reacts to my program. I have tried to use "UserPort" but error occurs when running inportb (at __asm in al,dx).
My sample code is listed below, please tell me if there is anything I missed:

//---------------------------------------------------------------------------
#include <windows.h>
#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
static BOOL bPrivException = FALSE;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void outport(UINT portid, UINT value)
{
  __asm mov edx,portid;
  __asm mov eax,value;
  __asm out dx,ax;
}
void outportb(UINT portid, BYTE value)
{
  __asm mov edx,portid
  __asm mov al,value
  __asm out dx,al
}

BYTE inportb(UINT portid)
{
  unsigned char value;
 
  __asm mov edx,portid
  __asm in al,dx
  __asm mov value,al
  return value;
}

UINT inport(UINT portid)
{
  int value=0;
  __asm mov edx,portid
  __asm in ax,dx
  __asm mov value,eax
  return value;
}
LONG WINAPI HandlerExceptionFilter ( EXCEPTION_POINTERS *pExPtrs )
{

      if (pExPtrs->ExceptionRecord->ExceptionCode == EXCEPTION_PRIV_INSTRUCTION)
      {
            pExPtrs->ContextRecord->Eip ++; // Skip the OUT or IN instruction that caused the exception
            bPrivException = TRUE;
            return EXCEPTION_CONTINUE_EXECUTION;
      }
      else
            return EXCEPTION_CONTINUE_SEARCH;
}
BOOL StartUpIoPorts(UINT PortToAccess)
{
    HANDLE hUserPort;
    hUserPort = CreateFile("\\\\.\\UserPort", GENERIC_READ, 0, NULL,OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
    CloseHandle(hUserPort); // Activate the driver
    Sleep(100); // We must make a process switch
    SetUnhandledExceptionFilter(HandlerExceptionFilter);
    bPrivException = FALSE;
    inportb(PortToAccess);  // Try to access the given port address
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    StartUpIoPorts(0x378);
}
//---------------------------------------------------------------------------

Thanks!Start Free Trial
 
Loading Advertisement...
 
[+][-]01.10.2006 at 03:07AM PST, ID: 15658701

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: C++ Builder
Tags: port, parallel, control
Sign Up Now!
Solution Provided By: gtokas
Participating Experts: 1
Solution Grade: A
 
 
[+][-]01.10.2006 at 03:15AM PST, ID: 15658741

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]01.10.2006 at 03:40AM PST, ID: 15658872

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]01.11.2006 at 11:04AM PST, ID: 15673916

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32