-3

I have a large public class which called Telescope. I have created a new form (form1.cs) and I want to call some of the public void that the class has.

I do something like this in the form to initialise the class

Telescope controls = new Telescope(); controls.CommandString("Gs#",true);

After that I can see the all the methods but it fails in the execution as the class is already initialized and there is an existing serial port connection ongoing, so it reports that there is no serial port connection.

Any help? How can I use the existing methods from the new form?

Telescope class is in Driver.cs

public string CommandString(string command, bool raw)
{
    CheckConnected("CommandString");
    serialPort.ClearBuffers();
    serialPort.Transmit(command);
    return serialPort.ReceiveTerminated("#");

}

When I use the CommandString in the Driver.cs (where the telescope class is) it works. It does not work from the form1.cs

I get an exception:

************** Exception Text ************** 
ASCOM.NotConnectedException: CommandString
evans
  • 23
  • 4
  • This works. Now what exactly happens in your Telescope class and why it doesn't work only you can know unless you publish the code. – Ivan Ičin Aug 27 '16 at 09:10
  • @IvanIčin he is trying to connect to a serial port... which is somewhat outside the scope of C#. But a comm port can only maintain one connection at a time. – Joe_DM Aug 27 '16 at 09:11
  • Wait, I don't want to connect to the serial port again. I just want to utilise the existing connection. Let me edit the post – evans Aug 27 '16 at 09:15

2 Answers2

0

Ideally you would be using some sort of IoC container and your class would implement an interface containing the bare minimal methods to interface with your serial connection. The IoC container would then manage the lifetime of the instance as a singleton and on every request to resolve the interface would pass you back the existing instance.

Since this may not be the case and since only a single instance can access the serial port, you could move these methods into a static class... But be careful, when you start sharing static methods, unexpected bugs can arise. Depending on how the code is structured, you would want only the serial connection to be static.

One example of how this could be implemented in a class:

private Lazy<SerialConnection> _serialConnection =new Lazy<SerialConnection>(StaticClass.GetStaticSerialConnection);
public SerialConnection MySerialConnection
{
  get { return _serialConnection.Value; }
}
Joe_DM
  • 985
  • 1
  • 5
  • 12
  • I modified the post for more information. I think it was obscure and the solution is simplier – evans Aug 27 '16 at 09:27
  • @evans I believe my answer covers the question correctly. The issue is that you can't open two serial connections on one comm port. For example, if you open up putty and try to connect on the port it too will fail. You will need to share the comm connection somehow. – Joe_DM Aug 27 '16 at 11:38
0

You should keep the reference to your first (and only) instance of Telescope class somewhere in your app and then access it by that reference. The reference could be kept in some static class, you could initialize it there and always call it using that static class.

Ivan Ičin
  • 9,672
  • 5
  • 36
  • 57
  • What's the difference between treating an instance like static and passing it around like static, and just using the method as static? – Joe_DM Aug 27 '16 at 11:39