Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Windows API for LPTn port

Status
Not open for further replies.

andre_luis

Super Moderator
Staff member
Joined
Nov 7, 2006
Messages
9,591
Helped
1,190
Reputation
2,399
Reaction score
1,206
Trophy points
1,403
Location
Brazil
Activity points
55,665
file_device_parallel_port

Hi All,

I nedd to access paralel port using windows API, like is usually done with serial port ( CreateFile, FileOpen, etc... ).

Cold someone give me some piece of example program ? It could be prefereably at Delphi, Borland C++.

Thanks in advance.


+++
 

mcs7705 delphi

I found this link :

http://msdn2.microsoft.com/en-us/library/aa363198(VS.85).aspx

That is provided by Microsoft, but it doesn´t have enough informations about how to use this functions at LPT port.

Could please someone help me at this query ?

Regards


+++
 

ioctl_par_port_read

Hi. Look here:
\**broken link removed**
\**broken link removed**

Best Regards.
 

deviceiocontrol mcs7705 delphi

Hi Peter,

I thank you for answer, but my problem is not allow the port, but handle it.

Let me explain better : When I attach a USB/LPT converter at Laptop, a device LPT3 ( or LPT4, depending of USB slot used ) appears at Hardware manager. If I use a Legacy port, LPT1 or LPT2, the base address is well known - 0x278 or 0x378 - but it is a direct I/O access; and I would not like do this way. The best way - I suppose - is to use Windows API functions, due the source code may be independent of Windows version used.

Regards.

Andre.
 

how to moschip into spp mode

Just to share the information :

I received answer from MOSCHIP manufacturer.
I didn´t had time to test, but seems promising.

Follow :


Byte.cpp
Code:
#include<windows.h>
#include<stdio.h>
#include<conio.h>
#include<winioctl.h>
#include "byte.h"
#include "winerror.h"

////////////////////////////////////////////////////////////////////////////////////////////
//								P R O T O T Y P E S
////////////////////////////////////////////////////////////////////////////////////////////
unsigned char WriteByte( HANDLE hDevice, unsigned char pRegIndex, unsigned char pRegValue );

unsigned char ReadByte( HANDLE hDevice, unsigned char pRegIndex );

////////////////////////////////////////////////////////////////////////////////////////////
//									  M A I N 
////////////////////////////////////////////////////////////////////////////////////////////
void main()
{
	HANDLE h;
	char portname[10];
	int port = 1;
	unsigned char *Buffer = NULL;
	unsigned char Value;
	FILE *byteLog = NULL;

	// Get the LPT Port No. from user and open the port..
	fflush(stdin);
	printf("Enter LPT number to open (1/2/3) : ");
	scanf("%d",&port);
	sprintf(portname,"LPT%d",port);
	
	
	// Method for opening the Driver Handle
	// Portname is available from the Device Manager and this cannot be hardcoded.
		
	h=CreateFile(portname,GENERIC_READ|GENERIC_WRITE,
							FILE_SHARE_READ,NULL,
							OPEN_EXISTING,
							FILE_ATTRIBUTE_NORMAL,NULL);
	if(h==INVALID_HANDLE_VALUE)
	{
		printf("\nUNABLE TO OPEN HANDLE TO LPT port. Errorcode : %d \n", GetLastError());
		return;
	}

	// Switch the port mode to BYTE
	// Procedure to enable SPP mode 

	Value = ReadByte(h, PP_ECR);			// Read the Value of the PP_ECR Register
	printf("Before setting BYTE Mode..\n");	 
	printf("ECR Register : %x\n", Value);
	Value &= 0x1F;							// Write the 000x xxxx in the ECR Register
	WriteByte( h, PP_ECR, Value );			// Write the Value to PP_ECR To Enables the SPP Mode
	Value = ReadByte( h, PP_ECR );
	printf("After setting BYTE Mode..\n");
	printf("ECR Register : %x\n", Value);


	// IOCTL codes for accessing data port, status port, and control port

	// Reading the status register
	// Status Register is READ ONLY
	Value = ReadByte(h, PP_DSR);
	printf("DSR Register : %x\n", Value);

	// Reading the Control Register
	Value = ReadByte(h, PP_DCR);
	printf("DCR Register : %x\n", Value);

	// Reading from the Data Register
	Value = ReadByte(h, PP_DATA);
	printf("Data Register : %x\n", Value);
	
	// Writing to the Data Register
	Value=0x56;		// Value to be written to the Data Register
	Value = WriteByte(h, PP_DATA,Value);

	// Reading from the Data Register
	Value = ReadByte(h, PP_DATA);
	printf("Data Register : %x\n", Value);

	if( Buffer != NULL )
		delete []Buffer, Buffer = NULL;
	CloseHandle( h );
}


///////////////////////////////////////////////////////////////////////////////////////////
// Function			:	WriteByte
// Module			:	Byte.exe
// Inputs			:	Handle to the device, register offset and value to be written.
// Returns			: 	1 if successful else 0.
// Notes			:	Writes the given value into the given register offset index. 
///////////////////////////////////////////////////////////////////////////////////////////
static UCHAR WriteByte(HANDLE hDevice, UCHAR pRegIndex, UCHAR pRegValue)
{
	ULONG BytesWritten = 0;
	PORT_STRUCT Port;

	Port.m_nRegIndex = pRegIndex;
	Port.m_nRegValue = pRegValue;

	if(!DeviceIoControl(hDevice, 
						IOCTL_PAR_PORT_WRITE,
						&Port,
						sizeof(Port),
						NULL,
						NULL,
						&BytesWritten,
						NULL ) ) {
		printf( "WriteByte Failed with error code : %d\n",GetLastError() );
		CloseHandle( hDevice );
		exit(1);
	}
	return (UCHAR)BytesWritten;
}


///////////////////////////////////////////////////////////////////////////////////////////
// Function			:	ReadByte
// Module			:	Byte.exe
// Inputs			:	Handle to the device, register index to read.
// Returns			: 	Read value.
// Notes			:	Reads the given reg offset index and returns the read value.
///////////////////////////////////////////////////////////////////////////////////////////
static UCHAR ReadByte(HANDLE hDevice, UCHAR pRegIndex)
{
	ULONG BytesRead = 0;
	PORT_STRUCT Port;

	Port.m_nRegIndex = pRegIndex;

	if(!DeviceIoControl(hDevice, 
						IOCTL_PAR_PORT_READ,
						&Port,
						sizeof(Port),
						&Port,
						sizeof(Port),
						&BytesRead,
						NULL ) ) {
		printf( "ReadByte Failed with error code : %d\n",GetLastError() );
		CloseHandle( hDevice );
		exit(1);
	}
	return 	Port.m_nRegValue;
}


Byte.h
Code:
#ifndef __BYTE_H__
#define __BYTE_H__


/*--------------------------------------------------------------------------------*/

// The Registers to access Data, Status and Control.
#define PP_DATA					0x00	// Reg Offset to Access Data
#define PP_DSR					0x01	// Reg Offset to Access Status
#define PP_DCR					0x02	// Reg Offset to Access Control
#define PP_ECR					0x0A	// Reg Offset to Access a Register which puts 
										// the device in SPP Mode(Value in PP_ECR Register
										// should be 000x xxxx in order to set in SPP Mode).

/*--------------------------------------------------------------------------------*/

// IOCTL's To Write into the Regs

#define IOCTL_PAR_PORT_WRITE			CTL_CODE(	FILE_DEVICE_PARALLEL_PORT,	\
													97,							\
													METHOD_BUFFERED,			\
													FILE_ANY_ACCESS				\
												)

// IOCTL's To Read from the Regs
#define IOCTL_PAR_PORT_READ				CTL_CODE(	FILE_DEVICE_PARALLEL_PORT,	\
													98,							\
													METHOD_BUFFERED,			\
													FILE_ANY_ACCESS				\
												)

/*--------------------------------------------------------------------------------*/

// Structure Defns used in the DeviceIoControl
typedef struct PORT_STRUCT_TAG
{
	UCHAR m_nRegIndex;
	UCHAR m_nRegValue;	
} PORT_STRUCT, *PPORT_STRUCT;


#endif //__BYTE_H__

readme.txt
Code:
This Package Contains Byte.h and Byte.cpp Files, which illustrates about how to use the IOCTLs Related to MCS7705.

The Byte.h Contains the,
Register offsets,
IOCTLs and 
Structure Definitins used in the DeviceIoControl.

PP_DATA  	0x00	This is the Data Register,where the Data is Written and Read.
PP_DSR		0x01	This is the Status Register, which is READ ONLY. the status can only be read.
PP_DCR		0x02	This is the Control Register.
PP_ECR		0x0A	
This is the Register which puts 	the Device into SPP Mode.
To make the device work in the SPP Mode, We have to Write the Value '0' in the First Three Bits (starting from MSB) of the ECR Register.
i.e., the Value of the ECR Register should be 000x xxxx to make the device work in SPP Mode.


IOCTL_PAR_PORT_WRITE is the IOCTL used to Write Data to the Device.
IOCTL_PAR_PORT_READ    is the IOCTL used to Read  Data From the Device.

PORT_STRUCT_TAG is the Structure used to send data through the IOCTLs to the Driver.
m_nRegIndex: is the variable which is provided with the Register Index:(PP_DATA/PP_DSR/PP_DCR/PP_ECR)
m_nRegValue : is the Value to be Written to the register.

Following is the Pseudo Code of byte.cpp :

--> ask the Port Number from the User.
(the port number is available in the ports under the Device Manager)
--> open a handle to the Device using CreateFile and portname took from the user.
--> program the SPP mode by writing into the ECR Register.
--> read the Status Register
--> read the Control Register
--> write to the Data Register
--> read the Data Register Back.
--> Deallocate the buffer.
--> Close the handle.

+++
 

for picpgm m geting error as "Error opening LPT driver!"..........
can any 1 plz help me?
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top