|
-
Aug 24th, 2001, 12:44 PM
#1
Thread Starter
Lively Member
createfile to open comm port...
I am writing a program to receive data from a serial device. Works great on my win2k pro machine, but my win98se machine won't open com1.
Here's my code:
hCom = CreateFile("COM1:", GENERIC_READ, 0, Null, OPEN_EXISTING, 0, 0)
If hCom <= 0 Then Exit Sub
...
On a win2k pro machine, this returns the handle, and the rest of my code works perfectly.
On a win98se machine, hCom = -1, code exits.
I've tried several permutations of the lpFileName parameter:
"COM1", "COM1:", "\\.\COM1", "\\.\COM1:", ...
Is there a trick?
David <><
Last edited by DaveAMS; Aug 24th, 2001 at 12:49 PM.
-
Aug 24th, 2001, 05:58 PM
#2
Frenzied Member
I tried your code and it wouldn't even compile at first because I
had a different declaration (lpSecurityAttributes) when I did get it
to run it would not open COM1 jast as you. I opened up the
Device Manager and found that it said I HAVE NO COM1. But wait
a minute, my modem is on COM1 and the Device Manager
confirmed this.
I went back to my code and switch the dwCreationDisposition
param from OPEN_EXISTING to OPEN_ALWAYS and it worked.
Below is my code.
Code:
'BAS
Option Explicit
Public Const OPEN_EXISTING = 3
Public Const OPEN_ALWAYS = 4
Declare Function WriteFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToWrite As Long, lpNumberOfBytesWritten As Long, ByVal lpOverlapped As Long) As Long
Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByVal lpSecurityAttributes As Long, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes&, ByVal hTemplateFile As Long) As Long
Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Declare Function FlushFileBuffers Lib "kernel32" (ByVal hFile As Long) As Long
'FORM
Option Explicit
Private Sub Form_Load()
DialNumber "767-8900", "COM1"
End Sub
Private Sub DialNumber(sPhoneNumber, sCommPort As String)
Dim sMsg As String
Dim bModemCommand() As Byte, sCommand As String
Dim lOpenPort As Long
Dim lRet As Long, lRetBytes As Long, i As Integer
If MsgBox("Please pickup the phone and choose OK to dial " & sPhoneNumber, vbOKCancel, App.Title) = vbCancel Then
Exit Sub
End If
lOpenPort = CreateFile(sCommPort, &HC0000000, 0, 0, OPEN_ALWAYS, 0, 0)
If lOpenPort = -1 Then
sMsg = "Unable to open communication port " & sCommPort
GoTo Err_DialNumber
End If
sCommand = "ATDT" & sPhoneNumber & vbCrLf
ReDim bModemCommand(Len(sCommand))
For i = 0 To Len(sCommand) - 1
bModemCommand(i) = Asc(Mid$(sCommand, i + 1, 1))
Next
lRet = WriteFile(lOpenPort, bModemCommand(0), Len(sCommand), lRetBytes, 0)
If lRet = 0 Then
sMsg = "Unable to dial number " & sPhoneNumber
GoTo Err_DialNumber
End If
lRet = FlushFileBuffers(lOpenPort)
MsgBox "Click OK when the phone finishes dialing. "
sCommand = "ATH0" & vbCrLf
ReDim bModemCommand(Len(sCommand))
For i = 0 To Len(sCommand) - 1
bModemCommand(i) = Asc(Mid$(sCommand, i + 1, 1))
Next
lRet = WriteFile(lOpenPort, bModemCommand(0), Len(sCommand), lRetBytes, 0)
lRet = FlushFileBuffers(lOpenPort)
lRet = CloseHandle(lOpenPort)
Exit Sub
Err_DialNumber:
MsgBox sMsg & vbCr & vbCr & "Make sure no other devices are using Com port " & sCommPort
End Sub
Greg
Free VB Add-In - The Reference Librarian
Click Here for screen shot and download link.
-
Aug 24th, 2001, 09:56 PM
#3
Thread Starter
Lively Member
I tried switching to open_always, to no avail...but wait!
You inspired me to do something I hadn't thought of yet...try mixing up the settings. Everything I found on MSDN said that lpSecurityAttributes had to be Null, not zero...and this is true on win2k. Passing zero won't work on win2k...but passing null apparently won't work on win9x! I changed the lpSecurityAttributes from zero to byval 0&, and I'm smokin'!!!
Basically I've gone to the following code to maintain compatibility between win2k and win9x:
'Declare statement for openfile
Public Declare Function CreateFile Lib "kernel32.dll" Alias "CreateFileA" _
(ByVal lpFileName As String, ByVal dwDesiredAccess As Long, _
ByVal dwShareMode As Long, lpSecurityAttributes As Any, _
ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, _
ByVal hTemplateFile As Long) As Long
'...in subroutine
strCom = "COM1"
'Open port in win2k
hCom = CreateFile(strCom, GENERIC_READ, 0, Null, OPEN_EXISTING, 0, 0
if hCom<=0 then
'Open Port in win9x
hCom = CreateFile(strCom, GENERIC_READ, 0, ByVal 0&, OPEN_EXISTING, 0, 0
if hCom <=0 then Exit Sub
end if
Hope this will be of benefit to anyone else who needs to maintain portability between win2k and win9x...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|