阅读:1496回复:5
求助:一个非常难的串口方面的小问题
我做了个打开关闭串口的小程序,在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 |
|
沙发#
发布于:2005-02-05 16:34
将COM0、COM1 等修改为\\\\.\\COM0、\\\\.\\COM1等
|
|
|
板凳#
发布于:2005-02-06 14:55
在com前加//
|
|
地板#
发布于:2005-02-14 20:20
加了还是老问题,不行啊!怎么办呢?郁闷啊!
|
|
地下室#
发布于:2005-02-15 10:39
加了还是老问题,不行啊!怎么办呢?郁闷啊! 你的SECURITYA没有给值,直接将SECURITYA修改为NULL,可能是2000和98处理堆栈中的变量有所不同 |
|
|
5楼#
发布于:2005-02-15 14:45
在vb里不存在null,我把SecurityA的值改为0以后在win98里还是出现“无法打开串口”这个问题,然后我只能把SecurityA改成空值即0&在win98还是出现“无法打开串口”这个问题!奇怪啊,请再为我分析一下好吗?谢谢!
|
|