def postMessage(self, blk_alias, port, msg): ''' blk_alias: the alias of the block we are posting the message to; must have an open message port named 'port'. Provide as a string. port: The name of the message port we are sending the message to. Provide as a string. msg: The actual message. Provide this as a PMT of the form right for the message port. The alias and port names are converted to PMT symbols and serialized. The msg is already a PMT and so just serialized. ''' self.thriftclient.radio.postMessage(pmt.serialize_str(pmt.intern(blk_alias)), pmt.serialize_str(pmt.intern(port)), pmt.serialize_str(msg));
def postMessage(self, blk_alias, port, msg): ''' blk_alias: the alias of the block we are posting the message to; must have an open message port named 'port'. Provide as a string. port: The name of the message port we are sending the message to. Provide as a string. msg: The actual message. Provide this as a PMT of the form right for the message port. The alias and port names are converted to PMT symbols and serialized. The msg is already a PMT and so just serialized. ''' self.thriftclient.radio.postMessage(pmt.serialize_str(pmt.intern(blk_alias)), pmt.serialize_str(pmt.intern(port)), pmt.serialize_str(msg))
def test23_absolute_serialization_float_uvecs(self): # f32_vector SERDES in_vec = [0x01020304, 2.1, 3.1415, 1e-9] # old serialization (f32 serialized as f64): # in_str = b'\n\x08\x00\x00\x00\x04\x01\x00Ap 0@\x00\x00\x00@\x00\xcc\xcc\xc0\x00\x00\x00@\t!\xca\xc0\x00\x00\x00>\x11.\x0b\xe0\x00\x00\x00' in_str = b'\n\x08\x00\x00\x00\x04\x01\x00K\x81\x01\x82@\x06ff@I\x0eV0\x89p_' out_str = pmt.serialize_str(pmt.init_f32vector(len(in_vec), in_vec)) self.assertEqual(out_str, in_str) # old serialization (f32 serialized as f64): # in_str = b'\n\x08\x00\x00\x00\x04\x01\x00?\xf0\x00\x00\x00\x00\x00\x00@\x00\x00\x00\x00\x00\x00\x00\xbf\xc0\x00\x00\x00\x00\x00\x00@\xfe$\x00\x00\x00\x00\x00' in_str = b'\n\x08\x00\x00\x00\x04\x01\x00?\x80\x00\x00@\x00\x00\x00\xbe\x00\x00\x00G\xf1 \x00' in_vec = [1., 2., -0.125, 123456.0] out_vec = pmt.f32vector_elements(pmt.deserialize_str(in_str)) self.assertEqual(out_vec, in_vec) # f64_vector SERDES in_vec = [0x010203040506, 2.1, 1e-9] in_str = b'\n\t\x00\x00\x00\x03\x01\x00Bp 0@P`\x00@\x00\xcc\xcc\xcc\xcc\xcc\xcd>\x11.\x0b\xe8&\xd6\x95' out_str = pmt.serialize_str(pmt.init_f64vector(len(in_vec), in_vec)) self.assertEqual(out_str, in_str) in_str = b'\n\t\x00\x00\x00\x04\x01\x00?\xf0\x00\x00\x00\x00\x00\x00@\x00\x00\x00\x00\x00\x00\x00\xbf\xc0\x00\x00\x00\x00\x00\x00A\x9do4T\x00\x00\x00' in_vec = [1., 2., -0.125, 123456789.0] out_vec = pmt.f64vector_elements(pmt.deserialize_str(in_str)) self.assertEqual(out_vec, in_vec) # c32_vector SERDES in_vec = [0x01020304 - 1j, 3.1415 + 99.99j] # old serialization (c32 serialized as c64): # in_str = b'\n\n\x00\x00\x00\x02\x01\x00Ap 0@\x00\x00\x00\xbf\xf0\x00\x00\x00\x00\x00\x00@\t!\xca\xc0\x00\x00\x00@X\xff\\ \x00\x00\x00' in_str = b'\n\n\x00\x00\x00\x02\x01\x00\xbf\x80\x00\x00K\x81\x01\x82B\xc7\xfa\xe1@I\x0eV' out_str = pmt.serialize_str(pmt.init_c32vector(len(in_vec), in_vec)) self.assertEqual(out_str, in_str) # old serialization (c32 serialized as c64): # in_str = b'\n\n\x00\x00\x00\x02\x01\x00?\xf0\x00\x00\x00\x00\x00\x00?\xf0\x00\x00\x00\x00\x00\x00?\xc0\x00\x00\x00\x00\x00\x00\xc1c\x12\xcf\xe0\x00\x00\x00' in_str = b'\n\n\x00\x00\x00\x02\x01\x00?\x80\x00\x00?\x80\x00\x00\xcb\x18\x96\x7f>\x00\x00\x00' in_vec = [1 + 1j, .125 - 9999999j] out_vec = pmt.c32vector_elements(pmt.deserialize_str(in_str)) self.assertEqual(out_vec, in_vec) # c64_vector SERDES in_vec = [0xffffffff, .9j] in_str = b'\n\x0b\x00\x00\x00\x02\x01\x00A\xef\xff\xff\xff\xe0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00?\xec\xcc\xcc\xcc\xcc\xcc\xcd' out_str = pmt.serialize_str(pmt.init_c64vector(len(in_vec), in_vec)) self.assertEqual(out_str, in_str) in_str = b'\n\x0b\x00\x00\x00\x02\x01\x00?\xf0\x00\x00\x00\x00\x00\x00?\xf0\x00\x00\x00\x00\x00\x00?\xc0\x00\x00\x00\x00\x00\x00\xc1c\x12\xcf\xe0\x00\x00\x00' in_vec = [1 + 1j, .125 - 9999999j] out_vec = pmt.c64vector_elements(pmt.deserialize_str(in_str)) self.assertEqual(out_vec, in_vec)
def test14(self): const = self.MAXINT32 - 1 x_pmt = pmt.from_long(const) s = pmt.serialize_str(x_pmt) deser = pmt.deserialize_str(s) self.assertTrue(pmt.equal(x_pmt, deser)) self.assertEqual(const,pmt.to_long(deser))
def nodeid_handler_method(self, msg): self.nodeid = pmt.to_long(msg) pmt_to_send = pmt.make_dict() attributes = pmt.make_dict() attributes = pmt.dict_add(attributes, pmt.string_to_symbol("nodeID"), pmt.from_long(self.nodeid)) attributes = pmt.dict_add(attributes, pmt.string_to_symbol("nodeMac"), pmt.intern(self.mac)) attributes = pmt.dict_add(attributes, pmt.string_to_symbol("nodeIp"), pmt.intern(self.ip)) attributes = pmt.dict_add(attributes, pmt.string_to_symbol("latitude"), pmt.from_double(self.latitude)) attributes = pmt.dict_add(attributes, pmt.string_to_symbol("longitude"), pmt.from_double(self.longitude)) attributes = pmt.dict_add(attributes, pmt.string_to_symbol("nodeType"), pmt.from_long(1)) command = pmt.make_dict() command = pmt.dict_add(command, pmt.string_to_symbol("attributes"), attributes) pmt_to_send = pmt.dict_add(pmt_to_send, pmt.string_to_symbol("NodeParam"), command) serialized_pmt = pmt.serialize_str(pmt_to_send) #print pmt_to_send UDP_IP = self.host UDP_PORT = self.port sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.sendto(serialized_pmt, (UDP_IP, UDP_PORT))
def send(self, data): car = pmt.make_dict() data = bytes(data) data = numpy.frombuffer(data, dtype=numpy.uint8) cdr = pmt.to_pmt(data) pdu = pmt.cons(car, cdr) self._socketOut.send(pmt.serialize_str(pdu))
def set_land(self, land): self.land = land meta = pmt.to_pmt('land') data = pmt.to_pmt(1) msg = pmt.cons(meta, data) print('send_zmq') self.zmqc.send(pmt.serialize_str(msg))
def set_takeoff(self, takeoff): if (takeoff == 1): meta = pmt.to_pmt('takeoff') data = pmt.to_pmt(self.altitude) msg = pmt.cons(meta, data) print('send_zmq') self.zmqc.send(pmt.serialize_str(msg))
def test17(self): const = self.MININT32 + 1 x_pmt = pmt.from_long(const) s = pmt.serialize_str(x_pmt) deser = pmt.deserialize_str(s) self.assertTrue(pmt.equal(x_pmt, deser)) x_long = pmt.to_long(deser) self.assertEqual(const, x_long)
def set_disarm(self, disarm): self.disarm = disarm if self.disarm == 1: meta = pmt.to_pmt('disarm') pmtdata = pmt.to_pmt(self.data) msg = pmt.cons(meta, pmtdata) print('send_zmq disarm') self.zmqc.send(pmt.serialize_str(msg))
def msg_handler(self, message): serialized_msg = pmt.serialize_str(message) while True: try: self.msg_queue.put_nowait(serialized_msg) break except Queue.Full: self.msg_queue.get(False)
def zmq_msg_payload_to_str(raw_msg): msg = pmt.deserialize_str(raw_msg) cdr = pmt.cdr(msg) packet_len = pmt.length(cdr) cdr_str = pmt.serialize_str(cdr) output_str = cdr_str[-1 * packet_len:] return output_str
def test_valid_pmt(self): """Test receiving of valid PMT messages""" msg = pmt.to_pmt('test_valid_pmt') self.zmq_sock.send(pmt.serialize_str(msg)) time.sleep(0.1) self.assertEqual(1, self.message_debug.num_messages()) self.assertTrue(pmt.equal(msg, self.message_debug.get_message(0)))
def request(self, id_str, args=None): socks = dict(self.poller_req_out.poll(10)) if socks.get(self.req_socket) == zmq.POLLOUT: self.req_socket.send(pmt.serialize_str(pmt.to_pmt((id_str,args)))) socks = dict(self.poller_req_in.poll(10)) if socks.get(self.req_socket) == zmq.POLLIN: reply = pmt.to_python(pmt.deserialize_str(self.req_socket.recv())) print("[RPC] reply:", reply) return reply
def request(self, id_str, args=None): socks = dict(self.poller_req_out.poll(10)) if socks.get(self.req_socket) == zmq.POLLOUT: self.req_socket.send(pmt.serialize_str(pmt.to_pmt((id_str, args)))) socks = dict(self.poller_req_in.poll(10)) if socks.get(self.req_socket) == zmq.POLLIN: reply = pmt.to_python(pmt.deserialize_str(self.req_socket.recv())) print("[RPC] reply:", reply) return reply
def send_heartbeat(self): while (self.running): if self.flying == 1: meta = pmt.to_pmt('heartbeat') pmtdata = pmt.to_pmt(self.data) msg = pmt.cons(meta, pmtdata) print('send_zmq heartbeat') self.zmqc.send(pmt.serialize_str(msg)) time.sleep(1.0)
def set_land(self, land): self.land = land if self.land == 1: meta = pmt.to_pmt('land') pmtdata = pmt.to_pmt(self.data) msg = pmt.cons(meta, pmtdata) print('send_zmq') self.zmqc.send(pmt.serialize_str(msg)) self.flying = 0
def test18(self): if(self.sizeof_long <= 4): return const = self.MININT32 - 1 x_pmt = pmt.from_long(const) s = pmt.serialize_str(x_pmt) deser = pmt.deserialize_str(s) self.assertTrue(pmt.equal(x_pmt, deser)) x_long = pmt.to_long(deser) self.assertEqual(const, x_long)
def zmq_msg_payload_to_bytes(raw_msg): msg = pmt.deserialize_str(raw_msg) cdr = pmt.cdr(msg) packet_len = pmt.length(cdr) cdr_str = pmt.serialize_str(cdr) output_str = cdr_str[-1 * packet_len:] byte_list = list() for i in xrange(len(output_str)): byte_list.append(ord(output_str[i])) return byte_list
def watcher(self): self.keep_running = True while self.keep_running: # poll for calls socks = dict(self.poller_rep.poll(10)) if socks.get(self.rep_socket) == zmq.POLLIN: # receive call msg = self.rep_socket.recv() (id_str, args) = pmt.to_python(pmt.deserialize_str(msg)) print("[RPC] request:", id_str, ", args:", args) reply = self.callback(id_str, args) self.rep_socket.send(pmt.serialize_str(pmt.to_pmt(reply)))
def test21_absolute_serialization_nums(self): # uint64 SERDES in_num = 9999876 in_str = b'\x0b\x00\x00\x00\x00\x00\x98\x96\x04' out_str = pmt.serialize_str(pmt.from_uint64(in_num)) self.assertEqual(out_str, in_str) in_str = b'\x0b\xff\xff\xff\xff\xff\xff\xff\xff' in_num = 0xffffffffffffffff out_num = pmt.to_uint64(pmt.deserialize_str(in_str)) self.assertEqual(out_num, in_num) # long int SERDES in_num = 2432141 in_str = b'\x03\x00%\x1c\x8d' out_str = pmt.serialize_str(pmt.from_long(in_num)) self.assertEqual(out_str, in_str) in_str = b'\x03\xfdy\xe4\xb7' in_num = -42343241 out_num = pmt.to_long(pmt.deserialize_str(in_str)) self.assertEqual(out_num, in_num) # float SERDES in_num = -1.11 in_str = b'\x04\xbf\xf1\xc2\x8f`\x00\x00\x00' out_str = pmt.serialize_str(pmt.from_float(in_num)) self.assertEqual(out_str, in_str) in_str = b'\x04@\x8e\xdd;`\x00\x00\x00' in_num = 987.6539916992188 out_num = pmt.to_float(pmt.deserialize_str(in_str)) self.assertEqual(out_num, in_num) # double SERDES in_num = 987654.321 in_str = b'\x04A.$\x0c\xa4Z\x1c\xac' out_str = pmt.serialize_str(pmt.from_double(in_num)) self.assertEqual(out_str, in_str) in_str = b'\x04\xbf\xdb\x19\x84@A\r\xbc' in_num = -.42343241 out_num = pmt.to_double(pmt.deserialize_str(in_str)) self.assertEqual(out_num, in_num)
def test22_absolute_serialization_int_uvecs(self): # u8_vector SERDES in_vec = [1, 3, 128, 255] in_str = b'\n\x00\x00\x00\x00\x04\x01\x00\x01\x03\x80\xff' out_str = pmt.serialize_str(pmt.init_u8vector(len(in_vec), in_vec)) self.assertEqual(out_str, in_str) in_str = b'\n\x00\x00\x00\x00\x07\x01\x00\x02\x03\x04\t\n\x19@' in_vec = [2, 3, 4, 9, 10, 25, 64] out_vec = pmt.u8vector_elements(pmt.deserialize_str(in_str)) self.assertEqual(out_vec, in_vec) # s8_vector SERDES in_vec = [1, 3, 127, -128] in_str = b'\n\x01\x00\x00\x00\x04\x01\x00\x01\x03\x7f\x80' out_str = pmt.serialize_str(pmt.init_s8vector(len(in_vec), in_vec)) self.assertEqual(out_str, in_str) in_str = b'\n\x01\x00\x00\x00\x07\x01\x00\x02\x00\x04\xf7\n\x19\xc0' in_vec = [2, 0, 4, -9, 10, 25, -64] out_vec = pmt.s8vector_elements(pmt.deserialize_str(in_str)) self.assertEqual(out_vec, in_vec) # u16_vector SERDES in_vec = [0xfffe, 0, 256, 0xffff] in_str = b'\n\x02\x00\x00\x00\x04\x01\x00\xff\xfe\x00\x00\x01\x00\xff\xff' out_str = pmt.serialize_str(pmt.init_u16vector(len(in_vec), in_vec)) self.assertEqual(out_str, in_str) in_str = b'\n\x02\x00\x00\x00\x04\x01\x00\xff\xff\x00\x00\x00\x01\x00\x02' in_vec = [0xffff, 0, 1, 2] out_vec = pmt.u16vector_elements(pmt.deserialize_str(in_str)) self.assertEqual(out_vec, in_vec) # s16_vector SERDES in_vec = [0x7fff, 0, -256, -0x8000] in_str = b'\n\x03\x00\x00\x00\x04\x01\x00\x7f\xff\x00\x00\xff\x00\x80\x00' out_str = pmt.serialize_str(pmt.init_s16vector(len(in_vec), in_vec)) self.assertEqual(out_str, in_str) in_str = b'\n\x03\x00\x00\x00\x05\x01\x00\x00\x01\x00\x02\x00\x03\x00\x04\xff\xfb' in_vec = [1, 2, 3, 4, -5] out_vec = pmt.s16vector_elements(pmt.deserialize_str(in_str)) self.assertEqual(out_vec, in_vec) # u32_vector SERDES in_vec = [0x01020304, 0x05060708, 0, 100000000] in_str = b'\n\x04\x00\x00\x00\x04\x01\x00\x01\x02\x03\x04\x05\x06\x07\x08\x00\x00\x00\x00\x05\xf5\xe1\x00' out_str = pmt.serialize_str(pmt.init_u32vector(len(in_vec), in_vec)) self.assertEqual(out_str, in_str) in_str = b'\n\x04\x00\x00\x00\x06\x01\x00\x00\x00\x000\xff\xff\xff\xff\x00\x00\x00\x00\x00\x00\x00\t\x00\x00\x00\x0c\x00\x00\x10\x00' in_vec = [48, 0xffffffff, 0, 9, 12, 4096] out_vec = pmt.u32vector_elements(pmt.deserialize_str(in_str)) self.assertEqual(out_vec, in_vec) # s32_vector SERDES in_vec = [-0x0, -0x8000000, 1, 0x7ffffff] in_str = b'\n\x05\x00\x00\x00\x04\x01\x00\x00\x00\x00\x00\xf8\x00\x00\x00\x00\x00\x00\x01\x07\xff\xff\xff' out_str = pmt.serialize_str(pmt.init_s32vector(len(in_vec), in_vec)) self.assertEqual(out_str, in_str) in_str = b'\n\x05\x00\x00\x00\x05\x01\x00\x00\x00\x000\x7f\xff\xff\xff\x00\x00\x00\x00\xff\xff\xff\xf7\xff\xff\xff\xf3' in_vec = [48, 0x7fffffff, 0, -9, -13] out_vec = pmt.s32vector_elements(pmt.deserialize_str(in_str)) self.assertEqual(out_vec, in_vec)
def test20_absolute_serialization_misc(self): # null SERDES in_str = b'\x06' out_str = pmt.serialize_str(pmt.PMT_NIL) self.assertEqual(out_str, in_str) in_str = b'\x07\x06\x06' out_pmt = pmt.deserialize_str(in_str) self.assertTrue(pmt.equal(out_pmt, pmt.PMT_EOF)) # T/F SERDES in_str = b'\x01' out_str = pmt.serialize_str(pmt.PMT_F) self.assertEqual(out_str, in_str) in_str = b'\x00' out_pmt = pmt.deserialize_str(in_str) self.assertTrue(pmt.equal(out_pmt, pmt.PMT_T)) # pair SERDES in_pmt = pmt.cons(pmt.intern('string'), pmt.from_long(1000)) in_str = b'\x07\x02\x00\x06string\x03\x00\x00\x03\xe8' out_str = pmt.serialize_str(in_pmt) self.assertEqual(out_str, in_str) in_str = b'\x07\x07\x06\x06\x07\x00\x01' in_pmt = pmt.cons(pmt.PMT_EOF, pmt.cons(pmt.PMT_T, pmt.PMT_F)) out_pmt = pmt.deserialize_str(in_str) self.assertTrue(pmt.equal(out_pmt, in_pmt)) # dict SERDES d = pmt.make_dict() d = pmt.dict_add(d, pmt.intern('k0'), pmt.from_long(1)) d = pmt.dict_add(d, pmt.intern('k1'), pmt.from_double(2.22222)) d = pmt.dict_add(d, pmt.intern('k2'), pmt.from_long(3)) in_str = b'\t\x07\x02\x00\x02k2\x03\x00\x00\x00\x03\t\x07\x02\x00\x02k1\x04@\x01\xc7\x1bG\x84#\x10\t\x07\x02\x00\x02k0\x03\x00\x00\x00\x01\x06' out_str = pmt.serialize_str(d) self.assertEqual(out_str, in_str) in_str = b'\t\x07\x02\x00\x03vec\n\x00\x00\x00\x00\x04\x01\x00\x01\x02\x03\x04\x06' d = pmt.dict_add(pmt.make_dict(), pmt.intern('vec'), pmt.init_u8vector(4, [1, 2, 3, 4])) out_pmt = pmt.deserialize_str(in_str) self.assertTrue(pmt.equal(out_pmt, d))
def main(): args = parse_args() # Make sure an action was specified if (args.tx_gain is None and args.rx_gain is None and not args.freeze and not args.train): print("At least one of {rx-gain, tx-gain, freeze, train} is required") sys.exit(0) # Initialize ZMQ ctx = zmq.Context() tx_sock = ctx.socket(zmq.PUSH) rx_sock = ctx.socket(zmq.PUSH) # Connect to flowgraph tx_sock.connect("{}:{}".format(args.address, args.tx_port)) if args.rx_port is not None: rx_port = args.rx_port else: rx_port = args.tx_port + 1 rx_sock.connect("{}:{}".format(args.address, rx_port)) # Build message dicts tx_dict = pmt.make_dict() rx_dict = pmt.make_dict() if args.tx_gain is not None: tx_dict = pmt.dict_add(tx_dict, pmt.intern("gain"), pmt.to_pmt(args.tx_gain)) if args.rx_gain is not None: rx_dict = pmt.dict_add(rx_dict, pmt.intern("gain"), pmt.to_pmt(args.rx_gain)) if args.freeze: tx_dict = pmt.dict_add(tx_dict, pmt.intern("freeze"), pmt.PMT_NIL) rx_dict = pmt.dict_add(rx_dict, pmt.intern("freeze"), pmt.PMT_NIL) if args.train: tx_dict = pmt.dict_add(tx_dict, pmt.intern("train"), pmt.PMT_NIL) rx_dict = pmt.dict_add(rx_dict, pmt.intern("train"), pmt.PMT_NIL) # Send message dicts tx_sock.send(pmt.serialize_str(tx_dict)) rx_sock.send(pmt.serialize_str(rx_dict)) # Let ZMQ finish sending time.sleep(1.)
def test19(self): max_key = pmt.intern("MAX") _max = pmt.from_long(self.MAXINT32) min_key = pmt.intern("MIN") _min = pmt.from_long(self.MININT32) d = pmt.make_dict() d = pmt.dict_add(d, max_key, _max) d = pmt.dict_add(d, min_key, _min) s = pmt.serialize_str(d) deser = pmt.deserialize_str(s) self.assertTrue(pmt.equal(d, deser)) p_dict = pmt.to_python(deser) self.assertEqual(self.MAXINT32, p_dict["MAX"]) self.assertEqual(self.MININT32, p_dict["MIN"])
def occ_handler_method(self, msg): occvec = [] for i in range(0, self.num_split): occvec.append(pmt.to_double(pmt.vector_ref(msg, i))) pmt_to_send = pmt.make_dict() curtime = strftime("%Y-%m-%d %H:%M:%S", gmtime()) attributes = pmt.make_dict() attributes = pmt.dict_add(attributes, pmt.string_to_symbol("center_freq"), pmt.from_double(self.center_freq)) attributes = pmt.dict_add(attributes, pmt.string_to_symbol("occ"), pmt.init_f32vector(self.num_split, occvec)) attributes = pmt.dict_add(attributes, pmt.string_to_symbol("bandwidth"), pmt.from_double(self.bw)) attributes = pmt.dict_add(attributes, pmt.string_to_symbol("timetag"), pmt.intern(curtime)) attributes = pmt.dict_add( attributes, pmt.string_to_symbol("noise_floor"), pmt.init_f32vector(self.num_split, self.noise_floor)) attributes = pmt.dict_add(attributes, pmt.string_to_symbol("nodeid"), pmt.from_long(self.nodeid)) attributes = pmt.dict_add(attributes, pmt.string_to_symbol("latitude"), pmt.from_double(self.latitude)) attributes = pmt.dict_add(attributes, pmt.string_to_symbol("longitude"), pmt.from_double(self.longitude)) command = pmt.make_dict() command = pmt.dict_add(command, pmt.string_to_symbol("table"), pmt.string_to_symbol("spectruminfo")) command = pmt.dict_add(command, pmt.string_to_symbol("attributes"), attributes) pmt_to_send = pmt.dict_add(pmt_to_send, pmt.string_to_symbol("INSERT"), command) serialized_pmt = pmt.serialize_str(pmt_to_send) #print pmt_to_send UDP_IP = self.host UDP_PORT = self.port sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.sendto(serialized_pmt, (UDP_IP, UDP_PORT))
def set_takeoff(self, takeoff): print takeoff if (takeoff == 1): meta = pmt.to_pmt('takeoff') if (self.mode == 'STABILIZE'): self.data[7] = 0 elif (self.mode == 'ALT_HOLD'): self.data[7] = 1 elif (self.mode == 'LOITER'): self.data[7] = 2 pmtdata = pmt.to_pmt(self.data) msg = pmt.cons(meta, pmtdata) print('send_zmq') self.zmqc.send(pmt.serialize_str(msg)) self.flying = 1
def insert_pdu_into_table(self, pdu): # Only process PDUs if pmt.is_pair(pdu): meta = pmt.to_python(pmt.car(pdu)) if meta is None: meta = dict() # Create table if we haven"t already if not self.created_table: # Find the non fixed-position columns and sort alphabetically non_fixed_position_columns = meta.keys() for key in self.fixed_position_columns: try: non_fixed_position_columns.remove(key) except: continue ordered_keys = self.fixed_position_columns + sorted(non_fixed_position_columns) if self.vector_column_name not in ordered_keys: # Add the vector column at the end unless otherwise specified in the Fixed-Position Columns list cols = "(" + ", ".join(ordered_keys) + ", " + self.vector_column_name + ")" else: cols = "(" + ", ".join(ordered_keys) + ")" # Attempt to create the table self.c.execute("CREATE TABLE IF NOT EXISTS " + self.table_name + " " + cols) self.conn.commit() self.created_table = True # Get list of true column names (whether we just made the table or it already existed) self.c.execute("SELECT * FROM " + self.table_name) self.column_names = [description[0] for description in self.c.description] # Set the PDU vector into the meta dictionary with appropriate key # meta[self.vector_column_name] = buffer(pmt.to_python(pmt.cdr(pdu))) meta[self.vector_column_name] = buffer(pmt.serialize_str(pmt.cdr(pdu))) # Insert PDU into table, with only columns that already exist in the table valid_keys = [key for key in meta.keys() if key in self.column_names] cols = "(" + ", ".join(valid_keys) + ")" question_marks = "(" + ", ".join(["?"]*len(valid_keys)) + ")" vals = [meta[key] for key in valid_keys] self.c.execute("INSERT INTO " + self.table_name + " " + cols + " VALUES " + question_marks, vals) self.conn.commit()
def main(): sub = 'tcp://127.0.0.1:52000' pub = 'tcp://127.0.0.1:52001' tx_pkt_len = 64 if (len(sys.argv) == 1 or sys.argv[1] == 'rx'): num_packets = 0 # Socket to talk to server context = zmq.Context() socket = context.socket(zmq.SUB) socket.connect(sub) socket.setsockopt_string(zmq.SUBSCRIBE, u'') while True: pkt = socket.recv_serialized( lambda x: pmt.deserialize_str(b''.join(x))) print(pmt.to_python(pkt)) num_packets += 1 print('Received {} packet(s)'.format(num_packets)) elif (sys.argv[1] == 'tx' and len(sys.argv) == 3): sleep(1) context = zmq.Context() socket = context.socket(zmq.PUB) #socket.connect(pub) socket.bind(pub) for i in range(int(sys.argv[2])): print(str(i)) data = 'NOCALL '.ljust(tx_pkt_len, 'x').encode('utf-8') # Python 2.7 is dumb, but that's what GR 3.7 requires... msg = pmt.cons(pmt.intern('out'), pmt.init_u8vector(len(data), map(ord, data))) socket.send(pmt.serialize_str(msg)) print('Sent ' + str(len(data)) + ' byte(s)') sleep(0.5) else: print('Usage: python radio.py [rx | tx n]')
def send_raw_bytes(self, byte_list, verbose=0): if verbose: print "Sending Raw Bytes:", print byte_list # get payload size payload_size = len(byte_list) # build an empty vector data = pmt.make_u8vector(payload_size, 0x00) # fill the vector with unsigned byte data to send for i in xrange(payload_size): pmt.u8vector_set(data, i, byte_list[i]) # build the message, which is a pair consisting of # the message metadata (not needed here) and the # information we want to send (the vector) msg = pmt.cons(pmt.PMT_NIL, data) # the message must be serialized as a string so that # it's in the form the gnuradio source expects msg = pmt.serialize_str(msg) self.socket.send(msg)
def request(self, id_str, args=None): time_waiting = 0 while self.busy: sleep = 0.1 time_waiting = time_waiting + sleep if self.debug: print "waiting" time.sleep(sleep) if time_waiting > 1: self.busy = False pass self.busy = True socks = dict(self.poller_req_out.poll(10)) if socks.get(self.req_socket) == zmq.POLLOUT: self.req_socket.send(pmt.serialize_str(pmt.to_pmt((id_str, args)))) if self.debug: print "[RPC] request:", id_str, ", args:", args socks = dict(self.poller_req_in.poll(1000)) if socks.get(self.req_socket) == zmq.POLLIN: reply = pmt.to_python(pmt.deserialize_str(self.req_socket.recv())) if self.debug: print "[RPC] reply:", reply self.busy = False return reply
def test12(self): v = pmt.init_c64vector(3, [11 + -101j, -22 + 202j, 33 + -303j]) s = pmt.serialize_str(v) d = pmt.deserialize_str(s) self.assertTrue(pmt.equal(v, d))
def test03(self): v = pmt.init_f32vector(3, [11, -22, 33]) s = pmt.serialize_str(v) d = pmt.deserialize_str(s) self.assertTrue(pmt.equal(v, d)) self.assertEqual(pmt.uniform_vector_itemsize(v), 4)
def propagate_headers(options, args): infile = args[0] outfile = args[1] infile_hdr = infile + ".hdr" outfile_hdr = outfile + ".hdr" sample_cnt_end = 0 sample_offset = long(options.start) # Open input header try: handle_in = open(infile_hdr, "rb") except IOError: sys.stderr.write("Unable to open input file header\n") sys.exit(1) # Open output header try: handle_out = open(outfile_hdr, "wb") except IOError: sys.stderr.write("Unable to open output file header\n") sys.exit(1) # Read first header separately to get file type hdr_in, hdr_extra_in, handle_in = read_single_header(handle_in) info_in = parse_file_metadata.parse_header(hdr_in, False) sample_cnt_end += info_in["nitems"] # Parse file type - ensure support for it shortname_intype = find_shortname(info_in["cplx"], info_in["type"], info_in["size"]) if shortname_intype == SNAME_TO_ENUM["unknown"]: sys.stderr.write("Unsupported data type\n") sys.exit(1) if options.output_type == "unknown": shortname_outtype = shortname_intype else: shortname_outtype = SNAME_TO_ENUM[options.output_type] # Calc sample_len from file size if not specified if options.nsamples is not None: sample_len = long(options.nsamples) else: sample_len = os.path.getsize(infile) / SNAME_DEFS[shortname_intype][0] final_index = sample_offset + sample_len # Search input headers until we find the correct one while sample_cnt_end <= sample_offset: hdr_in, hdr_extra_in, handle_in = read_single_header(handle_in) info_in = parse_file_metadata.parse_header(hdr_in, False) sample_cnt_end += info_in["nitems"] time_in = info_in["rx_time"] # Starting sample of current segment sample_cnt_start = sample_cnt_end - info_in["nitems"] # Interpolate new timestamp delta = sample_offset - sample_cnt_start new_ts = time_in + delta / info_in["rx_rate"] # Calc new segment size (samples) if sample_cnt_end > final_index: first_seg_len = final_index - sample_offset else: first_seg_len = sample_cnt_end - sample_offset # Write the first output header hdr_out = hdr_in new_secs = long(new_ts) new_fracs = new_ts - new_secs time_val = pmt.make_tuple(pmt.from_uint64(new_secs), pmt.from_double(new_fracs)) size_val = pmt.from_long(SNAME_DEFS[shortname_outtype][0]) bytes_val = pmt.from_uint64(first_seg_len * SNAME_DEFS[shortname_outtype][0]) type_val = pmt.from_long(SNAME_DEFS[shortname_outtype][2]) hdr_out = pmt.dict_add(hdr_out, pmt.intern("rx_time"), time_val) hdr_out = pmt.dict_add(hdr_out, pmt.intern("bytes"), bytes_val) hdr_out = pmt.dict_add(hdr_out, pmt.intern("type"), type_val) hdr_out = pmt.dict_add(hdr_out, pmt.intern("size"), size_val) hdr_out_str = pmt.serialize_str(hdr_out) + pmt.serialize_str(hdr_extra_in) handle_out.write(hdr_out_str) # Continue reading headers, modifying, and writing last_seg_len = info_in["nitems"] print "sample_cnt_end=%d,final_index=%d" % (sample_cnt_end, final_index) # Iterate through remaining headers while sample_cnt_end < final_index: hdr_in, hdr_extra_in, handle_in = read_single_header(handle_in) info_in = parse_file_metadata.parse_header(hdr_in, False) nitems = info_in["nitems"] sample_cnt_start = sample_cnt_end sample_cnt_end += nitems hdr_out = hdr_in # For last header, adjust segment length accordingly if sample_cnt_end > final_index: last_seg_len = final_index - sample_cnt_start else: last_seg_len = nitems size_val = pmt.from_long(SNAME_DEFS[shortname_outtype][0]) bytes_val = pmt.from_uint64(last_seg_len * SNAME_DEFS[shortname_outtype][0]) type_val = pmt.from_long(SNAME_DEFS[shortname_outtype][2]) hdr_out = pmt.dict_add(hdr_out, pmt.intern("bytes"), bytes_val) hdr_out = pmt.dict_add(hdr_out, pmt.intern("type"), type_val) hdr_out = pmt.dict_add(hdr_out, pmt.intern("size"), size_val) hdr_out_str = pmt.serialize_str(hdr_out) + pmt.serialize_str(hdr_extra_in) handle_out.write(hdr_out_str) if options.verbose: print "Input File:" + infile print "Input Header:" + infile_hdr print "Input Type:" + ENUM_TO_SNAME[shortname_intype] print "Output File:" + outfile print "Output File Length (Samples):%d" % (final_index - sample_offset) print "Output Header:" + outfile_hdr print "File subsection: [%d,%d]" % (sample_offset, final_index) print "Output Type:" + ENUM_TO_SNAME[shortname_outtype] print "First Segment Length: %e samples" % first_seg_len print "Last Segment Length: %e samples" % last_seg_len print "delta=%f,new ts=%f" % (delta, new_ts) # Clean up handle_in.close() handle_out.close() # Return header info return { "infile": infile, "intype": shortname_intype, "outfile": outfile, "outtype": shortname_outtype, "sample_offset": sample_offset, "sample_len": sample_len, }
def make_header(options, filename): extras_present = False if options.freq is not None: extras_present = True # Open the file and make the header hdr_filename = filename + '.hdr' hdr_file = open(hdr_filename, 'wb') header = pmt.make_dict() # Fill in header vals # TODO - Read this from blocks.METADATA_VERSION ver_val = pmt.from_long(long(0)) rate_val = pmt.from_double(options.sample_rate) time_val = pmt.make_tuple(pmt.from_uint64(options.time_sec), pmt.from_double(options.time_fsec)) ft_to_sz = parse_file_metadata.ftype_to_size # Map shortname to properties enum_type = SNAME_TO_ENUM[options.format] type_props = SNAME_DEFS[enum_type] size_val = pmt.from_long(type_props[0]) cplx_val = pmt.from_bool(type_props[1]) type_val = pmt.from_long(type_props[2]) fmt = type_props[2] file_samp_len = long(options.length) seg_size = long(options.seg_size) bytes_val = pmt.from_uint64(long(seg_size*ft_to_sz[fmt])) # Set header vals header = pmt.dict_add(header, pmt.intern("version"), ver_val) header = pmt.dict_add(header, pmt.intern("size"), size_val) header = pmt.dict_add(header, pmt.intern("type"), type_val) header = pmt.dict_add(header, pmt.intern("cplx"), cplx_val) header = pmt.dict_add(header, pmt.intern("rx_time"), time_val) header = pmt.dict_add(header, pmt.intern("rx_rate"), rate_val) header = pmt.dict_add(header, pmt.intern("bytes"), bytes_val) if extras_present: freq_key = pmt.intern("rx_freq") freq_val = pmt.from_double(options.freq) extras = pmt.make_dict() extras = pmt.dict_add(extras, freq_key, freq_val) extras_str = pmt.serialize_str(extras) start_val = pmt.from_uint64(blocks.METADATA_HEADER_SIZE + len(extras_str)) else: start_val = pmt.from_uint64(blocks.METADATA_HEADER_SIZE) header = pmt.dict_add(header, pmt.intern("strt"), start_val) num_segments = file_samp_len/seg_size if options.verbose: print "Wrote %d headers to: %s (Version %d)" % (num_segments+1, hdr_filename,pmt.to_long(ver_val)) for x in range(0,num_segments,1): # Serialize and write out file if extras_present: header_str = pmt.serialize_str(header) + extras_str else: header_str = pmt.serialize_str(header) hdr_file.write(header_str) # Update header based on sample rate and segment size header = update_timestamp(header,seg_size) # Last header is special b/c file size is probably not mult. of seg_size header = pmt.dict_delete(header,pmt.intern("bytes")) bytes_remaining = ft_to_sz[fmt]*(file_samp_len - num_segments*long(seg_size)) bytes_val = pmt.from_uint64(bytes_remaining) header = pmt.dict_add(header,pmt.intern("bytes"),bytes_val) # Serialize and write out file if extras_present: header_str = pmt.serialize_str(header) + extras_str else: header_str = pmt.serialize_str(header) hdr_file.write(header_str) hdr_file.close()
def _msg_handler(self, msg): self._file.write(pmt.serialize_str(msg))
def test_001(self): N = 1000 outfile = "test_out.dat" detached = False samp_rate = 200000 key = pmt.intern("samp_rate") val = pmt.from_double(samp_rate) extras = pmt.make_dict() extras = pmt.dict_add(extras, key, val) extras_str = pmt.serialize_str(extras) data = sig_source_c(samp_rate, 1000, 1, N) src = blocks.vector_source_c(data) fsnk = blocks.file_meta_sink(gr.sizeof_gr_complex, outfile, samp_rate, 1, blocks.GR_FILE_FLOAT, True, 1000000, extras_str, detached) fsnk.set_unbuffered(True) self.tb.connect(src, fsnk) self.tb.run() fsnk.close() handle = open(outfile, "rb") header_str = handle.read(parse_file_metadata.HEADER_LENGTH) if(len(header_str) == 0): self.assertFalse() try: header = pmt.deserialize_str(header_str) except RuntimeError: self.assertFalse() info = parse_file_metadata.parse_header(header, False) extra_str = handle.read(info["extra_len"]) self.assertEqual(len(extra_str) > 0, True) handle.close() try: extra = pmt.deserialize_str(extra_str) except RuntimeError: self.assertFalse() extra_info = parse_file_metadata.parse_extra_dict(extra, info, False) self.assertEqual(info['rx_rate'], samp_rate) self.assertEqual(pmt.to_double(extra_info['samp_rate']), samp_rate) # Test file metadata source src.rewind() fsrc = blocks.file_meta_source(outfile, False) vsnk = blocks.vector_sink_c() tsnk = blocks.tag_debug(gr.sizeof_gr_complex, "QA") ssnk = blocks.vector_sink_c() self.tb.disconnect(src, fsnk) self.tb.connect(fsrc, vsnk) self.tb.connect(fsrc, tsnk) self.tb.connect(src, ssnk) self.tb.run() fsrc.close() # Test to make sure tags with 'samp_rate' and 'rx_rate' keys # were generated and received correctly. tags = tsnk.current_tags() for t in tags: if(pmt.eq(t.key, pmt.intern("samp_rate"))): self.assertEqual(pmt.to_double(t.value), samp_rate) elif(pmt.eq(t.key, pmt.intern("rx_rate"))): self.assertEqual(pmt.to_double(t.value), samp_rate) # Test that the data portion was extracted and received correctly. self.assertComplexTuplesAlmostEqual(vsnk.data(), ssnk.data(), 5) os.remove(outfile)
def test16(self): const = self.MININT32 x_pmt = pmt.from_long(const) s = pmt.serialize_str(x_pmt) deser = pmt.deserialize_str(s) self.assertTrue(pmt.equal(x_pmt, deser))
def test07(self): v = pmt.init_u16vector(3, [11, 22, 33]) s = pmt.serialize_str(v) d = pmt.deserialize_str(s) self.assertTrue(pmt.equal(v, d))
def test10(self): v = pmt.init_s32vector(3, [11, -22, 33]) s = pmt.serialize_str(v) d = pmt.deserialize_str(s) self.assertTrue(pmt.equal(v, d))
def test11(self): v = pmt.init_c32vector(3, [11 + -101j, -22 + 202j, 33 + -303j]) s = pmt.serialize_str(v) d = pmt.deserialize_str(s) self.assertTrue(pmt.equal(v, d)) self.assertEqual(pmt.uniform_vector_itemsize(v), 8)