-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandom_trame.py
More file actions
executable file
·38 lines (33 loc) · 1.48 KB
/
random_trame.py
File metadata and controls
executable file
·38 lines (33 loc) · 1.48 KB
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
#!/usr/bin/python3
import trame
import argparse
import json
import struct
import os
import sys
import time
parser = argparse.ArgumentParser(description='Generates a random trame')
parser.add_argument('--length', type=int, default=5, help='Number of sensors (defaults to 5)')
parser.add_argument('--type', type=str, default='i', help='type of sensor value (struct.pack compatible) (defaults to \'i\')')
parser.add_argument('--ecclength', default=10, type=int, help='length of the reed-solomon error correcting code (defaults to 10)')
parser.add_argument('--startbyte', default=0xFF, type=int, help='Start byte (defaults to 0xFF)')
parser.add_argument('--file', type=str, help='Input config file')
parser.add_argument('--out', default=sys.stdout.buffer, type=argparse.FileType('wb'), help='Output file (defaults to stdout)')
parser.add_argument('-N', type=int, default=-1, help='Number of frames to be generated (defaults to infinite)')
parser.add_argument('-t', type=int, default=1, help='Time between two frames in seconds (defaults to 1)')
args = parser.parse_args()
if args.file :
conf = json.load(open(args.file, "r"))
else:
conf = {"sensors": [args.type]*args.length, "trame":{"startbyte": args.startbyte, "ecc":{"length":args.ecclength}}}
N = args.N
while N != 0:
vals = []
for fmt in conf["sensors"]:
val = struct.unpack("!" + fmt, os.urandom(struct.calcsize(''.join(fmt))))
vals.append(val)
frame = trame.trame(vals, conf)
args.out.write(frame)
N -= 1
args.out.flush()
time.sleep(args.t)