otsdaq_demo  v2_05_02_indev
multi_udp_send_artdaq.py
1 #!/usr/bin/env python
2  # This file (udp_send_artdaq.py) was created by Ron Rechenmacher <ron@fnal.gov> on
3  # Jan 15, 2015. "TERMS AND CONDITIONS" governing this file are in the README
4  # or COPYING file. If you do not have such a file, one can be obtained by
5  # contacting Ron or Fermi Lab in Batavia IL, 60510, phone: 630-840-3000.
6  # $RCSfile: .emacs.gnu,v $
7  # rev="$Revision: 1.23 $$Date: 2012/01/23 15:32:40 $";
8 
9 import sys
10 import socket
11 import random
12 from time import sleep
13 USAGE='send host:port seqnum [packetCount] [outoforder]'
14 
15 # first byte is cmd
16 # 2nd byte is seqnum (yes, just 8 bits)
17 # rest is data (up to 1498)
18 
19 buf=''
20 
21 def main(argv):
22  print('len(argv)=%d'%(len(argv),))
23  packetCount = 1
24  outOfOrder = False
25  if len(argv) < 3: print(USAGE); sys.exit()
26  if len(argv) >= 4: packetCount = int(argv[3])
27  if len(argv) >= 5: outOfOrder = int(argv[4]) == 1
28  if len(argv) >= 6: json = int(argv[5]) == 1
29 
30  node,port = argv[1].split(':')
31  seqnum= int(argv[2])&0xff
32  s = socket.socket( socket.AF_INET, socket.SOCK_DGRAM )
33 
34  if json:
35  packetsSent = 0
36  while packetsSent < packetCount:
37  buf=chr(0x10)
38  buf+=chr(seqnum)
39  temperature = random.randrange(55,100)
40  humidity = random.randrange(10,99)
41  buf+="{\"temperature\":" + str(temperature) + ",\"humidity\":" + str(humidity) + ",\"ledState\":\"green\"}"
42  s.sendto(buf, (node,int(port)) )
43  packetsSent += 1
44  seqnum += 1
45  sleep(0.5)
46  else:
47  if packetCount > 1:
48  buf=chr(0x21)
49  buf+=chr(seqnum)
50  buf+="This is the first ARTDAQ UDP test string. It contains exactly 115 characters, making for a total size of 117 bytes."
51  s.sendto( buf, (node,int(port)) )
52  seqnum += 1
53 
54  packetsSent = 2
55  while packetsSent < packetCount:
56  buf=chr(0x22)
57  buf+=chr(seqnum & 0xff)
58  buf+="This is a ARTDAQ UDP test string. It contains exactly 107 characters, making for a total size of 109 bytes."
59  s.sendto( buf, (node,int(port)) )
60  packetsSent += 1
61  seqnum += 1
62 
63  if outOfOrder:
64  buf=chr(0x22)
65  buf+=chr((seqnum + 1) & 0xff)
66  buf+="This is the first out-of-order ARTDAQ UDP Test String. It should go after the second one in the output."
67  s.sendto( buf, (node,int(port)) )
68  buf=chr(0x22)
69  buf+=chr(seqnum & 0xff)
70  buf+="This is the second out-of-order ARTDAQ UDP Test String. It should come before the first one in the output."
71  s.sendto( buf, (node,int(port)) )
72  seqnum += 2
73 
74  buf=chr(0x23)
75  buf+=chr(seqnum & 0xff)
76  buf+="This is the last ARTDAQ UDP test string. It contains exactly 114 characters, making for a total size of 116 bytes."
77  s.sendto( buf, (node,int(port)) )
78  else:
79  buf=chr(0x20)
80  buf+=chr(seqnum & 0xff)
81  buf+="This is the ARTDAQ UDP test string. It contains exactly 109 characters, making for a total size of 111 bytes."
82  s.sendto( buf, (node,int(port)) )
83 
84  pass
85 
86 
87 if __name__ == "__main__": main(sys.argv)