串口負責采集一維資料,想每隔一段時間采集一次,并實時保存到.txt檔案中,我自己按照教程寫了程式,但是發現可以建立檔案,但是只能保存一組資料,求大神幫助
uj5u.com熱心網友回復:
你這是要點一次保存一次嗎?是的話每點一次,你就創建個新檔案,把原來的洗掉了,所以只有一包,你應該先判斷有沒有檔案存在,沒有則創建,有的話在檔案末尾追加。不是點一次保存一次的話應該用回圈方式,或者開一個計時器,每隔一段時間保存一次。
uj5u.com熱心網友回復:
CFile::modeWrite意思是寫檔案,CFile::modeCreate意思是創建一個新檔案。uj5u.com熱心網友回復:
CFile::modeCreate | CFile::modeNoTruncate 不要重置長度SeekToEnd 到末尾再寫入
uj5u.com熱心網友回復:
3樓正解!!!uj5u.com熱心網友回復:
謝謝您,新手上路,還是老司機厲害??uj5u.com熱心網友回復:
您好,問題已經解決,可以保存,但是我現在想像呼叫子程式一樣呼叫串口,采集資料,并保存,一直不知道怎么寫,求大牛幫忙????uj5u.com熱心網友回復:
參考:
// use ON_MESSAGE(a,b)
void CUload::OnCommNotify(WPARAM wParam,LPARAM lParam)
{
// TODO: Add your specialized code here and/or call the base class
int nLength;
BYTE abIn[6000];// for 115200 needs 4096 and more
DWORD totalrecv;
CString txt;
switch(wParam)
{
case 0:// from CommWatchCTS()
SetCTSIcon();
SetEvent(m_hPostEventRead);// tell watcher that CommNotify has been done
break;
case 1:// from CommWatchRead()
// append on each entry
CFile uldFile(UpLoadFileName,CFile::modeCreate|CFile::modeNoTruncate|CFile::modeWrite);
uldFile.SeekToEnd( );
if (m_bConnected)
{
if(nLength=ReadCommBlock(abIn,BUFLEN))// 38400 needs 1024
{ // save into file
if (!m_bStart)
{ // only 1st
m_bStart=1;
InforOut("Receiving "+UpLoadFileName);
}
uldFile.Write(abIn,nLength);
}
}
uldFile.Close();
// get total bytes received
m_uldINs.GetText(0,txt);// CListBox m_uldINs;
totalrecv=atol(txt.GetBuffer(40));
totalrecv+=nLength;
txt.Format("%d",totalrecv);
m_uldINs.ResetContent();
m_uldINs.AddString(txt);
if (nLength>128)
{
Animate();
// reset "time waiting"
m_Elapse.ResetContent();
m_Elapse.AddString("0 s");
}
// tell watcher that CommNotify has been done
SetEvent(m_hPostEventRead);// volatile HANDLE m_hPostEventRead;
break;
}
}
///////////////////////////////////////////////////////
int CUload::ReadCommBlock(BYTE *pBlock,int nBlockLen)
{ //return 0 if error
int WaitErr;
BOOL fReadStart;
COMSTAT ComStat;
DWORD dwErrorFlags,dwLength;
if (!m_bConnected) return 0;
if (ClearCommError(m_idComDev,&dwErrorFlags,&ComStat))
{ //ComStat filled
if (dwErrorFlags)
{
if (dwErrorFlags & CE_RXOVER) AfxMessageBox("Receive Queue overflow");
else if(dwErrorFlags & CE_OVERRUN) AfxMessageBox("Receive Overrun Error");
else if(dwErrorFlags & CE_RXPARITY) AfxMessageBox("Receive Parity Error");
else if(dwErrorFlags & CE_FRAME ) AfxMessageBox("Receive Framing error");
else if(dwErrorFlags & CE_BREAK) AfxMessageBox("Break Detected");
else AfxMessageBox("CE_OTHERS");
}
}
// nBlockLen may >,=,< ComStat.cbInQue !
dwLength=min((DWORD)nBlockLen,ComStat.cbInQue);
if(dwLength>0)
{ // read required
fReadStart=ReadFile(m_idComDev,pBlock,dwLength,&dwLength,&m_osRead);
if(!fReadStart)
{ // if there was a problem, or the async. operation's still pending ...
if(GetLastError()==ERROR_IO_PENDING)
{ // asynchronous i/o is still in progress
if (WaitErr=WaitForSingleObject(m_osRead.hEvent,60000))// 1 minute
{ // time over
if(WaitErr==WAIT_TIMEOUT)
{ // time out
dwLength=0;
AfxMessageBox("Time out !");
}// end time out
}// end waiterr
} // end io_pending
else
{ // WaitErr=0.if you SetEvent(m_osRead.hEvent) anywhere else
GetOverlappedResult(m_idComDev,&m_osRead,&dwLength,FALSE);
m_osRead.Offset +=dwLength;
}
}
} //end if dwLength>0
return dwLength;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/57507.html
標籤:硬件/系統
