Up to the last time, measurement can be automated. This time, when the measuring device is in a bad mood, it will throw an error, so we will deal with it.
-Microsoft Support Page --As mentioned on the above site, on Windows ** COM ports may continue to grow as the number of connected devices increases, but problems may occur when the number exceeds 10. In my environment, when ADVANTEST's R6441 is connected using a USB hub etc., measurement may fail if the COM port is 10 or more.
If you write the name of the exception (printed when an error occurs) after ʻexcept, you can determine the behavior when it fails. This time, I will do
ser.close ()`.
try:
while 1:
if pulse >= MAX:
##End the while statement when the position reaches MAX
break
##Record current location information
pulse_list.append(pulse/2)
##Measure the current(Take 5 times and average it as the value at that position)
for i in range(5):
ser = serial.Serial(COMampere,bitRate,timeout=0.1)
ser.write(b"F5, R0,PR2\r\n")
time.sleep(1)
ser.write(b"MD?\r\n")
time.sleep(1)
tmp = ser.read_all()
#Skip if no current is available
if len(tmp)== 0:
ser.close()
continue
ampere = float(tmp.split()[2])
ampere_average_list.append(ampere)
time.sleep(1)
ser.close()
##Current and pulse(position)To list
ampere_list.append(sum(ampere_average_list)/len(ampere_average_list))
ampere_average_list = []
##Move the optical table
pulse += 1000
position = "A:2+P"+str(pulse)+"\r\n"
ser = serial.Serial(COMpulse,bitRate,timeout=0.1)
ser.write(bytes(position, 'UTF-8'))
time.sleep(1)
ser.write(b"G:\r\n")
ser.close()
##Turn the list into a dataframe
print(ampere_list)
print(pulse_list)
df = pd.DataFrame({'ampere(A)':ampere_list,'pulse':pulse_list})
def pulseToMilliMeter(pulse):
return pulse*0.006
df["position(mm)"] = df["pulse"].map(pulseToMilliMeter)
df.to_csv('./csv/result.csv',index=False)
plt.figure()
df.plot(x='position(mm)',y='ampere(A)',marker='o')
plt.savefig('./img/sample.png')
plt.close('all')
except IndexError:
ser.close()
Recommended Posts