Help - Addressing a port from a variable
#1
Posted 04 June 2011 - 11:59 AM
I'm new to this stuff, but my first home automation is on it's way.
I have programmed some time ago in C++, but not to good with C# and Micro Framework.
I have been trying to address digital I/O ports with variables e.g.
// Read status of all Ports.
using a "for i loop"
portstatus[i] = portname[i].Read();
The above line doesn't work so I've had to type every port on a separate line:
portstatus[0] = led0.Read();
portstatus[1] = led1.Read();
portstatus[2] = led2.Read();
etc.
Can any body help on this matter?
Regards Gordon
#2
Posted 04 June 2011 - 01:36 PM
OutputPort leds[8];
bool portstatus[8];
The above would create arrays that can be looped.
Total BOCS Traveled Distance: 9708 miles | States Visited: 5
Track the Box
#3
Posted 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
#4
Posted 04 June 2011 - 03:01 PM
I ment to put:
Debug.Print(portnameStr[i].read().ToString());
Gordon
#5
Posted 04 June 2011 - 03:13 PM
To be or not to be = 0xFF
Blogging about Netduino, .NET, STM8S and STM32 and generally waffling on about life
Follow @nevynuk on Twitter
#6
Posted 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)
#7
Posted 05 June 2011 - 04:01 PM
Gordon,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.
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
To be or not to be = 0xFF
Blogging about Netduino, .NET, STM8S and STM32 and generally waffling on about life
Follow @nevynuk on Twitter
#8
Posted 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
3 user(s) are reading this topic
0 members, 3 guests, 0 anonymous users