Source code :: device_control

[Return]
 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
47
48
49
50
51
52
53
from pydcpf.appliances.DAS1210 import Device
import sys
from pygolem_lite.utilities import read_config

config = read_config('das.cfg')
IP_address = config['io_config']['ip']
port = int(config['io_config']['port'])
file_name = config['io_config']['file']

#IP_address = '192.168.2.221'
#port = 10001
number_of_samples = 10 * 4096 #TODO should calculate from used frequency and recorded time
debug = False
error_tolerance = 3 #how many errors to tolerate per channel

from socket import timeout as TimeoutError
timeout = 2

papouch = Device(IP_address, port, timeout=timeout)

if sys.argv[1] == "arming":
	from time import sleep
	if not papouch.set_ready(): #set all channels to armed state
		RuntimeError("Cannot arm device ..")
	sleep(0.01) # must not terminate the connection immediately
elif sys.argv[1] == "acquisition":
	from pydcpf.protocols.spinel97 import CheckSumError, ACKError
	import numpy as np
	#papouch.get_data(number_of_samples, 1)
	from threading import Thread #multiprocessing would copy data

	for channel in xrange(1, 13):
		if debug:
			print "Acquisition of channel %i" % channel
		error_count = 0
		while True:
			try:
				Thread(target=np.savez_compressed, args=(file_name+'_%02i' % channel,), kwargs=dict(
					data = np.hstack(
						[ np.frombuffer(buf, '<i2') for buf in papouch.get_data(number_of_samples, channel) ]
						)	
					, t_start = 0
					, t_end = number_of_samples * 1e-6 #TODO use actual frequency on channel
					, scale = 10.0 / 2**15 # 2**16 / 2 #TODO use actual amplification 
					)
				).run()
				break
			except (CheckSumError, ACKError, TimeoutError) as e:
				error_count += 1
				print "%i. failure on channel %i: " % (error_count, channel) + str(e)
				if error_count > error_tolerance:
					print "Too many (more than %i) errors on channel %i, aborting acquisition of this channel" % (error_tolerance, channel)
					break

Navigation