datalogger programming
emmainidaho
Posts: 3
·
I have been attempting to use the datalogger to hold a database to be created/used by a Stamp sx.· The attached program is an adaptation of the datalogger example from·the·Parallax·site.· It is supposed to allow a user to input two variables and to read them back, or to just read them back.· When I run this it will allow the user to do the first function a couple of times, but then it unexpectedly runs through the synchronization routine again.· If you look at the code, there does not appear to be an instruction that would cause the synchronization to rerun after the program’s first scan.
·Also, I want to reuse and build a single database, not build a new one for each run, if I comment out the portion of the program that deletes the old database, or the portion that creates a new database file; the program does not execute properly.· Other than opening the existing file for writing, do I need a different set of instructions for initially opening an existing file?
·' =========================================================================
'·· File...... DataloggerDemoV1.0.bs2
'·· {$STAMP BS2sx}
'·· {$PBASIC 2.5}
' =========================================================================
'
[noparse][[/noparse] I/O Definitions ]
TX············· PIN···· 8·············· ' Transmit Data·· --> 27937.4 (RXD)
RTS············ PIN···· 9·············· ' Request To Send --> 27937.6 (CTS)
RX············· PIN···· 10············· ' Receive Data··· <-- 27937.5 (TXD)
CTS············ PIN···· 11············· ' Clear To Send·· <-- 27937.2 (RTS)
'
[noparse][[/noparse] Constants ]
Baud··········· CON···· 240 '16624 '84············· ' Serial Baud Rate 9600 bps (BS2)
NumSamples····· CON···· 2············· ' Number Of Samples To Log
'
[noparse][[/noparse] Variables ]
sched0 VAR B0 'Item number, Seconds ** Will replace "result" variable
Minutes VAR B1
Hours VAR B2
sched1 VAR B3 'Active days, Date
day VAR B4
sched2 VAR B5 'Sched Hour, Month
sched3 VAR B6 'Sched Minute, Year
sched4 VAR B7 'Sched Variable, VAO
sched5 VAR B8 'Sched New value, VDO
sched6 VAR B9 'Pending (1 or 0), SEQ
x VAR B10 'Unused for accounting
y VAR B11 'Unused for accounting
index VAR B12 'Used
ioByte VAR B13 'Used
res VAR B14 'Used
buffer VAR B15 'Used !!Was a 15 byte array
flag VAR B16 'Used
RTCCmd VAR B17 'Unused
counter········ VAR···· W11··········· ' Counter Variable
work··········· VAR···· W12··········· ' Work Variable
'
[noparse][[/noparse] Initialization ]
DEBUG "Memory Stick Datalogger Demo V1.0", CR, CR, "Initializing..."
PAUSE 200······························ ' Allow Time To Settle
HIGH TX································ ' Initialize Transmit Line
LOW RTS································ ' Take Vinculum Out Of Reset
PAUSE 600······························ ' Allow Time To Settle
DEBUG "Done!", CR, "Synchronizing..."
DO
· SEROUT TX\CTS, Baud, [noparse][[/noparse]"E", CR]······· ' Sync Command Character
· GOSUB Get_Data······················· ' Get Response
· PAUSE 250
LOOP UNTIL ioByte = $0D················ ' Wait For Carriage Return
DO
· SEROUT TX\CTS, Baud, [noparse][[/noparse]"e", CR]······· ' Sync Command Character
· GOSUB Get_Data······················· ' Get Response
· PAUSE 250
LOOP UNTIL ioByte = $0D················ ' Wait For Carriage Return
'
[noparse][[/noparse] Program Code ]
Main:
· DEBUG "Done", CR, "Switching to Short Command Mode..."
· SEROUT TX\CTS, Baud, [noparse][[/noparse]"SCS", CR]····· ' Switch To Short Command Mode
· GOSUB Get_Data······················· ' Purge Receive Buffer
· DEBUG "Done!", CR, "Waiting for Memory Stick..."
Check_Drive:
· DO
··· SEROUT TX\CTS, Baud, [noparse][[/noparse]CR]·········· ' Prompt Device For Status
··· GOSUB Get_Data····················· ' Purge Receive Buffer
··· IF buffer(0) = ">" THEN············ ' Check For Ready Prompt
····· EXIT····························· ' If Ready Then Exit Loop
··· ELSEIF buffer(0) = "N" AND buffer(1) = "D" THEN
····· DEBUG "."························ ' Device Ready But No Memory Stick
··· ELSEIF buffer(0) = "D" AND buffer(1) = "D" AND flag = 0 THEN
····· DEBUG "Connected!", CR, "Accessing..."
····· flag = 1························· ' Memory Stick Ready
··· ELSE
····· DEBUG "."
··· ENDIF
··· PAUSE 250·························· ' Command Retry Delay
· LOOP
· DEBUG "Ready!", CR
·'**** Change for application
· DEBUG "Deleting Data File...",CR········· ' First Delete File
· SEROUT TX\CTS, Baud, [noparse][[/noparse]$07, $20, "datafile.txt", CR]
· GOSUB Get_Data······················· ' Purge Receive Buffer
· DEBUG "Creating Data File..."···································· ' Then Create File
· SEROUT TX\CTS, Baud, [noparse][[/noparse]$09, $20, "datafile.txt", CR]
· GOSUB Get_Data
·· DEBUG CR,"To Display data type 1",CR
·· DEBUG "To Input Data type 2",CR
·· SERIN 16,16624,[noparse][[/noparse]DEC RTCCmd]
·· IF RTCCmd = 1 THEN GOTO readdata
· 'Counter can be replaced with schedule item number
· DEBUG CR,"Input variable 0",CR
· SERIN 16,16624,[noparse][[/noparse]DEC sched0]
· DEBUG "Input variable 1",CR
· SERIN 16,16624,[noparse][[/noparse]DEC sched1]
· DEBUG "Sched #",DEC counter, " Result 0 = ",DEC sched0,CR
· DEBUG "Open!", CR, CR, "Writing Data...", CR
· FOR counter = 1 TO NumSamples········ ' Number Of Samples To Log
· IF counter = 1 THEN res = sched0
· IF counter = 2 THEN res = sched1
··· DEBUG "Sample ", DEC5 counter, " of ", DEC5 NumSamples,
········· " Sched 0 --> ", DEC5 sched0," Sched 1 --> ", DEC5 sched1,CR····· ' Display Results
··· SEROUT TX\CTS, Baud, [noparse][[/noparse]$08, $20, $00, $00, $00, $0D, CR,
········· DEC5 counter, ",", DEC5 res, CR, LF, CR]
··· PAUSE 500·························· ' Write Results/Delay
··· GOSUB Get_Data····················· ' Purge Receive Buffer
· NEXT
· res = 0
· sched0 = 0
· DEBUG "Cleared... Result 0 = ",DEC sched0
·sched1 = 0
·readdata:
· DEBUG "Closing Data File..."········· ' Close File (MUST CLOSE!)
· SEROUT TX\CTS, Baud, [noparse][[/noparse]$0A, $20, "datafile.txt", CR]
· GOSUB Get_Data······················· ' Purge Receive Buffer
· DEBUG "Done!", CR, CR, "Opening Data File..." ' Open Fil For Read Mode
· SEROUT TX\CTS, Baud, [noparse][[/noparse]$0E, $20, "datafile.txt", CR]
· GOSUB Get_Data······················· ' Purge Receive Buffer
· DEBUG "Open!", CR, "Reading Data...", CR
· sched0 = 0
· sched1 = 0
· DEBUG "Zero sched variables",CR
· FOR counter = 1 TO NumSamples········ ' Number Of Samples To Read
··· SEROUT TX\CTS, Baud, [noparse][[/noparse]$0B, $20, $00, $00, $00, $0D, CR]
··· SERIN RX\RTS, Baud, [noparse][[/noparse]DEC5 work, ioByte, DEC5 res, ioByte, ioByte]
··· GOSUB Get_Data····················· ' Purge Receive Buffer
··· IF counter = 1 THEN sched0 = res
··· IF counter = 2 THEN sched1 = res
··· IF counter = 1 THEN DEBUG "Result 0 = > ", DEC5 sched0, CR····· ' Display Results
··· IF counter = 2 THEN DEBUG "Result 1 = > ", DEC5 sched1,10,13
··· PAUSE 500·························· ' Delay
· NEXT
· PAUSE 5000
· DEBUG "Closing Data File...Program Complete!"
· SEROUT TX\CTS, Baud, [noparse][[/noparse]$0A, $20, "datafile.txt", CR]
· GOSUB Get_Data······················· ' Purge Receive Buffer
· GOTO main
· 'STOP
'
[noparse][[/noparse] Subroutines ]
Get_Data:
· index = 0···························· ' Reset Index Pointer
· DO··································· ' Receive Data
··· SERIN RX\RTS, Baud, 100, Timeout, [noparse][[/noparse]ioByte]
··· buffer(index) = ioByte············· ' Add Received Byte To Buffer
··· index = index + 1·················· ' Increment Index Pointer
··· IF index > 14 THEN Timeout········· ' Check For Overflow
· LOOP
Timeout:
RETURN
I have been attempting to use the datalogger to hold a database to be created/used by a Stamp sx.· The attached program is an adaptation of the datalogger example from·the·Parallax·site.· It is supposed to allow a user to input two variables and to read them back, or to just read them back.· When I run this it will allow the user to do the first function a couple of times, but then it unexpectedly runs through the synchronization routine again.· If you look at the code, there does not appear to be an instruction that would cause the synchronization to rerun after the program’s first scan.
·Also, I want to reuse and build a single database, not build a new one for each run, if I comment out the portion of the program that deletes the old database, or the portion that creates a new database file; the program does not execute properly.· Other than opening the existing file for writing, do I need a different set of instructions for initially opening an existing file?
·' =========================================================================
'·· File...... DataloggerDemoV1.0.bs2
'·· {$STAMP BS2sx}
'·· {$PBASIC 2.5}
' =========================================================================
'
[noparse][[/noparse] I/O Definitions ]
TX············· PIN···· 8·············· ' Transmit Data·· --> 27937.4 (RXD)
RTS············ PIN···· 9·············· ' Request To Send --> 27937.6 (CTS)
RX············· PIN···· 10············· ' Receive Data··· <-- 27937.5 (TXD)
CTS············ PIN···· 11············· ' Clear To Send·· <-- 27937.2 (RTS)
'
[noparse][[/noparse] Constants ]
Baud··········· CON···· 240 '16624 '84············· ' Serial Baud Rate 9600 bps (BS2)
NumSamples····· CON···· 2············· ' Number Of Samples To Log
'
[noparse][[/noparse] Variables ]
sched0 VAR B0 'Item number, Seconds ** Will replace "result" variable
Minutes VAR B1
Hours VAR B2
sched1 VAR B3 'Active days, Date
day VAR B4
sched2 VAR B5 'Sched Hour, Month
sched3 VAR B6 'Sched Minute, Year
sched4 VAR B7 'Sched Variable, VAO
sched5 VAR B8 'Sched New value, VDO
sched6 VAR B9 'Pending (1 or 0), SEQ
x VAR B10 'Unused for accounting
y VAR B11 'Unused for accounting
index VAR B12 'Used
ioByte VAR B13 'Used
res VAR B14 'Used
buffer VAR B15 'Used !!Was a 15 byte array
flag VAR B16 'Used
RTCCmd VAR B17 'Unused
counter········ VAR···· W11··········· ' Counter Variable
work··········· VAR···· W12··········· ' Work Variable
'
[noparse][[/noparse] Initialization ]
DEBUG "Memory Stick Datalogger Demo V1.0", CR, CR, "Initializing..."
PAUSE 200······························ ' Allow Time To Settle
HIGH TX································ ' Initialize Transmit Line
LOW RTS································ ' Take Vinculum Out Of Reset
PAUSE 600······························ ' Allow Time To Settle
DEBUG "Done!", CR, "Synchronizing..."
DO
· SEROUT TX\CTS, Baud, [noparse][[/noparse]"E", CR]······· ' Sync Command Character
· GOSUB Get_Data······················· ' Get Response
· PAUSE 250
LOOP UNTIL ioByte = $0D················ ' Wait For Carriage Return
DO
· SEROUT TX\CTS, Baud, [noparse][[/noparse]"e", CR]······· ' Sync Command Character
· GOSUB Get_Data······················· ' Get Response
· PAUSE 250
LOOP UNTIL ioByte = $0D················ ' Wait For Carriage Return
'
[noparse][[/noparse] Program Code ]
Main:
· DEBUG "Done", CR, "Switching to Short Command Mode..."
· SEROUT TX\CTS, Baud, [noparse][[/noparse]"SCS", CR]····· ' Switch To Short Command Mode
· GOSUB Get_Data······················· ' Purge Receive Buffer
· DEBUG "Done!", CR, "Waiting for Memory Stick..."
Check_Drive:
· DO
··· SEROUT TX\CTS, Baud, [noparse][[/noparse]CR]·········· ' Prompt Device For Status
··· GOSUB Get_Data····················· ' Purge Receive Buffer
··· IF buffer(0) = ">" THEN············ ' Check For Ready Prompt
····· EXIT····························· ' If Ready Then Exit Loop
··· ELSEIF buffer(0) = "N" AND buffer(1) = "D" THEN
····· DEBUG "."························ ' Device Ready But No Memory Stick
··· ELSEIF buffer(0) = "D" AND buffer(1) = "D" AND flag = 0 THEN
····· DEBUG "Connected!", CR, "Accessing..."
····· flag = 1························· ' Memory Stick Ready
··· ELSE
····· DEBUG "."
··· ENDIF
··· PAUSE 250·························· ' Command Retry Delay
· LOOP
· DEBUG "Ready!", CR
·'**** Change for application
· DEBUG "Deleting Data File...",CR········· ' First Delete File
· SEROUT TX\CTS, Baud, [noparse][[/noparse]$07, $20, "datafile.txt", CR]
· GOSUB Get_Data······················· ' Purge Receive Buffer
· DEBUG "Creating Data File..."···································· ' Then Create File
· SEROUT TX\CTS, Baud, [noparse][[/noparse]$09, $20, "datafile.txt", CR]
· GOSUB Get_Data
·· DEBUG CR,"To Display data type 1",CR
·· DEBUG "To Input Data type 2",CR
·· SERIN 16,16624,[noparse][[/noparse]DEC RTCCmd]
·· IF RTCCmd = 1 THEN GOTO readdata
· 'Counter can be replaced with schedule item number
· DEBUG CR,"Input variable 0",CR
· SERIN 16,16624,[noparse][[/noparse]DEC sched0]
· DEBUG "Input variable 1",CR
· SERIN 16,16624,[noparse][[/noparse]DEC sched1]
· DEBUG "Sched #",DEC counter, " Result 0 = ",DEC sched0,CR
· DEBUG "Open!", CR, CR, "Writing Data...", CR
· FOR counter = 1 TO NumSamples········ ' Number Of Samples To Log
· IF counter = 1 THEN res = sched0
· IF counter = 2 THEN res = sched1
··· DEBUG "Sample ", DEC5 counter, " of ", DEC5 NumSamples,
········· " Sched 0 --> ", DEC5 sched0," Sched 1 --> ", DEC5 sched1,CR····· ' Display Results
··· SEROUT TX\CTS, Baud, [noparse][[/noparse]$08, $20, $00, $00, $00, $0D, CR,
········· DEC5 counter, ",", DEC5 res, CR, LF, CR]
··· PAUSE 500·························· ' Write Results/Delay
··· GOSUB Get_Data····················· ' Purge Receive Buffer
· NEXT
· res = 0
· sched0 = 0
· DEBUG "Cleared... Result 0 = ",DEC sched0
·sched1 = 0
·readdata:
· DEBUG "Closing Data File..."········· ' Close File (MUST CLOSE!)
· SEROUT TX\CTS, Baud, [noparse][[/noparse]$0A, $20, "datafile.txt", CR]
· GOSUB Get_Data······················· ' Purge Receive Buffer
· DEBUG "Done!", CR, CR, "Opening Data File..." ' Open Fil For Read Mode
· SEROUT TX\CTS, Baud, [noparse][[/noparse]$0E, $20, "datafile.txt", CR]
· GOSUB Get_Data······················· ' Purge Receive Buffer
· DEBUG "Open!", CR, "Reading Data...", CR
· sched0 = 0
· sched1 = 0
· DEBUG "Zero sched variables",CR
· FOR counter = 1 TO NumSamples········ ' Number Of Samples To Read
··· SEROUT TX\CTS, Baud, [noparse][[/noparse]$0B, $20, $00, $00, $00, $0D, CR]
··· SERIN RX\RTS, Baud, [noparse][[/noparse]DEC5 work, ioByte, DEC5 res, ioByte, ioByte]
··· GOSUB Get_Data····················· ' Purge Receive Buffer
··· IF counter = 1 THEN sched0 = res
··· IF counter = 2 THEN sched1 = res
··· IF counter = 1 THEN DEBUG "Result 0 = > ", DEC5 sched0, CR····· ' Display Results
··· IF counter = 2 THEN DEBUG "Result 1 = > ", DEC5 sched1,10,13
··· PAUSE 500·························· ' Delay
· NEXT
· PAUSE 5000
· DEBUG "Closing Data File...Program Complete!"
· SEROUT TX\CTS, Baud, [noparse][[/noparse]$0A, $20, "datafile.txt", CR]
· GOSUB Get_Data······················· ' Purge Receive Buffer
· GOTO main
· 'STOP
'
[noparse][[/noparse] Subroutines ]
Get_Data:
· index = 0···························· ' Reset Index Pointer
· DO··································· ' Receive Data
··· SERIN RX\RTS, Baud, 100, Timeout, [noparse][[/noparse]ioByte]
··· buffer(index) = ioByte············· ' Add Received Byte To Buffer
··· index = index + 1·················· ' Increment Index Pointer
··· IF index > 14 THEN Timeout········· ' Check For Overflow
· LOOP
Timeout:
RETURN
Comments
The first thing I would do is to let the compiler assign the necessary storage, rather than your doing it. Change the following as shown:
sched0 VAR Byte 'Item number, Seconds ** Will replace "result" variable
Minutes VAR Byte
Hours VAR Byte
sched1 VAR Byte 'Active days, Date
day VAR Byte
sched2 VAR Byte 'Sched Hour, Month
sched3 VAR Byte 'Sched Minute, Year
sched4 VAR Byte 'Sched Variable, VAO
sched5 VAR Byte 'Sched New value, VDO
sched6 VAR Byte 'Pending (1 or 0), SE
x VAR Byte 'Unused for accounting
y VAR Byte 'Unused for accounting
index VAR Byte 'Used
ioByte VAR Byte 'Used
res VAR Byte 'Used
buffer VAR Byte 'Used !!Was a 15 byte array
flag VAR Byte 'Used
RTCCmd VAR Byte 'Unused
counter· VAR···· Word··········· ' Counter Variable
work····· VAR···· Word··········· ' Work Variable
See if that doesn't straighten things out first. Even if it doesn't it will make debugging it a whole lot easier!
When you speak of the "Synchronization Routine" are you speaking of the "Initialization Routine"? If so, the Stamp will restart there if it is being RESET. That could be caused by a number of reasons. Add this as the first line in your program:
DEBUG "PGM Starting"
If you see that more than once during any iteration of the program, you will know a RESET is occuring.
Regards,
Bruce Bates
Bruce is correct…You should always allow the compiler to assign the variable space. If you’re using multiple slots, all you need to do is declare the variables exactly the same in all slots and they will be preserved. The only things that could cause the program to do what you describe are a program error/bug, or the program restarting, which could be caused by a power problem or by a program issue, such as RETURN without a GOSUB. I hope this helps. Take care.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support