Formatting USART.Write

General discussion relating to the library modules supplied with the compiler

Moderators: David Barker, Jerry Messina

Post Reply
Francesco.C
Posts: 41
Joined: Thu Feb 26, 2009 6:54 pm
Location: UK

Formatting USART.Write

Post by Francesco.C » Thu Aug 18, 2011 7:24 pm

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

2brain
Posts: 18
Joined: Fri Jan 22, 2010 1:04 pm
Location: Hong Kong

Post by 2brain » Fri Aug 19, 2011 2:19 am

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!

User avatar
David Barker
Swordfish Developer
Posts: 1214
Joined: Tue Oct 03, 2006 7:01 pm
Location: Saltburn by the Sea, UK
Contact:

Post by David Barker » Fri Aug 19, 2011 12:03 pm

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

Francesco.C
Posts: 41
Joined: Thu Feb 26, 2009 6:54 pm
Location: UK

Post by Francesco.C » Sun Aug 21, 2011 6:50 pm

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

User avatar
David Barker
Swordfish Developer
Posts: 1214
Joined: Tue Oct 03, 2006 7:01 pm
Location: Saltburn by the Sea, UK
Contact:

Post by David Barker » Sun Aug 21, 2011 7:02 pm

Glad you found something - the "string.bas" module has a number of string manipulation routines which are always worth a look at...

Post Reply