-
Notifications
You must be signed in to change notification settings - Fork 0
/
trigger.py
99 lines (79 loc) · 3.75 KB
/
trigger.py
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
import serial
#\r was added before every command to make it compatible with serial port protocol in c++/cli
feedbackmode = {'Normal':0, 'Time':1, 'Count':2, 'Frequency': 3, 'All':4}
class PSOC5_trigger:
def __init__(self, COM, BR = 115200, timeout = 1): #COM port and BauRate=default 115200
self.COM = COM
self.BR = BR
try:
self.PSOC5_serial_COM = serial.Serial(self.COM, self.BR ,timeout = timeout )
except serial.serialutil.SerialException:
print(f'Connection to {COM} port failed')
#Key is the command to gerate method in python
#First iteam of the list is the PSOC command
#Second item of the list is the mode
#Possible Notes
self.PSOCTriggerCommands = {'ID': ['*IDN?', 'R', ''],
'GetFrequencyMessage': ['freq', 'R', ''],
'GetFrequency': ['freq_int','R', 'in Hz'],
'GetTriggeredFrames': ['trig_frames','R', ''],
'GetSweepTime': ['sweep_time','R', 'in us'],
'SetModeNormal': ['SetMode_normal','W', ''],
'SetModeTime': ['SetMode_time','W', ''],
'SetModeCount': ['SetMode_count','W', ''],
'SetModeFrequeny': ['SetMode_freq', 'W', ''],
'SetModeAll': ['SetMode_all','W' , ''],
'GetMode': ['GetMode','R', feedbackmode]}
self.GenerateMethodsFromDictionary()
def discoverMethods(self):
for idx, key in enumerate(self.PSOCTriggerCommands.keys()):
print(f'{idx} - {key} - {self.PSOCTriggerCommands[key][2]}')
def SetFreqDivision(self, count):
encoded_count = self.encondeCommand(count)
return(self.decodeMessage(self.GetSerial(encoded_count)))
# Pure pyserial stuff:
def GetSerial(self,cmd):
self.PSOC5_serial_COM.write(cmd)
return(self.PSOC5_serial_COM.readline())
def SetSerial(self, cmd):
self.PSOC5_serial_COM.write(cmd)
def pullbuffer(self):
return(self.PSOC5_serial_COM.readline())
def waitForSignal(self):
return(self.decodeMessage(self.PSOC5_serial_COM.readline()))
def SetSerialTimeOut(self, t):
self.PSOC5_serial_COM.timeout = t
def GetSerialTimeOut(self):
return self.PSOC5_serial_COM.timeout
def flushSerialBuffer(self):
self.PSOC5_serial_COM.reset_input_buffer()
def CloseSerialConnection(self):
self.PSOC5_serial_COM.close()
print("Serial communication with PSOC closed")
#Gerate PSOC TRIGGER methods
@staticmethod
def encondeCommand(cmd):
command = (f'\r{cmd}\n').encode('utf-8')
return(command)
@staticmethod
def decodeMessage(msg):
message = msg.decode('utf-8').strip()
return(message)
def GenerateMethodsFromDictionary(self):
supportedCmd = self.PSOCTriggerCommands
for key,item in supportedCmd.items():
#print(key,item)
def makeCmd(cmd):
def makeSetCmd(self):
command = self.encondeCommand(cmd)
self.SetSerial(command)
def makeGetCmd(self):
command = self.encondeCommand(cmd)
msgout = self.decodeMessage(self.GetSerial(command))
return msgout
return makeSetCmd, makeGetCmd
Setcmd,Getcmd = makeCmd(item[0])
if 'W' in item[1]:
setattr(self, key, Setcmd.__get__(self,self.__class__))
if 'R' in item[1]:
setattr(self, key, Getcmd.__get__(self,self.__class__))