Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathexample.py
More file actions
Latest commit
96 lines (79 loc) · 2.93 KB
/
Copy pathexample.py
File metadata and controls
96 lines (79 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
"""
Example of using the wlmodem library
"""
from __future__ importdivision, print_function
importlogging
importsys
importtime
fromwlmodemimportWlModem
defmain():
""" Demo code """
logging.basicConfig(level=logging.DEBUG)
importargparse
parser=argparse.ArgumentParser(description="Water Linked Modem example")
parser.add_argument('-D', '--device', action="store", required=True, type=str, help="Serial port.")
parser.add_argument('-r', '--role', action="store", type=str, default="a", help="Role: a or b.")
parser.add_argument('-c', '--channel', action="store", type=int, default=4, help="Channel: 1-7.")
parser.add_argument('--baudrate', action="store", type=int, default=115200, help="Serial baudrate.")
parser.add_argument('-v' , '--verbose', action="store_true", help="Verbose.")
args=parser.parse_args()
ch=args.channel
ifch<1orch>7:
print("Error: invalid channel: {}".format(ch))
sys.exit(1)
role=args.role
ifrolenotin ["a", "b"]:
print("Error: invalid role: {}".format(role))
sys.exit(1)
modem=WlModem(args.device, debug=args.verbose)
ifnotmodem.connect():
print("Error connecting to modem")
sys.exit(1)
print("Set modem role ({}) and channel ({}): ".format(role, ch), end="")
success=modem.cmd_configure(role, ch)
ifsuccess:
print("success")
else:
print("failed")
sys.exit(1)
success=modem.cmd_flush_queue()
print("Flush :", "success"ifsuccesselse"failed")
data=b"HelloSea"
print("Sending '{}': ".format(data), end="")
success=modem.cmd_queue_packet(data)
print("success"ifsuccesselse"failed")
importstruct
data=struct.pack(">bbbbbbbb", 8, 7, 6, 5, 4, 3, 2, 1)
print("Sending encoded numbers {}: ".format(data), end="")
success=modem.cmd_queue_packet(data)
print("success"ifsuccesselse"failed")
print("Queue length: ", modem.cmd_get_queue_length())
print("Diagnostic : ", modem.cmd_get_diagnostic())
timeout=10
whiletimeout>0:
link_up=modem.cmd_get_diagnostic().get("link_up")
print("Link is "+ ("UP"iflink_upelse"down"))
iflink_up:
break
time.sleep(1.0)
timeout-=1
iftimeout<1:
print("Link cannot be established. Did you start the other modem?")
return
print("Wait for packet from other modem. Ctrl-C to abort")
t0=time.time()
try:
whileTrue:
pkt=modem.get_data_packet()
ifpkt:
print("Got:", pkt)
ift0+5>time.time():
# Queue another packet after some time
modem.cmd_queue_packet(data)
t0=time.time()
time.sleep(0.1)
exceptKeyboardInterrupt:
print("Stopping")
print("Diagnostic : ", modem.cmd_get_diagnostic())
if__name__=="__main__":
main()