男
男生111
Unregistered / Unconfirmed
GUEST, unregistred user!
大家好:
我刚才写了一个用COM1口自发自收的程序,但是测试的时候发现FILEWRITE报错,用SysErrorMessage(GetLastError())获取错误代码,发现报的是"重叠I/O正在操作"希望能够解释一下,我的代码是BCB6下写的,如下:
HANDLE hcom;
//定义句柄
DCB dcb;
OVERLAPPED e;
//定义重叠结构
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
char successflag=0;
///////// listen sql
listensql=new MyThread(true);
listensql->FreeOnTerminate=true ;
listensql->Resume();
////////////
hcom=CreateFile("COM1",GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED,NULL);
//打开通讯口
if(hcom==INVALID_HANDLE_VALUE)
exit;
successflag=SetupComm(hcom,512,512);
if(successflag==0){
exit;
}
successflag=GetCommState(hcom, &dcb ) ;
if(successflag==0){
exit;
}
dcb.BaudRate =9600;
dcb.ByteSize =8;
dcb.Parity = NOPARITY;
dcb.StopBits = ONESTOPBIT ;
successflag=SetCommState(hcom, &dcb ) ;
if(successflag==0){
exit;
}
e.hEvent=CreateEvent(NULL,true,false,NULL);
if(e.hEvent==NULL){
exit;
}
successflag=SetCommMask(hcom,EV_RXCHAR|EV_TXEMPTY) ;
if(successflag==0){
exit;
}
PurgeComm( hcom,PURGE_TXCLEAR| PURGE_RXCLEAR );
/*SetCommMask(hcom,EV_RXCHAR|EV_TXEMPTY);
SetupComm(hcom,512,512);
PurgeComm( hcom, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR| PURGE_RXCLEAR );
COMMTIMEOUTS CommTimeOuts ;
CommTimeOuts.ReadIntervalTimeout=20;
CommTimeOuts.ReadTotalTimeoutMultiplier=20;
CommTimeOuts.ReadTotalTimeoutConstant=20;
CommTimeOuts.WriteTotalTimeoutMultiplier=20;
CommTimeOuts.WriteTotalTimeoutConstant=20;
SetCommTimeouts( hcom, &CommTimeOuts ) ;
GetCommState(hcom, &dcb ) ;
dcb.BaudRate =9600;
dcb.ByteSize =8;
dcb.Parity = NOPARITY;
dcb.StopBits = ONESTOPBIT ;dcb.fBinary = TRUE ;dcb.fParity = FALSE;
SetCommState(hcom, &dcb ) ;*/
/*BuildCommDCB("9600,0,8,1",&dcb);
//第一个字符串参数实际使用时由图1选择后组合,这里仅简单说明其格式
SetCommState(hcom,&dcb);
SetupComm(hcom,512,512);//设置读写缓冲区
e.hEvent=CreateEvent(NULL,false,false,NULL);
//设置事件
SetCommMask(hcom,EV_RXCHAR|EV_TXEMPTY);
//设置事件掩码
*/
//e.hEvent=CreateEvent(NULL,false,false,NULL);
//设置事件
//SetCommMask(hcom,EV_RXCHAR|EV_TXEMPTY);
//设置事件掩码
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
DWORD wCount = 0;
char successflag;
AnsiString lll;
int sult;
char *buf="uuu";
DWORD length=4;
successflag=WriteFile(hcom, buf, length, &length, &e);
if(successflag==0){
//sult=GetLastError();
lll=SysErrorMessage(GetLastError());
}
}
我刚才写了一个用COM1口自发自收的程序,但是测试的时候发现FILEWRITE报错,用SysErrorMessage(GetLastError())获取错误代码,发现报的是"重叠I/O正在操作"希望能够解释一下,我的代码是BCB6下写的,如下:
HANDLE hcom;
//定义句柄
DCB dcb;
OVERLAPPED e;
//定义重叠结构
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
char successflag=0;
///////// listen sql
listensql=new MyThread(true);
listensql->FreeOnTerminate=true ;
listensql->Resume();
////////////
hcom=CreateFile("COM1",GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED,NULL);
//打开通讯口
if(hcom==INVALID_HANDLE_VALUE)
exit;
successflag=SetupComm(hcom,512,512);
if(successflag==0){
exit;
}
successflag=GetCommState(hcom, &dcb ) ;
if(successflag==0){
exit;
}
dcb.BaudRate =9600;
dcb.ByteSize =8;
dcb.Parity = NOPARITY;
dcb.StopBits = ONESTOPBIT ;
successflag=SetCommState(hcom, &dcb ) ;
if(successflag==0){
exit;
}
e.hEvent=CreateEvent(NULL,true,false,NULL);
if(e.hEvent==NULL){
exit;
}
successflag=SetCommMask(hcom,EV_RXCHAR|EV_TXEMPTY) ;
if(successflag==0){
exit;
}
PurgeComm( hcom,PURGE_TXCLEAR| PURGE_RXCLEAR );
/*SetCommMask(hcom,EV_RXCHAR|EV_TXEMPTY);
SetupComm(hcom,512,512);
PurgeComm( hcom, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR| PURGE_RXCLEAR );
COMMTIMEOUTS CommTimeOuts ;
CommTimeOuts.ReadIntervalTimeout=20;
CommTimeOuts.ReadTotalTimeoutMultiplier=20;
CommTimeOuts.ReadTotalTimeoutConstant=20;
CommTimeOuts.WriteTotalTimeoutMultiplier=20;
CommTimeOuts.WriteTotalTimeoutConstant=20;
SetCommTimeouts( hcom, &CommTimeOuts ) ;
GetCommState(hcom, &dcb ) ;
dcb.BaudRate =9600;
dcb.ByteSize =8;
dcb.Parity = NOPARITY;
dcb.StopBits = ONESTOPBIT ;dcb.fBinary = TRUE ;dcb.fParity = FALSE;
SetCommState(hcom, &dcb ) ;*/
/*BuildCommDCB("9600,0,8,1",&dcb);
//第一个字符串参数实际使用时由图1选择后组合,这里仅简单说明其格式
SetCommState(hcom,&dcb);
SetupComm(hcom,512,512);//设置读写缓冲区
e.hEvent=CreateEvent(NULL,false,false,NULL);
//设置事件
SetCommMask(hcom,EV_RXCHAR|EV_TXEMPTY);
//设置事件掩码
*/
//e.hEvent=CreateEvent(NULL,false,false,NULL);
//设置事件
//SetCommMask(hcom,EV_RXCHAR|EV_TXEMPTY);
//设置事件掩码
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
DWORD wCount = 0;
char successflag;
AnsiString lll;
int sult;
char *buf="uuu";
DWORD length=4;
successflag=WriteFile(hcom, buf, length, &length, &e);
if(successflag==0){
//sult=GetLastError();
lll=SysErrorMessage(GetLastError());
}
}