Jump to content

Banner.jpg.b83b14cd4142fe10848741bb2a14c66b.jpg

C# Help please!


blinky

Recommended Posts

I think there may well be a few questions asked in this thread......  Im pretty much a complete beginner when it comes to c# and programming in general.  I have dabbled in the past but that was a few years ago and I think Ive forgotten everything I learned back then!

Im looking to write some code that will operate via an Arduino a roll of roof - this is going to be built next year but Im starting on the software at the moment....

So, I want to get a list of the available com ports and populate a list box with this to allow the user to select, then connect to that port.  Ive created a ObservatorySerialPort class and have a method (think thats the terminology) to get a list of the com ports:

using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ObservatoryControl
{
    class ObservatorySerialPort
    {       
        public string[]  GetAvailableComPorts()
        {
           string[] ListOfSerialPorts = SerialPort.GetPortNames();
           return ListOfSerialPorts;            
        }
    } 

}
I then have a Win form, with a button and in the button click method I want to call ObservatorySerialPort.GetAvailableComPorts() method to populate the list box:

private void ButtonQuerySerialPort_Click(object sender, EventArgs e)
        {
            ListOfSerialPports = ObservatorySerialPort.GetAvailableComPorts();
        }

 

But this gives me an error: An object reference is required for the non static field - Im now lost! What do I need to do to fix this?

Link to comment
Share on other sites

Method you are trying to call is not static / class method, this means you need to have instance of the class in order to call that method.

Here is what you should do:

private void ButtonQuerySerialPort_Click(object sender, EventArgs e)
{

ObservatorySerialPort myPort = new ObservatorySerialPort();
ListOfSerialPports = myPort.GetAvailableComPorts();
}

Also, consider having myPort as class variable rather than local variable in one method if you are going to reuse that port.

Link to comment
Share on other sites

Am I doing this correctly..... Im loooking to have the arduino control 3 or 4 12v outputs to power the mount etc and to switch the roof to open or close.  My thinking is that I should have a class with a serial port and that once I create the serial port I should pass it around.  So if I want to open the roof, I call the open roof method in the roof class and pass in the serial port in my call to the method.  This would be the same if I want to power the mount on - I would call the 'mount on' method in the mount class and pass in the serial port in the call to the method.  Is that the best OOP way to go - create a class for roof, mount and say, lights and pass my serial port 'object' to each of them when calling any of the methods?  All the methods will do is send a letter to the arduino to enable/disable one of the digitial outputs.

Link to comment
Share on other sites

52 minutes ago, blinky said:

Am I doing this correctly..... Im loooking to have the arduino control 3 or 4 12v outputs to power the mount etc and to switch the roof to open or close.  My thinking is that I should have a class with a serial port and that once I create the serial port I should pass it around.  So if I want to open the roof, I call the open roof method in the roof class and pass in the serial port in my call to the method.  This would be the same if I want to power the mount on - I would call the 'mount on' method in the mount class and pass in the serial port in the call to the method.  Is that the best OOP way to go - create a class for roof, mount and say, lights and pass my serial port 'object' to each of them when calling any of the methods?  All the methods will do is send a letter to the arduino to enable/disable one of the digitial outputs.

I would say that is sensible.

Link to comment
Share on other sites

Cheers - want to use this as a learning exercise!  My head does struggle sometimes with OOP and why its useful if it leads to lots more code and what seems to me to be more complex code but.... I have to say its slowly starting to make sense... 

Link to comment
Share on other sites

OK - next question - I have it all sort of working, the user can list the com ports, select one then connect to it.  However I want to then stop you from clicking connect again and creating another comport object.  So in my OpenComPort method I put in an If statement to check if the port is open already, however..... As the first part of the method instantiates a new serial port, when it comes to the if statement its never going to be open!  I tried moving the SerialPort ObservatoryCOMPort = new SerialPort(); line to the top of the class but then I get the object reference required error again arghh....

 

public static SerialPort OpenComPort(string SelectedComPort)
        {
           
            SerialPort ObservatoryCOMPort = new SerialPort();
            ObservatoryCOMPort.PortName = SelectedComPort;

            if (!ObservatoryCOMPort.IsOpen)
            {
                
                ObservatoryCOMPort.Open();
                return ObservatoryCOMPort;
            }
            if (ObservatoryCOMPort.IsOpen)
            {
                return ObservatoryCOMPort;
            }
            return null;
        }

Link to comment
Share on other sites

Try something like this:

public static class ObservatoryPortProvider

{

private SerialPort ObservatoryCompPort = null;

public static SerialPort GetObservatoryComPort(string SelectedComPort)

{

if(ObservatoryComPort==null) {

ObservatoryComPort = new SerialPort();

ObservatoryCOMPort.PortName = SelectedComPort;

ObservatoryComPort.Open();

}

return ObservatoryComPort;

}

public static void CloseObservatoryComPort()

{

if(ObservatoryComPort!=null) {

ObservatoryComPort.Close(); //I'm just guessing there is close method

ObservatoryComPort = null;

}

}

}

With above class you can then do something like:

MyComPort = ObservatoryPortProvider.GetObservatoryComPort("COM1"); //or however you intend to call it ...

It will always return the same instance of the com port. You can close it once you no longer need it with:

ObservatoryPortProivder.CloseObservatoryComPort();

 

Link to comment
Share on other sites

8 minutes ago, blinky said:

on this line: private SerialPort ObservatoryCompPort = null; I get: cannot declare instance members in a static class

Ah sorry, that should be private static SerialPort ...

(static member in static class)

Link to comment
Share on other sites

private void BtnConnect_Click(object sender, EventArgs e)
        {
            if (ObservatoryComPort != null) { }
            else
            {
                string SelectedComPort = (string)ListBoxSerialPorts.SelectedItem;
                ObservatoryComPort = ObservatorySerialPort.OpenComPort(SelectedComPort);
            }
           

        }

 

That works but is that a no no since its checking the status of the serial port from within the form?

Link to comment
Share on other sites

2 minutes ago, blinky said:

That works but is that a no no since its checking the status of the serial port from within the form?

Depends on how you look at it - if it works for you and won't cause any issues down the line - then it is fine. If you are strict in how you design your software then yes, it's sort of a code smell.

 

Link to comment
Share on other sites

58 minutes ago, AlexK said:

Programming lessons on the Astronomy forum? That's... at least inefficient...
I would recommend stackoverflow.com instead, And https://stackoverflow.com/questions/tagged/c%23 in particular for C#
I can assure you, you'll find more than just 2 folks coding in C# there :D 

Yeah but I find the answers there a little more erm... cryptic to understand!

Link to comment
Share on other sites

Maybe just look around there first. There are plenty of examples how to ask a question to get it registered and answered. I'm using that forum for eons (but mostly for Android Java, my C# years time is well in the distant past).

Edited by AlexK
Link to comment
Share on other sites

OK - this is more astro related so hoping somebody here can help rather than a generic programming forum:

I can get the ascom chooser box to appear with the code below, but I cant get it to actually connect to the scope - when the chooser pops up I select the simulator and then click OK but nothing happens, the simulator does not appear, what am I missing?

public  static void OpenChooser()
        {
            Chooser chooser = new Chooser();
           string SelectedTelescope =  chooser.Choose("");

        }

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue. By using this site, you agree to our Terms of Use.