Skip to content

Commit 8de7e49

Browse files
committed
Merge pull request #173 from mfine/mfine-udp-logger
UDP logger for SBP
2 parents dee2566 + 2a77058 commit 8de7e49

File tree

3 files changed

+67
-15
lines changed

3 files changed

+67
-15
lines changed

python/sbp/client/examples/udp.py

100644100755
Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@
1414
"""
1515

1616
from sbp.client.drivers.pyserial_driver import PySerialDriver
17-
from sbp.client.handler import Handler
17+
from sbp.client.handler import Handler
18+
from sbp.client.loggers.udp_logger import UdpLogger
1819

19-
from contextlib import closing
20-
import socket
2120
import struct
2221
import time
2322

@@ -49,23 +48,15 @@ def get_args():
4948
help="specify the baud rate to use.")
5049
return parser.parse_args()
5150

52-
def send_udp_callback_generator(udp, args):
53-
def send_udp_callback(msg):
54-
s = ""
55-
s += struct.pack("<BHHB", 0x55, msg.msg_type, msg.sender, msg.length)
56-
s += msg.payload
57-
s += struct.pack("<H", msg.crc)
58-
udp.sendto(s, (args.address[0], args.udp_port[0]))
59-
60-
return send_udp_callback
61-
6251
def main():
6352
args = get_args()
53+
address = args.address[0]
54+
udp_port = args.udp_port[0]
6455

6556
with PySerialDriver(args.serial_port[0], args.baud[0]) as driver:
6657
with Handler(driver.read, driver.write) as handler:
67-
with closing(socket.socket(socket.AF_INET, socket.SOCK_DGRAM)) as udp:
68-
handler.add_callback(send_udp_callback_generator(udp, args))
58+
with UdpLogger(address, udp_port) as udp:
59+
handler.add_callback(udp)
6960

7061
try:
7162
while True:
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Copyright (C) 2015 Swift Navigation Inc.
2+
# Contact: Mark Fine <[email protected]>
3+
#
4+
# This source is subject to the license found in the file 'LICENSE' which must
5+
# be be distributed together with this source. All other rights reserved.
6+
#
7+
# THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
8+
# EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
9+
# WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
10+
11+
import socket
12+
import struct
13+
14+
from .base_logger import BaseLogger
15+
16+
class UdpLogger(BaseLogger):
17+
"""
18+
UdpLogger
19+
20+
The :class:`UdpLogger` logs SBP messages over UDP.
21+
22+
Parameters
23+
----------
24+
address : string
25+
IP Address to send UDP packets to.
26+
port : int
27+
IP Port to send UDP packets to.
28+
"""
29+
def __init__(self, address, port):
30+
self.handle = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
31+
self.address = address
32+
self.port = port
33+
34+
def __call__(self, msg):
35+
self.call(msg)
36+
37+
def fmt_msg(self, msg):
38+
s = ""
39+
s += struct.pack("<BHHB", 0x55, msg.msg_type, msg.sender, msg.length)
40+
s += msg.payload
41+
s += struct.pack("<H", msg.crc)
42+
return s
43+
44+
def flush(self):
45+
pass
46+
47+
def call(self, msg):
48+
self.handle.sendto(self.fmt_msg(msg), (self.address, self.port))
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env python
2+
# Copyright (C) 2015 Swift Navigation Inc.
3+
# Contact: Bhaskar Mookerji <[email protected]>
4+
#
5+
# This source is subject to the license found in the file 'LICENSE' which must
6+
# be be distributed together with this source. All other rights reserved.
7+
#
8+
# THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
9+
# EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
10+
# WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
11+
12+
def test_udp_logger():
13+
pass

0 commit comments

Comments
 (0)