Jump to content

Recommended Posts

Posted
Hallo,
Ich versuche, die Sensordaten alle 10 Sekunden abzurufen und in einer CSV-Datei zu speichern. 
Ich brauche kontinuierliche Daten. Aber ich erhalte einen Fehler. Den Code habe ich angehängt

 

 

HOST = "192.168.0.104"
PORT = 4223
UID_tem = "Nwv" # Change XYZ to the UID of your Temperature Bricklet 2.0

import csv

from tinkerforge.ip_connection import IPConnection

def cb_temperature(temperature):
    print("Temperature: " + str(temperature/100.0) + " °C")
    output1.writerow([str(temperature/100.0)])

if __name__ == "__main__":
    ipcon = IPConnection() # Create IP connection
    t = BrickletTemperatureV2(UID_tem, ipcon) # Create device object
    
    ipcon.connect(HOST, PORT) # Connect to brickd
    
    t.set_temperature_callback_configuration(1000, False, "x", 0, 0)
    
    csvfile1= open ('/home/pi/temp1_data.csv','wb')
    output1 = csv.writer(csvfile1,delimiter=';', quotechar='|')
    output1.writerow(['temperature'])
    t.register_callback(t.CALLBACK_TEMPERATURE, cb_temperature)
    csvfile1.close()

    
    input("Press key to exit\n") # Use raw_input() in Python 2
    ipcon.disconnect()
Ich erhalte den folgenden Fehler:

Exception in thread Callback-Processor:
Traceback (most recent call last):
  File "/usr/lib/python3.9/threading.py", line 954, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.9/threading.py", line 892, in run
    self._target(*self._args, **self._kwargs)
  File "/home/pi/.local/lib/python3.9/site-packages/tinkerforge/ip_connection.py", line 1239, in callback_loop
    self.dispatch_packet(data)
  File "/home/pi/.local/lib/python3.9/site-packages/tinkerforge/ip_connection.py", line 1219, in dispatch_packet
    cb(unpack_payload(payload, form))
  File "/home/pi/Desktop/testtempcsv.py", line 23, in cb_temperature
    output1.writerow([str(temperature/100.0)])
ValueError: I/O operation on closed file.

 

Kann mir jemand freundlicherweise sagen, wie ich das beheben kann? Danke :)

Posted

Moin,

ohne das jetzt ausprobiert zu haben, würde ich sagen, dass die beiden Zeilen "csvfile1.close()" und "input("Press key to exit\n") # Use raw_input() in Python 2" getauscht werden müssen. Wenn der Callback aufgerufen wird, ist die Datei ansonsten nämlich schon wieder geschlossen...

Gruß Thomas

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...