kseybold
Posts: 18
Joined: Tue Oct 31, 2006 5:47 pm

MFC & binary data problem

Tue Oct 31, 2006 5:51 pm

Hello,

we are evaluating your Serial Tool with our
MFC Application.

At this point we compiled the supplied sample and trying to get it working to receive/send some binary data from/to an application. The sample already does 90% of what we need.
Unfortunatly I can't figure out how to get binary data out of the VARIANT*. The doc says I need to use the StringToByteArray function, but I don't know how.
Do you have an extented example or can you send me the needed code snippet?

Thanks for your help

K. Seybold

User avatar
johan
GpsGate
Posts: 14356
Joined: Wed Aug 04, 2004 10:40 pm
Location: Sweden
Contact: Website

RE: MFC & binary data problem

Tue Oct 31, 2006 5:58 pm

Let's assume p_var is a pointer to the VARIANT

Code: Select all

	BSTR bstr = NULL;

	switch(p_var->vt)
	{
	case VT_BSTR:
		bstr = p_var->bstrVal;
		break;

	case (VT_BSTR | VT_BYREF):
		bstr = *p_var->pbstrVal;
		break;

	case VT_NULL:
		bstr = NULL;
		break;

	}

	int len = bstr ? SysStringLen(bstr) : NULL;
	
	char* pData = new char[len];
	
	for(int iIndex = 0; iIndex < 0; iIndex++)
	{
		pData[iIndex] = (char) bstr[iIndex];
	}
	
	// You got your binary data in pData now!
Regards,
Johan

Franson Support

kseybold
Posts: 18
Joined: Tue Oct 31, 2006 5:47 pm

RE: MFC & binary data problem

Tue Oct 31, 2006 6:22 pm

Hello Johan,

thanks for the quick reply.

the problem is that in my case the value of

Code: Select all

p_var->vt
is

Code: Select all

VT_VARIANT | VT_BYREF
(0x400c) and this is not handles by your snippet.

Regards
Kalle

User avatar
johan
GpsGate
Posts: 14356
Joined: Wed Aug 04, 2004 10:40 pm
Location: Sweden
Contact: Website

RE: MFC & binary data problem

Tue Oct 31, 2006 6:30 pm

Code: Select all

	BSTR bstr = NULL;

	// If variant is pointing to another variant
	if(p_var->vt == (VT_VARIANT | VT_BYREF))
	{
		p_var = p_src->pvarVal;
	}

	switch(p_var->vt)
	{
	case VT_BSTR:
		bstr = p_var->bstrVal;
		break;

	case (VT_BSTR | VT_BYREF):
		bstr = *p_var->pbstrVal;
		break;

	case VT_NULL:
		bstr = NULL;
		break;

	}

	int len = bstr ? SysStringLen(bstr) : NULL;
	
	char* pData = new char[len];
	
	for(int iIndex = 0; iIndex < 0; iIndex++)
	{
		pData[iIndex] = (char) bstr[iIndex];
	}
	
	// You got your binary data in pData now!


Regards,
Johan

Franson Support

kseybold
Posts: 18
Joined: Tue Oct 31, 2006 5:47 pm

RE: MFC & binary data problem

Wed Nov 01, 2006 2:39 pm

Hello Johan,

reading is now working.

But I got the same problem with writing binay data.
If the BST contains a 0x00 it dosn't work.

Thanks for your help
Kalle

User avatar
johan
GpsGate
Posts: 14356
Joined: Wed Aug 04, 2004 10:40 pm
Location: Sweden
Contact: Website

RE: MFC & binary data problem

Wed Nov 01, 2006 2:47 pm

Make sure you create a BSTR with the correct length.

Regards,
Johan

Franson Support

kseybold
Posts: 18
Joined: Tue Oct 31, 2006 5:47 pm

RE: MFC & binary data problem

Wed Nov 01, 2006 4:41 pm

Hello Johan,

well it works if the string dosen't contain a 0x00.
otherwise it will only transfer to the first 0x00....

sample 1:

Code: Select all

	CComBSTR cBstr(buf_len+1,buf);

	cBstr[0]=0x01;
	cBstr[1]=0x02;
	cBstr[2]=0x03;
	cBstr[3]=0x04;

	_variant_t data((BSTR)cBstr);
	_variant_t ret_val;

	m_hres = m_IVPort->DataToPort(data, &ret_val);
        
        // OK, 01 02 03 04 are sent to the COM Port

sample 2:

Code: Select all

	CComBSTR cBstr(buf_len+1,buf);

	cBstr[0]=0x01;
	cBstr[1]=0x02;
	cBstr[2]=0x00;
	cBstr[3]=0x04;

	_variant_t data((BSTR)cBstr);
	_variant_t ret_val;

	m_hres = m_IVPort->DataToPort(data, &ret_val);
        
        // ERROR, only 01 02 are sent to the COM Port



Any ideas?

Regards

Kalle

User avatar
johan
GpsGate
Posts: 14356
Joined: Wed Aug 04, 2004 10:40 pm
Location: Sweden
Contact: Website

RE: MFC & binary data problem

Wed Nov 01, 2006 5:33 pm

Maybe _variant_t changes the length of the BSTR.

Try the Win32 methods instead.

BSTR unicode = SysAllocStringLen(NULL, m_len);

etc...

Regards,
Johan

Franson Support

kseybold
Posts: 18
Joined: Tue Oct 31, 2006 5:47 pm

RE: MFC & binary data problem

Wed Nov 01, 2006 5:43 pm

Sorry, doesn't work. Same result.
Binary data are just transfered up to the first 0x00..

Reagrds

Kalle

User avatar
johan
GpsGate
Posts: 14356
Joined: Wed Aug 04, 2004 10:40 pm
Location: Sweden
Contact: Website

RE: MFC & binary data problem

Wed Nov 01, 2006 5:49 pm

VARIANT var;
BSTR bstr = SysAllocStringLen(NULL, m_len);

// init BSTR
bstr[0] = 0;
...

VariantInit(&var);
var.vt = VT_BSTR;
var.bstrVal = bstr;

m_IVPort->DataToPort(var, &ret_val);

Regards,
Johan

Franson Support

claudiofeijao
Posts: 1
Joined: Wed Jan 20, 2010 5:47 pm
Location: Brazil

RE: MFC & binary data problem

Wed Jan 20, 2010 5:56 pm

I'm having the same problem, but I use the C # language. Is there any solution for the transmission of value 0x00 ?

Return to “SerialTools”