我用mscomm控制元件獲取到北斗模塊上的字串,但是我怎么樣也截取不到需要的資訊。請各位大大幫忙?
代碼如下
void Cuse_mscommDlg::OnCommMscomm1()
{
static unsigned int cnt=0;
VARIANT variant_inp;
COleSafeArray safearray_inp;
long len,k;
byte data[2048]={0};
CString strtemp;
if(m_mscomm.get_CommEvent()==2){
cnt++;
variant_inp = m_mscomm.get_Input();
safearray_inp = variant_inp;
len=safearray_inp.GetOneDimSize();
for(k=0; k<len; k++){
safearray_inp.GetElement(&k,data+k);
}
//清空快取
m_mscomm.put_OutBufferCount(0);
m_mscomm.put_InBufferCount(0);
safearray_inp.Clear();
for(k=0; k<len; k++){
byte a = * (char*)(data+k);
strtemp.Format(_T("%c"),a);
m_receive += strtemp;
}
UpdateData(false);
}
}
m_mscomm是控制元件變數名,m_receive是接收框的變數名。得到結果如下:

我想是判斷這樣解決的,但是就是得不到想要的結果,
for(k=0; k<len; k++){
byte a = * (char*)(data+k);
strtemp.Format(_T("%c"),a);
stringtemp2 += strtemp;
}
int leth = stringtemp2.GetLength();
while(!strtemp.IsEmpty()){
int inttemp = stringtemp2.Find('$',stringtemp2.Find('*')+2);
m_receive= stringtemp2.Left(stringtemp2.Find('*')+2);
stringtemp2.Mid(inttemp+1,len-inttemp);
}想著用stringtemp2存strtemp的資料,再對它進行遍歷,匹配對應的字串然后截取下來,但是這段代碼運行后控制元件直接卡死了
除錯時發現stringtemp2沒獲取到整個的字串

有什么辦法可以截取到想要的字串嗎?是不是可以直接對data陣列操作?
uj5u.com熱心網友回復:
CString::GetBufferLPTSTR GetBuffer( int nMinBufLength );
throw( CMemoryException );
Return Value
An LPTSTR pointer to the object’s (null-terminated) character buffer.
Parameters
nMinBufLength
The minimum size of the character buffer in characters. This value does not include space for a null terminator.
Remarks
Returns a pointer to the internal character buffer for the CString object. The returned LPTSTR is not const and thus allows direct modification of CString contents.
If you use the pointer returned by GetBuffer to change the string contents, you must call ReleaseBuffer before using any other CString member functions.
The address returned by GetBuffer may not be valid after the call to ReleaseBuffer since additional CString operations may cause the CString buffer to be reallocated. The buffer will not be reallocated if you do not change the length of the CString.
The buffer memory will be freed automatically when the CString object is destroyed.
Note that if you keep track of the string length yourself, you should not append the terminating null character. You must, however, specify the final string length when you release the buffer with ReleaseBuffer. If you do append a terminating null character, you should pass –1 for the length to ReleaseBuffer and ReleaseBuffer will perform a strlen on the buffer to determine its length.
Example
The following example demonstrates the use of CString::GetBuffer.
// example for CString::GetBuffer
CString s( "abcd" );
#ifdef _DEBUG
afxDump << "CString s " << s << "\n";
#endif
LPTSTR p = s.GetBuffer( 10 );
strcpy( p, "Hello" ); // directly access CString buffer
s.ReleaseBuffer( );
#ifdef _DEBUG
afxDump << "CString s " << s << "\n";
#endif
CString Overview | Class Members | Hierarchy Chart
See Also CString::GetBufferSetLength, CString::ReleaseBuffer
uj5u.com熱心網友回復:
沒看懂樓主要干什么,如果得到的字串是正確的,那要扁要圓還不是自己代碼處理的事情?這種格式的字串如果c/c++一次掃描運用不熟練的話,建議按逗號分隔成一個vector,然后按下標也就是列來處理,估計你會輕松些
uj5u.com熱心網友回復:
主要是字符陣列的處理,需要根據協議格式,截取對應的字符,然后再截取,轉換字串等處理轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/101195.html
標籤:基礎類
上一篇:activex安裝程序中會重繪,下載2次才彈出安裝提示框怎么解決
下一篇:oepncv識別
