我在使用以下代码时遇到问题:
f=open('config.txt')
lines=f.readlines()
print(lines[1])
print(lines[3])
print(lines[5])
print(lines[7])
print(lines[9])
print(lines[11])
#Import and define
import serial
import time
time.sleep(5)
#Settings
Port = lines[1]
TargetMaxVoltage = float(lines[3])
TargetStartVoltage = float(lines[5])
TargetIntervalVoltage = float(lines[7])
DelayTime = float(lines[9])
LogInterval = float(lines[11])
CV = True
ser = serial.Serial(Port, 9600)
TargetStartVoltageLock = TargetStartVoltage
#Starting Settings
ser.write(b'>Start\r\n')
ser.write(b'>VoSet 0.5\r\n')
time.sleep(1)
ser.write(b'>expMode 1\r\n')
ser.write(b'>runTime 1000000000\r\n')
ser.write(b'>stDly 1000000000\r\n')
ser.write(b'>IoSet 1\r\n')
ser.write(b'>Imax 50\r\n')
ser.write(b'>Imin 1\r\n')
ser.write(b'>FarEfc 1\r\n')
ser.write(b'>MolMax 9999\r\n')
LogTimeInterval = ">logIntv "+str(format(LogInterval, '.4f'))+"\r\n"
ser.write(LogTimeInterval.encode('utf-8'))
time.sleep(5)
#Running Method None CV Screening
while (TargetStartVoltage != TargetMaxVoltage and CV == False):
DataSend = ">VoSet "+str(format(TargetStartVoltage, '.2f'))+"\r\n"
print (DataSend)
ser.write(DataSend.encode('utf-8'))
TargetStartVoltage += TargetIntervalVoltage
time.sleep(DelayTime)
#Running Method CV Screening
CVComplete = False
while (TargetStartVoltage <= TargetMaxVoltage and CV == True and CVComplete != True):
DataSend = ">VoSet "+str(format(TargetStartVoltage, '.2f'))+"\r\n"
print (DataSend)
ser.write(DataSend.encode('utf-8'))
TargetStartVoltage += TargetIntervalVoltage
time.sleep(DelayTime)
else:
TargetStartVoltage -= 2*TargetIntervalVoltage
while(TargetStartVoltageLock <= TargetStartVoltage and CV == True and CVComplete != True):
DataSend = ">VoSet "+str(format(TargetStartVoltage, '.2f'))+"\r\n"
print (DataSend)
ser.write(DataSend.encode('utf-8'))
TargetStartVoltage -= TargetIntervalVoltage
time.sleep(DelayTime)
else:
CVComplete = True
ser.close()
我收到以下错误消息:
我正在从名为“config.txt”的txt文件加载数据,看来这是
Port = lines[1]
要么
ser = serial.Serial(Port,9600)
这就是问题所在。我使用的格式错误吗?我尝试引入“str”,“init”和“float”,但没有任何效果。第[1]行上的数据是COM11,它是当前端口。
最佳答案
check out 错误日志
写的无法打开COM11\n
,因此您必须删除换行符(\n
)。
示例:Port.strip()