Solved

port using Visual C++

Posted on 2000-04-11
3
352 Views
Last Modified: 2013-11-15
please teach me to creat a source code programe
that i can get input from the paralel port and how the data that i have at the paralel port can i show using the visual c ++
please answer with the source code and teach from step by step
thank
0
Comment
Question by:hamdanik
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 4

Expert Comment

by:Oliver_Dornauf
ID: 2707307
Wich Version of VC++, wich OS ? Easy for DOS but difficult for NT.
0
 

Author Comment

by:hamdanik
ID: 2709343
so how about using just windows 9x ?
0
 

Accepted Solution

by:
yuxiliu earned 100 total points
ID: 2718502
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);
}



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
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today, still in the boom of Apple, PC's and products, nearly 50% of the computer users use Windows as graphical operating systems. If you are among those users who love windows, but are grappling to keep the system's hard drive optimized, then you s…
Ever wonder what it's like to get hit by ransomware? "Tom" gives you all the dirty details first-hand – and conveys the hard lessons his company learned in the aftermath.
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.
XMind Plus helps organize all details/aspects of any project from large to small in an orderly and concise manner. If you are working on a complex project, use this micro tutorial to show you how to make a basic flow chart. The software is free when…

623 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