1. 程式人生 > >MFC中根據登錄檔獲取串列埠

MFC中根據登錄檔獲取串列埠

HANDLE hComm = CreateFile(Com_name, GENERIC_READ | GENERIC_WRITE, 0, 
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);


//sett DCB 
DCB dcb;


//Initial DCB
DefaultDCB(&dcb);


//get the default setting
GetCommState(hComm, &dcb);


//modify the setting parameters
dcb.BaudRate = 9600;
dcb.ByteSize = 8;
dcb.StopBits = 0;
dcb.Parity = EVENPARITY;//2;//偶校驗


if(!SetCommState( hComm, &dcb ))
{
//not success, close the handle
CloseHandle(hComm);
return ;
}


//setup the serial com's buffer sizes:RX_QUEU = 1024 TX_QUEU = 512
SetupComm(hComm, 1024, 512);


//set communication mask
SetCommMask(hComm, EV_RXFLAG | EV_RXCHAR | EV_BREAK | EV_TXEMPTY);
// clear the com buffer
PurgeComm(hComm, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR);
//set timeout construct

COMMTIMEOUTS CommTimeOuts;
//calculate method: ReadTotalTimeout = (ReadTotalTimeoutMultiplier * bytes_to_read)+ ReadToTaltimeoutConstant

CommTimeOuts.ReadIntervalTimeout = MAXDWORD;

CommTimeOuts.ReadTotalTimeoutMultiplier = 0;

CommTimeOuts.ReadTotalTimeoutConstant = 0;
//calculate mothod :WriteTotalTimeout = (WriteTotalTimeoutMultiplier * bytes_to_write)+ WriteToTaltimeoutConstant

CommTimeOuts.WriteTotalTimeoutMultiplier = 2 * CBR_9600 / dcb.BaudRate;

CommTimeOuts.WriteTotalTimeoutConstant = 25;

//set
SetCommTimeouts(hComm, &CommTimeOuts);


m_hComm = hComm;
is_com_open = TRUE;
AfxMessageBox(_T("開啟成功"));


GetDlgItem(IDC_BUTTON3)->SetWindowText("關閉");