set_urg_ip.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 # Copyright (c) 2014 Unbounded Robotics Inc.
4 # All right reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions are met:
8 #
9 # * Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
11 # * Redistributions in binary form must reproduce the above copyright
12 # notice, this list of conditions and the following disclaimer in the
13 # documentation and/or other materials provided with the distribution.
14 # * Neither the name of Unbounded Robotics Inc. nor the names of its
15 # contributors may be used to endorse or promote products derived
16 # from this software without specific prior written permission.
17 #
18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 # DISCLAIMED. IN NO EVENT SHALL UNBOUNDED ROBOTICS INC. BE LIABLE FOR ANY DIRECT, INDIRECT,
22 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24 # OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25 # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26 # OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27 # ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 
29 """
30 Change the IP address of a Hokuyo URG Laser
31 """
32 
33 import argparse
34 import socket
35 
36 def parse_and_validate_ipv4(argument, name):
37  """
38  Each address must have 4
39  """
40  if len(argument.split(".")) != 4:
41  print("Invalid %s, must be of the form xxx.yyy.zzz.www" % name)
42  exit(-1)
43  parsed = ""
44  for x in argument.split("."):
45  if len(x) > 3:
46  print("Invalid %s, must be of the form xxx.yyy.zzz.www" % name)
47  exit(-1)
48  while len(x) < 3:
49  x = "0" + x
50  parsed += x
51  return parsed
52 
53 if __name__ == "__main__":
54  parser = argparse.ArgumentParser(description=__doc__)
55  parser.add_argument('new_ip', help='The desired IP address for the laser')
56  parser.add_argument('new_gw', help='The desired gateway for the laser')
57  parser.add_argument('--nm', help='The desired netmask for the laser', default="255.255.255.0")
58  parser.add_argument('--ip', help='The current IP address of the laser', default="192.168.0.10")
59  args = parser.parse_args()
60 
61  # Packet starts with $IP, ends with \x0a, contains:
62  # 12 character IP
63  # 12 character netmask
64  # 12 character gateway
65  ip = parse_and_validate_ipv4(args.new_ip, "IP address")
66  gw = parse_and_validate_ipv4(args.new_gw, "gateway address")
67  nm = parse_and_validate_ipv4(args.nm, "netmask")
68  msg = "$IP" + ip + nm + gw + "\x0a"
69 
70  print("Connecting to %s" % args.ip)
71  sock = socket.socket()
72  sock.connect((args.ip, 10940))
73 
74  print("Updating settings")
75  sock.send(msg)
76  try:
77  sock.settimeout(5)
78  returned = sock.recv(40)
79  except socket.timeout:
80  print("Laser did not return any packet, is probably not updated.")
81  exit(-1)
82  if msg != returned:
83  print("Laser does not appear to have updated")
84  exit(-1)
85  print("Done updating, cycle power on laser")
def parse_and_validate_ipv4(argument, name)
Definition: set_urg_ip.py:36


urg_node
Author(s): Chad Rockey , Mike O'Driscoll
autogenerated on Wed Oct 28 2020 03:39:16