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

Referencing Boolean variables from other Methods, Serial issues too...


Best Answer ziggurat29, 15 July 2013 - 11:50 PM

some things:

*  Is 'Light_State' reachable in Status()?  Because it is a local in Lights().  So it shouldn't compile as you have shown.  You could define Light_State as a (static) member variable, like your did with the serialA and serialB; then it will be reachable.

*  Light_State is not a boolean itself, it's an OutputPort, but you need to call Read().  (much like you don't assign, but you call Write()).  So your line 63 would read something like:

  if ( Light_State.Read() )

*  you initialized LIght_State on D2, but this will presumably conflict with serialB when you get around to implementing that, which uses that line as COM2 RXD.

*  you're creating and opening your serial port in Status().  Normally you wouldn't do this.  Normally you'd do that early in the program, and then just use the open instance later.  As written here, it goes out-of-scope at the end of Status().  But this doesn't mean the hardware is actually released at that time, because you did not close and (more importantly) Dispose().  So the next time you go into Status(), you might find you can't open it.

 

HTH

Go to the full post


  • Please log in to reply
3 replies to this topic

#1 ncsteinb

ncsteinb

    Member

  • Members
  • PipPip
  • 27 posts

Posted 15 July 2013 - 11:16 PM

Hi guys, 

 

I'm having some issues trying to make an IF statement comparing to a Boolean. The code should be self explanatory as to what i'm trying to do, i'm just doing it wrong.. the issue is at Line 63. 

 

Also, it seems that I have missed something with outputting serial commands. I'm just trying to send a Sparkfun serial LCD a few commands. Any tips? BTW, it's unfinished, so the 'commandA' is just a placeholder.

 

Here's the code that's relevant:

namespace HydroControl{    public class Program    {        static SerialPort serialA; // Serial port on pins D0 & D1 for LCD comms        static SerialPort serialB; // Serial port on pine D2 & D3 for Stepper Controller comms        public static void Main()        {            Thread t1 = new Thread(Lights);            t1.Start();            Thread t2 = new Thread(Pump);            t2.Start();            Thread t3 = new Thread(Turn_Table);            t3.Start();            Thread t4 = new Thread(Status);            t4.Start();        }        public static void Lights()        {            OutputPort Light_State = new OutputPort(Pins.GPIO_PIN_D2, false); // Define output for lights            int Light_ON_Time_min = 960; // In minutes, length of time lights are ON. 960 mins = 16 hrs **MUST BE WHOLE NUMBERS**            int Light_OFF_Time_min = 3600 - Light_ON_Time_min; // Calculates OFF time in minutes            int Light_ON_Time_uSec = (60 * 1000 * Light_ON_Time_min); // Calculates ON time in micro-seconds            int Light_OFF_Time_uSec = (60 * 1000 * Light_OFF_Time_min); // Calculates OFF time in micro-seconds            Light_State.Write(true);            Thread.Sleep(Light_ON_Time_uSec);            Light_State.Write(false);            Thread.Sleep(Light_OFF_Time_uSec);                    } 
public static void Status()        {            serialA = new SerialPort(SerialPorts.COM1, 9600, Parity.None, 8, StopBits.One); // Initialize COM1            serialA.Open();            if (Light_State = true)            {                serialA.Write("0x157");            }            else            {                serialA.Write("0x143");            }            string commandA = "157"; // LCD backlight fully on            string sendA = "0x";            sendA += commandA;            serialA.Write("sendA");        } 

Thanks!



#2 ziggurat29

ziggurat29

    Advanced Member

  • Members
  • PipPipPip
  • 244 posts

Posted 15 July 2013 - 11:50 PM   Best Answer

some things:

*  Is 'Light_State' reachable in Status()?  Because it is a local in Lights().  So it shouldn't compile as you have shown.  You could define Light_State as a (static) member variable, like your did with the serialA and serialB; then it will be reachable.

*  Light_State is not a boolean itself, it's an OutputPort, but you need to call Read().  (much like you don't assign, but you call Write()).  So your line 63 would read something like:

  if ( Light_State.Read() )

*  you initialized LIght_State on D2, but this will presumably conflict with serialB when you get around to implementing that, which uses that line as COM2 RXD.

*  you're creating and opening your serial port in Status().  Normally you wouldn't do this.  Normally you'd do that early in the program, and then just use the open instance later.  As written here, it goes out-of-scope at the end of Status().  But this doesn't mean the hardware is actually released at that time, because you did not close and (more importantly) Dispose().  So the next time you go into Status(), you might find you can't open it.

 

HTH



#3 ncsteinb

ncsteinb

    Member

  • Members
  • PipPip
  • 27 posts

Posted 16 July 2013 - 12:27 AM

Awesome. Thanks for the input! I think I have it worked out, as in, there are no more red squiggly lines.. We'll see if it actually works when I test the hardware.



#4 ziggurat29

ziggurat29

    Advanced Member

  • Members
  • PipPipPip
  • 244 posts

Posted 16 July 2013 - 03:48 AM

The loss of red squiggles

oft gives one the giggles,

but the code that does run

is really more fun.

 

Good luck, have fun debugging!






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.