Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

Thermometer Reading, 3rd App using VB


  • Please log in to reply
10 replies to this topic

#1 BernardG

BernardG

    Advanced Member

  • Members
  • PipPipPip
  • 34 posts

Posted 12 June 2011 - 08:58 AM

Here is my little contribution to show a little more VB code on Netduino. I would not have made it work without this help explaining how to connect 3.3v to Aref to get proper values..

The little twist I added is just to use an event to send the result to be displayed by Debug.print. Also, instead of showing the read directly every second, the code calculate the average for a minute. Just hoping it may give ideas to some of you, I would never have done as much without this community....

Imports Microsoft.SPOT
Imports SecretLabs.NETMF.Hardware
Imports SecretLabs.NETMF.Hardware.NetduinoPlus


Module Module_Un
   
    Dim tempSensor As AnalogInput = New AnalogInput(Pins.GPIO_PIN_A0)
    Event DisplayTemp(ByVal tmp As Double)

    Sub Main()

        AddHandler DisplayTemp, AddressOf DisplayResults
        While True
            GetAverageTemperature()
        End While

    End Sub

    Sub DisplayResults(tmp As Double)
       
        Dim temp As ULong = CULng(tmp)        
        Debug.Print("Centigrades: " & tmp & " Farhenheit: " & (tmp * 9 / 5) + 32)

    End Sub

    
    Function GetTemperature() As Double

        Dim read As Integer = tempSensor.Read()
        Dim voltage As Double = read * 3.3 / 1024
        Return (voltage - 0.5) * 100

    End Function


    Sub GetAverageTemperature()        
        Dim totalTemp As Double
        Dim averagetemp As Double
        For i = 0 To 59 Step 1
            totalTemp += GetTemperature()
            Thread.Sleep(1000)
        Next        
        averagetemp = totalTemp / 60
        RaiseEvent DisplayTemp(averagetemp) 
    End Sub

End Module

Attached Files



#2 BernardG

BernardG

    Advanced Member

  • Members
  • PipPipPip
  • 34 posts

Posted 12 June 2011 - 09:08 AM

By the way, how do I round up the results as double? It looks like format and round are not available in NETMF. Is that right, or did I miss something? Thanks!

#3 Stefan

Stefan

    Moderator

  • Members
  • PipPipPip
  • 1965 posts
  • LocationBreda, the Netherlands

Posted 12 June 2011 - 11:00 AM

By the way, how do I round up the results as double? It looks like format and round are not available in NETMF. Is that right, or did I miss something? Thanks!


Dim DValue As Double, IValue As Integer
DValue = 34645.34634
IValue = DValue
Debug.Print(DValue.ToString() + "-" + IValue.ToString())
That sort of rounds it;

Output:
34645.34634-34645
"Fact that I'm a moderator doesn't make me an expert in things." Stefan, the eternal newb!
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs

#4 BernardG

BernardG

    Advanced Member

  • Members
  • PipPipPip
  • 34 posts

Posted 12 June 2011 - 11:37 AM

Dim DValue As Double, IValue As Integer
DValue = 34645.34634
IValue = DValue
Debug.Print(DValue.ToString() + "-" + IValue.ToString())
That sort of rounds it;

Output:
34645.34634-34645


;) hehe... I did something equivalent, but what I would like is to round, up or down, to next 10th or 100th... ho well, does not matter that much...

#5 Stefan

Stefan

    Moderator

  • Members
  • PipPipPip
  • 1965 posts
  • LocationBreda, the Netherlands

Posted 12 June 2011 - 11:45 AM

;) hehe... I did something equivalent, but what I would like is to round, up or down, to next 10th or 100th... ho well, does not matter that much...

That's not so much different:

Module Module1

    Sub Main()
        Debug.Print(MyOwnRound(4353.235547, 3).ToString())
    End Sub

    Function MyOwnRound(ByVal Value As Double, ByVal precision As Integer) As Double
        Dim Multiplier As Integer = (10 ^ precision) ' 0 = 1, 1 = 10, 2 = 100, 3 = 1000, etc.
        Dim IntValue As Integer = Value * Multiplier

        MyOwnRound = IntValue / Multiplier
    End Function

End Module

"Fact that I'm a moderator doesn't make me an expert in things." Stefan, the eternal newb!
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs

#6 BernardG

BernardG

    Advanced Member

  • Members
  • PipPipPip
  • 34 posts

Posted 13 June 2011 - 07:55 AM

That's not so much different:

Module Module1

    Sub Main()
        Debug.Print(MyOwnRound(4353.235547, 3).ToString())
    End Sub

    Function MyOwnRound(ByVal Value As Double, ByVal precision As Integer) As Double
        Dim Multiplier As Integer = (10 ^ precision) ' 0 = 1, 1 = 10, 2 = 100, 3 = 1000, etc.
        Dim IntValue As Integer = Value * Multiplier

        MyOwnRound = IntValue / Multiplier
    End Function

End Module


Thanks Stefan, did not thought of that. Cool, going to try this code immediately.

#7 skytribe

skytribe

    Member

  • Members
  • PipPip
  • 11 posts

Posted 13 June 2011 - 03:31 PM

Thanks Stefan, did not thought of that. Cool, going to try this code immediately.



If you want to implement functionality such as Round etc. that are contained in Microsoft.VisualBasic.Dll on the desktop framework you can use reflector or Telerik's Decompiler to decompile the assembly and create your own functionality.

The reason why all these functions are not supported out the box is that they consume memory and memory is in rather short supply for microframework. There is a hit you already take for using VB and unneccessrily bloating it with a lot of functions that you don't need all the time would reduced down amount of memory available for your code.

If you want this functionality you can simply implement you own as these functions tend to be just wrapper classes around .NET Framework functionality. Many are already implemented in C# solutions - so you may want to consider taking C# functionality and wrapping it in familiar method signature.

You may want to check out the system.Math namespace which has most of these functions in. If you use these then you code will be easily converted between C# and VB - should users of the other language want to use you code examples.

#8 BernardG

BernardG

    Advanced Member

  • Members
  • PipPipPip
  • 34 posts

Posted 13 June 2011 - 09:12 PM

There is a hit you already take for using VB


Could you explain? I thought that (on "normal" .Net) both languages after being pre-compiled by MSIL were basically the very same code. Is it very different for NETMF ?

#9 skytribe

skytribe

    Member

  • Members
  • PipPip
  • 11 posts

Posted 13 June 2011 - 09:43 PM

Could you explain? I thought that (on "normal" .Net) both languages after being pre-compiled by MSIL were basically the very same code. Is it very different for NETMF ?


Sure, the hit is the additional assembly that is being used when you are using VB. Look at the list of assemblies and you will see one called Microsoft.VisualBasic.dll which is included for VB applications. This contains some important items as well as conversion functions which enable VB to work.

This occurs on ALL .NET platforms not just MF. When compiled VB and C# both produce IL. These functions consume additional memory, I think its around 17 KBytes. However this may be more important on the MF devices which are much more memory constrained.

The WP7 / XNA project types now use a feature called VB Core which embeds these items in the assembly avoiding having to deploy an additional assembly but this still requires these items to be present.

If you look at what else is in this Microsoft.Visualbasic.dll assembly on the desktop you will see it contains the Late binder, legacy function (Left, right, mid....), Constants etc. Many of which are simply wrapper classes around existing .NET framework functionality or are simply not supported on other platforms. So reduced runtimes were created for these new platforms and this unsupported functionality was removed to reduce the hit you would take.

If you want these function then you can simply create them yourself.

As these are all new platforms the general idea was that basic VB.NET support would be provided. Some desktop code would need to be modified when porting to these platforms but using the .NET framework functionality is probably a better bet than the vb runtime because it allows easy conversions between languages.

#10 Pinto Novo

Pinto Novo

    New Member

  • Members
  • Pip
  • 8 posts
  • LocationPortugal

Posted 06 October 2011 - 08:43 AM

So Skytribe I can decompile the Microsoft.VisualBasic.dll from microframework and add functions and then recompile and add to my projects and works? Thats the ideia? Thanks Ricardo

#11 Ellen

Ellen

    Advanced Member

  • Members
  • PipPipPip
  • 65 posts
  • LocationRotterdam, Netherlands

Posted 14 June 2012 - 10:18 AM

Thanks Stefan, did not thought of that. Cool, going to try this code immediately.


I am a newbee and I know, it is an old post.

Is it right to do it like this, rounding up.?.

dim dblSecsAndMsecs as Double = 12345.6789
Debug.Print(dblSecsAndMsecs.ToString("N0"))

N = numeric and the zero is for no decimals, so 2 decimals = N2




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.