-
Notifications
You must be signed in to change notification settings - Fork 8
/
OPCN3_test.py
414 lines (356 loc) · 13.2 KB
/
OPCN3_test.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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
# -*- coding: utf-8 -*-
"""
Created on Mon Mar 18 16:22:16 2019
@author: Daniel Jarvi
Working with Sebastien Sikora we found the command 0x61 what needed before ever bytes to be sent.
Adding this correction, sending double the data bytes the filtering out the unwanted 0x61 responses,
The OPC N3 return data !!!!!
This is not specified in the datasheet, and may not be an issue on the audiono code (Currently not tested),
"""
from __future__ import print_function
import serial
import time
import struct
import datetime
import sys
import os.path
integration=5
# NAMING VARIABLES
OPCNAME = "TestOPC"
OPCPORT= "/dev/ttyACM0"
LOCATION = "Lab2"
wait=1e-06
#
# Init OPC for spi connection
def initOPC(ser):
#print("Init:")
time.sleep(1)
ser.write(bytearray([0x5A,0x01]))
nl = ser.read(3)
print(nl)
time.sleep(wait)
ser.write(bytearray([0x5A,0x03]))
nl=ser.read(9)
print(nl)
time.sleep(wait)
#SPI conncetion
ser.write(bytearray([0x5A,0x02,0x92,0x07]))
nl=ser.read(2)
print(nl)
time.sleep(wait)
# Turn fan
def fanOff(ser):
print("Fan turn off")
#start the flow chart the flow chart
T=0 #Triese counter
while True:
ser.write(bytearray([0x61,0x03]))
nl = ser.read(2)
# print(nl)
T=T+1
if nl== (b"\xff\xf3" or b"xf3\xff"):
time.sleep(wait)
#fan off
ser.write(bytearray([0x61,0x02]))
nl = ser.read(2)
# print(nl)
time.sleep(2)
fan="OFF"
print("Fan off")
return fan
elif T > 20:
print("Reset SPI")
time.sleep(3) #time for spi buffer to reset
#reset SPI conncetion
#initOPC(ser)
T=0
else:
time.sleep(wait*10) #wait 1e-05 before next commnad
# Turn fan and laser on
def fanOn(ser):
print("Fan turn on")
#start the flow chart the flow chart
T=0 #Triese counter
while True:
ser.write(bytearray([0x61,0x03]))
nl = ser.read(2)
# print(nl)
T=T+1
if nl== (b"\xff\xf3" or b"xf3\xff"):
time.sleep(wait)
#fan on
ser.write(bytearray([0x61,0x03]))
nl = ser.read(2)
# print(nl)
time.sleep(2)
fan="ON"
print("Fan On")
return fan
elif T > 20:
print("Reset SPI")
time.sleep(3) #time for spi buffer to reset
#reset SPI conncetion
# initOPC(ser)
T=0
else:
time.sleep(wait*10) #wait 1e-05 before next commnad
#Lazer on 0x07 is SPI byte following 0x03 to turn laser ON.
def LazOn(ser):
print("Lazer turn On")
T=0 #Triese counter
while True:
ser.write(bytearray([0x61,0x03]))
nl = ser.read(2)
# print(nl)
T=T+1
if nl== (b"\xff\xf3" or b"xf3\xff"):
time.sleep(wait)
#Lazer on
ser.write(bytearray([0x61,0x07]))
nl = ser.read(2)
# print(nl)
time.sleep(wait)
Laz="ON"
print("Fan On")
return Laz
elif T > 20:
print("Reset SPI")
time.sleep(3) #time for spi buffer to reset
#reset SPI conncetion
# initOPC(ser)
T=0
else:
time.sleep(wait*10) #wait 1e-05 before next commnad
#Lazer off 0x06 is SPI byte following 0x03 to turn laser off.
def LazOff(ser):
print("Lazer Off")
T=0 #Triese counter
while True:
ser.write(bytearray([0x61,0x03]))
nl = ser.read(2)
# print(nl)
T=T+1
if nl== (b"\xff\xf3" or b"xf3\xff"):
time.sleep(wait)
#Lazer off
ser.write(bytearray([0x61,0x06]))
nl = ser.read(2)
# print(nl)
time.sleep(wait)
Laz="Off"
print("Lazer Off")
return Laz
elif T > 20:
print("Reset SPI")
time.sleep(3) #time for spi buffer to reset
#reset SPI conncetion
# initOPC(ser)
T=0
else:
time.sleep(wait*10) #wait 1e-05 before next commnad
def RHcon(ans):
#ans is combine_bytes(ans[52],ans[53])
RH=100*(ans/(2**16-1))
return RH
def Tempcon(ans):
#ans is combine_bytes(ans[52],ans[53])
Temp=-45+175*(ans/(2**16-1))
return Temp
def combine_bytes(LSB, MSB):
return (MSB << 8) | LSB
def Histdata(ans):
#function for all the hist data, to break up the getHist
#time.sleep(wait)
data={}
data['Bin 0'] = combine_bytes(ans[0],ans[1])
data['Bin 1'] = combine_bytes(ans[2],ans[3])
data['Bin 2'] = combine_bytes(ans[4],ans[5])
data['Bin 3'] = combine_bytes(ans[6],ans[7])
data['Bin 4'] = combine_bytes(ans[8],ans[9])
data['Bin 5'] = combine_bytes(ans[10],ans[11])
data['Bin 6'] = combine_bytes(ans[12],ans[13])
data['Bin 7'] = combine_bytes(ans[14],ans[15])
data['Bin 8'] = combine_bytes(ans[16],ans[17])
data['Bin 9'] = combine_bytes(ans[18],ans[19])
data['Bin 10'] = combine_bytes(ans[20],ans[21])
data['Bin 11'] = combine_bytes(ans[22],ans[23])
data['Bin 12'] = combine_bytes(ans[24],ans[25])
data['Bin 13'] = combine_bytes(ans[26],ans[27])
data['Bin 14'] = combine_bytes(ans[28],ans[29])
data['Bin 15'] = combine_bytes(ans[30],ans[31])
data['Bin 16'] = combine_bytes(ans[32],ans[33])
data['Bin 17'] = combine_bytes(ans[34],ans[35])
data['Bin 18'] = combine_bytes(ans[36],ans[37])
data['Bin 19'] = combine_bytes(ans[38],ans[39])
data['Bin 20'] = combine_bytes(ans[40],ans[41])
data['Bin 21'] = combine_bytes(ans[42],ans[43])
data['Bin 22'] = combine_bytes(ans[44],ans[45])
data['Bin 23'] = combine_bytes(ans[46],ans[47])
data['Bin 24'] = combine_bytes(ans[48],ans[49])
data['period'] = combine_bytes(ans[52],ans[53])
data['FlowRate'] = combine_bytes(ans[54],ans[55])
data['Temp']=Tempcon(combine_bytes(ans[56],ans[57]))
data['RH'] = RHcon(combine_bytes(ans[58],ans[59]))
data['pm1'] = struct.unpack('f',bytes(ans[60:64]))[0]
data['pm2.5'] = struct.unpack('f',bytes(ans[64:68]))[0]
data['pm10'] = struct.unpack('f',bytes(ans[68:72]))[0]
data['Check']= combine_bytes(ans[84],ans[85])
# print(data)
return(data)
def read_all(port, chunk_size=86):
"""Read all characters on the serial port and return them."""
if not port.timeout:
raise TypeError('Port needs to have a timeout set!')
read_buffer = b''
while True:
# Read in chunks. Each chunk will wait as long as specified by
# timeout. Increase chunk_size to fail quicker
byte_chunk = port.read(size=chunk_size)
read_buffer += byte_chunk
if not len(byte_chunk) == chunk_size:
break
return read_buffer
def rightbytes(response):
'''
Get ride of the 0x61 byeste responce from the hist data, returning just the wanted data
'''
hist_response=[]
for j, k in enumerate(response): # Each of the 86 bytes we expect to be returned is prefixed by 0xFF.
if ((j + 1) % 2) == 0: # Throw away 0th, 2nd, 4th, 6th bytes, etc.
hist_response.append(k)
return hist_response
def getData(ser):
print("Get PM data")
T=0
while True:
#initsiate getData commnad
ser.write([0x61,0x32])
nl=ser.read(2)
# time.sleep(1e-05)
T=T+1
print(nl)
if nl== (b'\xff\xf3' or b'\xf3\xff' ):
#write to the OPC
for i in range(14): # Send the whole stream of bytes at once.
ser.write([0x61, 0x01])
time.sleep(0.00001)
#time.sleep(.1)
#read the data
ans=bytearray(ser.readall())
# print("ans=",ans)
ans=rightbytes(ans)
# print("ans=",ans)
b1 = ans[0:4]
b2 = ans[4:8]
b3 = ans[8:12]
c1=struct.unpack('f',bytes(b1))[0]
c2=struct.unpack('f',bytes(b2))[0]
c3=struct.unpack('f',bytes(b3))[0]
check=combine_bytes(ans[12],ans[13])
print("Check=",check)
return([c1,c2,c3])
elif T > 20:
print("Reset SPI")
time.sleep(3) #time for spi buffer to reset
#reset SPI conncetion
initOPC(ser)
T=0
return
else:
time.sleep(wait*10) #wait 1e-05 before next commnad
#get hist data
def getHist(ser):
#OPC N2 method
T=0 #attemt varaible
while True:
print("get hist attempt ",T)
#reques the hist data set
ser.write([0x61,0x30])
# time.sleep(wait*10)
nl = ser.read(2)
# print(nl)
T=T+1
print("Reading Hist data")
print(nl)
if nl== (b'\xff\xf3' or b'\xf3\xff' ):
for i in range(86): # Send the whole stream of bytes at once.
ser.write([0x61, 0x01])
time.sleep(0.000001)
# ans=bytearray(ser.read(1))
# print("ans=",ans,"len",len(ans))
time.sleep(wait) #delay
ans=bytearray(ser.readall())
print("ans=",ans,"len",len(ans))
ans=rightbytes(ans) #get the wanted data bytes
# ans=bytearray(test)
print("ans=",ans,"len",len(ans))
#print("test=",test,'len',len(test))
data=Histdata(ans)
return data
if T > 20:
print("Reset SPI")
time.sleep(3) #time for spi buffer to reset
#reset SPI conncetion
initOPC(ser)
T=0
return "No Data"
else:
time.sleep(wait*10) #wait 1e-05 before next commnad
# br.append(0x30)
#print("br",br,len(br))
#older vertion
# br = bytearray([0x61])
# time.sleep(wait)
# for i in range(0,85):
# br.append(0x30)
#print(i,len(br),br)
if __name__ == "__main__":
serial_opts = {
# built-in serial port is "COM1"
# USB serial port is "COM4"
"port": OPCPORT,
"baudrate": 9600,
"parity": serial.PARITY_NONE,
"bytesize": serial.EIGHTBITS,
"stopbits": serial.STOPBITS_ONE,
"xonxoff": False,
"timeout": wait,
# "inter_byte_timeout":wait,# Alternative
# "write_timeout":wait,
}
# wait for opc to boot
#time.sleep(10)
ser = serial.Serial(**serial_opts)
#ser.open()
print("**************************************************")
print("DID YOU CHECK THE DATE/TIME ????????")
print("**************************************************")
print("integration time (seconds)",integration)
print("**************************************************")
print("Init:")
initOPC(ser)
time.sleep(1)
print("Fan & Lazer Off:")
fan=fanOff(ser)
time.sleep(.1)
laz=LazOff(ser)
time.sleep(5)
print("Fan & Lazer on:")
fan=fanOn(ser)
time.sleep(5)
LazOn(ser)
# time.sleep(5)
# PM data#
for x in range(0,1):
# print(getData(ser))
print(getData(ser))
time.sleep(1)
print(getHist(ser))
time.sleep(integration)
print("Turning off")
time.sleep(2)
LazOff(ser)
time.sleep(wait)
fanOff(ser)
time.sleep(5)
ser.close()