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.

gordon128

Member Since 14 May 2011
Offline Last Active Nov 28 2015 11:44 PM
-----

Posts I've Made

In Topic: string to int's

06 June 2011 - 06:59 PM

Try:

string mystring = "1234,34,26,6,1,0";
string[] numbersAsStrings = mystring.Split(',');
int[] numberAsInts = new int[numbersAsStrings.Length];
for (int index = 0; index < numbersAsStrings.Length; index++)
{
    numberAsInts[index] = int.Parse(numbersAsStrings[index]);
}

Would not expect too much in the way of performance though - there are two expensive operations there, the Split and the Parse.

Regards,
Mark




Mark,

Your a star - cheers!

That does the job perfectly.
This is one big learning curve!

Thanks again for your help.

Regards Gordon.

In Topic: Help - Addressing a port from a variable

05 June 2011 - 04:14 PM

Gordon,

Does the following achieve what you are trying to do?

string[] portNames = { "PortA", "PortB", "PortC" };
Hashtable ports;

ports = new Hashtable();
ports.Add(portNames[0], new InputPort(Pins.GPIO_PIN_D0, true, Port.ResistorMode.Disabled));
ports.Add(portNames[2], new InputPort(Pins.GPIO_PIN_D1, true, Port.ResistorMode.Disabled));
ports.Add(portNames[1], new InputPort(Pins.GPIO_PIN_D2, true, Port.ResistorMode.Disabled));
for (int index = 0; index < portNames.Length; index++)
{
    Debug.Print("Port '" + portNames[index] + "' - " + ((InputPort) ports[portNames[index]]).Read());
}

Note:
- I mixed up the assignment to illustrate that the Hashtable does not need things added in the same order as they are addressed. Hence PortC is GPIO_D1.
- This code has not been tested on hardware.

You can find more examples using Hashtables here.

Regards,
Mark




Mark,

Thanks, will check your code out and have a look at the link you included.

Regards Gordon

In Topic: Help - Addressing a port from a variable

05 June 2011 - 01:59 PM

I think a HashTable may help you.

Regards,
Mark


Nevyn,

Mark, I've had a look at the link above, as I am new to C# and .NET Microframework, I don't understand how I can use a HashTable. Maybe you could give me pointer with a sample code.

I wanted to store the names of the ports in a variable array and then use the array to address the ports from a loop.

so something like, say the port names are "port_a", "port_b", "port_c"........
stored in some kind of array so that, array_variable[0] equals "port_a", array_variable[1] equals "port_b"...etc

Then us something like: (portstatus being a bool)

"portstatus = array_variable[i].read();"

so assuming i = 1,

then the line would be interpreted to be: "portstatus = port_b.read();"

Is this possible or am I barking up the wrong tree!!

Regards Gordon (newby)

In Topic: Help - Addressing a port from a variable

04 June 2011 - 03:01 PM

ItsDan,

I ment to put:

Debug.Print(portnameStr[i].read().ToString());

Gordon

In Topic: Help - Addressing a port from a variable

04 June 2011 - 02:56 PM

That will work fine but if you're saying you made led1 and are trying to do led[i] where i=1, no that won't work.

OutputPort leds[8];
bool portstatus[8];

The above would create arrays that can be looped.


ItsDan,

Thanks for the reply. I have probably confused the issue by putting led0 etc.
led0 is the name of the output port:

private static OutputPort _led0 = new OutputPort(Pins.GPIO_PIN_D0, false);
private static OutputPort _led1 = new OutputPort(Pins.GPIO_PIN_D1, false);

I want to be able to asign he name of the port to an array:

string[] portnameStr = "led0", "led1", etc......


for ( int i, i <= 5, i++)
{
Debug.Print(portnameStr[i].read().ToString());
}


This is something like I'm trying to achieve, to address the ports using a string variable.
Is it possible and if so, what is the correct code.


Regards Gordon

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.