我创建了此代码来与注射泵进行通信。一切都工作正常,但我需要它连续运行几个小时,而且每次它都在似乎相同的时间停止(我没有计时它需要多长时间或计算迭代次数,它运行大约。 30-60分钟)。如果有人不介意看一下并让我知道可能导致此问题的原因。我对 python 不太有经验。
我认为值得注意的是,当代码“停止”时,内核仍在运行并且COM端口仍然打开。当它停止时,它并没有破坏循环,它只是发送一个 0mL/min 的流速命令,仅此而已。 谢谢
import numpy as np
import serial
import time
import math
#connect to serial port
ser = serial.Serial()
ser.port = 'COM3'
ser.baudrate = 115200
ser.bytesize = serial.EIGHTBITS
ser.parity = serial.PARITY_NONE
ser.stopbits = 1
ser.xonxoff = 0
ser.rtscts = 0
ser.dsrdtr = 0
ser.close()
ser.open()
ser.timeout = 1
# number of steps in one cycle
steps_per_cycle = 20
#variables
A = 1 #peak to peak displacement of piston
omega = 1 #frequency
a = 1 #tube inner radius
#volume functions
pump_rates = 0.5 * (np.sin(2 * np.pi * np.arange(steps_per_cycle) / steps_per_cycle)) # generate sinusoidal pump rates
pump_rates0 = 0.5 * A * omega * np.pi * (a**2) * np.cos(omega*np.arange(steps_per_cycle) / steps_per_cycle) #Harris and Goren Flow Rate fxn
#command loop
if ser.isOpen():
try:
i = 0
while i<=2: #infinite loop
print(ser.name + ' is open...')
for rate in pump_rates:
if rate > 0: #infusing
cmd = f'irate {"%.5f" % rate} ml/min'
print(f'Sending command: {cmd}')
# ser.write(cmd.encode())
ser.write(cmd.encode('ascii'))
ser.write(bytes(cmd, 'utf-8'))
ser.write(str.encode(cmd + '\r\n'))
cmd = f'irun'
# print(f'Sending command: {cmd}')
ser.write(cmd.encode('ascii'))
ser.write(bytes(cmd, 'utf-8'))
ser.write(str.encode(cmd + '\r\n'))
cmd = f'ctime'
# print(f'Sending command: {cmd}')
ser.write(cmd.encode('ascii'))
ser.write(bytes(cmd, 'utf-8'))
ser.write(str.encode(cmd + '\r\n'))
time.sleep(1)
if rate < 0: #withdrawing
cmd = f'wrate {"%.5f" % -rate} ml/min'
print(f'Sending command: {cmd}')
# ser.write(cmd.encode())
ser.write(cmd.encode('ascii'))
ser.write(bytes(cmd, 'utf-8'))
ser.write(str.encode(cmd + '\r\n'))
cmd = f'wrun'
# print(f'Sending command: {cmd}')
ser.write(cmd.encode('ascii'))
ser.write(bytes(cmd, 'utf-8'))
ser.write(str.encode(cmd + '\r\n'))
cmd = f'ctime'
# print(f'Sending command: {cmd}')
ser.write(cmd.encode('ascii'))
ser.write(bytes(cmd, 'utf-8'))
ser.write(str.encode(cmd + '\r\n'))
time.sleep(1)
if rate == 0:
cmd = f'stop'
print(f'Sending command: {cmd}')
ser.write(cmd.encode('ascii'))
ser.write(bytes(cmd, 'utf-8'))
ser.write(str.encode(cmd + '\r\n'))
time.sleep(1)
# except ser.SerialException as e:
# print("Serial port error: " + str(e))
finally:
if ser.isOpen():
ser.close()
起初,我有一个包含重复值的大型数组,这些值会定期发送到 COM 端口,但这很笨重,而且我的内存不足,所以我做了一个无限循环。我不知道为什么它停止了。
它已经运行了 2 个多小时,这肯定比之前要长,所以我会说问题已解决。我更改了代码以在每个循环结束时关闭和打开串行端口。如果有人能解释为什么这解决了问题,我将不胜感激。附件是编辑后的代码部分:
try:
i = 0
while i<=1: #infinite loop
print(ser.name + ' is open...')
for rate in pump_rates:
if rate > 0: #infusing
cmd = f'irate {"%.5f" % rate} ml/min'
print(f'Sending command: {cmd}')
# ser.write(cmd.encode())
ser.write(cmd.encode('ascii'))
ser.write(bytes(cmd, 'utf-8'))
ser.write(str.encode(cmd + '\r\n'))
cmd = f'irun'
# print(f'Sending command: {cmd}')
ser.write(cmd.encode('ascii'))
ser.write(bytes(cmd, 'utf-8'))
ser.write(str.encode(cmd + '\r\n'))
cmd = f'ctime'
# print(f'Sending command: {cmd}')
ser.write(cmd.encode('ascii'))
ser.write(bytes(cmd, 'utf-8'))
ser.write(str.encode(cmd + '\r\n'))
time.sleep(1)
if rate < 0: #withdrawing
cmd = f'wrate {"%.5f" % -rate} ml/min'
print(f'Sending command: {cmd}')
# ser.write(cmd.encode())
ser.write(cmd.encode('ascii'))
ser.write(bytes(cmd, 'utf-8'))
ser.write(str.encode(cmd + '\r\n'))
cmd = f'wrun'
# print(f'Sending command: {cmd}')
ser.write(cmd.encode('ascii'))
ser.write(bytes(cmd, 'utf-8'))
ser.write(str.encode(cmd + '\r\n'))
cmd = f'ctime'
# print(f'Sending command: {cmd}')
ser.write(cmd.encode('ascii'))
ser.write(bytes(cmd, 'utf-8'))
ser.write(str.encode(cmd + '\r\n'))
time.sleep(1)
if rate == 0:
cmd = f'stop'
print(f'Sending command: {cmd}')
ser.write(cmd.encode('ascii'))
ser.write(bytes(cmd, 'utf-8'))
ser.write(str.encode(cmd + '\r\n'))
time.sleep(1)
ser.close()
ser.open()
except ser.SerialException as e:
print("Serial port error: " + str(e))