473,405 Members | 2,176 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Opening LPT port in C++

Hi,
I've posted earlier a question regarding the call to GetCommState.
Here is the code for the function.
The problem is that GetCommState always returns false!!
Why?

void Open()
{
// the DCB and COMMTIMEOUTS structure are declare a __value struct
DCB *dcbCommPort = __nogc new DCB();
COMMTIMEOUTS *ctoCommPort = __nogc new COMMTIMEOUTS();
// OPEN THE COMM PORT.
hComm = CreateFile("LPT1",GENERIC_READ | GENERIC_WRITE,0, 0,OPEN_EXISTING,0,0);
// IF THE PORT CANNOT BE OPENED, BAIL OUT.
if(hComm == INVALID_HANDLE_VALUE)
{
throw(new ApplicationException("Comm Port Can Not Be Opened"));
}
// SET THE COMM TIMEOUTS.
GetCommTimeouts(hComm,ctoCommPort);
ctoCommPort->ReadTotalTimeoutConstant = ReadTimeout;
ctoCommPort->ReadTotalTimeoutMultiplier = 0;
ctoCommPort->WriteTotalTimeoutMultiplier = 0;
ctoCommPort->WriteTotalTimeoutConstant = 0;
SetCommTimeouts(hComm,ctoCommPort);

// SET BAUD RATE, PARITY, WORD SIZE, AND STOP BITS.
GetCommState(hComm, dcbCommPort);
dcbCommPort->BaudRate=BaudRate;
dcbCommPort->flags=0;
//dcb.fBinary=1;
dcbCommPort->flags|=1;
if (Parity>0)
{
dcbCommPort->flags|=2;
}
dcbCommPort->Parity=Parity;
dcbCommPort->ByteSize=ByteSize;
dcbCommPort->StopBits=StopBits;
if (!SetCommState(hComm, dcbCommPort)) // always returns false
{
throw(new ApplicationException("Comm Port Can Not Be Opened"));
}
Opened = true;
}

Thanks!
Nov 17 '05 #1
5 5825
"CeZaR" <pa****@go.ro> wrote in message
news:fc**************************@posting.google.c om...
I've posted earlier a question regarding the call to GetCommState.
Here is the code for the function.
The problem is that GetCommState always returns false!!


You shouldn't have to guess. _Immediately_ after the function returns, call
GetLastError().

Now, I haven't researched it, but if IJW is trampling on the thread's last
Win32 error by making some intervening Win32 API call, take a look at this:

http://blogs.msdn.com/adam_nathan/ar.../25/56643.aspx

Just by the way, is the LPT port in question controlled by a printer driver?

Regards,
Will
Nov 17 '05 #2
I've stopped the spooler service before running the program.
Then i call CreateFile with "\\\\.\\NONSPOOLED_LPT1".
BTW GetLastError returns 2 -> Invalid function.
"William DePalo [MVP VC++]" <wi***********@mvps.org> wrote in message news:<OG**************@TK2MSFTNGP12.phx.gbl>...
"CeZaR" <pa****@go.ro> wrote in message
news:fc**************************@posting.google.c om...
I've posted earlier a question regarding the call to GetCommState.
Here is the code for the function.
The problem is that GetCommState always returns false!!


You shouldn't have to guess. _Immediately_ after the function returns, call
GetLastError().

Now, I haven't researched it, but if IJW is trampling on the thread's last
Win32 error by making some intervening Win32 API call, take a look at this:

http://blogs.msdn.com/adam_nathan/ar.../25/56643.aspx

Just by the way, is the LPT port in question controlled by a printer driver?

Regards,
Will

Nov 17 '05 #3
And, finally i've fixed it!
The LPT port doesn't support all of the function in this code.
They all work for COM ports.
For LPT use just CreateFile, WriteFile.
"William DePalo [MVP VC++]" <wi***********@mvps.org> wrote in message news:<OG**************@TK2MSFTNGP12.phx.gbl>...
"CeZaR" <pa****@go.ro> wrote in message
news:fc**************************@posting.google.c om...
I've posted earlier a question regarding the call to GetCommState.
Here is the code for the function.
The problem is that GetCommState always returns false!!


You shouldn't have to guess. _Immediately_ after the function returns, call
GetLastError().

Now, I haven't researched it, but if IJW is trampling on the thread's last
Win32 error by making some intervening Win32 API call, take a look at this:

http://blogs.msdn.com/adam_nathan/ar.../25/56643.aspx

Just by the way, is the LPT port in question controlled by a printer driver?

Regards,
Will

Nov 17 '05 #4
I've stopped the spooler service before running the program.
Then i call CreateFile with "\\\\.\\NONSPOOLED_LPT1".
BTW GetLastError returns 2 -> Invalid function.
"William DePalo [MVP VC++]" <wi***********@mvps.org> wrote in message news:<OG**************@TK2MSFTNGP12.phx.gbl>...
"CeZaR" <pa****@go.ro> wrote in message
news:fc**************************@posting.google.c om...
I've posted earlier a question regarding the call to GetCommState.
Here is the code for the function.
The problem is that GetCommState always returns false!!


You shouldn't have to guess. _Immediately_ after the function returns, call
GetLastError().

Now, I haven't researched it, but if IJW is trampling on the thread's last
Win32 error by making some intervening Win32 API call, take a look at this:

http://blogs.msdn.com/adam_nathan/ar.../25/56643.aspx

Just by the way, is the LPT port in question controlled by a printer driver?

Regards,
Will

Nov 17 '05 #5
And, finally i've fixed it!
The LPT port doesn't support all of the function in this code.
They all work for COM ports.
For LPT use just CreateFile, WriteFile.
"William DePalo [MVP VC++]" <wi***********@mvps.org> wrote in message news:<OG**************@TK2MSFTNGP12.phx.gbl>...
"CeZaR" <pa****@go.ro> wrote in message
news:fc**************************@posting.google.c om...
I've posted earlier a question regarding the call to GetCommState.
Here is the code for the function.
The problem is that GetCommState always returns false!!


You shouldn't have to guess. _Immediately_ after the function returns, call
GetLastError().

Now, I haven't researched it, but if IJW is trampling on the thread's last
Win32 error by making some intervening Win32 API call, take a look at this:

http://blogs.msdn.com/adam_nathan/ar.../25/56643.aspx

Just by the way, is the LPT port in question controlled by a printer driver?

Regards,
Will

Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Guillaume Brocker | last post by:
Hi ! I'm opening a socket to a web server on port 80 located on my LAN with fsockopen. The socket creation fails and PHP returns "php_network_getaddresses: getaddrinfo failed: Name or service...
4
by: Lizard King | last post by:
Hi. I have a computer running a third party software (POS). The thing is this application opens the printer port and I need to use another program while the first one is still running. Since...
2
by: Martin Fuzzey | last post by:
I am using xmlrpclib (based on httplib) in Python 2.3 on Mandrake Linux. When my client attempts to connect to a server using a "http://localhost:port" style URL there is a long delay before the...
1
by: Bryan Hahn | last post by:
I would like to open our firewall to allow traffic from the w3c validator in our development enviornment. I tried to open port 80 from 18.29.1.50 (validator.w3.org), but that didn't do the trick....
1
by: Alfons | last post by:
Hello, I have build a program that can do file transferring between a Windows XP computer and a DOS computer via a serial port. The Windows program I have build in C++ with Visual Studio 6.0....
1
by: CeZaR | last post by:
Hi, I've posted earlier a question regarding the call to GetCommState. Here is the code for the function. The problem is that GetCommState always returns false!! Why? void Open() { // the...
0
by: David | last post by:
I am having trouble with "ACCESS DENIED" error messages in a VB.NET 2003 application when attempting to open serial comms ports. The application has 2 ports that connect via serial cable to 2...
3
by: Luke Davis | last post by:
I'm looking for an effective way to open and close TCP ports. Can I do this through Tcpclient? And I know this is a potential security risk, so what kind of permission must the person running...
3
by: Suresh P | last post by:
Hi All, I tried to access the mysql database in ODBC using ip address and username/password. It returns, "cannot connect to MySQL server on IP ADDRESS(10060)". This could be related to Firewall...
4
by: cmdolcet69 | last post by:
I have the following code: When i run this code it gieve me an error saying the COM port isn;t opened? Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.