发表于:2005/1/18 15:51:00
#0楼
我用vb调用人家写的动态库做了个串口通信的程序,但是我的串口通信程序在Win2000里完全可以用但是到了win98里怎么运行程序就弹出对话框为“无法打开串口”,这是为什么啊?请各位高手指教一下,谢谢!
VB写的关于串口的程序如下:
Private Sub StartCom
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
FIleName for inpute Lock Read Write As #1
on Error Resume Next
Input #1, temp(0),temp(1),temp(2),temp(3),temp(4)
Close #1
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,"错误"
StatusBar1.Panels(1).Text="无法打开串口!"
else: if iniCom(hcom,Temp(0),8,temp(1),temp(2),Temp(4)=False Then
i=MsgBOX("串口初始化失败",vbOKONLY,"错误")
StatusBar1.Panels(1).Text="串口初始化失败!"
CLoseHandle(hCom)
else:
StartCom.Enabled=False
playCommand.Enabled=False
stopcom.Enabled=true
stopcommand.enabled=true
Combo1.Enabled=True
searchadd.Enabled=True
StatusBAR1.Panels(1).Text="串口已成功打开!"
searchAdd_Click
end if
end sub
调用的动态库程序如下:
extern"C"_declspec(dellexport)BOOL IniCom(HANDLE hCom, long Baurate,short DataBits,short Parity,short StopBits,short TimeOut)
{
DCB dcb
COMMTIMEOUTS CommTime;
BOOL Error=TRUE;
if(hCom==INVALID_HANDLE_VALUE)
{
//MessageBox(NULL,"无法打开串口! " ,"故障",MB_ICONERROR);
return FALSE;
}
Error=SetupComm(hCom,1024,1024);
if(!Error)
{
//MessageBox(NULL,"设定串口缓冲区时出错! " ,"故障",MB_ICONERROR);
return False;
}
GEtCommTimeots(hcom,&CommTimes);
CommTimes.ReadIntervalTimeout=0x00;//间隔超时(mS);0为不使用
commTimes.ReadTotalTimeoutMultiplier=0X01;//乘量(mS);总超时=乘量*字节+常量
commTimes.ReadTotalTimeoutConstant=TimeOut;//OX64;//常量(ms)
Error=SetcommTimeouts(hCom,&CommTimes);
if(!Error)
{
//MessageBox(NULL,"设定串口读写超时时出错! " ,"故障",MB_ICONERROR);
return FALSE
}
Error=GetCommState(hcom,&dcb);
if (!ERROR)
{
//MessageBox(NULL,"读写串口参数时出错! " ,"故障",MB_ICONERROR);
return FALSE
}
/********初始化串口参数**********/
dcb.BaudRate=BaudRate;
dcb.ByteSize=DataBits;
dcb.PArity=Parity;
dcb.StopBits=StopBits;
/*********结束*******************/
Error=setCommState(hCom,&dcb);
if(!ERROR)
{
//MessageBox(NULL,"设定串口参数时出错! " ,"故障",MB_ICONERROR);
return FASE
}
rturn TRUE;
}
我的email为:jiechen111@163.com,请各位高手联系我,给点解决问题的方法的建议我,谢谢!
VB写的关于串口的程序如下:
Private Sub StartCom
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
FIleName for inpute Lock Read Write As #1
on Error Resume Next
Input #1, temp(0),temp(1),temp(2),temp(3),temp(4)
Close #1
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,"错误"
StatusBar1.Panels(1).Text="无法打开串口!"
else: if iniCom(hcom,Temp(0),8,temp(1),temp(2),Temp(4)=False Then
i=MsgBOX("串口初始化失败",vbOKONLY,"错误")
StatusBar1.Panels(1).Text="串口初始化失败!"
CLoseHandle(hCom)
else:
StartCom.Enabled=False
playCommand.Enabled=False
stopcom.Enabled=true
stopcommand.enabled=true
Combo1.Enabled=True
searchadd.Enabled=True
StatusBAR1.Panels(1).Text="串口已成功打开!"
searchAdd_Click
end if
end sub
调用的动态库程序如下:
extern"C"_declspec(dellexport)BOOL IniCom(HANDLE hCom, long Baurate,short DataBits,short Parity,short StopBits,short TimeOut)
{
DCB dcb
COMMTIMEOUTS CommTime;
BOOL Error=TRUE;
if(hCom==INVALID_HANDLE_VALUE)
{
//MessageBox(NULL,"无法打开串口! " ,"故障",MB_ICONERROR);
return FALSE;
}
Error=SetupComm(hCom,1024,1024);
if(!Error)
{
//MessageBox(NULL,"设定串口缓冲区时出错! " ,"故障",MB_ICONERROR);
return False;
}
GEtCommTimeots(hcom,&CommTimes);
CommTimes.ReadIntervalTimeout=0x00;//间隔超时(mS);0为不使用
commTimes.ReadTotalTimeoutMultiplier=0X01;//乘量(mS);总超时=乘量*字节+常量
commTimes.ReadTotalTimeoutConstant=TimeOut;//OX64;//常量(ms)
Error=SetcommTimeouts(hCom,&CommTimes);
if(!Error)
{
//MessageBox(NULL,"设定串口读写超时时出错! " ,"故障",MB_ICONERROR);
return FALSE
}
Error=GetCommState(hcom,&dcb);
if (!ERROR)
{
//MessageBox(NULL,"读写串口参数时出错! " ,"故障",MB_ICONERROR);
return FALSE
}
/********初始化串口参数**********/
dcb.BaudRate=BaudRate;
dcb.ByteSize=DataBits;
dcb.PArity=Parity;
dcb.StopBits=StopBits;
/*********结束*******************/
Error=setCommState(hCom,&dcb);
if(!ERROR)
{
//MessageBox(NULL,"设定串口参数时出错! " ,"故障",MB_ICONERROR);
return FASE
}
rturn TRUE;
}
我的email为:jiechen111@163.com,请各位高手联系我,给点解决问题的方法的建议我,谢谢!