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 and VS 2010?


  • Please log in to reply
6 replies to this topic

#1 nightlynx

nightlynx

    New Member

  • Members
  • Pip
  • 3 posts

Posted 27 August 2010 - 02:24 AM

So I'm having an issue with VS 2010 trying to use the serial port of the Netduino to display to a matrix orbital lcd display. Intellisense isn't showing any errors in my code but when I go to deploy it to the board it has the following error at the bottom of the page:

Error 1 0x80131700 C:\Documents and Settings\Administrator\my documents\visual studio 2010\Projects\NetduinoApplication1\NetduinoApplication1\MMP NetduinoApplication1

I'm a total newb to C# and visual studio so maybe I'm doing something stupid but I don't know what the issue is. My code is:

using System;
using System.IO.Ports;
using System.Text;                              // UTF Encoding
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;

namespace NetduinoApplication1
{
    public class Program
    {
        static OutputPort led = new OutputPort(Pins.ONBOARD_LED, false);

        //instantiate the serial port connection                           
        static SerialPort MyserialPort = new SerialPort("COM1",19200);

        public static void Main()
        {
            MyserialPort.Open();

            if (MyserialPort.IsOpen)
            {
                Debug.Print("Serial Port is open.");
            }

            InterruptPort button = new InterruptPort(Pins.ONBOARD_SW1, false, Port.ResistorMode.Disabled, Port.InterruptMode.InterruptEdgeBoth);
            button.OnInterrupt += new NativeEventHandler(button_OnInterrupt);

            Thread.Sleep(Timeout.Infinite);
        }

        static void button_OnInterrupt(uint data1, uint data2, DateTime time)
        {
            // Set the led
            led.Write(data2 == 0);

            // Encode to UTF8
            UTF8Encoding enc = new UTF8Encoding();
            // Encode Byte array
            string tempString = "Button Press";
            byte[] tempData = enc.GetBytes(tempString);
            // Write to Serial Port
            int bytesWritten = MyserialPort.Write(tempData, 0, tempData.Length);

            Debug.Print("Bytes Written" + bytesWritten.ToString());
        }
    }
}

I did look at the document on adding the reference to Microsoft.SPOT.Hardware.SerialPort and I think I did it correctly. Do I need to add a Using statement for Microsoft.SPOT.Hardware.SerialPort as well? I tried it and it didn't like it so I left it out

Oh and I boosted the code from someone else on here so if it was you don't get mad :P

Thanks

#2 Steven Behnke

Steven Behnke

    Advanced Member

  • Members
  • PipPipPip
  • 70 posts
  • LocationLas Vegas, NV

Posted 27 August 2010 - 02:49 AM

The using statement like this:

using (MyserialPort)
{
  MySerialPort.Open();
  ...
}

Is the same as:

MyserialPort.Open();
...
MyserialPort.Dispose();

Using is quite nice to make sure you don't forget to call Dispose on anything that implements the IDisposable interface, but beyond that it isn't much different.

#3 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 27 August 2010 - 03:08 AM

Hi nightlynx, This appears to be a compilation error in Visual Studio... Could you try the following two troubleshooting steps? 1. Reboot your PC. Can you compile now? 2. If #1 fails, please install all Windows Update "critical" and "important" updates--especially patches to .NET. Can you compile now? We've only ever seen this happen once before, and these steps fixed the problem... Try resetting as a first step though... Chris

#4 amphibian

amphibian

    Member

  • Members
  • PipPip
  • 16 posts

Posted 27 August 2010 - 03:33 AM

Using is quite nice to make sure you don't forget to call Dispose on anything that implements the IDisposable interface, but beyond that it isn't much different.


They're only the same if there's no exceptions and nothing calls "return"

With "using", if an exception occurs or if something inside the "using" calls "return", then the serial port would still be disposed properly.
if you do open.....dispose, and an exception occurs between them, it would leave the port active until it went out of scope and got gc'd (an unknown amount of time).

That's why "using" is considered the best practice instead of calling Dispose manually.

That said, I don't think this difference is what's causing your problem.

#5 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 27 August 2010 - 03:59 AM

I think nightlynx was talking about using in the "using [namespace] at top of code" context... But I might be wrong...

#6 Steven Behnke

Steven Behnke

    Advanced Member

  • Members
  • PipPipPip
  • 70 posts
  • LocationLas Vegas, NV

Posted 27 August 2010 - 04:14 AM

I think nightlynx was talking about using in the "using [namespace] at top of code" context... But I might be wrong...


Oh. Haha. You're probably right.

#7 nightlynx

nightlynx

    New Member

  • Members
  • Pip
  • 3 posts

Posted 29 August 2010 - 02:57 AM

You were right I needed to update my .net sdks. It's working great now!




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.