Skip to content
Permalink
Browse files

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
  • Loading branch information...
schofiaj committed Mar 11, 2019
1 parent 1014ef2 commit 3bf1b7e5d4f3c0a2b6d88c5334e8a7656d6a56a5
Showing with 24 additions and 16 deletions.
  1. +24 −16 psychopy/hardware/crs/colorcal.py
@@ -14,7 +14,6 @@

from __future__ import absolute_import, division, print_function


from builtins import bytes
from builtins import range
from builtins import object
@@ -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())
@@ -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()
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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)

0 comments on commit 3bf1b7e

Please sign in to comment.
You can’t perform that action at this time.