Connecting Tech Pros Worldwide Forums | Help | Site Map

About system( "mode com1 ...

Newbie
 
Join Date: Aug 2008
Posts: 4
#1: Aug 30 '08
I use system( "mode com1 : baud=9600 parity=n data=8 stop=1 to=off xon=off") to turn on com1 and send some data to MCU. Could anyone tell me how I can retrieve data from MCU without using C++ or MFC class or code?

Another que: Why the data showed in UltraEdit such as " 06 00 5b c6" is showed "06 00 ffffff5b ffffffc6" when using printf and %x ?

With great thanks!

Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,200
#2: Aug 30 '08

re: About system( "mode com1 ...


Look up the functions

CreateFile
WriteFile
ReadFile
BuildCommDCB
SetCommState

on MSDN (search in Google it's often the top result and Google search is better than MSDN site search)
Newbie
 
Join Date: Aug 2008
Posts: 4
#3: Aug 30 '08

re: About system( "mode com1 ...


Quote:

Originally Posted by Banfa

Look up the functions

CreateFile
WriteFile
ReadFile
BuildCommDCB
SetCommState

on MSDN (search in Google it's often the top result and Google search is better than MSDN site search)

Thank you!
Do you have any suggestion on the second question?
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,200
#4: Aug 30 '08

re: About system( "mode com1 ...


Quote:

Originally Posted by dragon123456789

Another que: Why the data showed in UltraEdit such as " 06 00 5b c6" is showed "06 00 ffffff5b ffffffc6" when using printf and %x ?

This looks like sign extension to me. When you have a negative value store in a variable in 2s compliment format ( which is what most computers use) the top bit is effectively the sign bit, set for negative numbers clear for positive numbers.

If you convert a signed integer type to another signed integer type with more precision, say signed char to signed int (taking ints as 4 bytes for this example) then if you just copied the bits from one type to the other the following would happen.

signed char sc = -1; // bit pattern 0xFF

copying to a signed int without sign extension would result in the bit pattern 0x000000FF or the value 255, not the desired result. Sign extension involves copying the value to the top bit of the lower precision value to all the upper bits of higher precision value so since the top bit of 0xFF is 1 the value 1 is copied to all the higher bits of the unsigned int resulting in the value 0xFFFFFFFF or the value -1.

This doesn't happen for unsigned variables as there is no sign bit, all the bits are value bits. However if you have values that should be unsigned but are held in signed variables if you convert them to a higher precision value you get unwanted signed extension.


When you call printf with its varidac function prototype the compiler does not know how to put the variables onto the stack for passing to the function. In this case all integer type variables with a lower precision than an int are automatically converted to int before they are placed on the stack.

I am guessing that you read the data from your file into a char (which is probably signed by default), then when you pass it to printf it automatically gets converted to an int resulting in unwanted sign extension.

You should either read the data in unsigned char if the data really is unsigned or you can fudge the printf call by casting the data to an unsigned char or unsigned int explicitly which will prevent the signed extension.
Reply