summaryrefslogtreecommitdiff
path: root/webserver.py
blob: cc17d707b28f9fd7945e2ca310001adec04a81b2 (plain)
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
import zmq
import pmt

def zmq_pdu_subscriber():
    context = zmq.Context()
    socket = context.socket(zmq.SUB)
    
    # 连接到GNU Radio的ZMQ Pub Sink
    # 地址和端口需要与GNU Radio流图中的配置一致
    socket.connect("tcp://localhost:5001")
    socket.setsockopt_string(zmq.SUBSCRIBE, "")
    
    print("等待接收ADS-B PDU消息...")
    
    try:
        while True:
            # 接收二进制PDU数据
            pdu_bin = socket.recv()
            
            # 反序列化PDU
            pdu = pmt.deserialize_str(pdu_bin)
            
            # 提取数据部分(pmt.car获取PDU的数据)
            plane = pmt.to_python(pmt.car(pdu))
            
            # 处理ADS-B飞机数据
            print("收到飞机数据:")
            print(f"  ICAO地址: {plane.get('icao', 'N/A')}")
            print(f"  时间: {plane.get('datetime', 'N/A')}")
            print(f"  经度: {plane.get('longitude', 'N/A')}")
            print(f"  纬度: {plane.get('latitude', 'N/A')}")
            print(f"  高度: {plane.get('altitude', 'N/A')} 英尺")
            print(f"  速度: {plane.get('speed', 'N/A')} 节")
            print(f"  航向: {plane.get('heading', 'N/A')}°")
            print("-" * 50)
            
    except KeyboardInterrupt:
        print("\n接收端停止")
    except Exception as e:
        print(f"处理数据时出错: {e}")
    finally:
        socket.close()
        context.term()

if __name__ == "__main__":
    zmq_pdu_subscriber()