forked from haineb-zz/ASLHackfest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzmq_sub.py
27 lines (22 loc) · 863 Bytes
/
zmq_sub.py
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
import zmq
from zmq import Again as ZMQ_sub_timeout
import numpy, pmt
import binascii
class ZMQ_sub(object):
def __init__(self, portIn, timeout = 100):
self._ipAddress = '127.0.0.1'
self._portIn = portIn
self._zmqContext = zmq.Context()
self._socketIn = self._zmqContext.socket(zmq.SUB)
self._socketIn.RCVTIMEO = timeout
self._socketIn.connect('tcp://%s:%s' % (self._ipAddress,self._portIn))
try:
self._socketIn.setsockopt(zmq.SUBSCRIBE, '') # python2
except TypeError:
self._socketIn.setsockopt_string(zmq.SUBSCRIBE, '') # python3, if this throws an exception... give up...
def recv(self):
msg = self._socketIn.recv()
pdu = pmt.deserialize_str(msg)
cdr = pmt.to_python(pmt.cdr(pdu))
cdr = numpy.getbuffer(cdr)
return cdr