UdpHelpers.py
Go to the documentation of this file.
1 # MIT License
2 #
3 # Copyright (c) 2017 Hordur K Heidarsson / USC RESL
4 #
5 # Permission is hereby granted, free of charge, to any person obtaining a copy
6 # of this software and associated documentation files (the "Software"), to deal
7 # in the Software without restriction, including without limitation the rights
8 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 # copies of the Software, and to permit persons to whom the Software is
10 # furnished to do so, subject to the following conditions:
11 #
12 # The above copyright notice and this permission notice shall be included in all
13 # copies or substantial portions of the Software.
14 #
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 # SOFTWARE.
22 
23 # Adapted from https://github.com/uscresl/piksi_ros/blob/master/src/piksi_driver.py
24 
25 from socket import *
26 from sbp.client.drivers.base_driver import BaseDriver
27 from sbp.client import Handler, Framer
28 from collections import deque
29 import threading
30 
31 # Driver class for handling UDP connections for SBP
32 class UDPDriver(BaseDriver):
33  def __init__(self, host, port):
34  self.buf = deque()
35  self.handle = socket(AF_INET, SOCK_DGRAM)
36  self.handle.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
37  try:
38  #self.handle.connect((host, port))
39  self.handle.bind(("", port))
40  except socket.error, msg:
41  pass
42  super(UDPDriver, self).__init__(self.handle)
43  self._write_lock = threading.Lock()
44 
45  def read(self, size):
46  if len(self.buf) < size:
47  try:
48  data, addr = self.handle.recvfrom(4096)
49  if not data:
50  print "PIKSI UDP ERROR - no data from " + str(addr)
51  for d in data:
52  self.buf.append(d)
53  except socket.error, msg:
54  print "PIKSI UDP ERROR " + str(msg)
55 
56  res = ''.join([self.buf.popleft() for i in xrange(min(size, len(self.buf)))])
57  return res
58 
59  def flush(self):
60  pass
61 
62  def write(self, s):
63  return
64  """
65  Write wrapper.
66  Parameters
67  ----------
68  s : bytes
69  Bytes to write
70  """
71  try:
72  self._write_lock.acquire()
73  self.handle.sendall(s)
74  except socket.error, msg:
75  raise IOError
76  finally:
77  self._write_lock.release()
78 
79 
80 
82 
83  def __init__(self, broadcast, port):
84  self.multicast_address = broadcast
85  self.port = port
86  self.socket = socket(AF_INET, SOCK_DGRAM)
87  self.socket.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)
88 
89  def sendPacket(self, data):
90  self.socket.sendto(data, (self.multicast_address, self.port))
91 
92 
93 
95  def __init__(self, broadcast, port):
96  UdpMulticaster.__init__(self, broadcast, port)
97 
98  def sendSbpPacket(self, sbp_data):
99  self.sendPacket(sbp_data.pack())
100 
101 
103  def __init__(self, port, ext_callback):
104  self._callback = ext_callback
105  self.driver = UDPDriver(' ', port)
106  self.framer = Framer(self.driver.read, None, verbose=False)
107  self.piksi = Handler(self.framer)
108  self.piksi.add_callback(self.recv_callback)
109  self.piksi.start()
110 
111  def recv_callback(self, msg, **metadata):
112  self._callback(msg, **metadata)
def __init__(self, broadcast, port)
Definition: UdpHelpers.py:83
def __init__(self, broadcast, port)
Definition: UdpHelpers.py:95
def __init__(self, host, port)
Definition: UdpHelpers.py:33


piksi_multi_rtk
Author(s):
autogenerated on Mon Jun 10 2019 13:08:19