Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

access the LPT Port with CreateFile under Win2000

HoschiHoschi Member Posts: 1
Hi folks,
I try to write to the LPT Port under win2000.
There for I use the CreateFile and WriteFile API-Functions.
I can open the Port but when I want to write to it, the programm is hanging. The amazing thing is .. when I use the COM1 port instead of LPT1 with the same Parameters, everything is working fine.

Is it possible that win2000 doesn't support LPT communication anymore???

Here's my little test-programm

//----------------------------------------------------------
#include "stdafx.h"
#include
#include
#include

HANDLE hndDevice;

int initPort(void)
{
hndDevice = CreateFile( "LPT1", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_WRITE_THROUGH , NULL );

if (hndDevice == INVALID_HANDLE_VALUE)
{
printf("Port couldn't be opend: %li
",GetLastError() );
return (-1);
}

printf("Port has been opend...
");
return(1);
}

/**********************************************************************
* Write-Function
* Here's the Problem The Programm is hanging when trying to write
*******************************************************************/
int writePort(BYTE send_data)
{
unsigned int data; // daten to send
unsigned long bytesWritten; // number of bytes written
BOOL status;
data = send_data;

//char xData[3]; //testdaten
//xData[0]=1; //testdaten
//xData[1]=2; //testdaten
//xData[2]=3; //testdaten

status = WriteFile( hndDevice, &data, sizeof(data), &bytesWritten, NULL );

printf("Status : %d
", status);
printf("Number of Bytes written: %d
", bytesWritten);

if (status == 0)
{
printf("error while writing:%li
",GetLastError() );
}

return(1);
}

int closePort(void)
{
CloseHandle(hndDevice);
printf("Port has been closed...");
return(1);
}

int main(int argc, char* argv[])
{



initPort();
writePort(24);
closePort();
return 0;
}




Comments

  • brycebryce Member Posts: 25
    status = WriteFile( hndDevice, &data, sizeof(data), &bytesWritten, NULL );
    I am not familiar with this function. Why is 0 there? My understanding
    NULL really is the same as assigning 0.

    Please let me know.

    Thanks
    Bryce

    : Hi folks,
    : I try to write to the LPT Port under win2000.
    : There for I use the CreateFile and WriteFile API-Functions.
    : I can open the Port but when I want to write to it, the programm is hanging. The amazing thing is .. when I use the COM1 port instead of LPT1 with the same Parameters, everything is working fine.
    :
    : Is it possible that win2000 doesn't support LPT communication anymore???
    :
    : Here's my little test-programm
    :
    : //----------------------------------------------------------
    : #include "stdafx.h"
    : #include
    : #include
    : #include
    :
    : HANDLE hndDevice;
    :
    : int initPort(void)
    : {
    : hndDevice = CreateFile( "LPT1", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_WRITE_THROUGH , NULL );
    :
    : if (hndDevice == INVALID_HANDLE_VALUE)
    : {
    : printf("Port couldn't be opend: %li
    ",GetLastError() );
    : return (-1);
    : }
    :
    : printf("Port has been opend...
    ");
    : return(1);
    : }
    :
    : /**********************************************************************
    : * Write-Function
    : * Here's the Problem The Programm is hanging when trying to write
    : *******************************************************************/
    : int writePort(BYTE send_data)
    : {
    : unsigned int data; // daten to send
    : unsigned long bytesWritten; // number of bytes written
    : BOOL status;
    : data = send_data;
    :
    : //char xData[3]; //testdaten
    : //xData[0]=1; //testdaten
    : //xData[1]=2; //testdaten
    : //xData[2]=3; //testdaten
    :
    : status = WriteFile( hndDevice, &data, sizeof(data), &bytesWritten, NULL );
    :
    : printf("Status : %d
    ", status);
    : printf("Number of Bytes written: %d
    ", bytesWritten);
    :
    : if (status == 0)
    : {
    : printf("error while writing:%li
    ",GetLastError() );
    : }
    :
    : return(1);
    : }
    :
    : int closePort(void)
    : {
    : CloseHandle(hndDevice);
    : printf("Port has been closed...");
    : return(1);
    : }
    :
    : int main(int argc, char* argv[])
    : {
    :
    :
    :
    : initPort();
    : writePort(24);
    : closePort();
    : return 0;
    : }
    :
    :
    :
    :
    :

  • brycebryce Member Posts: 25
    status = WriteFile( hndDevice, &data, sizeof(data), &bytesWritten, NULL );
    I am not familiar with this function. Why is 0 there? My understanding
    NULL really is the same as assigning 0.

    Please let me know.

    Thanks
    Bryce

    : Hi folks,
    : I try to write to the LPT Port under win2000.
    : There for I use the CreateFile and WriteFile API-Functions.
    : I can open the Port but when I want to write to it, the programm is hanging. The amazing thing is .. when I use the COM1 port instead of LPT1 with the same Parameters, everything is working fine.
    :
    : Is it possible that win2000 doesn't support LPT communication anymore???
    :
    : Here's my little test-programm
    :
    : //----------------------------------------------------------
    : #include "stdafx.h"
    : #include
    : #include
    : #include
    :
    : HANDLE hndDevice;
    :
    : int initPort(void)
    : {
    : hndDevice = CreateFile( "LPT1", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_WRITE_THROUGH , NULL );
    :
    : if (hndDevice == INVALID_HANDLE_VALUE)
    : {
    : printf("Port couldn't be opend: %li
    ",GetLastError() );
    : return (-1);
    : }
    :
    : printf("Port has been opend...
    ");
    : return(1);
    : }
    :
    : /**********************************************************************
    : * Write-Function
    : * Here's the Problem The Programm is hanging when trying to write
    : *******************************************************************/
    : int writePort(BYTE send_data)
    : {
    : unsigned int data; // daten to send
    : unsigned long bytesWritten; // number of bytes written
    : BOOL status;
    : data = send_data;
    :
    : //char xData[3]; //testdaten
    : //xData[0]=1; //testdaten
    : //xData[1]=2; //testdaten
    : //xData[2]=3; //testdaten
    :
    : status = WriteFile( hndDevice, &data, sizeof(data), &bytesWritten, NULL );
    :
    : printf("Status : %d
    ", status);
    : printf("Number of Bytes written: %d
    ", bytesWritten);
    :
    : if (status == 0)
    : {
    : printf("error while writing:%li
    ",GetLastError() );
    : }
    :
    : return(1);
    : }
    :
    : int closePort(void)
    : {
    : CloseHandle(hndDevice);
    : printf("Port has been closed...");
    : return(1);
    : }
    :
    : int main(int argc, char* argv[])
    : {
    :
    :
    :
    : initPort();
    : writePort(24);
    : closePort();
    : return 0;
    : }
    :
    :
    :
    :
    :

  • Shawn CarterShawn Carter Member Posts: 0

    ___ // http://forcoder.org // free video tutorials and ebooks about ( Objective-C C Go PHP C# Perl C++ Python Delphi Scratch Swift R Visual Basic .NET Java Ruby Assembly MATLAB Visual Basic JavaScript PL/SQL COBOL Awk Lua Alice Julia ABAP Bash Logo FoxPro D Dart Fortran LabVIEW Erlang Crystal VBScript Scala Hack F# SAS Clojure ML Rust Lisp Prolog Transact-SQL Ada Scheme Apex Kotlin ) ___

Sign In or Register to comment.