Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6592773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:35:52+00:00 2026-05-25T17:35:52+00:00

I need to request values using functions in a DLL provided by the manufacturer

  • 0

I need to request values using functions in a DLL provided by the manufacturer of my particular piece of hardware (a weather station). I’m new to C#, and the concepts of delegates/events are tough to wrap my head around. Nonetheless, I’ve managed to pull the functions from the DLL and verify that data makes it through. My issue is with polling the instrument periodically with a Timer. In Initialize(), an object is instantiated, but the event isn’t handled leaving the object null. I’m out of ideas, and would like some advice!

public class HardwareData : EventArgs
{
    public float OutsideTemp { get; set; }
    public int OutsideHum { get; set; }
    public float WindSpeed { get; set; }
    public int WindDirection { get; set; }
}

public class Hardware : IDisposable
{
    private static Hardware v;
    private System.Timers.Timer hardwareTimer;
    private int counter = 0;
    private static readonly object padlock = new object();
    public static Hardware Instance
    {
        get
        {
            lock (padlock)
            {
                if (v == null)
                    v = new Hardware();
                return v;
            }
        }
    }

    public void Initialize()
    {
        try
        {
            hardwareTimer = new System.Timers.Timer(500);
            hardwareTimer.Elapsed += new ElapsedEventHandler(hardwareTimer_Elapsed);
            HardwareVue.OpenCommPort_V(3, 19200); //COM port and baud rate are verified.
            hardwareTimer.Start();
        }
        catch (Exception ex)
        {
            throw new InvalidOperationException("Unable to initialize.", ex);
        }
    }

    public HardwareData LastHardware { get; set; }

    void hardwareTimer_Elapsed(object sender, ElapsedEventArgs e)
    {
        try
        {
            counter += 1;
            Console.WriteLine(counter);
            HardwareVue.LoadCurrentHardwareData_V();
            HardwareData v = new HardwareData()
            {
                OutsideTemp = HardwareVue.GetOutsideTemp_V(),
                OutsideHum = HardwareVue.GetOutsideHumidity_V(),
                WindSpeed = HardwareVue.GetWindSpeed_V(),
                WindDirection = HardwareVue.GetWindDir_V()
            };
            LastHardware = v;
        }
        catch (Exception) { }
    }

    public void Dispose()
    {
        HardwareVue.CloseCommPort_V();
        hardwareTimer.Stop();
    }
}


[TestClass]
public class UnitTest1
{
    [TestMethod]
    public void TestMethod1()
    {
        Hardware test = new Hardware();
        try
        {
            if (test != null)
            {
                test.Initialize();
                test.Dispose();
                Assert.AreEqual(0, test.LastHardware.OutsideHum);
            }
        }
        catch (NullReferenceException ex)
        {
            Console.WriteLine("Object is null.");
        }
        // Console.WriteLine(test.LastHardware.OutsideHum);

    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T17:35:52+00:00Added an answer on May 25, 2026 at 5:35 pm

    When working with timers, you need to enable the timer and make sure the events are rasied:

    hardwareTimer.Enabled = true;
    hardwareTimer.CanRaiseEvents = true;
    

    For Reference: Timers on MSDN

    Edit
    In addition to the other comments to both the OP’s question and this answer, the issue with the LastHardware being null is due to the property never being instantiated before the timer initially fires. To resolve this, you should instantiate the LastHardware property in the default constructor (or in the Initialize method):

    public Hardware()
    {
      LastHardware = new HardwareData();
    }
    

    Of course, you’d want to set some default values upon instantiation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to execute a http web request from Plesk's Task Scheduler (using shared
I need to detect if a request cookie - value is different from a
For my current project, I need to request XML data over a tcp/ip socket
I have code that does a web-service request. While doing this request I need
I need to create a request for a web page delivered to our web
I need loging all HTTP request (from any application). I have Delphi 7.0. Anybody
I need to send GET Request method with the below headers . I am
I need to make an AJAX request from a website to a REST web
I need to geocode around 200 addresses per request and do around 3,000 request
I need to make a cURL request to a https URL, but I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.