forked from yzy19900402/OFDR_PC_server
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathData_Processor.py
More file actions
129 lines (112 loc) · 3.94 KB
/
Data_Processor.py
File metadata and controls
129 lines (112 loc) · 3.94 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import numpy as np
import matplotlib.pyplot as plt
import time
import socket
import math
import sys
from scipy import signal
import binascii
import struct
TARGET_IP = ''
TARGET_PORT = 8000
Data_length = 65536 # the data length for u64, 8bytes
UDP_MAX = 32768 # the max byte length for UDP
# ADC_Offset = 135 # The bias of the ADC chip
COLUMN_NUM = math.floor(UDP_MAX / 8)
TARGET = (TARGET_IP, TARGET_PORT)
folder = './data/'
# Socket part capture the data
class Data_Processor:
global Data_length
ss = 0
Depth = math.floor(Data_length * 8 / UDP_MAX)
stop_thread = False
i = 0
data = 0
ADC_data_A = np.zeros(Data_length, dtype=np.int16)
ADC_data_B = np.zeros(Data_length, dtype=np.int16)
DAC_data = np.zeros(Data_length, dtype=np.uint16)
Other = np.zeros(COLUMN_NUM, dtype=np.uint16)
Trigger = np.zeros(Data_length, dtype=np.uint8)
Up = np.zeros(Data_length, dtype=np.uint8)
Dn = np.zeros(Data_length, dtype=np.uint8)
CMP_in = np.zeros(Data_length, dtype=np.uint8)
CMP_Delay = np.zeros(Data_length, dtype=np.uint8)
data_ready = False
temp_buffer = np.zeros([math.floor(Data_length*8/UDP_MAX),UDP_MAX],dtype=np.uint8)
def __init__(self):
self.ss = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.ss.bind(TARGET)
self.stop_thread = False
def close(self):
self.stop_thread = True
def reset(self):
self.i = self.Depth - 1;
def run(self):
if self.stop_thread:
self.stop_thread = False
while True:
if self.stop_thread:
self.ss.close()
print('closed!!!!!!!!!!!!!')
break
else:
self.ss.settimeout(1.0)
try:
data, addrRsv = self.ss.recvfrom(UDP_MAX)
except:
#print('Try failed')
pass
else:
print(self.i)
if (self.data_ready == False):
if data[3] == 0xaa:
self.i = 0;
if data[3] == 0x55:
if self.i == self.Depth - 1:
self.data_ready = True
self.temp_buffer[self.i,:] = np.frombuffer(data,dtype=np.uint8)
self.i = (self.i + 1) % self.Depth
# Debug_test = self.DAC_data
# print(Debug_test)
# break
def save_data(self):
pass
def shot_wave(self):
pass
def get_shot(self):
return self.DAC_data
def if_ready(self):
return self.data_ready
def get_data(self):
data = self.temp_buffer.tobytes();
self.data = np.frombuffer(self.temp_buffer, np.uint16)
data_temp = np.reshape(self.data, [Data_length, 4])
ADC_data_A = data_temp[:, 0]
ADC_data_B = data_temp[:, 1]
DAC_data = data_temp[:, 2]
Other = data_temp[:, 3]
CMP_Delay = Other % 2
Dn = (Other // 2) % 16
CMP_in = (Other // 2 ^ 5) % 2
Trigger = (Other // 2 ^ 6) % 2
Up = (Other // 2 ^ 7) % 2
self.data_ready = False
return ADC_data_A - np.mean(ADC_data_A), ADC_data_B - np.mean(ADC_data_B), DAC_data / (
2 ^ 13), CMP_Delay, Dn, Trigger, Up, CMP_in
# def BitReverse(self, Source, bitSize):
# ret = np.zeros(len(Source), np.uint8)
# for index in range(len(Source)):
# int_data = (Source[index])
# binary = bin(int_data)
# reverse = binary[-1:1:-1]
# reverse = reverse + (bitSize - len(reverse))*'0'
# ret[index] = reverse
# return ret
#
# def ByteReverse(self, Source):
# ret = np.zeros(len(Source), np.uint16)
# for index in range(len(Source)):
# ByteReverse = (Source[index])[::-1]
# ret[index] = ByteReverse.encode('hex')
# return ret