Skip to content

Commit 3bf1b7e

Browse files
authored
BF: Fixes str to byte conversions in PY3
PY3 doe snot automatically convert str to byte and vice versa. These changes fix that for talkign to colourCal MKII
1 parent 1014ef2 commit 3bf1b7e

File tree

1 file changed

+24
-16
lines changed

1 file changed

+24
-16
lines changed

psychopy/hardware/crs/colorcal.py

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
from __future__ import absolute_import, division, print_function
1616

17-
1817
from builtins import bytes
1918
from builtins import range
2019
from builtins import object
@@ -120,7 +119,6 @@ def sendMessage(self, message, timeout=0.1):
120119
"""Send a command to the photometer and wait an alloted
121120
timeout for a response.
122121
"""
123-
124122
# flush the read buffer first
125123
# read as many chars as are in the buffer
126124
prevOut = self.com.read(self.com.inWaiting())
@@ -130,7 +128,7 @@ def sendMessage(self, message, timeout=0.1):
130128
self.lastCmd = message
131129

132130
if message[-2:] not in ('\n', '\n\r'):
133-
message += '\n' # append a newline if necess
131+
message += "\n".encode('ascii') # append a newline if necess
134132
# send the message
135133
self.com.write(message)
136134
self.com.flush()
@@ -143,17 +141,21 @@ def sendMessage(self, message, timeout=0.1):
143141
lines = []
144142
thisLine = ''
145143
nEmpty = 0
146-
while (thisLine != '>') and (nEmpty <= self.maxAttempts):
144+
while (thisLine != '>'.encode('ascii')) and (nEmpty <= self.maxAttempts):
147145
# self.com.readline can't handle custom eol
148146
thisLine = self.readline(eol=eol)
149-
if thisLine in (eol, '>', ''): # lines we don't care about
147+
if thisLine in (eol.encode('ascii'), '>'.encode('ascii'), ''.encode('ascii')): # lines we don't care about
150148
nEmpty += 1
151149
continue
152150
else:
153151
# line without any eol chars
154-
lines.append(thisLine.rstrip(eol))
152+
#myLine=thisLine.encode('ascii')
153+
#myLine=thisLine.rstrip(eol.encode('ascii'))
154+
#lines.append(thisLine)
155+
lines.append(thisLine.rstrip(eol.encode('ascii')))
155156
nEmpty = 0
156157

158+
157159
# got all lines and reached '>'
158160
if len(lines) == 1:
159161
return lines[0] # return the string
@@ -178,10 +180,11 @@ def measure(self):
178180
"""
179181
# use a long timeout for measurement:
180182
val = self.sendMessage(b'MES', timeout=5)
181-
vals = val.split(',')
183+
valstrip=val.strip(b'\n\r>')
184+
vals = valstrip.split(b',')
182185
ok = (vals[0] == 'OK00')
183186
# transform raw x,y,z by calibration matrix
184-
xyzRaw = numpy.array([vals[1], vals[2], vals[3]], dtype=float)
187+
xyzRaw = numpy.array([vals[1].strip(), vals[2].strip(), vals[3].strip()], dtype=float)
185188
X, Y, Z = numpy.dot(self.calibMatrix, xyzRaw)
186189
self.ok, self.lastLum = ok, Y
187190
return ok, X, Y, Z
@@ -206,13 +209,16 @@ def getInfo(self):
206209
Other values will be a string or None.
207210
208211
"""
209-
val = self.sendMessage(b'IDR').split(',')
210-
ok = (val[0] == 'OK00')
212+
val = self.sendMessage(b'IDR')
213+
valstrip=val.strip(b'\n\r>')
214+
val=valstrip.split(b',')
215+
ok = (val[0] == b'OK00')
211216
if ok:
212217
firmware = val[2]
213218
serialNum = val[4]
214219
firmBuild = val[-1]
215220
else:
221+
print('not ok')
216222
firmware = 0
217223
serialNum = 0
218224
firmBuild = 0
@@ -231,7 +237,8 @@ def getNeedsCalibrateZero(self):
231237
232238
:returns: True or False
233239
"""
234-
if self.firmBuild < '877' and not self._zeroCalibrated:
240+
241+
if self.firmBuild < b'877' and not self._zeroCalibrated:
235242
return True
236243
else:
237244
return False
@@ -248,9 +255,9 @@ def calibrateZero(self):
248255
ColorCAL.getNeedsCalibrateZero()
249256
"""
250257
val = self.sendMessage(b"UZC", timeout=1.0)
251-
if val == 'OK00':
258+
if val == b'OK00':
252259
pass
253-
elif val == 'ER11':
260+
elif val == b'ER11':
254261
logging.error(
255262
"Could not calibrate ColorCAL2. Is it properly covered?")
256263
return False
@@ -282,9 +289,10 @@ def getCalibMatrix(self):
282289
# parsing?
283290
for rowN in range(3):
284291
rowName = 'r0%i' % (rowN + 1)
285-
val = self.sendMessage(rowName, timeout=1.0)
286-
vals = val.split(',') # convert to list of values
287-
if vals[0] == 'OK00' and len(vals) > 1:
292+
val = self.sendMessage(rowName.encode('ascii'), timeout=1.0)
293+
valstrip=val.strip(b'\n\r>')
294+
vals = valstrip.split(b',') # convert to list of values
295+
if vals[0] == b'OK00' and len(vals) > 1:
288296
# convert to numpy array
289297
rawVals = numpy.array(vals[1:], dtype=int)
290298
floats = _minolta2float(rawVals)

0 commit comments

Comments
 (0)