This code accepts, on the console serial port input, a line of text that comes from a Windows "date" command line as shown below. This ZBasic code then parses the date time from this and sets the ZBasic Virtual Machine (VM) date and time.
The Windows command line begins with "d-8". This ("d") can be used by the application to detect that the date command follows. The -8 is parsed as the GMT offset in hours, for applications that automatically adjust daylight savings time or calculate GMT and local time.
The parsing code may need tweaking if you are in a country where your date/time formats differ.
Can't redirect the echo command's output directly to a com port on the PC because the port opens and the data flows before the ZBasic VM has finished resetting due to the change in DTR on the port. You can cut/paste the echo command's output into the debug window of the IDE. Or write a PC program that opens the com port and sends this data as from the echo command.
Code: Select all
''''''''''''''''''''''''''''''''''''''''''''''''''''''
' echo d-8 %date% %time% Windows command in batch file yields...
' column numbers. second set is relative to i
' 0000000001111111112222
' 1234 0123456789012345678901
' d-8 Fri 10/06/2006 20:46:15.32 result of echo commmand, above. hr has leading space if < 10. 24 hr clock
function SetDateTime(s as string) as boolean
dim i as byte
dim mm as byte, d as byte, y as integer
dim h as byte, m as byte, secs as single
GMToffset = atoi(mid(s,2,3))
i=5 ' skip over GMT offset
do while not isnumeric(mid(s,i,1))
i=i+1
loop
mm = cbyte(atoi(mid(s,i+0,2)))
d = cbyte(atoi(mid(s,i+3,2)))
y = atoi(mid(s,i+6,4))
h = cbyte(atoi(mid(s,i+11,2)))
m = cbyte(atoi(mid(s,i+14,2)))
call valueS(mid(s,i+17, 5), secs, SetDateTime)
''' set date time in the VM
call PutTimeStamp(y, mm, d, h, m, secs)
'''debug.print cstr(GMToffset);" ";cstr(mm);" ";cstr(d);" ";cstr(y);" ";cstr(h);" ";cstr(m);" ";cstr(secs)
end function
'''''''''''''''''''''''''''''''''''''''''''''''''''''
function isnumeric(byval s1 as string) as BOOLEAN
dim b as byte
b = asc(mid(s1,1,1))
if b >= asc("0") and b <= asc("9") then
isnumeric = TRUE
else
isnumeric = FALSE
end if
end function
'''''''''''''''''''''''''''''''''''''''''''''''''''''
function atoi(byval s1 as string) as integer
dim ok as BOOLEAN
dim v as single
'debug.print "atoi ";s1
call valueS(s1, v, ok)
if (ok) then
atoi = cint(fix(v))
else
atoi = 0
end if
end function