udp_client.py
Go to the documentation of this file.
1 #!/usr/bin/python
2 
3 import argparse
4 import socket
5 import math
6 import json
7 from time import sleep
8 
9 parser = argparse.ArgumentParser(description="Send UDP test data.")
10 parser.add_argument("--address", default="127.0.0.1", help="UDP address")
11 parser.add_argument("--port", default=9870, type=int, help="UDP port")
12 args = parser.parse_args()
13 
14 sock = socket.socket(socket.AF_INET, # Internet
15  socket.SOCK_DGRAM) # UDP
16 time = 0.0
17 
18 while True:
19  sleep(0.05)
20  time += 0.05
21 
22  data = {
23  "timestamp": time,
24  "test_data": {
25  "cos": math.cos(time),
26  "sin": math.sin(time)
27  }
28  }
29  sock.sendto( json.dumps(data).encode(), (args.address, args.port) )
30 
31  test_str = "{ \
32  \"1252\": { \
33  \"timestamp\": { \
34  \"microsecond\": 0 \
35  }, \
36  \"value\": { \
37  \"current\": { \
38  \"ampere\": null \
39  }, \
40  \"voltage\": { \
41  \"volt\": 24.852617263793945 \
42  }\
43  }\
44  } }"
45 
46  sock.sendto( test_str.encode("utf-8"), (args.address, args.port) )


plotjuggler
Author(s): Davide Faconti
autogenerated on Mon Nov 11 2024 03:23:48