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

SerialPort as given parameter?


  • Please log in to reply
5 replies to this topic

#1 emence

emence

    Member

  • Members
  • PipPip
  • 17 posts

Posted 19 June 2012 - 04:07 PM

Hi!

I'm trying to use SerialPort as a parameter for my other classes constructor:

serial1 = new SerialPort(SerialPorts.COM1, Constants.baud_cam, Parity.None, 8, StopBits.One);
serial1.DataReceived += new SerialDataReceivedEventHandler(camera_DataReceived);
serial1.Open();
Ucamttl camera = new Ucamttl(serial1);

Ucamttl class:
SerialPort serial;

    public Ucamttl(SerialPort sp)
    {
        serial = sp;
    }

I can't seem to use the serialPort in my Ucamttl class. It works well in the main routine though.
The error occurs when the ucamttl class tries to write to the serial ( serial.Write(_command,0,Constants.CMD_SIZE); )

    #### Exception System.NullReferenceException - CLR_E_NULL_REFERENCE (1) ####
    #### Message: 
    #### Thermocam.Program::SyncCam [IP: 0007] ####
    #### Thermocam.Program::Main [IP: 008d] ####

Since I'm a noob there are a lot of questions..
- In which class do i need to open the port?
- In which class do i need to implement the event handlers?
- and if, then how, and why?

Thanks for helping! :blink:

#2 Stefan

Stefan

    Moderator

  • Members
  • PipPipPip
  • 1965 posts
  • LocationBreda, the Netherlands

Posted 19 June 2012 - 04:25 PM

Hi and welcome to the forums, Could it be that the garbage collector does this? Could you add a Thread.Sleep(Timeout.Infinite); at the bottom of your main method?
"Fact that I'm a moderator doesn't make me an expert in things." Stefan, the eternal newb!
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs

#3 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 19 June 2012 - 05:26 PM

Do you have by any chance another instance of "Ucamttl camera" declared above your Main method? If not, could you please post the whole source code, as the above snippets do not show what is happening inside SyncCam?

#4 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 20 June 2012 - 04:44 AM

As Stefan and CW2 alluled, the most likely scenario is that your SerialPort is not being passed properly...or is getting cleaned up by the garbage collector. If you can post a bit more of your code, we can help diagnose a bit deeper. Chris

#5 emence

emence

    Member

  • Members
  • PipPip
  • 17 posts

Posted 20 June 2012 - 10:40 AM

Do you have by any chance another instance of "Ucamttl camera" declared above your Main method?


Yes. exactly! :rolleyes:

Of course I used a static Object! And declared a new one with the same name when calling the constructor!
Ahhh... such a simple mistake! Everything is working properly now! :lol:

Thanks for Your time!

#6 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 20 June 2012 - 11:15 AM

Yes. exactly! :rolleyes:

Of course I used a static Object! And declared a new one with the same name when calling the constructor!
Ahhh... such a simple mistake! Everything is working properly now! :lol:

Good catch, CW2!

Glad it's working for you, emence. :)

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.