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

reading data through a serial port on netduino plus 2

c# netmf netduino plus 2 barcode scanner

  • Please log in to reply
6 replies to this topic

#1 m.sainath

m.sainath

    New Member

  • Members
  • Pip
  • 7 posts

Posted 28 April 2013 - 07:37 PM

using System.IO.Ports;using Microsoft.SPOT;using Microsoft.SPOT.Hardware;using SecretLabs.NETMF.Hardware;using SecretLabs.NETMF.Hardware.Netduino;namespace barcode_scanner{    public class Program    {        public static InputPort bsr = new InputPort(Pins.GPIO_PIN_D0, false, Port.ResistorMode.Disabled);        public static OutputPort led = new OutputPort(Pins.ONBOARD_LED, false);        public static void Main()        {            SerialPort mySerialPort = new SerialPort("COM1");            mySerialPort.BaudRate = 9600;            mySerialPort.Parity = Parity.None;            mySerialPort.StopBits = StopBits.One;            mySerialPort.DataBits = 8;            mySerialPort.Handshake = Handshake.None;            mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);        }        public static void DataReceivedHandler( object sender, SerialDataReceivedEventArgs e)        {            led.Write(true);            Thread.Sleep(1000);            led.Write(false);            Thread.Sleep(Timeout.Infinite);        }

 

[color=rgb(102,102,102);font-family:'Segoe UI', Helvetica, Garuda, Arial, sans-serif;]I'm using the above code to receive data through a serial port connected to my netduino plus 2 via a breakout board (RS232 to TTL converter- [/color]http://www.embeddedm...-TTL-Converter/[color=rgb(102,102,102);font-family:'Segoe UI', Helvetica, Garuda, Arial, sans-serif;]). The data i want to receive is from a barcode scanner whose output is a RS232 female, so i use a RS232 male to male cable and then use the above mentioned breakout board to connect to netduino plus 2. Upon use of the above code led does not blink. All i want to do is blink the led when data is read from barcode scanner i.e., when the barcode scanner scans a barcode. Please help me resolve this. Thanks in advance.[/color]



#2 NooM

NooM

    Advanced Member

  • Members
  • PipPipPip
  • 490 posts
  • LocationAustria

Posted 28 April 2013 - 09:50 PM

try:

 

SerialPort mySerialPort = new SerialPort("COM1", 9600);

 

remove all the other stuff like mySerialPort.Parity

(looks like its standard settings)

 

also add mySerialPort.Open();

 

and remove the Thread.Sleep(Timeout.Infinite);

from the event. instead add it after mySerialPort.Open();



#3 Kem

Kem

    Member

  • Members
  • PipPip
  • 17 posts

Posted 29 April 2013 - 08:31 PM

Like NooM said on the Thread.Sleep(Timeout.Infinite);, but as far as I know you also have to read the data from the serial buffer, otherwise you will not receive new data.

 

You could also change the sleep of 1000 ms to a timer that will turn off the led after 1 second. This way you do not have to pause the thread.



#4 m.sainath

m.sainath

    New Member

  • Members
  • Pip
  • 7 posts

Posted 30 April 2013 - 03:42 PM

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.Netduino;namespace barcode_scanner{    public class Program    {        public static InputPort bsr = new InputPort(Pins.GPIO_PIN_D0, false, Port.ResistorMode.Disabled);        public static OutputPort led = new OutputPort(Pins.ONBOARD_LED, false);        public static void Main()        {            // write your code here                        SerialPort mySerialPort = new SerialPort("COM1");            mySerialPort.BaudRate = 9600;                        mySerialPort.Open();            Thread.Sleep(Timeout.Infinite);            mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);                    }        public static void DataReceivedHandler( object sender, SerialDataReceivedEventArgs e)        {            led.Write(true);            Thread.Sleep(1000);            led.Write(false);        }    }} 

 

i get An unhandled exception of type 'System.InvalidOperationException' occurred in Microsoft.SPOT.Hardware.SerialPort.dll upon following the above changes.



#5 Nevyn

Nevyn

    Advanced Member

  • Members
  • PipPipPip
  • 1072 posts
  • LocationNorth Yorkshire, UK

Posted 30 April 2013 - 05:58 PM

Have you tried moving the Thread.Sleep(Timeout.Infinite) below the binding to the event handler for the serial port?

 

The code above as written will never bind to the event handler.

 

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


#6 ziggurat29

ziggurat29

    Advanced Member

  • Members
  • PipPipPip
  • 244 posts

Posted 30 April 2013 - 06:14 PM

the bsr:

new InputPort(Pins.GPIO_PIN_D0

is causing that.  If you comment out line 15, you'll get past your exception.  The reason is that D0 and D1 are used by COM1, so the open fails because the pins are already in use for something else.

 

also, you'll never get to line 29 because of line 27.  reverse those.

 

you may wish to make your serial port object accessible outside the scope of main(), like you did with your led and bsr.  this isn't strictly required here because you can get to it from your event handler params.

 

also, I'm sure that the code in DataReceivedHandler is just for test, to blink a light when data comes in, but you'll shouldn't delay in that handler unless you like buffer overruns.  also you must consume (read) the data that is available, which is not happening in your code above.  you could possibly do something like:

 

public static void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e){	//toggle lamp so we can see something happened	led.Write(!led.Read());	//consume (and discard) what is available	SerialPort mycerealport = (SerialPort) sender;	int nToRead = mycerealport.BytesToRead;	if ( nToRead > 0 )	//and really it always should be	{		byte[] buff = new byte[nToRead];		int nRead = mycerealport.Read(buff, 0, buff.Length);		//don't do anything with the data.  Or maybe we do!  send it back out!		mycerealport.Write(buff, 0, buff.Length);	}}

good luck!



#7 vicen.david@telefonica.net

vicen.david@telefonica.net

    New Member

  • Members
  • Pip
  • 2 posts

Posted 05 September 2013 - 05:38 PM

Hello,

 

Can you try with COM2 instead COM1.

 

Br.

 

David Asensio







Also tagged with one or more of these keywords: c#, netmf, netduino plus 2, barcode scanner

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.