Troldtoft Troldtoft - 3 months ago 20
Python Question

Loading variable from txt

I'm having problems with the following code:

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()


And I get the following error message:


Traceback (most recent call last): File
"C:\Users\Mikkel\Desktop\Python Scripts\CV_and_Screeningv100.py", line
24, in
ser = serial.Serial(Port, 9600) File "C:\Users\Mikkel\AppData\Local\Programs\Python\Python35\lib\site-packages\serial\serialwin32.py",
line 31, in init
super(Serial, self).init(*args, **kwargs) File "C:\Users\Mikkel\AppData\Local\Programs\Python\Python35\lib\site-packages\serial\serialutil.py",
line 182, in init self.open() File
"C:\Users\Mikkel\AppData\Local\Programs\Python\Python35\lib\site-packages\serial\serialwin32.py",
line 62, in open raise SerialException("could not open port {!r}:
{!r}".format(self.portstr, ctypes.WinError()))
serial.serialutil.SerialException: could not open port 'COM11\n':

FileNotFoundError(2, 'The system cannot find the file specified.',
None, 2)


I'm loading the data from a txt file called 'config.txt', and it seems that it is the

Port = lines[1]


or

ser = serial.Serial(Port,9600)


that are the problem. Am I using the wrong format? I tried to introduce a 'str', 'init' and 'float', but nothing works. The data on lines[1] is COM11, which is the current port.

Answer

Checkout the Error log there is written can't open COM11\n so you have to remove the newline (\n).

Example: Port.strip()