Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.
|
Forum Index : Microcontroller and PC projects : Check if file is already open?
Author | Message | ||||
bobo2 Newbie Joined: 26/10/2012 Location: SwedenPosts: 36 |
Hi, What is the optimal way to check if file is already open? I am logging data on the SD card, and have an open statement in the beginning of the logging function. I need to check if the file is already open, otherwise I am getting an error when trying to open it again. What is the best way to check if file is already open? Edit: I am using duinomite with MMBasic 4.5 |
||||
Geoffg Guru Joined: 06/06/2011 Location: AustraliaPosts: 3167 |
Use OPTION ERROR CONTINUE then (after you tried opening the file) check MM.ERRNO. Geoff Geoff Graham - http://geoffg.net |
||||
bobo2 Newbie Joined: 26/10/2012 Location: SwedenPosts: 36 |
Thanks Geoff, I tried that but the program still stops at the open statement. I get the following error: [283] Open logName$ For append As #6 Error: File number is already open I had to make this work and work around it with a variable for the file name and one for holding status if the file is open. Here is the relevant code (it is not pretty, but it seems to work)... Sub LogValues Local j local logNewName$ local ds$ ds$ = date$ logNewName$ = right$(ds$,4)+mid$(ds$,4,2)+left$(ds$,2)+".log" If (logNewName$ <> logFileName$) or isLogFileOpen = 0 then ' handle loggs that cross a day boundary OPTION ERROR CONTINUE if isLogFileOpen = 1 Then close #6 isLogFileOpen = 0 endif logFileName$ = logNewName$ open logFileName$ for append as #6 if mm.errno = 0 then isLogFileOpen = 1 Print #6,"OPENED_";DATE$;" ";TIME$ Else isLogFileOpen = 0 endif endif print #6,date$","time$; Print #6,","Format$(packV,"%4.1f"); Print #6,","Format$(modVHigh,"%3.1f"); Print #6,","Format$(modVAvg, "%3.1f"); Print #6,","Format$(modVLow, "%3.1f"); For j = 1 To 24 Print #6,","Format$(modData(j), "%3.1f"); Next j For j = 1 To 4 Print #6,","Format$(tempData(j), "%2.0f"); Next j print #6,","format$(PIN(21), "%4.3f"); ' new line print #6,"" End Sub I am using an RTC module so the DATE$ and TIME$ are correct and useful for name of the log file. Any advice to make the OPTION ERROR CONTINUE work would be highly appreciated! |
||||
MicroBlocks Guru Joined: 12/05/2012 Location: ThailandPosts: 2209 |
Did you check the performance when you open and close the file each time you need to log something. If it is fast enough that would be the easiest way. [code] Sub LogValues ds$ = date$ LogFilename$ = right$(ds$,4)+mid$(ds$,4,2)+left$(ds$,2)+".log" open logFileName$ for append as #6 print #6,date$","time$; 'rest of the data to be logged close #6 End Sub [/code] Microblocks. Build with logic. |
||||
Grogster Admin Group Joined: 31/12/2012 Location: New ZealandPosts: 9082 |
Yes, I had to use a flag for when the file handle is open, because I was getting exactly the same 'File is already open' error as you. Set the flag when the file is open, clear it when the handle is closed. Then have a do/loop that will WAIT till the flag is clear BEFORE you try to open the file again. This was suggested by other members here at the time, and it works a treat. Smoke makes things work. When the smoke gets out, it stops! |
||||
bobo2 Newbie Joined: 26/10/2012 Location: SwedenPosts: 36 |
I am logging every 1 second and after reading here and on other forums I am trying to avoid writing to the allocation table every second. From what I have read this is happening every time the file is closed. This is on an electric vehicle, and a charge can take from 2 to 8 hours. |
||||
bobo2 Newbie Joined: 26/10/2012 Location: SwedenPosts: 36 |
Thanks for confirming this - it tried everything and thought that I was loosing it... but then snapped out of it and wrote the code above. It is not very elegant but seems to work. |
||||
Print this page |