我做了个打开关闭串口的程序,程序如下: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
hCom = CreateFile("COM1", GENERIC_READ Or GENERIC_WRITE, 0, SecurityA, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0)
If hCom = -1 Then
i = MsgBox("无法打开串口", vbOKOnly, "错误")
End If
End Sub
Private Sub Command2_Click()
CloseHandle (hCom)
End Sub
模块里声明如下:
Public Type SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescriptor As Long
bInheritHandle As Long
End Type
Public Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, lpSecurityAttributes As SECURITY_ATTRIBUTES, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Public hCom As Long '串口句柄
这个程序编好以后,编译成.exe文件后在win2000下能够成功打开和关闭串口,但我在win98下,点击打开串口框,却弹出"无法打开串口"对话框,这是为什么啊?据我了解Createfile这个API函数在win98里也可以用的啊!我百思不得其解,请求各位高人给我指点谢谢!联系:jiechen111@163.com 作者:
Nothing 时间: 2005-2-16 10:17
不是系统的问题,而是串口的问题,在2000下你的com1口正好没被占用,在98下被占用了,你应该把hCom=CreateFile("COM1",...)中的第一个参数改为字符串变量,,如Dim s as string 然后做各combox,把com1-com4加进去,选择com口把值付给变量s就可以了。