Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checking whether the Comm port is working or not

Status
Not open for further replies.

alicemong

Programmer
Mar 20, 2003
17
0
0
HK
Hi,

I want to write a VB function to check whether a parallel comm port or serial comm port is working or not so that I can decide whether the device is communicating with the desktop through the comm port.

Does anyone know how to do it?

Any responses are welcome
Thanks

Alice
 
Hi Alice,

You could use CreateFile(...) using COM1, LPT1 etc. If it fails then the port is in use.

Hope this helps.


William
Software Engineer
ICQ No. 56047340
 
In VB it's easier to use the MSCOMM controls, I think...

Greetings,
Rick
 
Hi,

In fact, I have checked the MSCOMM controls before. I try to use the OnComm event to capture the comm port status, but no responses are returned except the send one. Does anyone know how to use the MSCOMM to check whether the comm port is running or not.

As for the CreateFile function, when the printer is printing its testing page, the createFile function without return any error. Can anyone give me some hints how to do that?

Alice

 
Hi Alice.

The CreateFile effectivly fails here because all the data need to print the page is buffered in the printer so there is no activity on the Port.

You'd really need to send it a BIG file to tie up the port, have you tried it by putting the printer Off-Line?

You could always put the CreateFile in an endless loop and start it off (breaking when the post's busy). Then go and print a page, see what results you get.



William
Software Engineer
ICQ No. 56047340
 
Hi william,

Thanks for you quick reply. I have tried the following code
hFile = CreateFile("LPT1", GENERIC_READ Or GENERIC_WRITE, 0, ByVal 0&, OPEN_EXISTING, 0, 0)
and
hFile = CreateFile("LPT1", GENERIC_READ Or GENERIC_WRITE, 0, ByVal 0&, CREATE_NEW, 0, 0).

When I print from a word document, both the above CreateFile functions does not return any error while the printer is printing and afterwards. Why?

Thanks
Alice
 
Hi Alice,

IMSMC you might want to try:

CreateFile("LPT1:", ...)

HTH

If not let me know and I'll test here.



William
Software Engineer
ICQ No. 56047340
 
Hi William,

I have changed the name to LPT1:, but still fail.
When I run the following code:

Do
DoEvents
name = Trim(UCase(name))
hFile = CreateFile("LPT1:", GENERIC_READ Or GENERIC_WRITE, 0, ByVal 0&, OPEN_EXISTING, 0, 0)
MsgBox hFile
Loop

At first, hfile > 100 but later on the value of hfile change to -1 no matter the printer is printing or not.

I don't know why
Thx for your response and kindly help

Alice
 
I'm not surprised.

You need to test the return value of CreateFile for INVALID_HANDLE_VALUE if this is true then the port is busy. If it's anything other than that then it's a handle to the Port.

You must then CLOSE the handle before returning to the top of the loop. Also, don't display the MsgBox on every cycle as this will slow the polling down, only show it when the return value is INVALID_HANDLE_VALUE.

HTH


William
Software Engineer
ICQ No. 56047340
 
>>Does anyone know how to use the MSCOMM to check whether the comm port is running or not.

If by this you mean "does anybody know how to capture COMM events when data is being send to the comm port", then the answer is: set the "RThreshold" property of the comm control to 1 (defaults to 0, I think...).

Greetings,
Rick
 
Hi William,

Here is my code used to check whether the comm port is used or not.

Public Function isCommUsed(ByVal name As String) As Boolean
Dim strError As String
strError = String(512, 0)
Dim hFile As Long

isCommUsed = False

Do
DoEvents
name = Trim(UCase(name))
hFile = CreateFile(name, GENERIC_READ Or
GENERIC_WRITE, 0, ByVal 0&, OPEN_EXISTING, 0, 0)

Call FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM Or _
FORMAT_MESSAGE_IGNORE_INSERTS Or_
FORMAT_MESSAGE_MAX_WIDTH_MASK, _
0&, GetLastError, 0&, _
strError, Len(strError), 0&)

If strError = "INVALID_HANDLE_VALUE" Then
isCommUsed = True
Exit Do
End If

CloseHandle (hFile)
Loop

End Function

However, the strError always returns "¾Þ§@¦¨¥\§¹¦¨" whenever the printer is printing or not.

Do u have any ideas?

Thx for your help
Alice

 
Format message will never return INVALID_HANDLE_VALUE; it is used to generate human readable error messages.

If I'm not mistaking the INVALID_HANDLE_VALUE constant resolves to a value of -1.

However, you can't assume by this return value that the handle is invalid because the port is in use; it can be invalid for numerous reasons. It just means that CreateFile() failed. You should use the return value of GetLastError() to see why it failed to check whether or not the port is already in use..

Greetings,
Rick
 
Rick.

According to the MS Docs assuming all things being syntactically correct when CreateFile fails the Port is in use. I'm only passing on what I've read.



William
Software Engineer
ICQ No. 56047340
 
The first thing that comes into mind (my mind at least...) is that opening the port might fail because, for whatever reason, the port hasn't been initialized properly while booting the OS (IRQ conflicts for instance, or hardware failure or whatever). You might even be trying to check a non-existing port; lots of things may happen which you can't control from within your app.

All those things will lead to failure when trying to open the port and CreateFile() will return INVALID_HANDLE_VALUE.

You're right that, when syntactically correct, the reason will be the port is in use, but this is assuming that the desired port is functioning properly....

Greetings,
Rick
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top