View Full Version : Append
oldpapa49
13-11-2009, 20:55
Hey all, long time..
Working on files,
for append, does the file need to exist first?
I have not tried yet.. but will..
And can I put filename.txt?
I'm trying to do record keeping for how long a computer runs on batteries. I want to append, but if need be, I will open and write current time and close.
Does this work like apple basic in other words..
What you open needs to be closed.. etc
Old
Michael Clease
13-11-2009, 21:09
This should do it and no you dont have to close the file. You can use any filename.****** you want.
USES "FILE"
DIM FileHandle as DWORD
DIM Status AS DWORD
DIM TestString AS STRING
DIM FileName as STRING
DIM sMsg as STRING
TestString = "1234567890ABCDEF" ' String to write to file
FileName = APP_SCRIPTPATH + "test.txt" ' Build filename
Status = FILE_Append( FileName, TestString )
TestString = "More Data"
Status = FILE_Append( FileName, TestString )
sMsg = FILE_Load(Filename)
MsgBox 0, sMsg
oldpapa49
13-11-2009, 21:16
TY, so fast as always..
I will write to program and test after lunch
Old
oldpapa49
13-11-2009, 23:04
Ok, here it is.. works great. the text.txt lives where the program runs from.
' Empty GUI script created on 11-13-2009 10:16:56 by (ThinAIR)
' Routine to write start time of program and
' writes every 15 min
' W Little
' TY to Michael Clease for file operation
uses "UI", "CONSOLE"
USES "FILE"
Dim Minutes As long
Dim Seconds As Long
Dim sMsg As String
Dim sMsgset As String
DIM errornumber as long
DIM FileHandle as DWORD
DIM Status AS DWORD
DIM TestString AS STRING
DIM FileName as STRING
'-----------------------------------------------------
' Start
TestString = Time$ ' String to write to file
FileName = APP_SCRIPTPATH + "test.txt" ' Build filename
Status = FILE_Append( FileName, "Time Started at -- "+ TestString )
DO
errorNumber = err
'console_writeline err
MINutes = mid$(Time$, 4, 2) ' Extract the Day from the returned string
Seconds = mid$(Time$, 7, 4)
'console_writeline MINutes
sMsg = Minutes + Seconds
IF Minutes = 00 and Seconds = 00 THEN
Call cbPrintScreen
end if
IF Minutes = 15 and Seconds = 00 then
Call cbPrintScreen
End IF
If Minutes = 30 and seconds = 00 then
Call cbPrintScreen
End IF
IF Minutes = 45 and seconds = 00 then
Call cbPrintScreen
end if
CALLBACK FUNCTION cbPrintScreen() AS LONG
if sMsg <> sMsgset then
console_writeline time$
TestString = Time$ ' String to write to file
FileName = APP_SCRIPTPATH + "test.txt" ' Build filename
Status = FILE_Append( FileName, TestString )
' sMsg = FILE_Load(Filename)
sMsgset = sMsg
end if
end function
Loop
Old
Petr Schreiber
13-11-2009, 23:25
Hi,
interesting code, by using DO/LOOP you make sure the CPU will be used at nearly 99%, so PC seems "under use". Thanks for sharing!
Few details you might consider for next versions or program (not necessary, they just make code shorter):
- CALLBACK FUNCTION is thing used mainly in User Interface programming (they prefill CB* variables), you can leave it as FUNCTION
- when FUNCTION does not return value, you can use SUB (like SUBroutine) instead
- no need to use CALL keyword for common function calls
- MOD function allows you to get reminder of division -> minutes/15 return 0 only for 0, 15, 30 and 45 minutes
So the code can look like this too:
' Empty GUI script created on 11-13-2009 10:16:56 by (ThinAIR)
' Routine to write start time of program and
' writes every 15 min
' W Little
' TY to Michael Clease for file operation
Uses "UI", "CONSOLE"
Uses "FILE"
Dim Minutes As Long
Dim Seconds As Long
Dim sMsg As String
Dim sMsgset As String
Dim errornumber As Long
Dim FileHandle As DWord
Dim Status As DWord
Dim TestString As String
Dim FileName As String
'-----------------------------------------------------
' Start
TestString = Time$ ' String to write to file
FileName = APP_ScriptPath + "test.txt" ' Build filename
Status = FILE_Append( FileName, "Time Started at -- "+ TestString )
Do
errorNumber = Err
'console_writeline err
Minutes = Mid$(Time$, 4, 2) ' Extract the Day from the returned string
Seconds = Mid$(Time$, 7, 4)
'console_writeline MINutes
sMsg = Minutes + Seconds
' -- MOD gives remainder of division minutes / 15
If Mod(minutes, 15) = 0 And Seconds = 0 Then
cbPrintScreen()
End If
Loop
Sub cbPrintScreen()
If sMsg <> sMsgset Then
Console_WriteLine Time$
TestString = Time$ ' String to write to file
FileName = APP_ScriptPath + "test.txt" ' Build filename
Status = FILE_Append( FileName, TestString )
' sMsg = FILE_Load(Filename)
sMsgset = sMsg
End If
End Sub
oldpapa49
13-11-2009, 23:48
Hey Petr, long time
You have me a bit confused at CPU at 99% and Under Used? I checked, with the programs running (our Dicom Scanner and this program) its 54%
I tried the callback function but was using for/next and it would not continue. So I went to search and found DO/LOOP.
Then I just worked from there..
I will use yours next week when I recharge the battery for the next run.
The program is being used on an ASUS B202 that we will be selling for what is called TMIFLASH for Sending WiFI and use of a flash drive to store medical images. But until we get an OK to use the AC from the medical workstation, we need to run off of batteries. Currently the last about 15 hrs or so. I just need proof. And I will not work for 15 hrs. :)
I have looked over your program, and learn from it. shorted better in some cases.
OLD
Petr Schreiber
14-11-2009, 10:48
Hi,
54% ... that I would expect on dual core processors. I think your Atom is not dual core, but it has hyper threading, which might cause this.
In case you would like low CPU usage, you might consider using DoEvents right after Do in the loop, on my PC it cut the usage to almost 0%:
' Empty GUI script created on 11-13-2009 10:16:56 by (ThinAIR)
' Routine to write start time of program and
' writes every 15 min
' W Little
' TY to Michael Clease for file operation
Uses "UI", "CONSOLE"
Uses "FILE"
Dim Minutes As Long
Dim Seconds As Long
Dim sMsg As String
Dim sMsgset As String
Dim errornumber As Long
Dim FileHandle As DWord
Dim Status As DWord
Dim TestString As String
Dim FileName As String
'-----------------------------------------------------
' Start
TestString = Time$ ' String to write to file
FileName = APP_ScriptPath + "test.txt" ' Build filename
Status = FILE_Append( FileName, "Time Started at -- "+ TestString )
Do
DoEvents ' < ---
errorNumber = Err
'console_writeline err
Minutes = Mid$(Time$, 4, 2) ' Extract the Day from the returned string
Seconds = Mid$(Time$, 7, 4)
'console_writeline MINutes
sMsg = Minutes + Seconds
' -- MOD gives remainder of division minutes / 15
If Mod(minutes, 15) = 0 And Seconds = 0 Then
cbPrintScreen()
End If
Loop
Sub cbPrintScreen()
If sMsg <> sMsgset Then
Console_WriteLine Time$
TestString = Time$ ' String to write to file
FileName = APP_ScriptPath + "test.txt" ' Build filename
Status = FILE_Append( FileName, TestString )
' sMsg = FILE_Load(Filename)
sMsgset = sMsg
End If
End Sub
If you want to try the maximum time with your EEE, lower the CPU usage is, longer it will stay awake.
Another cleaner solution would be to create dialog with timer - it executes code when timer event is generated:
Uses "UI", "File"
' -- ID numbers of controls
Begin Const
%bClose = 1000
%eLog
%tTimer
End Const
Global sMsg As String
Global sMsgset As String
Global errornumber As Long
Global FileHandle As DWord
Global TestString As String = Time$
Global FileName As String = APP_ScriptPath + "test.txt"
Global Status As DWord = FILE_Append( FileName, "Time Started at -- "+ TestString )
Global hDlg As DWord
' -- Create dialog here
Function TBMAIN()
Dialog NEW 0, "Notebook Tester",-1,-1, 160, 120, _
%WS_POPUP Or %WS_VISIBLE Or %WS_CAPTION Or %WS_SYSMENU Or %WS_MINIMIZEBOX To hDlg
' -- Place controls here
Control ADD TEXTBOX, hDlg, %eLog, "Time Started at -- "+ TestString+$CRLF, 5, 5, 150, 90, %ES_MULTILINE
Control ADD BUTTON, hDlg, %bClose, "Click to close", 95, 100, 60, 14, Call btnCloseProc
Dialog SHOW MODAL hDlg, Call dlgProc
End Function
' -- Callback for dialog
CallBack Function dlgProc()
Local Minutes, Seconds As Long
' -- Test for messages
Select Case CBMSG
Case %WM_INITDIALOG
' -- Put code to be executed after dialog creation here
' 30s timer
Dialog SET Timer CBHNDL, %tTimer, 1000*30, 0
' -- Timer event is launched on timer tick
Case %WM_TIMER
errorNumber = Err
Minutes = Mid$(Time$, 4, 2) ' Extract the Day from the returned string
Seconds = Mid$(Time$, 7, 4)
sMsg = Minutes + Seconds
' -- MOD gives remainder of division minutes / 15
If Mod(minutes, 15) = 0 And Seconds = 0 Then
cbPrintScreen()
End If
Case %WM_CLOSE
' -- Put code to be executed before dialog end here
FILE_Append( FileName, "Program ended at "+Time$ )
End Select
End Function
' -- Callback for close button
CallBack Function btnCloseProc()
If CBMSG = %WM_COMMAND Then
If CBCTLMSG = %BN_CLICKED Then
' -- Closes the dialog
Dialog End CBHNDL
End If
End If
End Function
Sub cbPrintScreen()
If sMsg <> sMsgset Then
Control APPEND TEXT hDlg, %eLog, Time$+$CRLF
TestString = Time$ ' String to write to file
FileName = APP_ScriptPath + "test.txt" ' Build filename
Status = FILE_Append( FileName, TestString )
sMsgset = sMsg
End If
End Sub
oldpapa49
17-11-2009, 17:31
Hey Petr,
Ok, I used one of your samples and dropped my cpu from 54% to 3-4%..
So the batteries lasted ~9hrs using my program, will see what happens by tomorrow on yours.. I can then make and avg.
Thanks again.
Now to see what you did..
oldpapa49
20-11-2009, 20:18
Petr,
Thought I let you know on the tests..
54% battery died after 9 hrs 12v 10A /Hr battery pack
3-4% Avg was 10 hrs and 20 mins
The ASUS was running Windows XP stripped, with our software and the test sw.
Quite good I say.
Does DoEvents mean any event like IF's and alike?
Old
Petr Schreiber
20-11-2009, 21:32
Hi,
I am happy it worked, so your Atom PC can really live for almost 11 hours, impressive.
DoEvents keyword "yields execution so that the operating system can process other events." That means all the CPU time is not consumed by parsing thinBASIC script and processing windows messages, but application behaves friendlier to other programs by letting the CPU breath a bit :)
Michael Hartlef
21-11-2009, 10:29
Also if your application can rest for a few milliseconds then a
SLEEP 10
will also help here.