External trigger to launch SharpCap script

Anything that doesn't fit into any of the other forums
Post Reply
GreggKing
Posts: 3
Joined: Wed May 10, 2017 9:15 pm

External trigger to launch SharpCap script

#1

Post by GreggKing »

Hi,
I'm using a raspberry pi to control some hardware using python 3.4. My camera is a QHY178 and I'm currently using SharpCap python scripts to take images. I'd like to coordinate the two functions. Is there a way to send a trigger from the pi to SharpCap or the camera to initiate image capture? Alternately, is there a way to control the camera directly from the pi using python 3.4? Thanks. Gregg
User avatar
admin
Site Admin
Posts: 13177
Joined: Sat Feb 11, 2017 3:52 pm
Location: Vale of the White Horse, UK
Contact:

Re: External trigger to launch SharpCap script

#2

Post by admin »

Hi Gregg,

with SharpCap 3.0 it should be possible to set up the 'Pyro' python remoting server within SharpCap and then send it commands from other python programs on the same or other systems. I got this working in a very basic form when testing the updates to the scripting module.

Note that the python within SharpCap is 2.7 - not sure if this will give you issues if you are using 3.4.

cheers,

Robin
pgn
Posts: 17
Joined: Wed Mar 08, 2017 9:46 am

Re: External trigger to launch SharpCap script

#3

Post by pgn »

Gregg

Can't help but if you get this working please let us know how.

I use files as flags using Spyder3 (python IDE) controlling an Arduino; running on same laptop as SharpCap' so different though similar problems.

Good luck.
User avatar
admin
Site Admin
Posts: 13177
Joined: Sat Feb 11, 2017 3:52 pm
Location: Vale of the White Horse, UK
Contact:

Re: External trigger to launch SharpCap script

#4

Post by admin »

If you update to the latest build of 3.0, you should be able to get Pyro4 up and running inside SharpCap (see https://pythonhosted.org/Pyro4/intro.html for an introduction). This lets you send messages from another python process to the pyton within SharpCap.

Two things to be aware of:

* You can't 'expose' the built in SharpCap object model types using pyro as they are .NET types and not compatible with this - you have to define your own types to expose that then call the SharpCap object model
* Except for testing, don't run the pyro server (ie daemon.requestLoop() ) in the main scripting thread otherwise you will lock up SharpCap - create a thread to run the request loop.

There are probably other approaches - for instance you could probably run a very simple python web server inside SharpCap that accepted commands via web requests.

cheers,

Robin
GreggKing
Posts: 3
Joined: Wed May 10, 2017 9:15 pm

Re: External trigger to launch SharpCap script

#5

Post by GreggKing »

Hi all,
I was unable to get Pyro4 to work. My Windows 7 box would refuse the connection. I tried turning off my antivirus and firewall but it didn't help. Possibly an encryption problem?

What I was able to make work is listed below, using Python2.7.

The remaining problem is that I have been unable to exit the SharpCap scripting window gracefully (or pretty much any python script running anywhere). The red stop button doesn't work and ctrl-c doesn't work. As a result, the port is left open. If I close the scripting window and reopen it nothing has changed. The only way to stop the script is to restart SharpCap. If I reload the script again it won't run because the port was left open. The only way to fix this is to change to a new port number in both the server and the client. Any suggestions regarding how to stop the script gracefully so I can close the port before exiting? This seems to be a very common Python problem and I have yet to find a way around it.

Setup a server within SharpCap

import socket, time, sys
s = socket.socket()
host = "192.168.1..." #the ip address of the SharpCap computer
port = 5035
s.bind((host,port))
s.listen(5)
c, addr = s.accept()
print("Connection accepted from " + repr(addr[1]))
c.send("Server approved connection\n")
while True:
print repr(addr[1]) + ": " + c.recv(1026)
time.sleep(.5)

Once the server is running, setup a client on the Pi

import socket, time
num = 0
s = socket.socket()
host = "192.168.1..." #the ip address of the SharpCap computer, not the Pi
port = 2035 #must be the same as the server port
s.connect((host, port))
print s.recv(1024)
while True:
s.send(str(num))
num +=1
print "the message has been sent"
time.sleep(3)
pgn
Posts: 17
Joined: Wed Mar 08, 2017 9:46 am

Re: External trigger to launch SharpCap script

#6

Post by pgn »

Firstly - doing something slightly different. Namely running python 3 on a Windows 7 PC.
I have similar problems when running your script. However the following seems to work:
1) click on the stop running script button in SharpCap python console (I suspect it also stops for you - though port stays open)
2) run "s.close()" in SharpCap

When I run line 2) the "client" script in the Python 3 ide drops out with a ConnectyionResetError.

I can then run the SharpCap server script again and restart the client script.

I also changed the port number in your scripts to be the same.

Will update if I learn anything else but hopefully this should work the same for you.
GreggKing
Posts: 3
Joined: Wed May 10, 2017 9:15 pm

Resolved

#7

Post by GreggKing »

Hi all, Thanks for your help. They key was as follows:
1. Always start the server first.
2. Start the client.
3. Always end the client first and socket.close().
4 Then stop the server and also socket.close().
Then the port number can be reused. I used Python 2.7 for both ends because it works with simple strings. Python 3.4 and up require Unicode with I was not able to use successfully.
One more challenge: How to interrupt an ongoing python script gracefully so that the port can be closed as the script cleans up, rather than using Ctrl-c and having to manually close the port at both ends.
Post Reply