CSV file logging data on timer event
-
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
-
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
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.
-
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.
-
The data are double values that represent temperature(the usb device is a teperature logger). The data will be logged for about 3 hours.
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 callingFile.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.
-
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
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.
-
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
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