fWriteState=WriteFile(hComm,buf,dwCharToWrite,&dwBytesWritten,NULL);//*sizeof(unsigned char)
if(!fWriteState)
{
AfxMessageBox(CString("can not write the data to port"));
}
delete[] buf;
}
BOOL CSerialPortDlg::OpenPort()
{
if(hComm==INVALID_HANDLE_VALUE)
{
hComm=CreateFile(PortID,GENERIC_READ|GENERIC_WRITE,0,0,OPEN_EXISTING,0,0);
if(hComm==INVALID_HANDLE_VALUE )
{
AfxMessageBox(CString("can not open the port! please check whether it has been used."));
return FALSE;
}
else
{
GetCommState(hComm,&dcb);
dcb.BaudRate=BaudRates[BaudRate];
dcb.ByteSize=DataBits+6;
dcb.Parity=Parity;
dcb.StopBits=StopBits;
dcb.fParity=FALSE;
dcb.fBinary=TRUE;
dcb.fDtrControl=0;
dcb.fRtsControl=0;
dcb.fOutX=dcb.fInX=dcb.fTXContinueOnXoff=0;
SetCommMask(hComm,EV_RXCHAR);
SetupComm(hComm,16384,16384);
if(!SetCommState(hComm,&dcb))
{
AfxMessageBox(CString("can not configure port with the current data!"));
PurgeComm(hComm,PURGE_TXCLEAR|PURGE_RXCLEAR);
CloseHandle(hComm);
hComm=INVALID_HANDLE_VALUE;
return FALSE;
}
else
{
GetCommTimeouts(hComm,&CommTimeOuts);