Solved

c++:parallel port

Posted on 2000-04-22
3
371 Views
Last Modified: 2007-10-18
How can i programm the parallel port in c++ Language?
0
Comment
Question by:mavdlc
  • 2
3 Comments
 

Accepted Solution

by:
yuxiliu earned 200 total points
ID: 2740077
the Key is:
DataPort=0x378;
ControlPort=0x37A;
StatuePort=0x379;
your need to check your LPT port number your self in your computer. you can see them in BIOS

a LPT port acctually has 3 ports:
1. "Data port" witch will send data from PC to something else.
2. "SstatuePort" you can use this port for input.(read data from something into PC)
3. "control port".

if you want send a data out do like this:
_outp(Dataport, yourdata);// at this point data does not acctually send out, just stay in LPT buffer
_outp(ControlPort, 1); // tell LPT send out data
_outp(ControlPort, 0); // remember to close it, esle some bad thing will happen when you send next data out

if you want to read data in, just do like this:
_inp(Statuport, yourBuffer); // you need sycronize your program yourself.

note: this only works under win95/98/Dos, does not work under NT

The following is a part of a real project witch talk with a 12K EEPROM via paralel port(LPT1)

// Port.h: interface for the Port class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_PORT_H__A4771279_43BA_11D1_8FCA_F82D06C10000__INCLUDED_)
#define AFX_PORT_H__A4771279_43BA_11D1_8FCA_F82D06C10000__INCLUDED_

#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000

//#include"e:\\lyx\\c++\\resource\\port.h"

//D0:SCK   D1:MSDO  D2:EPE   D3:MSDI  D:4 SW1  D5:SW2    D?:DCLK   D?:PIMON  D?:VDD
//00000001 00000010 00000100 00001000 00010000 00100000
//1        2        4         8        16       32

/*
CONST SW1 = &H10
CONST SW2 = &H20
CONST VBAT = &H80
CONST EPE = &H4
CONST CS = &H4
CONST SCK = &H1
CONST MSDO = &H2
CONST MSDI = &H2
CONST DCLK = &H20
CONST VDD = &H8
 */

#include<conio.h>

#ifndef OFF
#define OFF 0

#define PORT_SCK_IN 8  //in
#define PORT_SCK   1    //out
#define PORT_MSDO_IN 64 //in
#define PORT_MSDO  2    //out
#define PORT_EPE   4    //out
#define PORT_MSDI  16 //in  
#define PORT_SW1   16   //out
#define PORT_SW2   32   //out
#define PORT_DCLK  32   //in
#define ON (bool)1
#endif



class LptPort
{
private:
bool sck_s1; //the state of sck when reading monitor
bool sck_s2;
inline bool GetMsdo();
inline int GetBit();
inline bool GetSck();

void WriteWord(BYTE address,WORD buffer);
void Write16Bit(WORD buffer);
void WriteDisable();
void WriteEnable();
bool ReadBit();
unsigned short Read16Bit();
void SendAddress(BYTE address);
void OperateBit(char c);
void ResetPort();
void StartBit();
void SendBit(bool ONOFF);

int DataPort;
int StatuePort;
int ControlPort;

bool GetMsdi();

BYTE  EPE,SCK,MSDO;
BYTE  Command;

    bool     SetEpe(bool ONOFF);   //Open or Close EPE Set EPEON parament to 4 or 0
bool     SetSck(bool ONOFF);
bool     SetMsdo(bool ONOFF);
bool     SckON_OFF();
bool     SckOFF_ON();

public:
UINT GetMonitor(BYTE buffer[64]);//DCLK has already been low!
void WriteOneWord(BYTE address,WORD buffer);
unsigned short ReadWord(BYTE address);
inline bool GetDCLK();
LptPort();
~LptPort();

BYTE    SendCommand(int User_Command);  //Send a 8 bit Command to PLT Data Port(DataPort)
BYTE    SendCommand();


protected:

};


#endif // !defined(AFX_PORT_H__A4771279_43BA_11D1_8FCA_F82D06C10000__INCLUDED_)
// Port.cpp: implementation of the Port class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"

#include "Port.h"

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

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

LptPort::LptPort()
{
DataPort=0x378;
ControlPort=0x37A;
StatuePort=0x379;
MSDO=0;
SCK=0;
EPE=0;
Command=0;
ResetPort();
};

LptPort::~LptPort()
{
};

bool   LptPort::SckON_OFF()
{
if(SCK)
{
SetSck(OFF);
SendCommand();
return true;
}
else
return false;
}

bool  LptPort::SckOFF_ON()
{
if(!SCK)
{
SetSck(ON);
SendCommand();
return true;
}
else
return false;
}




BYTE LptPort::SendCommand()
{
Command=EPE+SCK+MSDO;
_outp(DataPort,Command);
_outp(ControlPort,1);
_outp(ControlPort,0);
return Command;

}

BYTE LptPort::SendCommand(int  User_Command)
{

_outp(DataPort,(BYTE)User_Command);
_outp(ControlPort,1);
_outp(ControlPort,0);
return (BYTE)Command;

}

bool LptPort::SetEpe(bool ONOFF)  //Set EPE Open;
{
if(ONOFF)
{
EPE=PORT_EPE;
return true;
}
else
{
EPE=0;
return false;
}

}

bool LptPort::SetSck(bool ONOFF)
{
if(ONOFF)
{
SCK=PORT_SCK;
return true;
}
else
{
SCK=0;
return false;
}
}


bool LptPort::SetMsdo(bool ONOFF)
{
if(!ONOFF)
{
MSDO=PORT_MSDO;
return false;
}
else
{
MSDO=0;
return true;
}
}

bool LptPort::GetMsdi()
{
BYTE Value=0;
BYTE Buffer=0;
Value=_inp(StatuePort);
Buffer=Value&PORT_MSDI;
if((bool)Buffer)
return true;
else
return false;
}

void LptPort::ResetPort()
{
SetEpe(OFF);
SetMsdo(ON);
SetSck(OFF);
SendCommand();
}

void LptPort::StartBit()
{
SetMsdo(OFF);
SetSck(OFF);
SetEpe(ON);
SendCommand();
SetMsdo(ON);
SendCommand();
SckOFF_ON();
    //SetMsdo(ON);  Delay?
SckON_OFF();
}

void LptPort::OperateBit(char c)
{
switch(c)
{
case 'R':
SendBit(1);
SendBit(0);
break;
case 'D':
SendBit(0);
SendBit(0);
break;
case 'E':
SendBit(0);
SendBit(0);
break;
case 'W':
SendBit(0);
SendBit(1);
}
}

void LptPort::SendBit(bool ONOFF)
{
SckON_OFF();
SetMsdo(ONOFF);
SckOFF_ON();
SckON_OFF();
}

void LptPort::SendAddress(BYTE address)
{
BYTE buffer=0;
for(int i=0;i<8;i++)
{
buffer=address&(BYTE)0x1;
SendBit((bool)buffer);
address=address>>1;
};
SetMsdo(false);
}


WORD LptPort::Read16Bit()
{
 WORD buffer=0;
 int temp=0;
for(int i=0;i<16;i++)
{
buffer=buffer>>1;
if(ReadBit())
buffer=buffer|0x8000;

};
return buffer;
}

bool LptPort::ReadBit()  //read eeprom
{
SckOFF_ON();
SckON_OFF();
return GetMsdi();
}

WORD LptPort::ReadWord(BYTE address)
{
WORD buffer;
ResetPort();
StartBit();
OperateBit('R');
SendAddress(address);
buffer=Read16Bit();
ResetPort();
return buffer;
}

void LptPort::WriteEnable()
{
ResetPort();
StartBit();
OperateBit('E');
SendAddress(3);
ResetPort();
}

void LptPort::WriteDisable()
{
ResetPort();
StartBit();
OperateBit('D');
SendAddress(0);
ResetPort();
}

void LptPort::Write16Bit(WORD buffer)
{
for(int i=0;i<16;i++)
{
if(buffer&0x1)
SendBit(1);
else
SendBit(0);
buffer=buffer>>1;

};

}

void LptPort::WriteWord(BYTE address, WORD buffer)
{
ResetPort();
StartBit();
OperateBit('W');
SendAddress(address);
    Write16Bit(buffer);
ResetPort();
}

void LptPort::WriteOneWord(BYTE address, WORD buffer)
{
WriteEnable();
WriteWord(address,buffer);
WriteDisable();
}
//Sleep 1-2ms !after you written data to eeprom.

UINT LptPort::GetMonitor(BYTE buffer[ 64 ])  //return value: bytes have been catched
{
int num=0;
int temp;
int counter=0;
while((!GetDCLK())&&(num<=512))
{
temp=GetBit();
if(temp==-1)
return num;
else
{
if(temp==1)
{

buffer[num]=buffer[num]<<1;
buffer[num]=buffer[num]+1;
if(counter<8)
counter+=1;
else
{
num=num+1;
counter=0;
}
}
else
{
buffer[num]=buffer[num]<<1;
num=num+1;
if(counter<8)
counter+=1;
else
{
num=num+1;
counter=0;
}
}
}
}
return num;

}

inline bool LptPort::GetDCLK()
{
return (bool)(_inp(StatuePort)&PORT_DCLK);
}

inline bool LptPort::GetSck()
{
return (bool)(_inp(StatuePort)&PORT_SCK_IN);
}

inline int LptPort::GetBit()  //read monitor
{
//long tick1,tick2;
//tick1=GetTickCount();
sck_s1=GetSck();
sck_s2=GetSck();
//++++++++++++++++++TEST!+++++++++++++++++//
SendCommand(16);
SendCommand(0);
//+++++++++++++++++++TEST!++++++++++++++++//
//if(GetDCLK())return -1;
while(!((sck_s1==0)&&(sck_s2==1)))
{
//tick2=GetTickCount();
//if((tick2-tick1)>=1000)return -1;
sck_s1=sck_s2;
sck_s2=GetSck();
}
return (int)GetMsdo();
}

inline bool LptPort::GetMsdo()
{
return (bool)(_inp(StatuePort)&PORT_MSDO_IN);
}



0
 

Author Comment

by:mavdlc
ID: 2749073
Adjusted points from 50 to 200
0
 

Author Comment

by:mavdlc
ID: 2749074
thank you very much for your answerd, i really apreciate your information.

and i wish you the good.
thanks a lot

Atte.
Miguel Angel Venegas
if you need help you just let me know!
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now