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

Serial Comms with Netduino Plus

Serial RS232 Netduino

  • Please log in to reply
2 replies to this topic

#1 ncsteinb

ncsteinb

    Member

  • Members
  • PipPip
  • 27 posts

Posted 14 July 2013 - 02:49 AM

Hi guys, 

 

I am having some troubles writing my code for a controller i'm building for an automated hydroponics project i'm working on. I have never used serial comms with the Netduino yet, so i'm kinda new at this thing. (I gave up on I2C, I guess i'm too stupid....) Excuse me for my terrible nomenclature, I've never had any programming training, but I will try my best.

 

I have two serial ports I need to communicate through. Both are just going to be sending commands, no Rx, RTS or CTS. Just one sending serial data to a Sparkfun serial enabled LCD, and the other to a LittleStep stepper controller. Should be straight forward...

 

This is the example I've been looking at for help.

 

http://blog.codeblac...with-RS232.aspx

 

Basically just trying to copy the code, but for some reason, the SerialPort class (nomenclature, not sure if this is actually a class...???) is not working properly... What am I doing wrong?

 

I also am having an issue with my Thread.Sleep(..) commands. Error: "The best overloaded method match for System.Threading.Thread.Sleep(int) has some invalid arguments." I basically want to turn the pump on for 30 mins, and off for 30 mins, and also control the lights ON and OFF timing.. The math converts hrs or mins to milliseconds. Anyway..Here's my code.. Thanks in advance guys!!! 

 

Edit, just noticed in my code, uSec and notes say 'microseconds'. I realize its milliseconds, and the math should compute milliseconds... I guess that's what happens when you're tired.

using System;using System.Net;using System.Net.Sockets;using System.Threading;using System.IO.Ports;using Microsoft.SPOT;using Microsoft.SPOT.Hardware;using SecretLabs.NETMF.Hardware;using SecretLabs.NETMF.Hardware.NetduinoPlus;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            uint Light_ON_Time_min = 960; // In minutes, length of time lights are ON. 960 mins = 16 hrs **MUST BE WHOLE NUMBERS**            uint Light_OFF_Time_min = 3600 - Light_ON_Time_min; // Calculates OFF time in minutes            uint Light_ON_Time_uSec = (60 * 1000 * Light_ON_Time_min); // Calculates ON time in micro-seconds            uint 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 Pump()        {            OutputPort Pump_State = new OutputPort(Pins.GPIO_PIN_D3,false); // Define output for pump            uint Cycle_Time_mins = 30;            uint Cycle_Time_uSec = (Cycle_Time_mins * 60 * 1000);            Pump_State.Write(true);            Thread.Sleep(Cycle_Time_uSec);            Pump_State.Write(false);            Thread.Sleep(Cycle_Time_uSec);        }        public static void Status()        {            serialA = new SerialPort(SerialPorts.COM1, 9600, Parity.None, 8, StopBits.One); // Initialize COM1            serialA.Open();            serialA.Write("To Be Filled In Later..");        }        public static void Turn_Table()        {            serialB = new SerialPort(SerialPorts.COM2, 9600, Parity.None, 8, StopBits.One); // Initialize COM2            serialB.Open();            serialB.Write("To Be Filled In Later..");        }    }}


#2 ncsteinb

ncsteinb

    Member

  • Members
  • PipPip
  • 27 posts

Posted 14 July 2013 - 03:22 AM

Figured out my problem with the SerialPort issue... needed to add the Reference, not just the 'using' reference... Sometimes, this is so confusing.. 

 

http://wiki.netduino...SerialPort.ashx



#3 NooM

NooM

    Advanced Member

  • Members
  • PipPipPip
  • 490 posts
  • LocationAustria

Posted 14 July 2013 - 09:46 AM

correct, you need to add the reference yourself.

about the thread.sleep: its int not uint, also its milliseconds not microseconds

 

Int32 Cycle_Time_mSec = (Cycle_Time_mins * 60 * 1000); // thats allready msec, wonder why you named it uSec ...

 

 

than: in your Main() add: Thread.Sleep(Timeout.Infinite);

or it will do unexpected things.

also your threads need a loop or they just will do what they do once and than stop.

 

 

about i2c and stuff: trial and error, dont give up, as soon as you understand whats going on its very easy, same for spi and uart (serial)







Also tagged with one or more of these keywords: Serial, RS232, Netduino

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.