artdaq_demo  v3_04_00
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] [json]'
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  jsonMode = False
26  if len(argv) < 3: print(USAGE); sys.exit()
27  if len(argv) >= 4: packetCount = int(argv[3])
28  if len(argv) >= 5: outOfOrder = int(argv[4]) == 1
29  if len(argv) >= 6: jsonMode = int(argv[5]) == 1
30 
31  node,port = argv[1].split(':')
32  seqnum= int(argv[2])&0xff
33  s = socket.socket( socket.AF_INET, socket.SOCK_DGRAM )
34 
35  if jsonMode:
36  packetsSent = 0
37  while packetsSent < packetCount:
38  buf=chr(0x10)
39  buf+=chr(seqnum)
40  temperature = random.randrange(55,100)
41  humidity = random.randrange(10,99)
42  buf+="{\"temperature\":" + str(temperature) + ",\"humidity\":" + str(humidity) + ",\"ledState\":\"green\"}"
43  s.sendto(buf, (node,int(port)) )
44  packetsSent += 1
45  seqnum += 1
46  sleep(0.5)
47  else:
48  if packetCount > 1:
49  buf=chr(0x21)
50  buf+=chr(seqnum)
51  buf+="This is the first ARTDAQ UDP test string. It contains exactly 115 characters, making for a total size of 117 bytes."
52  s.sendto( buf, (node,int(port)) )
53  seqnum += 1
54 
55  packetsSent = 2
56  while packetsSent < packetCount:
57  buf=chr(0x22)
58  buf+=chr(seqnum & 0xff)
59  buf+="This is a ARTDAQ UDP test string. It contains exactly 107 characters, making for a total size of 109 bytes."
60  s.sendto( buf, (node,int(port)) )
61  packetsSent += 1
62  seqnum += 1
63 
64  if outOfOrder:
65  buf=chr(0x22)
66  buf+=chr((seqnum + 1) & 0xff)
67  buf+="This is the first out-of-order ARTDAQ UDP Test String. It should go after the second one in the output."
68  s.sendto( buf, (node,int(port)) )
69  buf=chr(0x22)
70  buf+=chr(seqnum & 0xff)
71  buf+="This is the second out-of-order ARTDAQ UDP Test String. It should come before the first one in the output."
72  s.sendto( buf, (node,int(port)) )
73  seqnum += 2
74 
75  buf=chr(0x23)
76  buf+=chr(seqnum & 0xff)
77  buf+="This is the last ARTDAQ UDP test string. It contains exactly 114 characters, making for a total size of 116 bytes."
78  s.sendto( buf, (node,int(port)) )
79  else:
80  buf=chr(0x20)
81  buf+=chr(seqnum & 0xff)
82  buf+="This is the ARTDAQ UDP test string. It contains exactly 109 characters, making for a total size of 111 bytes."
83  s.sendto( buf, (node,int(port)) )
84 
85  pass
86 
87 
88 if __name__ == "__main__": main(sys.argv)