Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. CSV file logging data on timer event

CSV file logging data on timer event

Scheduled Pinned Locked Moved C#
csharpquestioncode-review
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    cages
    wrote on last edited by
    #1

    I am logging data from a USB device (pooling the device) using a timer component with C#. Every 250ms (timer tick) I read the data from the device and save it to a CSV (coma separated value) file. I just thought I should ask if there is anyway to improve on the saving of the file. I am new to C#/.net so would really like a review of the idea. 1. Every timer tick I read two values from an object (the USB device interface). 2. Open the file by creating a Steame writer object; StreamWrite out = new StreamWriter(filename, true). 3.Write the values to the file out.Writeline("{0},{1}",value1,value2); 4.Close the file. out.close(); The main question is should I keep opening and closing the file every tick or should I open it when the logging starts and close it when the user stops the logging of the data? The reason I close the file is so if the application fails I do not lose any data.( the file is being used only by this application). Any comments to improve the code will be useful. Thanks cages

    L P P 3 Replies Last reply
    0
    • C cages

      I am logging data from a USB device (pooling the device) using a timer component with C#. Every 250ms (timer tick) I read the data from the device and save it to a CSV (coma separated value) file. I just thought I should ask if there is anyway to improve on the saving of the file. I am new to C#/.net so would really like a review of the idea. 1. Every timer tick I read two values from an object (the USB device interface). 2. Open the file by creating a Steame writer object; StreamWrite out = new StreamWriter(filename, true). 3.Write the values to the file out.Writeline("{0},{1}",value1,value2); 4.Close the file. out.close(); The main question is should I keep opening and closing the file every tick or should I open it when the logging starts and close it when the user stops the logging of the data? The reason I close the file is so if the application fails I do not lose any data.( the file is being used only by this application). Any comments to improve the code will be useful. Thanks cages

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Can't compute. What kind of data is it? How does it evolve over time? Why are you doing this? :confused:

      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

      Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

      C 1 Reply Last reply
      0
      • L Luc Pattyn

        Can't compute. What kind of data is it? How does it evolve over time? Why are you doing this? :confused:

        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

        Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

        C Offline
        C Offline
        cages
        wrote on last edited by
        #3

        The data are double values that represent temperature(the usb device is a teperature logger). The data will be logged for about 3 hours.

        L 1 Reply Last reply
        0
        • C cages

          The data are double values that represent temperature(the usb device is a teperature logger). The data will be logged for about 3 hours.

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          OK. If getting maximum probability of getting all the data is paramount (say your USB is used while debugging a system that eventually crashes), then yes keep the file closed as much as possible. I do this while logging any application during its development, and that takes hundreds of text lines per second! (Of course your USB access might slow down things a bit, depending on the device). The easiest and cheapest way probably is by calculating a single string (including the NewLine terminators) first (string.Format() may be useful here), then calling File.AppendAllText(). :)

          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

          Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

          1 Reply Last reply
          0
          • C cages

            I am logging data from a USB device (pooling the device) using a timer component with C#. Every 250ms (timer tick) I read the data from the device and save it to a CSV (coma separated value) file. I just thought I should ask if there is anyway to improve on the saving of the file. I am new to C#/.net so would really like a review of the idea. 1. Every timer tick I read two values from an object (the USB device interface). 2. Open the file by creating a Steame writer object; StreamWrite out = new StreamWriter(filename, true). 3.Write the values to the file out.Writeline("{0},{1}",value1,value2); 4.Close the file. out.close(); The main question is should I keep opening and closing the file every tick or should I open it when the logging starts and close it when the user stops the logging of the data? The reason I close the file is so if the application fails I do not lose any data.( the file is being used only by this application). Any comments to improve the code will be useful. Thanks cages

            P Offline
            P Offline
            PIEBALDconsult
            wrote on last edited by
            #5

            Ummm... how much can the temperature change during so short a time period? :confused: First; yes, open and close each time you write to the log. But, you shouldn't keep writing needless repetitive data. My first professional programming job involved polling some equipment for temperature and such and then passing it to a specialized database system. The database didn't simply store everything my software sent; it tested the incoming data and made a determination as to whether or not it was significant. Each datum could be configured as to what was significant -- for instance if a temperature changed by a one-hundredth of a degree; or if a value hadn't been stored for some period of time (perhaps ten minutes). This greatly reduced the amount of data stored and sped up reporting without losing significant detail. I strongly suggest you implement a similar technique.

            1 Reply Last reply
            0
            • C cages

              I am logging data from a USB device (pooling the device) using a timer component with C#. Every 250ms (timer tick) I read the data from the device and save it to a CSV (coma separated value) file. I just thought I should ask if there is anyway to improve on the saving of the file. I am new to C#/.net so would really like a review of the idea. 1. Every timer tick I read two values from an object (the USB device interface). 2. Open the file by creating a Steame writer object; StreamWrite out = new StreamWriter(filename, true). 3.Write the values to the file out.Writeline("{0},{1}",value1,value2); 4.Close the file. out.close(); The main question is should I keep opening and closing the file every tick or should I open it when the logging starts and close it when the user stops the logging of the data? The reason I close the file is so if the application fails I do not lose any data.( the file is being used only by this application). Any comments to improve the code will be useful. Thanks cages

              P Offline
              P Offline
              Pete OHanlon
              wrote on last edited by
              #6

              Option 3. Use a logging framework such as log4net where somebody else has taken care of the development and testing of this for you.

              I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be

              Forgive your enemies - it messes with their heads

              My blog | My articles | MoXAML PowerToys | Onyx

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups