发表于:2005/2/5 14:58:00
#0楼
我做了个打开关闭串口的小程序,在win2000系统下,这个程序可以打开和关闭串口,但是到了win98系统下却老是弹出“无法打开串口”对话框,这是怎么回事啊?Createfile这个API函数在win98下应该也可以用的啊,程序在下面,请高手给我指教,谢谢!EMail:jiechen111@163.com
Private sub command1_Click( )
Const GENERIC_READ=&H80000000
Const GENERIC_WRITE=&H40000000
Const OPEN_EXISTING=3
Const FILE_ATTRIBUTE_NORMAL=&H80
Dim SecurityA As SECURITY_ATTRIBUTES
Dim Temp(5) As String
select case(temp(3))
Case 0:hCom=CreateFile("COM1",GENERIC_READ OR GENERIC_WRITE,0,SECURITYA,OPEN_EXISTING,FILE_ATTRIZBUTE_NORMAL,0)
Case 1:hCom=CreateFile("COM2",GENERIC_READ OR GENERIC_WRITE,0,SECURITYA,OPEN_EXISTING,FILE_ATTRIZBUTE_NORMAL,0)
Case 2:hCom=CreateFile("COM3",GENERIC_READ OR GENERIC_WRITE,0,SECURITYA,OPEN_EXISTING,FILE_ATTRIZBUTE_NORMAL,0)
Case 3:hCom=CreateFile("COM4",GENERIC_READ OR GENERIC_WRITE,0,SECURITYA,OPEN_EXISTING,FILE_ATTRIZBUTE_NORMAL,0)
Case 4:hCom=CreateFile("COM5",GENERIC_READ OR GENERIC_WRITE,0,SECURITYA,OPEN_EXISTING,FILE_ATTRIZBUTE_NORMAL,0)
Case 5:hCom=CreateFile("COM6",GENERIC_READ OR GENERIC_WRITE,0,SECURITYA,OPEN_EXISTING,FILE_ATTRIZBUTE_NORMAL,0)
Case 6:hCom=CreateFile("COM7",GENERIC_READ OR GENERIC_WRITE,0,SECURITYA,OPEN_EXISTING,FILE_ATTRIZBUTE_NORMAL,0)
Case 7:hCom=CreateFile("COM8",GENERIC_READ OR GENERIC_WRITE,0,SECURITYA,OPEN_EXISTING,FILE_ATTRIZBUTE_NORMAL,0)
Case 8:hCom=CreateFile("COM9",GENERIC_READ OR GENERIC_WRITE,0,SECURITYA,OPEN_EXISTING,FILE_ATTRIZBUTE_NORMAL,0)
Case 9:hCom=CreateFile("COM10",GENERIC_READ OR GENERIC_WRITE,0,SECURITYA,OPEN_EXISTING,FILE_ATTRIZBUTE_NORMAL,0)
Case 10:hCom=CreateFile("COM11",GENERIC_READ OR GENERIC_WRITE,0,SECURITYA,OPEN_EXISTING,FILE_ATTRIZBUTE_NORMAL,0)
Case 11:hCom=CreateFile("COM12",GENERIC_READ OR GENERIC_WRITE,0,SECURITYA,OPEN_EXISTING,FILE_ATTRIZBUTE_NORMAL,0)
end select
if hcom=-1 then
i=MSGBOX("无法打开串口",vbOKONLY,"错误")
End if
End sub
Private sub command2_Click( )
CloseHandle(hcom)
End sub
module里声明如下:
Public Declare Function Createfile lib "kernel32"Alias"CreateFileA"(Byval lpFileName As String,Byval dwdesiredAcess as Long ,Byval DwshareMode As Long ,LpsecurityAttribute As Long , Byval DwFlagsandAttributes As Long ,Byval HtTemplateFile As long)as Long
Public Declare Function CloseHandle lib "kernel32"(ByVal hobject As long)as long
Private sub command1_Click( )
Const GENERIC_READ=&H80000000
Const GENERIC_WRITE=&H40000000
Const OPEN_EXISTING=3
Const FILE_ATTRIBUTE_NORMAL=&H80
Dim SecurityA As SECURITY_ATTRIBUTES
Dim Temp(5) As String
select case(temp(3))
Case 0:hCom=CreateFile("COM1",GENERIC_READ OR GENERIC_WRITE,0,SECURITYA,OPEN_EXISTING,FILE_ATTRIZBUTE_NORMAL,0)
Case 1:hCom=CreateFile("COM2",GENERIC_READ OR GENERIC_WRITE,0,SECURITYA,OPEN_EXISTING,FILE_ATTRIZBUTE_NORMAL,0)
Case 2:hCom=CreateFile("COM3",GENERIC_READ OR GENERIC_WRITE,0,SECURITYA,OPEN_EXISTING,FILE_ATTRIZBUTE_NORMAL,0)
Case 3:hCom=CreateFile("COM4",GENERIC_READ OR GENERIC_WRITE,0,SECURITYA,OPEN_EXISTING,FILE_ATTRIZBUTE_NORMAL,0)
Case 4:hCom=CreateFile("COM5",GENERIC_READ OR GENERIC_WRITE,0,SECURITYA,OPEN_EXISTING,FILE_ATTRIZBUTE_NORMAL,0)
Case 5:hCom=CreateFile("COM6",GENERIC_READ OR GENERIC_WRITE,0,SECURITYA,OPEN_EXISTING,FILE_ATTRIZBUTE_NORMAL,0)
Case 6:hCom=CreateFile("COM7",GENERIC_READ OR GENERIC_WRITE,0,SECURITYA,OPEN_EXISTING,FILE_ATTRIZBUTE_NORMAL,0)
Case 7:hCom=CreateFile("COM8",GENERIC_READ OR GENERIC_WRITE,0,SECURITYA,OPEN_EXISTING,FILE_ATTRIZBUTE_NORMAL,0)
Case 8:hCom=CreateFile("COM9",GENERIC_READ OR GENERIC_WRITE,0,SECURITYA,OPEN_EXISTING,FILE_ATTRIZBUTE_NORMAL,0)
Case 9:hCom=CreateFile("COM10",GENERIC_READ OR GENERIC_WRITE,0,SECURITYA,OPEN_EXISTING,FILE_ATTRIZBUTE_NORMAL,0)
Case 10:hCom=CreateFile("COM11",GENERIC_READ OR GENERIC_WRITE,0,SECURITYA,OPEN_EXISTING,FILE_ATTRIZBUTE_NORMAL,0)
Case 11:hCom=CreateFile("COM12",GENERIC_READ OR GENERIC_WRITE,0,SECURITYA,OPEN_EXISTING,FILE_ATTRIZBUTE_NORMAL,0)
end select
if hcom=-1 then
i=MSGBOX("无法打开串口",vbOKONLY,"错误")
End if
End sub
Private sub command2_Click( )
CloseHandle(hcom)
End sub
module里声明如下:
Public Declare Function Createfile lib "kernel32"Alias"CreateFileA"(Byval lpFileName As String,Byval dwdesiredAcess as Long ,Byval DwshareMode As Long ,LpsecurityAttribute As Long , Byval DwFlagsandAttributes As Long ,Byval HtTemplateFile As long)as Long
Public Declare Function CloseHandle lib "kernel32"(ByVal hobject As long)as long