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

implementing serial communication for netduino plus 2

rs232 netmf c# netduino plus 2

  • Please log in to reply
4 replies to this topic

#1 m.sainath

m.sainath

    New Member

  • Members
  • Pip
  • 7 posts

Posted 21 April 2013 - 12:33 PM

using System;using System.Net;using System.Net.Sockets;using System.Threading;using System.IO.Ports;using System.IO.Ports.SerialPort;using Microsoft.SPOT;using Microsoft.SPOT.Hardware;using SecretLabs.NETMF.Hardware;using SecretLabs.NETMF.Hardware.Netduino;namespace Microsoft.SPOT.Hardware{     public sealed class SerialPort : IDisposable         {                 public SerialPort(SerialPort.Configuration config);         public SerialPort.Configuration Config { get; set; }         public void Dispose();                 public void Flush();                 public int Read(byte[] buffer, int offset, int count, int timeout);                 public int Write(byte[] buffer, int offset, int count);                  public enum BaudRate                    {                            Baud4800 = 4800,                            Baud9600 = 9600,                            Baud19200 = 19200,                            Baud38400 = 38400,                            Baud57600 = 57600,                            Baud115200 = 115200,                            Baud230400 = 230400,                    }        public enum Serial                    {                            COM1 = 0,                            COM2 = 1,                            COM3 = 2,                            COM4 = 3,                    }        public class Configuration                    {                            public readonly SerialPort.Serial Com;                            public readonly bool FlowControl;                            public readonly SerialPort.BaudRate Speed;                public Configuration(SerialPort.Serial com, SerialPort.BaudRate speed,                                 bool flowControl);                    }         } }namespace BarcodeScanner{    public class Program    {        public static void Main()        {            // write your code here            Microsoft.SPOT.Hardware.SerialPort.Configuration config = new Microsoft.SPOT.Hardware.SerialPort.Configuration(Microsoft.SPOT.Hardware.SerialPort.Serial.COM1, Microsoft.SPOT.Hardware.SerialPort.BaudRate.Baud9600, false);            Microsoft.SPOT.Hardware.SerialPort serialPort = new Microsoft.SPOT.Hardware.SerialPort(config);                        byte[] outBuffer = System.Text.Encoding.UTF8.GetBytes("Hello World!rn");            serialPort.Write(outBuffer, 0, outBuffer.Length);                        serialPort.Dispose();            //keeps the emulator running to see results                        Thread.Sleep(Timeout.Infinite);        }    }}

 

This is the code i'm using to achieve serial communication for a netduino plus 2 based on .NET micro framework. When i do i get a error message stating that "A using namespace directive can only be applied to namespaces; 'System.IO.Ports.SerialPort' is a type not a namespace ". Upon seeing this i comment out the line of code

using System.IO.Ports.SerialPort;

then I encounter couple of error messages stating that "'Microsoft.SPOT.Hardware.SerialPort.Configuration.Configuration(Microsoft.SPOT.Hardware.SerialPort.Serial, Microsoft.SPOT.Hardware.SerialPort.BaudRate, bool)' must declare a body because it is not marked abstract, extern, or partial"

Please help me resolve this issue as soon as possible. The scenario i'm working on is to get data from a barcode scanner via a rs232 cable connected to a breakout board which converts rs232 to serial. Thanks in advance.



#2 ziggurat29

ziggurat29

    Advanced Member

  • Members
  • PipPipPip
  • 244 posts

Posted 21 April 2013 - 02:01 PM

you figured out the 'using' problem -- you don't use the class name in c#.

 

Its not clear to me why you are sticking stuff in the Microsoft.SPOT.Hardware namespace, and defining a SerialPort there, but here is a reduced version that at least compiles:

using System.Threading;using System.IO.Ports;namespace BarcodeScanner{    public class Program    {        public static void Main()        {            // write your code here			SerialPort serialPort = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);                        byte[] outBuffer = System.Text.Encoding.UTF8.GetBytes("Hello World!rn");            serialPort.Write(outBuffer, 0, outBuffer.Length);                        serialPort.Dispose();            //keeps the emulator running to see results                        Thread.Sleep(Timeout.Infinite);        }    }}

 

Good luck!



#3 m.sainath

m.sainath

    New Member

  • Members
  • Pip
  • 7 posts

Posted 21 April 2013 - 02:18 PM

An unhandled exception of type 'System.ArgumentException' occurred in Microsoft.SPOT.Hardware.SerialPort.dll
 
I encounter this exception upon the use of the code specified above.


#4 ziggurat29

ziggurat29

    Advanced Member

  • Members
  • PipPipPip
  • 244 posts

Posted 21 April 2013 - 05:18 PM

Well I said I only compiled it not that I ran it!  haha!  OK Now I have run it.  You also need to do a serialPort.Open() on the serial port.  So change main to this:

public static void Main(){	// write your code here	SerialPort serialPort = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);	serialPort.Open();	byte[] outBuffer = System.Text.Encoding.UTF8.GetBytes("Hello World!rn");	serialPort.Write(outBuffer, 0, outBuffer.Length);            	serialPort.Dispose();            //keeps the emulator running to see results            	Thread.Sleep(Timeout.Infinite);}

 

which came from running code, and I can see the output from the serial port.

 

And I guess you've stumbled across one of the little joys: you got an ArgumentException, but the arguments (to Write) were fine, it was just the state of the object was unexpected.



#5 Fahdil

Fahdil

    Advanced Member

  • Members
  • PipPipPip
  • 59 posts
  • LocationJakarta, Indonesia

Posted 06 September 2013 - 07:56 AM

Well I said I only compiled it not that I ran it!  haha!  OK Now I have run it.  You also need to do a serialPort.Open() on the serial port.  So change main to this:

public static void Main(){	// write your code here	SerialPort serialPort = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);	serialPort.Open();	byte[] outBuffer = System.Text.Encoding.UTF8.GetBytes("Hello World!rn");	serialPort.Write(outBuffer, 0, outBuffer.Length);            	serialPort.Dispose();            //keeps the emulator running to see results            	Thread.Sleep(Timeout.Infinite);}

which came from running code, and I can see the output from the serial port.

 

And I guess you've stumbled across one of the little joys: you got an ArgumentException, but the arguments (to Write) were fine, it was just the state of the object was unexpected.

Hi all.

I'm using GSMshield Icomsat v1.1 with netduinoplus. I need to communicate with the GSmShield through the serial port. on your post above, I just dont see declaration about netduino digital Pins as Rx/Tx. can anybody explain?

 

thx







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.