fari
asked on
serial interface
i write a monitoring program and i need to read permanent the rs232 port i must receive 4 byte data
on com1. I write fallowing code fragment for initialisation the com1 and send and receive function
but when i send 4 byte data sometimes its ok sometimes not and when i will receive data i receive
everytime 2 byte or 3 byte, what can be wrong with my code,
i send a command message packet to a targetsystem and must wait for answer, how can i synchronize
send and receive sequence, the target system need time to execute the command(about 400 ms).
thank you
void MainFrame::OnDeviceSerial( ) /*initialisation the serial interface*/
{
COMMCONFIG com;
COMMTIMEOUTS cto;
int ans;
CString cst;
CString dw_par;
CString cw_par;
CString si_par;
HANDLE hdl;
ans=0;
DCB dcbp;
MONITORINGApp* mainapp;
mainapp=(MONITORINGApp*)Af xGetApp();
hdl=mainapp->hcomm;
lk = CommConfigDialog( "COM1",GetSafeHwnd( ),&com);/* show the Serial interface dialog for COM2*/
if(hdl==0){
hdl=CreateFile("COM1",GENE RIC_READ|G ENERIC_WRI TE,0,NULL, OPEN_EXIST ING,FILE_A TTRIBUTE_N ORMAL,0);/ *open com2 for read and write*/
}
if(GetLastError()==ERROR_I NVALID_HAN DLE) AfxMessageBox("INVALIDE HANDLE");
memset(&cto,0,sizeof(cto)) ;/* init timeout structur for com2*/
// cto.ReadTotalTimeoutMultip lier=1; /****125*4=500 ms time for read operation*/
cto.WriteTotalTimeoutMulti plier=1;
SetCommTimeouts(hdl,&cto); /*set timeouts */
com.dcb.DCBlength=sizeof(c om.dcb);
DCB dcb3=com.dcb;
GetCommState(hdl,&dcb3);
SetCommState(hdl,&com.dcb) ;/*set the com parameter*/
GetCommState(hdl,&dcbp);/* get the com parameter*/
comok=1;
}
========================== ========== ========== ==
========================== ========== ========== ==
BOOL transfer::send_msg()
{
BOOL mark;
BOOL send_ok;
int byte_count;
byte_count=0;
send_ok=false;
//fwritestat=WriteFile(APP T()->hcomm ,&write_bu ffer,bytes _to_write, &bytes_wri ten,&oswri te);
for(int m=0; m<4; m++){
::Sleep(50);
fwritestat=WriteFile(APPT( )->hcomm,& write_buff er[m],1,&b ytes_write n,&oswrite );
if(bytes_writen) byte_count++;}
if(bytes_to_write==(DWORD) byte_count ) send_ok=true;
if(!fwritestat)
{
if(GetLastError()== ERROR_IO_PENDING)
{
while(!GetOverlappedResult (APPT()->h comm,&oswr ite,&bytes _writen,TR UE)){
dwError=GetLastError();
if(dwError==ERROR_IO_INCOM PLETE)
{
dw_bytes_sent+=bytes_write n;
continue;
}
else{
ClearCommError(APPT()->hco mm,&dwerro rflag,&com stat);
break;
}
}
dw_bytes_sent+=bytes_write n;
if(dw_bytes_sent!=bytes_to _write) return(FALSE);
}else{
ClearCommError(APPT()->hco mm,&dwerro rflag,&com stat);
return(FALSE);
}
}
if(send_ok) mark=true;
if(!send_ok) mark=false;
return(mark);
}
int transfer::receive_msg(LPST R lpszBlock, int nMaxLength)
{
BOOL fReadStat;
COMSTAT ComStat;
DWORD dwErrorFlags;
DWORD dwLength;
DWORD dwError;
//char szError[10];
OVERLAPPED osRead;
ClearCommError(APPT()->hco mm,&dwErro rFlags,&Co mStat);
dwLength=min((DWORD)nMaxLe ngth,ComSt at.cbInQue );
if(dwLength>0)
{
fReadStat=ReadFile(APPT()- >hcomm,&lp szBlock,dw Length,&dw Length,&os Read);
if(!fReadStat)
{
if(GetLastError()==ERROR_I O_PENDING)
{
while(!GetLastError()==(AP PT()->hcom m,&osRead, &dwLength, TRUE))
{
dwError=GetLastError();
if(dwError==ERROR_IO_INCOM PLETE)
continue;
else
{
ClearCommError(APPT()->hco mm,&dwErro rFlags,&Co mStat);
break;
}
}
}
else
dwLength=0;
ClearCommError(APPT()->hco mm,&dwErro rFlags,&Co mStat);
}
}
rec_data=lpszBlock;
return(dwLength);
}
on com1. I write fallowing code fragment for initialisation the com1 and send and receive function
but when i send 4 byte data sometimes its ok sometimes not and when i will receive data i receive
everytime 2 byte or 3 byte, what can be wrong with my code,
i send a command message packet to a targetsystem and must wait for answer, how can i synchronize
send and receive sequence, the target system need time to execute the command(about 400 ms).
thank you
void MainFrame::OnDeviceSerial(
{
COMMCONFIG com;
COMMTIMEOUTS cto;
int ans;
CString cst;
CString dw_par;
CString cw_par;
CString si_par;
HANDLE hdl;
ans=0;
DCB dcbp;
MONITORINGApp* mainapp;
mainapp=(MONITORINGApp*)Af
hdl=mainapp->hcomm;
lk = CommConfigDialog( "COM1",GetSafeHwnd( ),&com);/* show the Serial interface dialog for COM2*/
if(hdl==0){
hdl=CreateFile("COM1",GENE
}
if(GetLastError()==ERROR_I
memset(&cto,0,sizeof(cto))
// cto.ReadTotalTimeoutMultip
cto.WriteTotalTimeoutMulti
SetCommTimeouts(hdl,&cto);
com.dcb.DCBlength=sizeof(c
DCB dcb3=com.dcb;
GetCommState(hdl,&dcb3);
SetCommState(hdl,&com.dcb)
GetCommState(hdl,&dcbp);/*
comok=1;
}
==========================
==========================
BOOL transfer::send_msg()
{
BOOL mark;
BOOL send_ok;
int byte_count;
byte_count=0;
send_ok=false;
//fwritestat=WriteFile(APP
for(int m=0; m<4; m++){
::Sleep(50);
fwritestat=WriteFile(APPT(
if(bytes_writen) byte_count++;}
if(bytes_to_write==(DWORD)
if(!fwritestat)
{
if(GetLastError()== ERROR_IO_PENDING)
{
while(!GetOverlappedResult
dwError=GetLastError();
if(dwError==ERROR_IO_INCOM
{
dw_bytes_sent+=bytes_write
continue;
}
else{
ClearCommError(APPT()->hco
break;
}
}
dw_bytes_sent+=bytes_write
if(dw_bytes_sent!=bytes_to
}else{
ClearCommError(APPT()->hco
return(FALSE);
}
}
if(send_ok) mark=true;
if(!send_ok) mark=false;
return(mark);
}
int transfer::receive_msg(LPST
{
BOOL fReadStat;
COMSTAT ComStat;
DWORD dwErrorFlags;
DWORD dwLength;
DWORD dwError;
//char szError[10];
OVERLAPPED osRead;
ClearCommError(APPT()->hco
dwLength=min((DWORD)nMaxLe
if(dwLength>0)
{
fReadStat=ReadFile(APPT()-
if(!fReadStat)
{
if(GetLastError()==ERROR_I
{
while(!GetLastError()==(AP
{
dwError=GetLastError();
if(dwError==ERROR_IO_INCOM
continue;
else
{
ClearCommError(APPT()->hco
break;
}
}
}
else
dwLength=0;
ClearCommError(APPT()->hco
}
}
rec_data=lpszBlock;
return(dwLength);
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.