Does anyone know who to access the parallel port Using C++?
Author |
Message |
Kameboy
|
Posted: Sat Mar 05, 2011 1:14 pm Post subject: Does anyone know who to access the parallel port Using C++? |
|
|
For school i am trying to help access the parallel port using C++. But i am not sure how to . |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Insectoid
|
Posted: Sat Mar 05, 2011 1:34 pm Post subject: RE:Does anyone know who to access the parallel port Using C++? |
|
|
the conio library has a function to write to a port, however this is not cross-platform and you need to know the port ID. |
|
|
|
|
|
bbi5291
|
Posted: Sat Mar 05, 2011 5:02 pm Post subject: Re: Does anyone know who to access the parallel port Using C++? |
|
|
I believe that Windows reserves the lowest 1024 ports (numbered from 0x0000 to 0x03ff), and the first parallel port is almost universally located at 0x0378. By "reserves", I mean that it locks them so that they cannot be accessed from userland. (If this were not the case, malicious applications could easily halt the system by messing with the microcontroller, corrupt the filesystem by talking directly to the hard disk, circumvent system security by changing the CMOS password, and so on.)
If you want to access them, you will have to install a driver (remember, drivers run with elevated privileges) that will accept requests to write to these ports from application programs. (For security reasons, I wouldn't be surprised if ports numbered lower than 0x0100 were still blocked --- these are the important ones.) Otherwise, the functions from conio.h will trigger an exception, which will either be handled silently by the OS, or terminate your program with an error.
There are many such drivers available for free; just Google up "windows port access" or something similar. |
|
|
|
|
|
Kameboy
|
Posted: Sat Mar 05, 2011 8:03 pm Post subject: Re: Does anyone know who to access the parallel port Using C++? |
|
|
Well is is for a Windows 2000 computer manufacturer is Dell. I can check the port number. I did find something online but when i tried it out it didn't work. When i tired linking the lib and the include. |
|
|
|
|
|
OneOffDriveByPoster
|
Posted: Sun Mar 06, 2011 4:44 pm Post subject: Re: Does anyone know who to access the parallel port Using C++? |
|
|
Depending on what you want to do, you can try using fopen() on "LPT1", "PRN", etc.
There also seems to be a DeviceIoControl function in the Win32 API. |
|
|
|
|
|
Kameboy
|
Posted: Mon Mar 07, 2011 10:00 am Post subject: Re: Does anyone know who to access the parallel port Using C++? |
|
|
Could you show me a open source code that would utlizes the COM1 port on a dell computer using intel pentium 3.
I read the conio libaray and read about i think that might work. |
|
|
|
|
|
Homer_simpson
|
Posted: Mon Mar 07, 2011 11:05 pm Post subject: Re: Does anyone know who to access the parallel port Using C++? |
|
|
hey, I'm not sure if this is useful to you.. but I used this to use my laptop serial port to communicate to an AVR chip..
Quote: #include <windows.h>
#include <stdio.h>
#include <string.h>
#include <cstdlib>
#include <iostream>
HANDLE hPort;
BOOL WriteByte(BYTE bybyte)
{
DWORD iBytesWritten=0;
DWORD iBytesToRead = 1;
if(WriteFile(hPort,(LPCVOID)
&bybyte,iBytesToRead,&iBytesWritten,NULL)==0)
return FALSE;
else return TRUE;
}
BOOL WriteString(const void *instring, int length)
{
int index;
BYTE *inbyte = (BYTE *) instring;
for(index = 0; index< length; ++index)
{
if (WriteByte(inbyte[index]) == FALSE)
return FALSE;
}
return TRUE;
}
BOOL ReadByte(BYTE &resp)
{
BOOL bReturn = TRUE;
BYTE rx;
DWORD dwBytesTransferred=0;
if (ReadFile (hPort, &rx, 1, &dwBytesTransferred, 0)> 0)
{
if (dwBytesTransferred == 1)
{
resp=rx;
bReturn = TRUE;
}
else bReturn = FALSE;
}
else bReturn = FALSE;
return bReturn;
}
BOOL ReadString(void *outstring, int *length)
{
BYTE data;
BYTE dataout[4096]={0};
int index = 0;
while(ReadByte(data)== TRUE)
{
dataout[index++] = data;
}
memcpy(outstring, dataout, index);
*length = index;
return TRUE;
}
void ClosePort()
{
CloseHandle(hPort);
return;
}
HANDLE ConfigureSerialPort(LPCSTR lpszPortName)
{
HANDLE hComm = NULL;
DWORD dwError;
DCB PortDCB;
COMMTIMEOUTS CommTimeouts;
// Open the serial port.
hComm = CreateFile (lpszPortName, // Pointer to the name of the port
GENERIC_READ | GENERIC_WRITE,
// Access (read-write) mode
0, // Share mode
NULL, // Pointer to the security attribute
OPEN_EXISTING, // How to open the serial port
0, // Port attributes
NULL); // Handle to port with attribute
// to copy
// Initialize the DCBlength member.
PortDCB.DCBlength = sizeof (DCB);
// Get the default port setting information.
GetCommState (hComm, &PortDCB);
// Change the DCB structure settings.
PortDCB.BaudRate = 115200; // Current baud
PortDCB.fBinary = TRUE; // Binary mode; no EOF check
PortDCB.fParity = FALSE; // Enable parity checking
PortDCB.fOutxCtsFlow = FALSE; // No CTS output flow control
PortDCB.fOutxDsrFlow = FALSE; // No DSR output flow control
PortDCB.fDtrControl = DTR_CONTROL_ENABLE;
// DTR flow control type
PortDCB.fDsrSensitivity = FALSE; // DSR sensitivity
PortDCB.fTXContinueOnXoff = TRUE; // XOFF continues Tx
PortDCB.fOutX = FALSE; // No XON/XOFF out flow control
PortDCB.fInX = FALSE; // No XON/XOFF in flow control
PortDCB.fErrorChar = FALSE; // Disable error replacement
PortDCB.fNull = FALSE; // Disable null stripping
PortDCB.fRtsControl = RTS_CONTROL_ENABLE;
// RTS flow control
PortDCB.fAbortOnError = FALSE; // Do not abort reads/writes on
// error
PortDCB.ByteSize = 8; // Number of bits/byte, 4-8
PortDCB.Parity = NOPARITY; // 0-4=no,odd,even,mark,space
PortDCB.StopBits = ONESTOPBIT; // 0,1,2 = 1, 1.5, 2
// Configure the port according to the specifications of the DCB
// structure.
if (!SetCommState (hComm, &PortDCB))
{
printf("Could not configure serial port\n");
return NULL;
}
// Retrieve the time-out parameters for all read and write operations
// on the port.
GetCommTimeouts (hComm, &CommTimeouts);
// Change the COMMTIMEOUTS structure settings.
CommTimeouts.ReadIntervalTimeout = MAXDWORD;
CommTimeouts.ReadTotalTimeoutMultiplier = 0;
CommTimeouts.ReadTotalTimeoutConstant = 0;
CommTimeouts.WriteTotalTimeoutMultiplier = 0;
CommTimeouts.WriteTotalTimeoutConstant = 0;
if (!SetCommTimeouts (hComm, &CommTimeouts))
{
printf("Could not set timeouts\n");
return NULL;
}
return hComm;
}
int main(void)
{
std::string txt;
BYTE data;
char ch;
int i;
hPort = ConfigureSerialPort("COM3");
if(hPort == NULL)
{
printf("Com port configuration failed\n");
return -1;
}
while(true)
while(ReadByte(data)== TRUE)
{
ch=(char)data;
//printf("byte :%i \n",data,i);
//txt.assign(&ch);
//printf(txt.c_str());
printf("%c",ch);
//printf("\n");
}
// Call your ReadString and WriteString functions here
ClosePort();/**/
system("PAUSE");
return 0;
}
|
|
|
|
|
|
|
|