Skip to content

Commit 64c1ab6

Browse files
committed
examples: add IP address example
1 parent f71b5b0 commit 64c1ab6

1 file changed

Lines changed: 65 additions & 0 deletions

File tree

examples/ip.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#!/usr/bin/env python
2+
3+
"""Shows how to use an RS-485 connection to read and write the unit's IP
4+
address."""
5+
6+
from absscpi import ScpiClient
7+
import argparse
8+
9+
def query_ip_demo(port: str, dev_id: int):
10+
"""Demonstrates how to query the IP address of an ABS over serial.
11+
12+
Args:
13+
port: The COM port to use.
14+
dev_id: The device's serial ID.
15+
"""
16+
with ScpiClient() as client:
17+
# open the serial port
18+
client.open_serial(port, dev_id)
19+
20+
# query the IP and netmask
21+
addr = client.get_ip_address()
22+
23+
print("IP:", addr.get_ip_address())
24+
print("Netmask:", addr.get_netmask())
25+
26+
def set_ip_demo(port: str, dev_id: int, ip: str, netmask: str):
27+
"""Demonstrates how to set the IP address of an ABS over serial.
28+
29+
Args:
30+
port: The COM port to use.
31+
dev_id: The device's serial ID.
32+
ip: The desired IP address.
33+
netmask: The desired subnet mask.
34+
"""
35+
with ScpiClient() as client:
36+
# open the serial port
37+
client.open_serial(port, dev_id)
38+
39+
# set the IP and netmask
40+
client.set_ip_address(ip, netmask)
41+
42+
if __name__ == "__main__":
43+
# since this one has a bit more complexity to it, we'll use argparse to
44+
# handle arguments for us
45+
parser = argparse.ArgumentParser()
46+
parser.add_argument('-p', '--port', required=True, help='COM port to use')
47+
parser.add_argument(
48+
'-i', '--id', required=True, type=int, help="ABS's serial ID")
49+
50+
subparsers = parser.add_subparsers(dest='command')
51+
parser_query = subparsers.add_parser(
52+
'get', help="query the ABS's IP address")
53+
54+
parser_set = subparsers.add_parser('set', help="set the ABS's IP address")
55+
parser_set.add_argument('ip', help='IPv4 address, e.g., 192.168.1.70')
56+
parser_set.add_argument(
57+
'netmask', nargs='?', default='255.255.255.0',
58+
help='optional subnet mask, e.g., 255.255.255.0')
59+
60+
args = parser.parse_args()
61+
62+
if args.command == 'get':
63+
query_ip_demo(args.port, args.id)
64+
elif args.command == 'set':
65+
set_ip_demo(args.port, args.id, args.ip, args.netmask)

0 commit comments

Comments
 (0)