USART.READFLOAT problem

General discussion relating to the library modules supplied with the compiler

Moderators: David Barker, Jerry Messina

Post Reply
garryp4
Posts: 125
Joined: Mon May 21, 2007 7:18 am
Location: Loveland, CO USA

USART.READFLOAT problem

Post by garryp4 » Mon Jan 31, 2011 11:17 pm

I am trying to use the readfloat instruction but only get 0.0000 as a result.

Code: Select all

// device and clock...                                                                                                                                                                                                                                         
Device = 18F4620
Clock = 20

// import modules...
Include "usart.bas"
Include "convert.bas"

Dim
  red         As PORTE.2,              ' Red LED
  green       As PORTE.1              ' Green lED

Dim
  f1          As Float
  
'******************************************************************************
Private Sub Led()
  High(green)
  DelayMS(80)
  Low(green)
  High(red)
  DelayMS(80)
  Low(red)
End Sub  
'******************************************************************************

USART.SetBaudrate(br9600)             ' and baud rate
USART.ReadTerminator = #13

ADCON0 = $00                           ' Turn off internal A/D
ADCON1 = $0E
ADCON2 = $B2
OSCCON = $70                           ' Interanl clock to 8mhz
OSCTUNE = $C0                          ' 80 for 8mhz, C0 for 32mhz PLL 

Led

'******************************************************************************
MAIN:

  f1 = USART.ReadFloat
  DelayMS(3)
  USART.Write(FloatToStr(f1,5),10,13)
  DelayMS(2)
  GoTo main

The hterm display is 0.0000 after any 4 digits typed in. Any help is greatly appreciated.

Thanks

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

Any Help, really?

Post by 2brain » Tue Feb 01, 2011 5:11 am

I haven't tried your code but 2 things occur to me; the brackets seem strange and you are only reading 4 bytes but trying to display 6.

Maybe try: USART.Write((FloatToStr(f1,3)),10,13)

Jerry Messina
Swordfish Developer
Posts: 1469
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Post by Jerry Messina » Tue Feb 01, 2011 3:56 pm

The hterm display is 0.0000 after any 4 digits typed in
The USART.ReadFloat() routine reads four binary bytes from the serial port, and assumes that the four bytes are a valid BINARY floating point representation.

The numbers you type in Hyperterm are ASCII, so typing "1234" would send $31, $32, $33, and $34, which has about a 0% chance of being what you intended.

You need to read the bytes in as a string and then convert them to a float, something like the Convert.StrToDec() function does for integers. Unfortunately, there's no "StrToFloat()" routine in the libraries.

garryp4
Posts: 125
Joined: Mon May 21, 2007 7:18 am
Location: Loveland, CO USA

Post by garryp4 » Tue Feb 01, 2011 9:40 pm

Thanks for the reply. Just looked at the usart routine and all it is just as Jerry said. Wonder the the purpose of the instruction is?

Thanks

Jerry Messina
Swordfish Developer
Posts: 1469
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Post by Jerry Messina » Tue Feb 01, 2011 10:21 pm

I suppose if you had two pics talking "SF" to one another it might be useful.

The same goes with all the other USART.ReadXXXX() routines. They're all binary, so if you're talking to anything else you've really got to watch it since the data representation needs to match on both ends, which is probably a disaster waiting to happen.

I only ever use the ReadByte() routine, and just build everything from there. I suppose the string ones might be of some use, too.

Rickado
Registered User
Registered User
Posts: 17
Joined: Tue Apr 28, 2009 9:43 pm
Location: Dunedin, New Zealand

Post by Rickado » Sun Feb 06, 2011 1:42 am

Here's a function to try, it converts HyperTerm into Float

Code: Select all

{***************************************************************************
* HyperTermFlt  = TermFlt          // pCount = Num+Fix    No BackSpace                           
Converts a TerminalIn String value into a Float    Outputs WriteByte thru Tx
***************************************************************************}
//"0" = 48   "." = 46     Fix 6  max      count max 10
Public Function TermFlt(pFix As Byte=2, pCount As Byte=10)  As Float

 Dim Point, Neg    As Boolean                                                
 Dim Value, Value2, Count As Byte
 Dim Divisor       As Float
 
 Result = 0
 Count = 0
 Point = false
 Neg   = false
 
Repeat
NextValue:
  Value = ReadByte
  Value2 = Value - 48  
  If Value2 >= 10 Then
     If (Point = false) And (Value = Byte(".")) Then
        Write(Value)
        If pFix = 0 GoTo  ExitTest2
        Divisor = 1        
        Point = true
        GoTo NextValue
     EndIf
     If (Count=0) And (Value=Byte("-")) Then  Write(Value)  Neg = True   EndIf
     GoTo ExitTest
  EndIf
    
  Write(Value)
  If Point Then
     Divisor = Divisor*10   
     Result = Result +  (Value2/Divisor)       
     Dec(pFix)
     If pFix = 0  GoTo ExitTest2
  Else
     Result = (Result*10) + Value2
  EndIf
  Inc(Count)                
Until Count = pCount

ExitTest:  If Count = 0  GoTo NextValue
ExitTest2:  If Neg Then Result = -Result  EndIf
End Function

Post Reply