Skip to content

Commit

Permalink
BF: Fixes str to byte conversions in PY3
Browse files Browse the repository at this point in the history
PY3 doe snot automatically convert str to byte and vice versa.
These changes fix that for talkign to colourCal MKII
  • Loading branch information
schofiaj committed Mar 11, 2019
1 parent 1014ef2 commit 3bf1b7e
Showing 1 changed file with 24 additions and 16 deletions.
40 changes: 24 additions & 16 deletions psychopy/hardware/crs/colorcal.py
Expand Up @@ -14,7 +14,6 @@

from __future__ import absolute_import, division, print_function


from builtins import bytes
from builtins import range
from builtins import object
Expand Down Expand Up @@ -120,7 +119,6 @@ def sendMessage(self, message, timeout=0.1):
"""Send a command to the photometer and wait an alloted
timeout for a response.
"""

# flush the read buffer first
# read as many chars as are in the buffer
prevOut = self.com.read(self.com.inWaiting())
Expand All @@ -130,7 +128,7 @@ def sendMessage(self, message, timeout=0.1):
self.lastCmd = message

if message[-2:] not in ('\n', '\n\r'):
message += '\n' # append a newline if necess
message += "\n".encode('ascii') # append a newline if necess
# send the message
self.com.write(message)
self.com.flush()
Expand All @@ -143,17 +141,21 @@ def sendMessage(self, message, timeout=0.1):
lines = []
thisLine = ''
nEmpty = 0
while (thisLine != '>') and (nEmpty <= self.maxAttempts):
while (thisLine != '>'.encode('ascii')) and (nEmpty <= self.maxAttempts):
# self.com.readline can't handle custom eol
thisLine = self.readline(eol=eol)
if thisLine in (eol, '>', ''): # lines we don't care about
if thisLine in (eol.encode('ascii'), '>'.encode('ascii'), ''.encode('ascii')): # lines we don't care about
nEmpty += 1
continue
else:
# line without any eol chars
lines.append(thisLine.rstrip(eol))
#myLine=thisLine.encode('ascii')
#myLine=thisLine.rstrip(eol.encode('ascii'))
#lines.append(thisLine)
lines.append(thisLine.rstrip(eol.encode('ascii')))
nEmpty = 0


# got all lines and reached '>'
if len(lines) == 1:
return lines[0] # return the string
Expand All @@ -178,10 +180,11 @@ def measure(self):
"""
# use a long timeout for measurement:
val = self.sendMessage(b'MES', timeout=5)
vals = val.split(',')
valstrip=val.strip(b'\n\r>')
vals = valstrip.split(b',')
ok = (vals[0] == 'OK00')
# transform raw x,y,z by calibration matrix
xyzRaw = numpy.array([vals[1], vals[2], vals[3]], dtype=float)
xyzRaw = numpy.array([vals[1].strip(), vals[2].strip(), vals[3].strip()], dtype=float)
X, Y, Z = numpy.dot(self.calibMatrix, xyzRaw)
self.ok, self.lastLum = ok, Y
return ok, X, Y, Z
Expand All @@ -206,13 +209,16 @@ def getInfo(self):
Other values will be a string or None.
"""
val = self.sendMessage(b'IDR').split(',')
ok = (val[0] == 'OK00')
val = self.sendMessage(b'IDR')
valstrip=val.strip(b'\n\r>')
val=valstrip.split(b',')
ok = (val[0] == b'OK00')
if ok:
firmware = val[2]
serialNum = val[4]
firmBuild = val[-1]
else:
print('not ok')
firmware = 0
serialNum = 0
firmBuild = 0
Expand All @@ -231,7 +237,8 @@ def getNeedsCalibrateZero(self):
:returns: True or False
"""
if self.firmBuild < '877' and not self._zeroCalibrated:

if self.firmBuild < b'877' and not self._zeroCalibrated:
return True
else:
return False
Expand All @@ -248,9 +255,9 @@ def calibrateZero(self):
ColorCAL.getNeedsCalibrateZero()
"""
val = self.sendMessage(b"UZC", timeout=1.0)
if val == 'OK00':
if val == b'OK00':
pass
elif val == 'ER11':
elif val == b'ER11':
logging.error(
"Could not calibrate ColorCAL2. Is it properly covered?")
return False
Expand Down Expand Up @@ -282,9 +289,10 @@ def getCalibMatrix(self):
# parsing?
for rowN in range(3):
rowName = 'r0%i' % (rowN + 1)
val = self.sendMessage(rowName, timeout=1.0)
vals = val.split(',') # convert to list of values
if vals[0] == 'OK00' and len(vals) > 1:
val = self.sendMessage(rowName.encode('ascii'), timeout=1.0)
valstrip=val.strip(b'\n\r>')
vals = valstrip.split(b',') # convert to list of values
if vals[0] == b'OK00' and len(vals) > 1:
# convert to numpy array
rawVals = numpy.array(vals[1:], dtype=int)
floats = _minolta2float(rawVals)
Expand Down

0 comments on commit 3bf1b7e

Please sign in to comment.