Reading from a cryochiller, and when it returns the temperature reading, it precedes the temperature with “TC”.
import serial
import time
#ser.close()
PORTS1 = ["COM7"] # "COM7": MT
SERS1 = [serial.Serial(port= p, baudrate=4800, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS, timeout=1) for p in PORTS1]
def start():
for s in SERS1:
s.write((("SET SSTOP=0" + "\r")).encode("utf-8"))
out = s.read_until().decode("utf-8")
print(out, end="")
def stop():
for s in SERS1:
s.write((("SET SSTOP=1" + "\r")).encode("utf-8"))
out = s.read_until().decode("utf-8")
print(out, end="")
def temps():
for s in SERS1:
s.write((("TC" + "\r")).encode("utf-8"))
out1 = s.read_until().decode("utf-8")
#out2 = out1.replace("TC", "").lstrip()
print(out1, end="")
while True:
cmd = input()
if cmd == "exit":
for s in SERS1:
s.close()
break
elif cmd == "start":
start()
elif cmd == "stop":
stop()
else:
while True:
temps()
time.sleep(.5)
Under for s in SERS1, s.write(((“TC” + “\r”)).encode(“utf-8”)) sends a command to the monitor requesting a temperature check, it returns two lines:
TC
((temperature))
When done on a terminal emulator like PuTTY, the two lines are spit out as one block. When done with Python, it will alternate between the two lines. On the first s.write, it will return “TC,” on the second it will return the temperature, and that repeats until the print function is stopped. Ideally, I’d like to omit the return of “TC” altogether and have the temperature returned to me upon every request.
Is this the line of code that gets the double line “TC” + temperature?
out1 = s.read_until().decode("utf-8")
You then immediately follow that by printing it, right?
It looks like the first line is “TC” followed by a newline. It is possible that there are trailing spaces as well, but I’m going to assume it is just the three characters “TC” plus a newline.
So let’s just delete them:
if out1.startswith('TC\n'):
# This should always be true, but play it safe.
out1 = out1[3:]
print(out1, end='')
If your version of Python is recent enough (3.10, maybe 3.9?) you can simplify that:
If these don’t work, you will need to show us exactly what the output is. Show us the output of print(repr(out1)) and we should be able to advise you better.