在 python 中从 cr3000 数据记录器串行读取数据

问题描述 投票:0回答:0

我正在尝试从 cr3000 数据记录器串行读取数据,并在我的笔记本电脑上将其转换为 python 中的 csv。但是当我试图打印解码后的字节时,一个不可见的光标在终端中移动。已生成 csv 文件,但未将数据写入其中。任何建议将不胜感激

import serial
import csv
import datetime
import time


ser = serial.Serial(port='COM4',baudrate=9600,timeout=1,bytesize=8,stopbits=serial.STOPBITS_ONE) 
time.sleep(2)
ser.flushInput()
ser.write(b"Output(ScanList(), 'data.csv', , , , , , , , Append)") 




filename = 'data.csv'
fields = ['Time', 'Thermocouple']
with open(filename, 'w', newline='') as csvfile:
    writer = csv.writer(csvfile)
    writer.writerow(fields)

while True:
        ser_bytes = ser.readline()
        decoded_bytes = ser_bytes[0:len(ser_bytes)-2].decode("utf-8")
        
        print(decoded_bytes)
   
   
        with open('data.csv','a') as csvfile:
            csvwriter = csv.writer(csvfile) 
            rows = [[time.strftime("%Y/%m/%d %H:%M:%S")],[ decoded_bytes]]
            csvwriter.writerow(rows)
        
        
    
    


    
python-3.x serial-port
© www.soinside.com 2019 - 2024. All rights reserved.