Send/riceve UDP message
-
- Posts: 6
- Joined: Sun Nov 01, 2020 9:16 am
Send/riceve UDP message
Hi!
is it possible to send and receive UDP messages with scripts at the same time?
I tried but I keep getting an "import soked" error.
The goal is to pause the photo session and then resume it with a UDP message.
import socket
UDP_IP = "xxx.xxx.xxx.xxx"
UDP_PORT = 12000
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, UDP_PORT))
while True:
data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
if ="SUSPEND"
***code for suspension***
I also have to send a command (for close observatory) when the photo session is end, so a trigger would be needed to fire at the end of the photo session
end session code
import socket
UDP_IP = "xxx.xxx.xxx.xxx"
UDP_PORT = 12000
MESSAGE = "CLOSE""
print("UDP target IP: %s" % UDP_IP)
print("UDP target port: %s" % UDP_PORT)
print("message: %s" % MESSAGE)
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock.sendto(MESSAGE, (UDP_IP, UDP_PORT))
is it possible to send and receive UDP messages with scripts at the same time?
I tried but I keep getting an "import soked" error.
The goal is to pause the photo session and then resume it with a UDP message.
import socket
UDP_IP = "xxx.xxx.xxx.xxx"
UDP_PORT = 12000
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, UDP_PORT))
while True:
data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
if ="SUSPEND"
***code for suspension***
I also have to send a command (for close observatory) when the photo session is end, so a trigger would be needed to fire at the end of the photo session
end session code
import socket
UDP_IP = "xxx.xxx.xxx.xxx"
UDP_PORT = 12000
MESSAGE = "CLOSE""
print("UDP target IP: %s" % UDP_IP)
print("UDP target port: %s" % UDP_PORT)
print("message: %s" % MESSAGE)
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock.sendto(MESSAGE, (UDP_IP, UDP_PORT))
- admin
- Site Admin
- Posts: 13287
- Joined: Sat Feb 11, 2017 3:52 pm
- Location: Vale of the White Horse, UK
- Contact:
Re: Send/riceve UDP message
Hi,
not something that I've tried, but I can't see why it couldn't be made to work.
Your first block of code loops forever, so if you are going to start that running then you need to start it in a separate thread otherwise you will block the Python interpreter thread forever and won't be able to run any other code.
You could also consider something a bit more structured that has been made to work in the past – using the Pyro remoting engine
viewtopic.php?t=302
Cheers, Robin
not something that I've tried, but I can't see why it couldn't be made to work.
Your first block of code loops forever, so if you are going to start that running then you need to start it in a separate thread otherwise you will block the Python interpreter thread forever and won't be able to run any other code.
You could also consider something a bit more structured that has been made to work in the past – using the Pyro remoting engine
viewtopic.php?t=302
Cheers, Robin
-
- Posts: 6
- Joined: Sun Nov 01, 2020 9:16 am
Re: Send/riceve UDP message
Hi!
Thanks for the answer. I also have another solution but I don't know if it can be done.
Are there APIs that can be used with Visul C or Vb.net to communicate with sharp?
If that's not possible I'll go ahead with the scripts.
Riccardo.
Thanks for the answer. I also have another solution but I don't know if it can be done.
Are there APIs that can be used with Visul C or Vb.net to communicate with sharp?
If that's not possible I'll go ahead with the scripts.
Riccardo.
-
- Posts: 6
- Joined: Sun Nov 01, 2020 9:16 am
Re: Send/riceve UDP message
Code: Select all
SyntaxError: expected an indented block>>>
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: No module named socket
error I get
-
- Posts: 6
- Joined: Sun Nov 01, 2020 9:16 am
Re: Send/riceve UDP message
Code: Select all
import socket
import string
import clr
import time
UDP_IP = "xxx.xxx.xxx.xxx"
UDP_PORT = 11000
MESSAGE = b"CLOSEOSS"
def executeSomething():
camera = str(SharpCap.SelectedCamera)
print(camera)
if ('None' in camera):
print("Not Camera connect")
time.sleep(60)
else:
print("UDP target IP: %s" % UDP_IP)
print("UDP target port: %s" % UDP_PORT)
print("message: %s" % MESSAGE)
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock.sendto(MESSAGE, (UDP_IP, UDP_PORT))
time.sleep(60)
while True: AttributeError: 'NoneType' object has no attribute 'Controls'
I'm experimenting, now I have this error: with this command: SharpCap.SelectedCamera.Controls.Exposure.AutoAvailable
Re: Send/riceve UDP message
Spectre,
I use TCP/IP for communication between the client (SharpCap) and the server (Raspberry PI), which controls a stepper motor. It has worked flawlessly for two years. I've copied snippets that you should be able to get working:
Brian
I use TCP/IP for communication between the client (SharpCap) and the server (Raspberry PI), which controls a stepper motor. It has worked flawlessly for two years. I've copied snippets that you should be able to get working:
Brian
Code: Select all
import socket
class MySocket:
def __init__(self, sock = None, delimiter = "|"):
if sock is None:
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
else:
self.sock = sock
self.delimiter = delimiter
self.recv_buffer = ""
def connect(self, host, port):
self.sock.connect((host, port))
def send(self, msg):
msg = self.delimiter + msg + self.delimiter
total_length = len(msg)
total_sent = 0
while total_sent < total_length:
sent = self.sock.send(msg[total_sent:])
if sent == 0:
raise RuntimeError("socket connection broken")
total_sent = total_sent + sent
def receive(self):
while True:
if len(self.recv_buffer) > 0:
if self.recv_buffer[0] != self.delimiter:
raise RuntimeError("expected delimiter in message")
end_delim = self.recv_buffer.find(self.delimiter, 1)
if end_delim != -1:
result = self.recv_buffer[1:end_delim]
self.recv_buffer = self.recv_buffer[end_delim+1:]
return result
chunk = self.sock.recv(4096)
if chunk == '':
raise RuntimeError("socket connection broken")
self.recv_buffer = self.recv_buffer + chunk
def close(self):
self.sock.close()
# --------------------------------------------------------------
# Windows
# IronPython Client:
comm = MySocket()
host = '192.168.0.200'
port = 12345
comm.connect(host, port)
comm.send("pec_start")
msg = comm.receive()
if msg == "wait":
time.sleep(2)
comm.close()
# --------------------------------------------------------------
# Raspberry PI
# Python Server:
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
host = '192.168.0.200'
port = 12345
server_socket.bind((host, port))
server_socket.listen(0)
while quitter.is_set() == False:
print('Listening for client connection')
(client_socket, address) = server_socket.accept()
print "Got connection from %s:%d" % (address[0], address[1])
client_thread = ClientThread(client_socket, quitter)
client_thread.start()
client_thread.join()
print "Main terminating"
class ClientThread(Thread):
def __init__(self, client_socket, quitter):
Thread.__init__(self)
self.socket = MySocket(client_socket)
self.quitter = quitter
def run(self):
print "ClientThread started"
while self.quitter.is_set() == False:
msg = self.socket.receive()
if msg == "":
print "Socket connection broken"
break
cmd = msg.split(',')
print "Command: %s" % cmd[0]
if cmd[0] == "shutdown":
elif cmd[0] == "enable_stepper":
elif cmd[0] == "get_status":
response = "%r,%s,%f,%d,%d,%d,%d" % self.tracking.get_status()
self.socket.send(response)
elif cmd[0] == "set_rate_multiplier":
value = float(cmd[1])
self.tracking.scale(value)
elif cmd[0] == "pec_start":
elif cmd[0] == "pec_mark":
elif cmd[0] == "pec_data":
elif cmd[0] == "ppec_init":
elif cmd[0] == "ppec_data":
elif cmd[0] == "ppec_boot":
elif cmd[0] == "pulse":
print "Closing socket"
self.socket.close()
print "ClientThread ended"
-
- Posts: 6
- Joined: Sun Nov 01, 2020 9:16 am
Re: Send/riceve UDP message
Hi, thanks for the code which is very useful.
But the problem is (see error on previous post) when I try to use the properties of the camera, to understand if the session is still in progress or end.
See:
Sorry.
But the problem is (see error on previous post) when I try to use the properties of the camera, to understand if the session is still in progress or end.
See:
Code: Select all
AttributeError: 'NoneType' object has no attribute 'Controls
- admin
- Site Admin
- Posts: 13287
- Joined: Sat Feb 11, 2017 3:52 pm
- Location: Vale of the White Horse, UK
- Contact:
Re: Send/riceve UDP message
Hi,
it sounds as though you don't have a camera open at that point in time, so there's no camera object to ask for the controls from. Basically SharpCap.SelectedCamera will be 'None' when no cameras open.
Cheers, Robin
it sounds as though you don't have a camera open at that point in time, so there's no camera object to ask for the controls from. Basically SharpCap.SelectedCamera will be 'None' when no cameras open.
Cheers, Robin