int Init_Comm()
{
CHAR port_name[10];
DCB dcb;
BOOL status;
DWORD error_number;
status = OK;
memset(port_name,0,sizeof(port_name));
memcpy(port_name,"COM1",4);
handle_com = CreateFile(port_name,
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
NULL,
NULL);
if(handle_com == INVALID_HANDLE_VALUE)
return(COMM_ERROR);
if(!GetCommState(handle_com,&dcb))
return(COMM_ERROR);
dcb.BaudRate = 9600;
dcb.ByteSize = 8;
dcb.Parity = NOPARITY;
dcb.StopBits = ONESTOPBIT;
dcb.fOutxCtsFlow = 0;
dcb.fRtsControl = RTS_CONTROL_ENABLE ;
if( !SetCommState(handle_com, &dcb) )
{
error_number = GetLastError();
status = COMM_ERROR;
}
if(status != OK)
CloseHandle(handle_com);
return(status);
}
....................................................................................................................................................
if( WriteFile(handle_com,buffer,length,&dwBytesWrite,NULL) == TRUE)
{
if(dwBytesWrite != length)
{
strMsg.Format("发送失败",NULL,NULL);
AfxMessageBox(strMsg,MB_OK);
}
}
......................................................................................................................................................
if(!(status=ReadFile(handle_com,&Recv_Buffer[i],1,&dwBytesRead,NULL)))
{
PurgeComm(handle_com,PURGE_RXCLEAR);
return(ABANDON);
}
本文地址:http://com.8s8s.com/it/it49.htm