Click here to Skip to main content
16,004,833 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi i am using vc++ 6.0 MSCOMM for serial communication i have to read bytes and from serial port.
example :
88 84 10 02 (4 bytes data from port)
C6 1E 10 02 (Received Data) but i have to display 88 84 but i am getting C6 and 1E because 88 84 are the extended ASCII Codes which i am unable to read please help.
my code is:

VARIANT data;
BSTR k;
static char dbuff[4];
int dcount=0;
CString data;

if(m_mscom.GetCommEvent()==2){ // Receiving data from port
	data          = m_mscom.GetInput();
	k             = data.bstrVal;
	dbuff[dcount] = char (k[0]);
	dcount++;
	if(dcount == 4){
		dcount=0;
		data.Format ("%02X%02X%02X%02X",(unsigned char)dbuff[0],(unsigned char)dbuff[1],(unsigned char)dbuff[2],(unsigned char)dbuff[3]);
	}
}
Posted
Updated 9-Jan-11 23:53pm
v3
Comments
[no name] 10-Jan-11 5:51am    
Added code formatting for you with the pre-tag.
Espen Harlinn 10-Jan-11 5:56am    
From your question it sounds like you are getting data - it's just not what you expected. Is this right?
shiv@nand 10-Jan-11 6:02am    
yes because i have to read Extended ASCII codes i dont know how to read.

You asked a similar question a few days ago, and got the suggestion[^] to not use CString and char variables; did you try that?
 
Share this answer
 
Comments
shiv@nand 10-Jan-11 6:00am    
no i dont know how to read without char and CString please give me the example.
[no name] 10-Jan-11 6:20am    
You could turn char dbuff[4] into byte dbuff[4] and then use printf to write them to screen.
data.bstrVal returns a BSTR - 16-bit wide utf-16

What's this: dbuff[dcount] = char (k[0]);
I read it as: dbuff[0] = char (k[0]); you are setting one byte sized char to the 8 least significant bits of the first unicode character in the BSTR assigned to k.

or there is a lot of code missing ....

Update
TurboPower Async Professional[^] can be used with C++ builder. There are lots of examples and good docs.

Also try Thinking in C++ 2nd Edition[^] by Bruce Eckel, Free Electronic Book
Volume 1 & Volume 2



Regards
Espen Harlinn
 
Share this answer
 
v2
Comments
shiv@nand 10-Jan-11 6:06am    
what is the wrong , how it can be corrected?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900