Skip to content

Commit

Permalink
another big refactor, got rid of network.py, placed send function in …
Browse files Browse the repository at this point in the history
…io.Network class
  • Loading branch information
natronics committed Jul 13, 2014
1 parent fdaab4a commit 01c192e
Show file tree
Hide file tree
Showing 9 changed files with 205 additions and 324 deletions.
56 changes: 31 additions & 25 deletions examples/send_packets.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,39 @@
Open a UDP socket, and send a message of type "ADIS" to port 25000 on localhost
"""

from psas_packet import network, messages
from psas_packet import io, messages
from contextlib import closing
import socket
import time

# set up a UDP packet sender
with network.SendUDP('127.0.0.1', 25000) as udp:

# data that will go in our message
data = {
'VCC': 5.0,
'Gyro_X': 0.0,
'Gyro_Y': 0,
'Gyro_Z': 1,
'Acc_X': -9.8,
'Acc_Y': 0,
'Acc_Z': 0,
'Magn_X': 53e-6,
'Magn_Y': 0,
'Magn_Z': 0,
'Temp': 20,
'Aux_ADC': 0,
}

# send a whole message (with header)
#udp.send_message(messages.ADIS, time.time(), data)
# Data type we're going to use
ADIS = messages.MESSAGES['ADIS']

# Data to pack
data = {
'VCC': 5.0,
'Gyro_X': 0.0,
'Gyro_Y': 0,
'Gyro_Z': 1,
'Acc_X': -9.8,
'Acc_Y': 0,
'Acc_Z': 0,
'Magn_X': 53e-6,
'Magn_Y': 0,
'Magn_Z': 0,
'Temp': 20,
'Aux_ADC': 0,
}

# Open a UDP socket
with closing(socket.socket(socket.AF_INET, socket.SOCK_DGRAM)) as sock:
sock.bind(('', 0))
sock.connect(('127.0.0.1', 25000))

# Network IO class, with our socket as connection
net = io.Network(sock)

# send just data (no header)
#udp.send_data(messages.ADIS, data)
# type, sequence no, data
net.send_data(ADIS, 0, data)

# send data preceded with a sequence number (0, in this case)
udp.send_seq_data(messages.ADIS, 0, data)
2 changes: 1 addition & 1 deletion psas_packet/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

__version__ = '0.1.15'
__version__ = '0.2.0'
23 changes: 22 additions & 1 deletion psas_packet/io.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function
from psas_packet import messages
import socket
import errno
import time
from psas_packet import messages


def _is_string_like(obj):
Expand Down Expand Up @@ -54,6 +56,25 @@ def listen(self):
print("Reader Broke!")
return

def send_data(self, msgtype, seqn, data):
"""Send message with a sequence number header over a socket. Does the packing for you.
:param Message msgtype: Message class to use for packing, see: psas_packet.messages
:param int seqn: Sequence number
:param dict data: Data to get packed and sent
"""
packed = msgtype.encode(data)
s = messages.SequenceNo.encode(seqn)

try:
self.conn.send(s + packed)
except socket.error as e:
if e.errno == errno.ECONNREFUSED:
print('connection refused, continuing')
else:
raise


class BinFile(object):
"""Read from a binary log file
Expand Down
Loading

0 comments on commit 01c192e

Please sign in to comment.