串口通讯问题???急需解决希望高手们给予指教!!! ( 积分: 100 )

  • 主题发起人 主题发起人 dxj1976
  • 开始时间 开始时间
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,&amp;Num,NULL))
{
return(BufferIn);
}
else return(&quot;$&quot;);
}



//Close com
extern &quot;C&quot; int PASCAL CloseCOM()
{
if (hCom==0)
{
return(3);
}
if(CloseHandle(hCom))
{
hCom=0;
return(0);
}
else
{
hCom=0;
return(1);
}
}


extern &quot;C&quot; int PASCAL ComOut(char *aa,int len1)
{
int retu=5,i=0,len=0,xy=0,cd;
char xy1;
char dd[2];
char *read=&quot;0&quot;,ch;
DCB Dcb;
GetCommState(hCom,&amp;Dcb);
Dcb.BaudRate=1200;
Dcb.ByteSize=8;
Dcb.Parity=NOPARITY;
Dcb.StopBits=ONESTOPBIT;
Dcb.fRtsControl=0;
Dcb.fParity=1;
SetCommState(hCom,&amp;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) &amp; 0x00FF;
}
else if(i>=10 &amp;&amp; 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) &amp; 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,&amp;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)&amp;0x00FF;
len=read[8];
read=ReadCOM(len+2);
read[len+2]='/0';
xy1=(xy1+xyprg(read,len))&amp;0x00FF;
if (read[len+1]==0x16 &amp;&amp; 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;

希望高手根据提供的原代码给予正确的指正!谢谢!
 
我用VC++做了个串口通讯的动态库,用DELPHI7进行调用。在设计的过程中发现以下问题:
动态库在WIN98下使用正常,但在WIN2000下串口只能被打开一次,再打开串口时提示错误。不知道问题出在什么地方。
动态库的原代码如下:
// InfraredCom.cpp : Defines the entry point for the DLL application.
//

#include &quot;stdafx.h&quot;
#include &quot;jshs.h&quot;
#include &quot;clock.h&quot;
#include &quot;dos.h&quot;
#include &quot;winuser.h&quot;
#include &quot;conio.h&quot;


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 &quot;C&quot; 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,&amp;Dcb);
Dcb.BaudRate=1200;
Dcb.ByteSize=8;
Dcb.Parity=NOPARITY;
Dcb.StopBits=ONESTOPBIT;
Dcb.fParity=1;
SetCommState(hCom,&amp;Dcb);
COMMTIMEOUTS Timeouts;
GetCommTimeouts(hCom,&amp;Timeouts);
Timeouts.ReadTotalTimeoutMultiplier=50;
Timeouts.ReadTotalTimeoutConstant=1500;
SetCommTimeouts(hCom,&amp;Timeouts);
return(0);
}


//Send data to com
extern &quot;C&quot; int PASCAL WriteCOM(char *aa,unsigned leng)
{
if (WriteFile(hCom,aa,leng,&amp;Num,NULL))
{
/*Sleep(100);*/
return(0);
}
else return(1);
}


//Recieve data from com
extern &quot;C&quot; char * PASCAL ReadCOM(unsigned leng)
{
int i=0;
for(i=0;i<255;i++)
BufferIn=NULL;
if(ReadFile(hCom,BufferIn,leng,&amp;Num,NULL))
{
return(BufferIn);
}
else return(&quot;$&quot;);
}



//Close com
extern &quot;C&quot; int PASCAL CloseCOM()
{
if (hCom==0)
{
return(3);
}
if(CloseHandle(hCom))
{
hCom=0;
return(0);
}
else
{
hCom=0;
return(1);
}
}


extern &quot;C&quot; int PASCAL ComOut(char *aa,int len1)
{
int retu=5,i=0,len=0,xy=0,cd;
char xy1;
char dd[2];
char *read=&quot;0&quot;,ch;
DCB Dcb;
GetCommState(hCom,&amp;Dcb);
Dcb.BaudRate=1200;
Dcb.ByteSize=8;
Dcb.Parity=NOPARITY;
Dcb.StopBits=ONESTOPBIT;
Dcb.fRtsControl=0;
Dcb.fParity=1;
SetCommState(hCom,&amp;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) &amp; 0x00FF;
}
else if(i>=10 &amp;&amp; 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) &amp; 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,&amp;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)&amp;0x00FF;
len=read[8];
read=ReadCOM(len+2);
read[len+2]='/0';
xy1=(xy1+xyprg(read,len))&amp;0x00FF;
if (read[len+1]==0x16 &amp;&amp; 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;

希望高手根据提供的原代码给予正确的指正!谢谢!
 
提示什么,CloseCom对了没有

另外当你的程序关闭时,用超级终端是否也不能打开,那就说明是串口没有个正确释放
 
www.51merit.com
上有成品!COM232全双工通信(SPCOMM D6),也是一个动态连接库!
而且在110kva电网系统中使用!
 
to 13708782004:你所提供的网址打不开啊。
 
后退
顶部