Page 1 of 1

Formatting USART.Write

Posted: Thu Aug 18, 2011 7:24 pm
by Francesco.C
Hi,

If I have:
.......................................
Dim myvar as Word

myvar=1234

USART.Write (DectoStr(myvar,1))
...................................................

I get '4' which is the last character in the string.

What do I have to write to get the first and/or second character in the string?

Thank you for your help.

Regards

Francesco

Posted: Fri Aug 19, 2011 2:19 am
by 2brain
I'm sure if you looked at the Help File you would understand what is really happening.

You have 'padded out' your decimal down to a single character. If you replace the '1' with 5 you should see '01234'.

If you want just your original number use:
USART.Write (DectoStr(myvar))

If all else fails Read the Manual!

Posted: Fri Aug 19, 2011 12:03 pm
by David Barker
There are many ways you could do this - this is one way (which assumes you want a string result). It should be quite easy to follow...

Code: Select all

Include "usart.bas"
Include "Convert.bas"

Function CharAt(pNum as word, pIndex as byte) as string(2)
   result = DecToStr(pNum)
   result = result(pIndex) + #0
end function

Dim myvar As Word
myvar=6789

USART.SetBaudrate(br19200)
USART.Write (CharAt(myvar, 2),13,10) 
The character access is zero indexed, so
0 = 6
1 = 7
2 = 8
3 = 9

Posted: Sun Aug 21, 2011 6:50 pm
by Francesco.C
Thanks David for the hint.
I have found my solution. like this:

---------------------------------------------------------
MyVar=1234
USART.Write(Mid(DecToStr(MyVar),0,2),10,13)
------------------------------------------------------

Using string function 'Mid' I can print any portion of may variable.

Regards

Francesco

Posted: Sun Aug 21, 2011 7:02 pm
by David Barker
Glad you found something - the "string.bas" module has a number of string manipulation routines which are always worth a look at...