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

Netduino Serial Port Code Review


  • Please log in to reply
31 replies to this topic

#1 hari

hari

    Advanced Member

  • Members
  • PipPipPip
  • 131 posts

Posted 22 September 2010 - 04:53 AM

I was learning how to do SerialPort communication for my next project and ended up taking some time working with buffers. I think this code could be useful to others. It uses SerialPort.DataReceived event handler so it is non-blocking (your program can go on doing other things instead of waiting for serial input).
Code is attached to this post, feel free to use it for your projects. Constructive comments welcomed. Posted Image

The SerialPortHelper class is very easy to use. Just instantiate it (you can overide its speed, etc, but you don't have to):
1. ReadLine() method returns a full line or a blank string coming from serial port.
2. Print() and PrintLine() prints a string to serial port.

As mentioned in the second video. I believe there is a bug, but I have never seen it happen in real life yet. If you know how to fix this, please post. Thanks!!!

http://www.youtube.com/watch?v=Os3bOCWbmHA

Demo of Netduino's SerialPort communication capability.


http://www.youtube.com/watch?v=gUB6UFyDkaA

Second video explains the wiring and reviews the actual code.


PS: Chris, If doesn't qualify as a project "Showcase", feel free to move it to more appropriate forum.


EDIT: 10/26/2010 - Attachment updated to add Locks and declare interruptPort as static.

Attached Files



#2 Crispin

Crispin

    Advanced Member

  • Members
  • PipPipPip
  • 65 posts
  • LocationLondon, England, Earth

Posted 22 September 2010 - 05:41 AM

Thanks! :) My new Venus GPS is winging it's way to me now so this will be a nice starting point! :D
--
Take a seat, I'll be right with you.

#3 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 22 September 2010 - 07:01 AM

hari, this is great. I love that you added the "" to the beginning of the string to make sure that users don't get null strings. Nice. And this is perfect for the project showcase. BTW, you can put a 'lock(object) {...}' block around your DataReceived code _and_ your code which reads data out of that buffer. That would take care of any possible cross-thread bugs with the buffer. Chris

#4 pascal06

pascal06

    Advanced Member

  • Members
  • PipPipPip
  • 95 posts
  • LocationFrance

Posted 22 September 2010 - 01:22 PM

Excellent video, Thanks for sharing, When I work with serial port and programming, I use this powerful free tools: http://www.der-hammer.info/terminal/ Pascal

#5 hari

hari

    Advanced Member

  • Members
  • PipPipPip
  • 131 posts

Posted 22 September 2010 - 02:07 PM

BTW, you can put a 'lock(object) {...}' block around your DataReceived code _and_ your code which reads data out of that buffer. That would take care of any possible cross-thread bugs with the buffer.

Aah... I thought it would be a lot more complicated than that. I will try that tonight.

Thanks for the comments everyone!

#6 sweetlilmre

sweetlilmre

    Advanced Member

  • Members
  • PipPipPip
  • 62 posts

Posted 22 September 2010 - 04:44 PM

This is awesome :) Let us know how the locking code goes, and can you tell me what you used to do the video? It looks really good and would be a nice alternative to a blog post for me to try out sometime. Thanks -(e)

#7 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 22 September 2010 - 05:06 PM

Aah... I thought it would be a lot more complicated than that. I will try that tonight.


You might want to arrange your code in a way that you can make the locks as quick as possible--but for what you're doing, putting one big lock around each section that works with the buffer and buffer pointers is probably okay.

Chris

#8 hari

hari

    Advanced Member

  • Members
  • PipPipPip
  • 131 posts

Posted 22 September 2010 - 05:55 PM

This is awesome :)

Let us know how the locking code goes, and can you tell me what you used to do the video?
It looks really good and would be a nice alternative to a blog post for me to try out sometime.

Thanks
-(e)

Thanks.

That Screencast was captured using Expression Encoder Screen Capture. There's a demo here.
Expression Encoder 4 is FREE, but limited to capturing 10 minute segments, which is ok since YouTube has an upload limit of 15 minutes anyway.

Expression Encoder Screen capture captures the whole screen including a live webcam preview that comes with the Microsoft webcam.
I use a Logitech USB mic propped on a cashew can to bring it close to my lips for louder audio.

The only annoying thing is it takes several minutes to encode the video before you can upload it to youtube, then youtube takes a few minutes more.

I've also used the free CamStudio with good results.

#9 SteveM

SteveM

    New Member

  • Members
  • Pip
  • 1 posts

Posted 14 December 2010 - 05:54 AM

Just wanted to add more thanks - both for the two videos and the code. Beautifully done and a great help.

#10 onedumbtrucker

onedumbtrucker

    Member

  • Members
  • PipPip
  • 13 posts

Posted 10 August 2011 - 06:03 PM

This is a great project example! Thank you for the awesome work. I had this up and running on my N+ with a Matrox Orbital Serial LCD in less than 5 minutes. Thanks again, Dan

#11 magarcan

magarcan

    Advanced Member

  • Members
  • PipPipPip
  • 43 posts

Posted 07 September 2011 - 03:38 PM

I've written a code using your library, but it is not working. can anyone take a look?
using System;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;
using System.Threading;
using Microsoft.SPOT;
using System.IO.Ports;

namespace SerialPortLearn
{
    public class Program
    {
        static OutputPort led = new OutputPort(Pins.ONBOARD_LED, false);
        
        static SerialPortHelper serialPortHelper1 = new SerialPortHelper();
        static SerialPortHelper serialPortHelper2 = new SerialPortHelper(SerialPorts.COM2, 9600, Parity.None, 8, StopBits.One);

        public static void Main()
        {
            while (true)
            {
                string line = serialPortHelper2.ReadLine();
                if (line.Length > 0)
                {
                    led.Write(true);
                    serialPortHelper1.PrintLine("Leido: " + line);
                    Debug.Print("Leido: " + line);
                }
            }
        }
    }
}
I'm able to write in UART, but reading method is not working.
Any idea? Thanks!!

#12 Niko

Niko

    New Member

  • Members
  • Pip
  • 5 posts
  • LocationSlovenia

Posted 16 November 2011 - 01:42 PM

While debugging the following piece of code

bufferLength += bytesReceived;
                    if (bufferLength >= bufferMax)
                        throw new ApplicationException("Buffer Overflow.  Send shorter lines, or increase lineBufferMax.");
                    string output = new string(Encoding.UTF8.GetChars(buffer));
                    Print(output);

I noticed the characters are not the same as the ones sent from the hyperterminal; for example when 'u' is pressed on PC keyboard the string output on netduino is 'E'.

At first I thought the problem might be related with encoding but the received byte on netduino is not equal to sent byte. Here are some bytes (characters) sent from the PC hyperterminal on the left and received bytes on netduino on the right:

PC sent byte (character) - Netduino received byte
61 (a) - 79
62 (B) - 39
63 (c) - 78
64 (d) - 19

The same hyperterminal and cable works normali while communicating with other hardware.

Currently I am testing the connection between netduino and PC via hyperterminal and the goal is to connect netduino with GSM/UMTS module which communicates with AT commands but until the encoding problem is present it is not possible to connect the netduino with other hardware.

Does anyone know what could cause the described problem? I would be very thankful for your help!

#13 Mitchell

Mitchell

    New Member

  • Members
  • Pip
  • 7 posts

Posted 13 January 2012 - 09:48 AM

Hello, Love the code, Will be using it in my next project. My question is about speed, what is the highest speed you can get between you computer and a netduino? If anyone knows the answer that would be very helpful. Thanks Mitchell

#14 Msbh

Msbh

    New Member

  • Members
  • Pip
  • 8 posts

Posted 17 January 2012 - 07:27 PM

Buffer Over flow error. how solve buffer flow error????

#15 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 18 January 2012 - 03:15 AM

Hi Msbh,

Buffer Over flow error.

how solve buffer flow error????

If your serial buffers are overflowing then you need to either (a) send data to your Netduino slower or (B) process it faster or © get rid of the data in the meantime.

What error are you getting, exactly, and at what point in your code?

Chris

#16 Bendage

Bendage

    Advanced Member

  • Members
  • PipPipPip
  • 153 posts
  • LocationIrvine, CA

Posted 24 January 2012 - 10:16 PM

So, without this extra component Netduino cannot communicate with built in serial like the Arduino? Disappointed :( I have a Sparkfun Xbee Explorer shield. This should do the trick right?

#17 Bendage

Bendage

    Advanced Member

  • Members
  • PipPipPip
  • 153 posts
  • LocationIrvine, CA

Posted 25 January 2012 - 03:12 AM

I just tested and confirmed that YES the Sparkfun Xbee shield does in fact work in place of the FTDI shield he is using. Still kind of blows that serial is not built into the Netduino like it is in the Arduino. I've just never tried since we have debug writing to the console. By the way, Hari... you rock man. I love your contribution and detailed videos to this community. Thank you!

#18 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 25 January 2012 - 07:16 AM

Hi Bendage,

So, without this extra component Netduino cannot communicate with built in serial like the Arduino?

With Arduino, you can either deploy code to a device or use the USB link for serial communication--but not on the same time.

With Netduino, the situation is similar. But since there's a nice debugger, users generally want to use the link for debugging.

We've built a USB HID communication sample which you can use to communicate between a PC and your Netduino...but it does require shutting off the debug channel on your Netduino (which brings you back to either doing debug via serial...needing that cable...or going through a process of erasing your app via bootloader every time you want to redeploy).

We're looking into ways to support simultaneous debug + serial communication...which would be awesome...but right now we're tied up trying to help clean up some final bugs in the new .NET MF 4.2. Once we're done with that, we should be able to revisit this feature.

Chris

#19 Bendage

Bendage

    Advanced Member

  • Members
  • PipPipPip
  • 153 posts
  • LocationIrvine, CA

Posted 25 January 2012 - 06:14 PM

Thank you for the clarification Chris. Do you have a link to what you are referring to? USB + HID does not return anything specific enough. Is there online docs maybe in the Arduino format or MSDN format for the Netduino?

#20 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 25 January 2012 - 08:29 PM

Hi Bendage, Here you go: http://forums.netdui...tion-using-usb/ Chris




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.