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's Content

There have been 9 items by gordon128 (Search limited from 24-September 23)


By content type

See this member's

Sort by                Order  

#13995 string to int's

Posted by gordon128 on 06 June 2011 - 06:59 PM in Visual Studio

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.



#13993 string to int's

Posted by gordon128 on 06 June 2011 - 05:27 PM in Visual Studio

I want to take a string that has a series on numbers separated by commas, like: string mystring = "1234,34,26,6,1,0"; and extract those numbers into a number of integers, like: int a, int b, int c, int d, int e, int f ....etc. or into an array -> int[1 to x] numbers; Can any one give an example of possible code? Regards Gordon



#13978 Help - Addressing a port from a variable

Posted by gordon128 on 05 June 2011 - 04:14 PM in Netduino Plus 2 (and Netduino Plus 1)

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



#13974 Help - Addressing a port from a variable

Posted by gordon128 on 05 June 2011 - 01:59 PM in Netduino Plus 2 (and Netduino Plus 1)

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)



#13957 Help - Addressing a port from a variable

Posted by gordon128 on 04 June 2011 - 03:01 PM in Netduino Plus 2 (and Netduino Plus 1)

ItsDan,

I ment to put:

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

Gordon



#13956 Help - Addressing a port from a variable

Posted by gordon128 on 04 June 2011 - 02:56 PM in Netduino Plus 2 (and Netduino Plus 1)

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



#13954 Help - Addressing a port from a variable

Posted by gordon128 on 04 June 2011 - 11:59 AM in Netduino Plus 2 (and Netduino Plus 1)

Hello fellow N+'s

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



#13953 Netduino Plus webserver Help

Posted by gordon128 on 04 June 2011 - 11:41 AM in Netduino Plus 2 (and Netduino Plus 1)

It could be that rawData length is less then 5, you can try replacing:
string commandData = rawData.Substring(5, rawData.Length – 5);
with:
string commandData = (rawData.Lenght >= 5) rawData.Substring(5, rawData.Length – 5) ? "";

However, the real issue is why is the rawData length is less than 5?



Veetrik,

Thanks for your reply. I tried what you suggested, but the assembler didn't like the code.
After some investigation I found my problem in the program.cs.
There was no trap for a bad command sent from the browser.

Thanks again Gordon



#13722 Netduino Plus webserver Help

Posted by gordon128 on 28 May 2011 - 08:40 PM in Netduino Plus 2 (and Netduino Plus 1)

Hello fellow N+'s Code taken from: http://www.schuurmans.cc/tag/webserver I’ve use the code to do with this article and it does the job I’m looking for. How ever, I send a request to the server and all works OK. When I go to send another request a little later, it is as though the server has frozen. The only thing I can do is to re-boot the Netduino Plus. Below are the debug reports: ===========debug report=========== #### Exception System.NullReferenceException – CLR_E_NULL_REFERENCE (5) #### #### Message: #### Blinq.Netduino.Web.WebServer::InterpretRequest [IP: 0007] #### #### Blinq.Netduino.Web.WebServer::StartServer [IP: 0059] #### A first chance exception of type ‘System.NullReferenceException’ occurred in Blinq.Netduino.dll An unhandled exception of type ‘System.NullReferenceException’ occurred in Blinq.Netduino.dll ===========webserver.cs highlighted lines below=========== // Remove GET + Space string commandData = rawData.Substring(5, rawData.Length – 5); // Convert to string, will include HTTP headers. string rawData = new string(Encoding.UTF8.GetChars(bytes)); WebCommand command = InterpretRequest(rawData); Code taken from: http://www.schuurmans.cc/tag/webserver Please can anyone help on this matter. 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.