-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLBDataComm_NoFluorescence.py
More file actions
131 lines (86 loc) · 3.16 KB
/
LBDataComm_NoFluorescence.py
File metadata and controls
131 lines (86 loc) · 3.16 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
130
131
#THIS VERSION IS TO USE THE LIGHTBOX AS A STANDALONE MODULE
import pandas as pd
import numpy as np
import xml.etree.ElementTree as ET
import serial
import time
print ()
print ()
#Set COM port depending on current user setup
serPort = 'COM6'
baudRate = 9600
ser = serial.Serial(serPort, baudRate)
startMarker = 60
endMarker = 62
def sendToArduino(sendStr):
ser.write(sendStr.encode('utf-8')) # change for Python3
#======================================
def recvFromArduino():
global startMarker, endMarker
ck = ""
x = "z" # any value that is not an end- or startMarker
byteCount = -1 # to allow for the fact that the last increment will be one too many
# wait for the start character
while ord(x) != startMarker:
x = ser.read()
# save data until the end marker is found
while ord(x) != endMarker:
if ord(x) != startMarker:
ck = ck + x.decode("utf-8") # change for Python3
byteCount += 1
x = ser.read()
return(ck)
#============================
def waitForArduino():
# wait until the Arduino sends 'Arduino Ready' - allows time for Arduino reset
# it also ensures that any bytes left over from a previous message are discarded
global startMarker, endMarker
msg = ""
while msg.find("Arduino is ready") == -1:
while ser.inWaiting() == 0:
pass
msg = recvFromArduino()
print (msg) # python3 requires parenthesis
print ()
#======================================
def runTest(td):
numLoops = len(td)
waitingForReply = False
n = 0
while n < numLoops:
teststr = td[n]
if waitingForReply == False:
sendToArduino(teststr)
print ("Sent from PC -- LOOP NUM " + str(n) + " TEST STR " + teststr)
waitingForReply = True
if waitingForReply == True:
while ser.inWaiting() == 0:
pass
dataRecvd = recvFromArduino()
print ("Reply Received " + dataRecvd)
n += 1
waitingForReply = False
print ("===========")
time.sleep(5)
#======================================
def blockshaped(arr, nrows, ncols):
"""
Return an array of shape (n, nrows, ncols) where
n * nrows * ncols = arr.size
If arr is a 2D array, the returned array should look like n subblocks with
each subblock preserving the "physical" layout of arr.
"""
h, w = arr.shape
assert h % nrows == 0, "{} rows is not evenly divisble by {}".format(h, nrows)
assert w % ncols == 0, "{} cols is not evenly divisble by {}".format(w, ncols)
return (arr.reshape(h//nrows, nrows, -1, ncols)
.swapaxes(1,2)
.reshape(-1, nrows, ncols))
print ("Serial port " + serPort + " opened Baudrate " + str(baudRate))
waitForArduino()
#Data sent to the Arduino for Lighting
a = [4095, 4095,4095,4095,4095,4095,4095,4095]
b = '<'+ ','.join(map(str, a)) + '>'
testData = [b]
runTest(testData)
ser.close()