D
dxj1976
Unregistered / Unconfirmed
GUEST, unregistred user!
我用VC++做了个串口通讯的动态库,用DELPHI7进行调用。在设计的过程中发现以下问题:
动态库在WIN98下使用正常,但在WIN2000下串口只能被打开一次,再打开串口时提示错误。不知道问题出在什么地方。
动态库的原代码如下:
// InfraredCom.cpp : Defines the entry point for the DLL application.
//
#include "stdafx.h"
#include "jshs.h"
#include "clock.h"
#include "dos.h"
#include "winuser.h"
#include "conio.h"
char BufferIn[254];
HANDLE hCom=0,h_handle;
DWORD Num;
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
return TRUE;
}
//init com
extern "C" int PASCAL OpenCOM(char *n)
{
//h_handle=0hCom
h_handle=CreateFile(
n,
GENERIC_READ|GENERIC_WRITE,
0, //
NULL,
OPEN_EXISTING, //
0,//
NULL); //
if(h_handle==INVALID_HANDLE_VALUE)
{
return(1);
}
hCom=h_handle;
DCB Dcb;
GetCommState(hCom,&Dcb);
Dcb.BaudRate=1200;
Dcb.ByteSize=8;
Dcb.Parity=NOPARITY;
Dcb.StopBits=ONESTOPBIT;
Dcb.fParity=1;
SetCommState(hCom,&Dcb);
COMMTIMEOUTS Timeouts;
GetCommTimeouts(hCom,&Timeouts);
Timeouts.ReadTotalTimeoutMultiplier=50;
Timeouts.ReadTotalTimeoutConstant=1500;
SetCommTimeouts(hCom,&Timeouts);
return(0);
}
//Send data to com
extern "C" int PASCAL WriteCOM(char *aa,unsigned leng)
{
if (WriteFile(hCom,aa,leng,&Num,NULL))
{
/*Sleep(100);*/
return(0);
}
else return(1);
}
//Recieve data from com
extern "C" char * PASCAL ReadCOM(unsigned leng)
{
int i=0;
for(i=0;i<255;i++)
BufferIn=NULL;
if(ReadFile(hCom,BufferIn,leng,&Num,NULL))
{
return(BufferIn);
}
else return("$"
}
//Close com
extern "C" int PASCAL CloseCOM()
{
if (hCom==0)
{
return(3);
}
if(CloseHandle(hCom))
{
hCom=0;
return(0);
}
else
{
hCom=0;
return(1);
}
}
extern "C" int PASCAL ComOut(char *aa,int len1)
{
int retu=5,i=0,len=0,xy=0,cd;
char xy1;
char dd[2];
char *read="0",ch;
DCB Dcb;
GetCommState(hCom,&Dcb);
Dcb.BaudRate=1200;
Dcb.ByteSize=8;
Dcb.Parity=NOPARITY;
Dcb.StopBits=ONESTOPBIT;
Dcb.fRtsControl=0;
Dcb.fParity=1;
SetCommState(hCom,&Dcb);
cd=len1/2;
len1=cd+3;
for(i=0;i<len1;i++)
{
if(i<10)
{
if ((aa[2*i]>0x40))
ch=(aa[2*i]%16+0x09)*16;
else
ch=(aa[2*i]-0x30)*16;
if ((aa[2*i+1]>0x40))
ch=ch+(aa[2*i+1]%16+0x09);
else
ch=ch+aa[2*i+1]-0x30;
xy=(xy+ch) & 0x00FF;
}
else if(i>=10 && i<cd)
{
if ((aa[2*i]>0x40))
ch=(aa[2*i]%16+0x09)*16;
else
ch=(aa[2*i]-0x30)*16;
if ((aa[2*i+1]>0x40))
ch=ch+(aa[2*i+1]%16+0x09);
else
ch=ch+aa[2*i+1]-0x30;
ch=ch+0x33;
xy=(xy+ch) & 0x00FF;
}
else if(i==cd)
ch=xy;
else
ch=0x16;
Sleep(30);
// Sleep(90);
dd[0]=ch;
dd[1]='/0';
retu=WriteCOM(dd,1);
}
Dcb.fRtsControl=1;
Dcb.fParity=1;
SetCommState(hCom,&Dcb);
i=0;
ll: read=ReadCOM(1);
if (read[0]=='h')
{
read=ReadCOM(9);
// Sleep(50);
read[15]='/0';
}
else
{
i++;
if (i>10)
return -3;
goto ll;
}
xy1=0x00;
xy1=(xyprg(read,9)+0x68)&0x00FF;
len=read[8];
read=ReadCOM(len+2);
read[len+2]='/0';
xy1=(xy1+xyprg(read,len))&0x00FF;
if (read[len+1]==0x16 && xy1==read[len])
{
hex_char(read,len);
// Sleep(350);
Sleep(250);
return 0;
}
else
{
return -1;
}
}
DELPHI7调用原代码如下:
function Tform1.r485_tx(com:string;read_ml:string;opencom_flag:integer;closecom_flag:integer):string;
var
fh,kbz,fhbz:integer;
file1:textfile;
buffer:string;
begin
if opencom_flag=0 then
begin
fh:=opencom(com);
if fh <> 0 then
begin
showmessage('串口错误!');
kbz := CloseCOM();
screen.Cursor:=crdefault;
err_flag:=1;
exit;
end;
end;
screen.Cursor:=crhourglass;
progressbar1.position:=30;
fhbz :=comout(read_ml, length(trim(read_ml)));
if fhbz <> 0 then
begin
showmessage('通讯失败!');
kbz := CloseCOM();
screen.Cursor:=crdefault;
err_flag:=1;
exit;
end
else
begin
progressbar1.position:=60;
AssignFile(file1, 'revc.dat');
Reset(file1);
Readln(file1, buffer);
CloseFile(file1);
progressbar1.position:=100;
end;
if closecom_flag=0 then
kbz := CloseCOM();
screen.Cursor:=crdefault;
progressbar1.position:=0;
result:=buffer;
end;
希望高手根据提供的原代码给予正确的指正!谢谢!
动态库在WIN98下使用正常,但在WIN2000下串口只能被打开一次,再打开串口时提示错误。不知道问题出在什么地方。
动态库的原代码如下:
// InfraredCom.cpp : Defines the entry point for the DLL application.
//
#include "stdafx.h"
#include "jshs.h"
#include "clock.h"
#include "dos.h"
#include "winuser.h"
#include "conio.h"
char BufferIn[254];
HANDLE hCom=0,h_handle;
DWORD Num;
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
return TRUE;
}
//init com
extern "C" int PASCAL OpenCOM(char *n)
{
//h_handle=0hCom
h_handle=CreateFile(
n,
GENERIC_READ|GENERIC_WRITE,
0, //
NULL,
OPEN_EXISTING, //
0,//
NULL); //
if(h_handle==INVALID_HANDLE_VALUE)
{
return(1);
}
hCom=h_handle;
DCB Dcb;
GetCommState(hCom,&Dcb);
Dcb.BaudRate=1200;
Dcb.ByteSize=8;
Dcb.Parity=NOPARITY;
Dcb.StopBits=ONESTOPBIT;
Dcb.fParity=1;
SetCommState(hCom,&Dcb);
COMMTIMEOUTS Timeouts;
GetCommTimeouts(hCom,&Timeouts);
Timeouts.ReadTotalTimeoutMultiplier=50;
Timeouts.ReadTotalTimeoutConstant=1500;
SetCommTimeouts(hCom,&Timeouts);
return(0);
}
//Send data to com
extern "C" int PASCAL WriteCOM(char *aa,unsigned leng)
{
if (WriteFile(hCom,aa,leng,&Num,NULL))
{
/*Sleep(100);*/
return(0);
}
else return(1);
}
//Recieve data from com
extern "C" char * PASCAL ReadCOM(unsigned leng)
{
int i=0;
for(i=0;i<255;i++)
BufferIn=NULL;
if(ReadFile(hCom,BufferIn,leng,&Num,NULL))
{
return(BufferIn);
}
else return("$"
}
//Close com
extern "C" int PASCAL CloseCOM()
{
if (hCom==0)
{
return(3);
}
if(CloseHandle(hCom))
{
hCom=0;
return(0);
}
else
{
hCom=0;
return(1);
}
}
extern "C" int PASCAL ComOut(char *aa,int len1)
{
int retu=5,i=0,len=0,xy=0,cd;
char xy1;
char dd[2];
char *read="0",ch;
DCB Dcb;
GetCommState(hCom,&Dcb);
Dcb.BaudRate=1200;
Dcb.ByteSize=8;
Dcb.Parity=NOPARITY;
Dcb.StopBits=ONESTOPBIT;
Dcb.fRtsControl=0;
Dcb.fParity=1;
SetCommState(hCom,&Dcb);
cd=len1/2;
len1=cd+3;
for(i=0;i<len1;i++)
{
if(i<10)
{
if ((aa[2*i]>0x40))
ch=(aa[2*i]%16+0x09)*16;
else
ch=(aa[2*i]-0x30)*16;
if ((aa[2*i+1]>0x40))
ch=ch+(aa[2*i+1]%16+0x09);
else
ch=ch+aa[2*i+1]-0x30;
xy=(xy+ch) & 0x00FF;
}
else if(i>=10 && i<cd)
{
if ((aa[2*i]>0x40))
ch=(aa[2*i]%16+0x09)*16;
else
ch=(aa[2*i]-0x30)*16;
if ((aa[2*i+1]>0x40))
ch=ch+(aa[2*i+1]%16+0x09);
else
ch=ch+aa[2*i+1]-0x30;
ch=ch+0x33;
xy=(xy+ch) & 0x00FF;
}
else if(i==cd)
ch=xy;
else
ch=0x16;
Sleep(30);
// Sleep(90);
dd[0]=ch;
dd[1]='/0';
retu=WriteCOM(dd,1);
}
Dcb.fRtsControl=1;
Dcb.fParity=1;
SetCommState(hCom,&Dcb);
i=0;
ll: read=ReadCOM(1);
if (read[0]=='h')
{
read=ReadCOM(9);
// Sleep(50);
read[15]='/0';
}
else
{
i++;
if (i>10)
return -3;
goto ll;
}
xy1=0x00;
xy1=(xyprg(read,9)+0x68)&0x00FF;
len=read[8];
read=ReadCOM(len+2);
read[len+2]='/0';
xy1=(xy1+xyprg(read,len))&0x00FF;
if (read[len+1]==0x16 && xy1==read[len])
{
hex_char(read,len);
// Sleep(350);
Sleep(250);
return 0;
}
else
{
return -1;
}
}
DELPHI7调用原代码如下:
function Tform1.r485_tx(com:string;read_ml:string;opencom_flag:integer;closecom_flag:integer):string;
var
fh,kbz,fhbz:integer;
file1:textfile;
buffer:string;
begin
if opencom_flag=0 then
begin
fh:=opencom(com);
if fh <> 0 then
begin
showmessage('串口错误!');
kbz := CloseCOM();
screen.Cursor:=crdefault;
err_flag:=1;
exit;
end;
end;
screen.Cursor:=crhourglass;
progressbar1.position:=30;
fhbz :=comout(read_ml, length(trim(read_ml)));
if fhbz <> 0 then
begin
showmessage('通讯失败!');
kbz := CloseCOM();
screen.Cursor:=crdefault;
err_flag:=1;
exit;
end
else
begin
progressbar1.position:=60;
AssignFile(file1, 'revc.dat');
Reset(file1);
Readln(file1, buffer);
CloseFile(file1);
progressbar1.position:=100;
end;
if closecom_flag=0 then
kbz := CloseCOM();
screen.Cursor:=crdefault;
progressbar1.position:=0;
result:=buffer;
end;
希望高手根据提供的原代码给予正确的指正!谢谢!