python采集rtu数据并写入csv

admin 7月前 333

try:
    from modbus_tk import modbus_rtu
    import serial
    import modbus_tk.defines as cst
except Exception as e:
    print(str(e))
import csv
import os
import time
#------------ 数据写入CSV文件
def save_to_csv(resultfile,header_list,result):#写入列表
    try:        
        if os.path.exists(resultfile):                
            pass
        else:
           with open(resultfile, "a",newline='',encoding='UTF-8',errors='ignore') as csvfile:
               writer = csv.writer(csvfile)
               writer.writerow(header_list)
        with open(resultfile, "a",newline='',encoding='UTF-8',errors='ignore') as csvfile:
           writer = csv.writer(csvfile)
           #result.append(get_timestr())
           writer.writerow(result)
           #print('    正在写入csv文件中.....')
    except Exception as e:            
        print("寫入問題"+str(e))
#--------------获取当前时间
def get_timestr():   
    current = time.time()
    current = time.localtime(current)
    timestr = "" + str(current.tm_year) + "/" + str(current.tm_mon) + "/" + str(current.tm_mday) + " " + str(current.tm_hour) + ":" + str(current.tm_min) + ":" + str(current.tm_sec)
    return timestr
def read_data():
    try:
        # 创建一个Modbus RTU主站
        # master = modbus_rtu.RtuMaster('/dev/ttyUSB0')
        master = modbus_rtu.RtuMaster(serial.Serial(port='COM10', baudrate=9600, bytesize=8, parity='N', stopbits=1, xonxoff=0))
        
        # 设置串口参数
        master.set_timeout(5.0)
        master.set_verbose(True)    
        # 连接到设备
        master.open()
    except Exception as e:
        print(47,str(e))
        pass
    r_list=[]
    try:
        result1 = master.execute(1, cst.READ_HOLDING_REGISTERS, 0, 2)
        result2 = master.execute(2, cst.READ_HOLDING_REGISTERS, 0, 2)
        result3 = master.execute(3, cst.READ_HOLDING_REGISTERS, 1, 1)
        result4 = master.execute(4, cst.READ_HOLDING_REGISTERS, 1, 1)
        result5 = master.execute(5, cst.READ_HOLDING_REGISTERS, 1, 1)
        result6 = master.execute(6, cst.READ_HOLDING_REGISTERS, 1, 1)
    
        var1 = result3[0] - 65535;
        if var1==-65535 :
            var1 = 0
        elif var1==-65534 :
            var1 = 0
        elif var1 ==-65533:
            var1 = 0
        elif var1==-65532 :
            var1 = 0
        elif var1 ==-65531:
            var1 = 0
        elif var1==-1 :
            var1 = 0
        elif var1 ==-2:
            var1 = 0
        elif var1==-3 :
            var1 = 0
        elif var1 ==-4:
            var1 = 0
        elif var1==-5 :
            var1 = 0
        elif var1 ==-6:
            var1 = 0
        var2 = result4[0] - 65535;
        if var2==-65535 :
            var2=0
        elif var2==-65534 :
            var2 = 0
        elif var2 ==-65533:
            var2 = 0
        elif var2==-65532 :
            var2 = 0
        elif var2 ==-65531:
            var2 = 0
        elif var2==-1 :
            var2 = 0
        elif var2 ==-2:
            var2 = 0
        elif var2==-3 :
            var2 = 0
        elif var2 ==-4:
            var2 = 0
        elif var2==-5 :
            var2 = 0
        elif var2 ==-6:
            var2 = 0
        var3 = result5[0] - 65535;
        if var3==-65535 :
            var3=0
        elif var3==-65534 :
            var3 = 0
        elif var3 ==-65533:
            var3 = 0
        elif var3==-65532 :
            var3 = 0
        elif var3 ==-65531:
            var3 = 0
        elif var3==-1 :
            var3 = 0
        elif var3 ==-2:
            var3 = 0
        elif var3==-3 :
            var3 = 0
        elif var3 ==-4:
            var3 = 0
        elif var3==-5 :
            var3 = 0
        elif var3 ==-6:
            var3 = 0
        var4 = result6[0] - 65535;
        if var4==-65535 :
            var4=0
        elif var4==-65534 :
            var4 = 0
        elif var4 ==-65533:
            var4 = 0
        elif var4==-65532 :
            var4 = 0
        elif var4 ==-65531:
            var4 = 0
        elif var4==-1 :
            var4 = 0
        elif var4 ==-2:
            var4 = 0
        elif var4==-3 :
            var4 = 0
        elif var4 ==-4:
            var4 = 0
        elif var4==-5 :
            var4 = 0
        elif var4 ==-6:
            var4 = 0
       # r_list=[result1[1],result2[1],var1,var2,var3,var4]
        #var1 = result3[0] - 65535;
        #var2 = result4[0] - 65535;
        #var3 = result5[0] - 65535;
        #var4 = result6[0] - 65535;
        r_list=[result1[1],result2[1],var1,var2,var3,var4]
        #print(result1[1])
        print(result3[0]-65535)
        #print(result2)
        
        count=r_list.count(0)
        if count>1:
            pass
        else:
            timestr=get_timestr()
            temp=timestr.split(" ")
            t1=temp[0].replace("/","-")
            t2=temp[1]
            print(t2)
            resultfile="D:/ding/"+t1+"曝光机温度.csv"
            header_list=['日期','時間','灯室温度','曝光室溫度','上底片真空值','下底片真空值','上框框架真空值','下框框架真空值']
            result=[t1,t2]
            result.extend(r_list)        
            save_to_csv(resultfile,header_list,result)
            print(result)
    except:
        pass
def main():
    while True:
         read_data()
         time.sleep(6) #读取间隔
         
if __name__ == '__main__':
    main()


欢迎联系本站长QQ:3216572
最新回复 (1)
  • admin 7月前
    2
    链接:https://pan.baidu.com/s/1fU_OR1feTRJ8nFbrE3UqsA 
    提取码:y3cy

    眼睛3d模型
返回