게시판 즐겨찾기
편집
드래그 앤 드롭으로
즐겨찾기 아이콘 위치 수정이 가능합니다.
안녕하세요 파이선으로 텍스트파일에 입력 시 딜레이 관련 질문입니다.
게시물ID : programmer_22052짧은주소 복사하기
작성자 : 못생긴남성
추천 : 0
조회수 : 452회
댓글수 : 3개
등록시간 : 2018/01/29 12:02:26
완전..완전..생초보입니다!

제가 MFC라는 질량 유량계에서..1초마다 아두이노를 통해서 값을 받고 있는데요..

플로팅을 하기 위해서 파이썬으로 어찌어찌 코딩을 해서 1초마다 그 값을 텍스트 파일에 쓰게 해놨습니다.

근데 이게 문제가..실행시키고 한 20초가 지나니...elapsed time은 1초인데 체감 시간이 2초 정도로 계속 딜레이 되는겁니다..

텍스트 파일에 계속 데이터를 입력해서 용량이 커지니 얘가 열고 닫는 시간이 조금씩 느려지나 보다 생각했는데..

또 다음줄에 새로 덮어쓰기를 하면 1~19초 정도는 제대로 나오다가 20초 정도면 딜레이가 시작됩니다..

혹시 이런 현상을 해결할 수 있는 방법이 있을까요!?

코드는 다음과 같습니다..

-----------------------------------------------------
import serial
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.animation as anim
plt.close('all')
fig = plt.figure()
ax1 = fig.add_subplot(3,1,1)
ax2 = fig.add_subplot(3,1,2)
ax3 = fig.add_subplot(3,1,3)
serial_port = '/dev/cu.usbmodem1441';
baud_rate = 38400; #In arduino, Serial.begin(baud_rate)
ser = serial.Serial(serial_port, baud_rate)
write_to_file_path = "data_2.txt";
#output_file = open(write_to_file_path, "w+");
lines = np.ones(100)
etime =[]
temp = []
RH = []
N2 = []
H2O = []
N2M = []
H2OM = []
def animate(i):
output_file = open(write_to_file_path, "a")
line = ser.readline()
line = line.decode("utf-8")
print(line)
output_file.write(line)
output_file.close()
newline = np.fromstring(line,dtype=float,sep=',')
for line in lines:
if len(newline) >1:
etimeC = newline[0]
etime.append(etimeC)
tempC = newline[7]
temp.append(tempC)
RHC = newline[8]
RH.append(RHC)
N2C = newline[5]*2000/255 #0 to 255 -> 0 to 2000 sccm
N2MC = newline[9]*2000/1023 #0 to 1023 -> 0 to 2000 sccm
H2OC = newline[6]*2000/255 #0 to 255 -> 0 to 2000 sccm
H2OMC = newline[10]*2000/1023 #0 to 1023 -> 0 to 2000 sccm
N2.append(N2C)
H2O.append(H2OC)
N2M.append(N2MC)
H2OM.append(H2OMC)
ax1.clear()
ax2.clear()
ax3.clear()
ax1.set_ylim(0,40)
ax2.set_ylim(0,100)
ax3.set_ylim(0,2000)
ax1.set_ylabel('Temp. (oC)')
ax2.set_ylabel('RH (%)')
ax3.set_xlabel('Time (s)')
ax3.set_ylabel('Flow rate (sccm)')
ax1.annotate(str(tempC)+'oC',xy=(10,10))
ax2.annotate(str(RHC)+'%',xy=(10,10))
ax3.annotate('N2 '+str(int(N2MC)),xy=(10,1800))
ax3.annotate('H2O '+str(int(H2OMC)),xy=(10,20))
ax1.plot(etime, temp, 'r-', label='temp')
ax2.plot(etime, RH, 'b-', label='RH')
ax3.plot(etime,N2,'m',etime,H2O,'g',etime,N2M,'m.',etime,H2OM,'g.')
plt.setp(ax1.get_xticklabels(), visible=False)
plt.setp(ax2.get_xticklabels(), visible=False)
ani = anim.FuncAnimation(fig,animate,interval=1000)
plt.show()

-----------------------------------------------------
전체 추천리스트 보기
새로운 댓글이 없습니다.
새로운 댓글 확인하기
글쓰기
◀뒤로가기
PC버전
맨위로▲
공지 운영 자료창고 청소년보호