RS232串口问题
对于一个打开的串口,连续接收同一数据包,造成内存不断上涨,有什么办法可以释放因接收数据包造成不断上涨的内存? --------------------编程问答-------------------- 是什么样子的啊?是一次接收很大的数据,还是每次数据少但次数多啊?我以前做过一个 短信模块的,通过串口来的数据是小而次数多,所以就先把它们存到数据库了,再从数据库里读出来。不知道你的是什么样的。 --------------------编程问答-------------------- 接受数据包难道你采用一个数组或者ArrayList等形式一致保存下来?如果是这样的话,肯定是涨内存的在我的处理中,接收到的数据放在缓冲区,如果已经处理完毕,则清除这部分的数据,内存就不怎么涨了
--------------------编程问答-------------------- 回zhangjie_xiaoke :
大概每次数据几十个byte,次数多 --------------------编程问答-------------------- cnming :
我是采用一个静态string变量保存接收到的数据,这样可行吗? --------------------编程问答-------------------- 线程+缓存 处理方式
--------------------编程问答-------------------- 缓存方法能给出代码吗,谢谢 --------------------编程问答-------------------- 再次顶起,希望能有高手给出代码 --------------------编程问答-------------------- 静态string变量
当然要涨了…… --------------------编程问答-------------------- 缓存的意义在于这段数据处理之后就没有用了,必须从缓存清除出去,静态string恐怕不好清除把,用一个可变数组比较好,例如ArrayList,每一组指令放在ArrayList的一个Item中 --------------------编程问答-------------------- #define MAX 1048575
#define BUFFERLEN 5242880
void CBulkusbDlg::OnButtonWritestart()
{
// TODO: Add your control notification handler code here
// TODO: Add your control notification handler code here
CString pipename;
if(m_combo_bulkwritepipe.GetCurSel()==CB_ERR)
{
MessageBox("请选择要操作的管道","警告");
return;
}
UpdateData(true);
m_BulkWrite.bInThread=TRUE;
m_BulkWrite.testDlg=this;
m_BulkWrite.buffersize=m_nEditWriteBufferSize;
m_combo_bulkwritepipe.GetLBText(m_combo_bulkwritepipe.GetCurSel(),pipename);
strcpy(m_BulkWrite.pipe_name,(LPCTSTR)pipename);
m_writetransfer_info.averspeed=0;
UpdateData(false);
EnableButton(false,true);
SetTimer(1,1000,NULL);
CWinThread * wt = AfxBeginThread(
BulkWritePipe, // thread function
&m_BulkWrite); // argument to thread function
m_BulkWrite.hThread=wt->m_hThread;
}
void CBulkusbDlg::OnButtonWritestop()
{
// TODO: Add your control notification handler code here
// TODO: Add your control notification handler code here
m_BulkWrite.bInThread=FALSE;
EnableButton(true,false);
KillTimer(1);
m_calwritetime.bInThread=FALSE;
}
UINT BulkWritePipe(void * pParam)
{
char temp [80],cc[80];
BOOLEAN bResult = FALSE;
int nResult=0;
unsigned long nBytes = 0;
unsigned long nReadBytes=0;
int nflag=0,nflag1=0;
unsigned long totaltoTx=0, toTxNow=0, exitFlag=0,exitFlag1=0,toTxNow1=0,totaltoTx1=0;
struct CThreadParam * threadParam;
CBulkusbDlg* testDlg;
HANDLE hDevice;
HANDLE hFile;
unsigned char *pbRead = new unsigned char[1048575];
threadParam=(struct CThreadParam *)pParam;
testDlg = threadParam->testDlg;
hDevice = ISP1581_OpenDev();
if(hDevice == INVALID_HANDLE_VALUE)
{
strcpy(temp, "Open device handle failed! Can't write to ");
strcat(temp, threadParam->pipe_name);
strcat(temp, ".");
testDlg->MessageBox((LPCSTR)temp, "Test Error");
testDlg->m_calwritetime.bInThread=FALSE;
return 0;
}
hFile = ISP1581_OpenPipe(threadParam->pipe_name);
if(hFile == INVALID_HANDLE_VALUE)
{
strcpy(temp, "Open driver failed! Can't write to ");
strcat(temp, threadParam->pipe_name);
strcat(temp, ".");
testDlg->MessageBox((LPCSTR)temp, "Test Error");
testDlg->m_calwritetime.bInThread=FALSE;
ISP1581_CloseDev(hDevice);
return 0;
}
HANDLE hFiles = CreateFile("c:\\temp\\large_file.dat",GENERIC_READ,0,
NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED|FILE_ATTRIBUTE_NORMAL,NULL);
unsigned long filelen=SetFilePointer(hFiles,0,NULL,FILE_END);
curtime=timeGetTime();
if(filelen>0)
do {
exitFlag=0;
if(nflag==0)
totaltoTx = filelen;
do
{
if(totaltoTx>BUFFERLEN)
{
totaltoTx = totaltoTx -BUFFERLEN;
toTxNow =BUFFERLEN;
filelen=totaltoTx;
}
else
{
toTxNow = totaltoTx;
exitFlag=1;
}
nResult=ISP1581_Vender_ScanOrPrint(hDevice,0x80,toTxNow);
if(nResult!=0)
{
threadParam->bInThread = FALSE;
wsprintf (cc, "Requesting data transfer on ");
strcat( cc, threadParam->pipe_name);
strcat( cc, " failed! Aborted.");
testDlg->MessageBox((LPCSTR)cc, "Test Error");
break;
}
unsigned long filelen1=toTxNow;
if(filelen1>0)
do{
exitFlag1=0;
if(nflag1==0)
totaltoTx1 = filelen1;
if(totaltoTx1>MAX)
{
totaltoTx1 = totaltoTx1 -MAX;
toTxNow1 =MAX;
filelen1=totaltoTx1;
}
else
{
toTxNow1 = totaltoTx1;
exitFlag1=1;
}
nReadBytes=ISP1581_Read(hFiles,pbRead,toTxNow1,1000);
if(nReadBytes==-1||nReadBytes==0)
break;
nBytes=ISP1581_Write(hFile,pbRead,toTxNow1,1500);
if(nBytes==-1||nBytes==0)
break;
}while(exitFlag1==0);
}while(exitFlag==0);
tertime=timeGetTime();
usetime=tertime-curtime;
testDlg->UpdateWriteInfo(usetime);
threadParam->bInThread = FALSE;
testDlg->m_calwritetime.bInThread=FALSE;
} while(threadParam->bInThread == TRUE);
testDlg->EnableButton(true,false);
delete pbRead;
ISP1581_ClosePipe(hFile);
ISP1581_CloseDev(hDevice);
return 0;
}
unsigned long __stdcall ISP1581_Write(HANDLE pipehandle,unsigned char *sendbuffer,unsigned long bufferlen,int waittime)
{
unsigned long nbytes=0,lasterror=0;
int i=0;
unsigned long tmpbufferlen=bufferlen;
OVERLAPPED os;
memset(&os,0,sizeof(OVERLAPPED));
os.hEvent=::CreateEvent(NULL,TRUE,FALSE,NULL);
if(!WriteFile(pipehandle,&sendbuffer[i],bufferlen,&nbytes,&os))
{
if((lasterror=::GetLastError())==ERROR_IO_PENDING)
{
switch(::WaitForSingleObject(os.hEvent,(DWORD)waittime))
{
case WAIT_OBJECT_0:
if(!::GetOverlappedResult(pipehandle,&os,&nbytes,FALSE))
{
lasterror=::GetLastError();
}
else
lasterror=0;
break;
case WAIT_TIMEOUT:
::CancelIo(pipehandle);
lasterror=ERROR_TIMEOUT;
break;
default:
lasterror=::GetLastError();
break;
}
}
}
if(lasterror!=0)
{
ResetPipe(pipehandle);
}
CloseHandle(os.hEvent);
if(lasterror!=0)
return -1;
return nbytes;
}
c++测试USB的程序原理一样的。
补充:.NET技术 , C#