Advertisement

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

Writing a parallel port control program with C++ Builder

[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

8.8
Zone:

C++ Builder

Tags:

port, parallel, control

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!
Answered By: gtokas
Expert Since: 09/08/2003
Accepted Solutions: 367
gtokas has been an Expert for 5 years 4 months, during which he has posted 1726 comments and answered 367 questions. gtokas is just one of 149 experts in the C++ Builder Zone. 1 expert collaborated on this answer, which was graded an "A" by the asker.
 
 
 
 
20081119-EE-VQP-48