Option Explicit 'default (OFF) -implicit variable definition not used Option Base 0 'arrays start with 0 not 1! ' '======================================================================================== 'Module Title: SD_Card_R2.Bas '======================================================================================== 'Author: ZbasicAndy '======================================================================================== 'Date: 4-24-06 '======================================================================================== 'PC Operating System: XP '======================================================================================== 'ZBasic IDE Version: 1.0.4 / ZBasic Compiler version 1-1-18 / ZX-40 Firmware 1.1.6 / ZLoad v1.1.2 '======================================================================================== 'Target: ZX-40 '======================================================================================== 'Hardware: Super Comm Z R1 (4 [4 software serial & 1 Hardware] RS232 port ZX-40 PCB) 'Main/Support Modules Needed: ComSupport_R1.bas - for basic serial communications subroutines. ' Super_Comm_Z_R1.bas - "sub main" basic module '======================================================================================== 'Comport used: comport 6 @ 9600 8 N 1 '======================================================================================== 'Device used: ComfileTech.com Part # SD-COM5 Firmware version 1.02 -> subject to change! '======================================================================================== 'Program Bugs fixes and updates: 'Please Note: This beta code is subject to change at any time! ' ' '======================================================================================== 'Comfiletech.com Vendor SD Com5 firmware bugs 4-24-06 '(Will be fixed?) ' '#1 Creating a subdir then going to it by change directory command cd e.g. SD_Cd_MCU(). ' When you use the "puts command" (put string) the directory changes back to the root directory. ' Workaround -> keep all files in the root directory. ' '#2 Puts command appends CR LF on the string end. Gets command (Get string) returns ' string + CR + CR + LF -- 1 extra CR. ' Workaround -> subtract three chars from return string to compare strings. ' '#3 Doc/PDF needs a lot of revisions to correct all the doc errors. ' '#4. SD Com5 requires 16k clusters on the formated SD card. Do not use windows program to ' format the SD card via card reader. Use SDcard.org PC utility program "SD formatter" ' (The PC will format it to 4k clusters and the SD card will fail to initilize and work) ' '#5. Filenames 8 chars MAX e.g 8.3 format (xxxxxxxx.xxx) <----- look out! ' Garbage files will be created? ' '======================================================================================== ' Module Description: ' ' All "high level commands" are re-written to functions and a few sub-routines for ' proper error / response / feedback. (See below) ' ' '--------------------------------- 'SD Device Communications Protocol: '--------------------------------- 'Refer to SD user manual for reference. 'Note all "SD high level commands" are converted to functions and subroutines for easy use 'and error checking. ' 'Command [Filename] [Option] [Data] 'Command, Filename, Option, and Data are separated by a space(HEX 0x20)! 'Depending on the Command, Filename, Option, and Data can be required. 'All Commands must be followed by a CR (Carriage Return, 0x0D) and LF (Line Feed, 0x0A). 'Command format-> Command Put String: "fputs", File: "test.Tx_Strt", Option: "/w" Write, ' Data: "Hello World" ' 'For example, to store “Hello World” into file "text.Tx_Strt", you would do in ZBasic: 'Tx_Str as string 'Tx_Str = “fputs test.Tx_Strt[sp]/w[sp]Hello World” 'Call PutLine(port,Tx_Str)(This subroutine includes the CR LF) ' 'Use module ComSupport_R2.bas subroutine "PutLine(ByVal port as Byte,ByRef Tx as STRING)" 'This subroutine command includes the appended CR LF on the text end. ' '---------------------------------------- 'SD high level commands application notes: '---------------------------------------- ' 'Do not use the long message terminal mode. Use only the short message "MCU Mode" with 'ZBasic (default). (For use with all functons and subroutines below) ' 'Send only strings or a "single char string" (character) to the SD interface card? 'No binary files? ' 'The SD file system will allow ONLY one file open at a time! ' 'Filenames 8 chars MAX e.g 8.3 format (xxxxxxxx.xxx) <----- look out! ' 'High level commands are case insensitive. e.g. init or INIT should work the same? ' 'On POR or reset, call SD_Card_MCU_Mode() several times to clear interface for the MCU mode. 'Then call SD_Card_Init(Success) to check SD card for "format" or clusters. 'SD_Card_Init does not format the SD card e.g. only checks the format. 'Formatting has to be done externally via card reader and PC or other device. 'The clusters will have to be 16k not 4k - check properties of card. (Init will fail - you can't 'use the SD card with the interface) ' 'Always check for "card insertion" before reading/writing e.g. function SD_Card_MCU() ' 'First create a "zero file" using Fcreate command e.g. function SD_Card_MCU() 'Second use (put string) Fputs /w to write to only the first line of this file e.g. 'function SD_Fputs_MCU() 'All other writes use Fputs /a "append" e.g. function SD_Fputs_MCU() ' 'File open/close commands are not needed using the function SD_Fputs_MCU() e.g. only when 'reading the file this command is needed. ' 'Do not read to or write more than the input/output buffers are setup for e.g. 40 chars max. 'use delays or increase the buffers. ' 'Note: on startup or POR there is always trash on the Tx pin(s) e.g. "null" hex 00. 'Always repeat the first command to any external serial device more than once! ' '------------------------------- 'ComSupport_R4.bas commands used '------------------------------- ' 'On transmit to SD Com5 device all commands need a CR LF. 'The public Sub Putline() (below) takes care of this transmit suffix. ' '=============================================================================== 'public sub NewLine(ByVal port as Byte) ' Outputs a to serial port n. 'CALL PutByte(port,&h0D) 'CALL delay(0.050) 'CALL PutByte(port,&h0A) 'end sub '=============================================================================== 'public sub PutLine(ByVal port as Byte,ByRef Tx as STRING) ' Outputs a STRING type, followed by . Output is to serial port n. 'CALL PutStr(port,Tx) 'CALL NewLine(port) 'end sub '=============================================================================== 'public sub PutStr(ByVal port as Byte, _ 'ByRef Tx as STRING) ' Outputs a STRING type to serial port n. 'dim Length as INTEGER 'dim Ch as STRING * 1 'dim bCh as BYTE 'dim I as INTEGER 'Length = Len(Tx) 'For I = 1 To Length 'Ch = Mid(Tx, I, 1) 'bCh = Asc(Ch) 'CALL PutByte(port,bCh) 'NEXT 'End sub '============================================================================== ' 'On the receive only one command is needed to get the SD Com5 interface response(s). 'It has an adjustable timeout and boolean Success flag. ' '=============================================================================== 'public Sub Get_String_40(Byval port as byte, Byref Rx_str as string, Byref Num_char as integer, _ 'ByRef TimeOuts as single, Byref Success as boolean) ' 'This routine will retreive up to 40 chars from the input buffer. '(Note in/out buffers are set at 41) (50 - 9 = 41) 'It has an adjustable timeout parameter (TimeOutS in (single) seconds) ' and a Timeout "flag" -> (True = Timeout) and (False is no Timeout) ' Return Success = no timeout! '==================================== ' Revision 1.0b 4/5/06 by Don Kinzer '==================================== 'Number of chars (Num_Char) has to be < = 40 chars! 'Dim RxBuf(1 to 40) as Byte 'Dim Timeout as boolean 'Timeout = Not Success ' make (default) timeout true for sub GetQueue 'CALL GetQueue(CByteArray(Inbuf(1, port ).DataAddress), RxBuf,Num_Char,TimeOutS,Timeout) 'Rx_str = MakeString(RxBuf.DataAddress, Num_Char) 'If (Timeout) then 'GetQueue char "timeout" due to not enough time e.g. TimeOutS 'Success = False 'Else 'Success = True 'End if 'End Sub '=============================================================================== ' ' ' '-------------------------------- ' Error Return Codes in MCU mode: ' for reference only! '-------------------------------- ' E00 Command not recognized ' E10 Card not inserted ' E11 Card inserted but not initialized ' E12 Error during card initializaion ' E20 File creation error during fcreate ' E21 File delete error ' E22 File rename error ' E30 md command error ' E31 rd command error ' E32 cd command error ' E40 fsize command error ' E50 ftime command error ' E51 fopen command error ' E52 fopen command error ' E53 fopen command error ' E54 file open already ' E55 fclose command error ' E56 command not available during fopen ' E57 fopen must be used beforehand ' E58 during fopen command, only options /r,/w /a may be used ' E60 fgetc option not between 1 and 256 bytes ' E61 fgetc command my only use option /r ' E70 fputc command may only use options /w or /a ' E71 data write error during fputc ' E72 diromg fputs command, only options /w or /a may be used ' E73 fputs command error ' E74 fwrite option must be between 1 and 512 bytes ' E75 baud rate setting error ' '======================================================================================= ' Function Definitions for SD cards '======================================================================================= 'SD Function "High level Command" Summary for the SD "MCU mode" ONLY!: ' '======================================================================================= ' [SD System Function Commands:] '======================================================================================= ' '---------------------------------- 'Function SD_Init_MCU() as Boolean '---------------------------------- '-> Initializes SD card for use.(Fat16 format only - 2GB max) - Does not format the card! '----------------------------------------------------- 'Function SD_Mode_MCU(Byval Mode as string) as Boolean '----------------------------------------------------- '-> Select "MCU" or "Terminal" mode. MCU mode is used with the microcontroller '---------------------------------- 'Function SD_Reset_MCU() as Boolean '---------------------------------- '->Resets SD card electronics MCU '------------------------------------------------------ 'Function SD_Baud_MCU(Byval SD_Baud as long) as Boolean '------------------------------------------------------ '-> Changes SD baud rate. 'Baud rates supported (bps) : 4800, 9600, 19200, 38400, 57600, 115200 (Default: 9600 bps) '--------------------------------- 'Function SD_Card_MCU() as Boolean '--------------------------------- '-> Check SD card for insertion. ' '======================================================================================== ' [SD Directory Function Commands:] '======================================================================================== ' '--------------------------------- 'Function SD_Dsize_MCU() as String '---------------------------------- '-> Display Total Disk Space of the SD/MMC Card. '-------------------------------------------------------- 'Function SD_CD_MCU(Byref Directory as string) as Boolean '-------------------------------------------------------- '-> Change Directory. '-------------------------------------------------------- 'Function SD_MD_MCU(Byref Directory as string) as Boolean '-------------------------------------------------------- '-> Make Directory. '------------------------------------------------------------- 'Function SD_Fcreate_MCU(Byref Directory as string) as Boolean '------------------------------------------------------------- '-> Create a new file with size 0. 'Fcreate command will create a file with size 0. 'Therefore all read/append commands with 'Fopen optionx "r" read or optionx "a" append cannot be used. 'Just use only the "w" optionx only! ' '======================================================================================== ' [SD File Function Commands:] '======================================================================================== ' 'Notes: Filenames 8 chars MAX e.g 8.3 format (xxxxxxxx.xxx) <----- look out! ' '--------------------------------------------------------- 'Function SD_Fsize_MCU(byref Filename as String) as String '--------------------------------------------------------- '-> Displays File Size Bytes. '--------------------------------------------------------- 'Function SD_Del_MCU(byref Filename as String) as boolean '--------------------------------------------------------- '-> Delete file. '----------------------------------------------------------------------------------- 'Function SD_Fopen_MCU(byval Filename as String, Byval Optionx as string) as Boolean '----------------------------------------------------------------------------------- '->File Open with optionx "r" File(Read), "w" File(Write) or "a" File(Append) 'After opening a File, you must use File Close (Fclose) command to close the File. 'Only 1 File can be opened at one time. '"r" and "a" Optionx cannot be used with Files of size 0. 'You can only use Optionx "w" to write to the file! '----------------------------------- 'Function SD_Fclose_MCU() as Boolean '----------------------------------- '-> File close - Note: only one file is allowed opened 'at a time - this command will close the one file. '-------------------------------------------------------------------------- 'Function SD_Fputc_MCU(ByRef Filename as string, Byref Optionx as string, _ ' Byref CharByte as byte ) as Boolean '-------------------------------------------------------------------------- '-> Write 1 byte to a file. 'Optionx "w" write or "a" append '-------------------------------------------------------------------------- 'Function SD_Fputs_MCU(ByVal Filename as String, ByVal Optionx as String, _ ' ByVal String_Data as string ) as Boolean. '-------------------------------------------------------------------------- '-> Write 255 bytes to a file. 'Optionx "w" write or "a" append. Appends CR LF at end of text line ??? '-------------------------------------------------------------------------- 'Function SD_Fgetc_MCU(Byval Number_Bytes as byte) as string '0 -255 bytes '-------------------------------------------------------------------------- '-> Read 0-255 bytes from a file. 'Fopen command must be used before using Fgetc command! '------------------------------------------------ 'Function SD_Fgets_MCU() as string '0 - 255 bytes '------------------------------------------------ '-> Read 1 line of characters terminated by a CR LF from a file. '------------------------------------------------------ 'Fopen command must be used before using Fgets command! '------------------------------------------------------ ' '====================================================================================== ' SD Commands not converted to functions: '====================================================================================== ' 'ftime [Filename] Display File creation and Last-Modified times. - SD board has no RTC 'rename [Source Filename] [Destination Filename] - for utility use only 'dir - List Directory - for utility use only 'rd [Directory] - remove directory - very dangerous - for utility use only - all files 'have to be deleted before using! 'fputs2 - write until ^Z ??? 'fwrite [/ # of bytes to write] - Write up to set # of bytes (Up to 512 bytes per Operation)??? 'fread [Filename] - Read all data in File - not practical for use with Zbasic ' ' ' ' ' ' '===================================================================================== 'Program Definitions: '===================================================================================== '===================================================================== 'Constants '[Public | Private] Const As = (default private) '===================================================================== ' Port Constants for all ports '----------------------------- ' Do not modify! Private Const Port3 as byte = 3 Private Const Port4 as byte = 4 Private Const Port5 as byte = 5 Private Const Port6 as byte = 6 '----------------------------- '===================================================================== 'Variables '{Public | Private | Dim} As (default private) '========================================================================================== '=========================================================================================== ' General Purpose SD Subroutines '=========================================================================================== Public Sub SD_Card_MCU_Mode(ByRef Success as Boolean) 'Put SD card interface into MCU mode (short message mode) 'Subroutine tested OK If (SD_Mode_MCU("MCU")) then Debug.print "SD Card Is In The MCU Mode!" Success = true Else Debug.print "SD Card MCU Mode Failure!" Success = false End if End Sub '============================================================================================ Public Sub SD_Card_Init(ByRef Success as Boolean) 'Init. SD card - returns Success = true if ok 'init only checks for card format but does not format the card! 'card format must have 16k clusters? 'Subroutine tested OK If (SD_Init_MCU()) then Debug.print "SD Card Is Initialized!" Success = true Else Debug.print "SD Card Initialized Failure!" Success = false End if End Sub '------------------------------------------------------------------------------------------- Public Sub SD_Card_Insertion(ByRef Success as Boolean) 'Checks for SD insertion - returns Success = true if SD card is inserted in SD connector. 'Subroutine tested OK If (SD_Card_MCU()) then Debug.print "SD Card Is Inserted!" Success = true Else Debug.print "SD Card Not Inserted!" Success = false End if End Sub '------------------------------------------------------------------------------------------- Public Sub SD_Reset(ByRef Success as Boolean) 'Resets, select "MCU" mode and sets baudrate "9600" for SD card. Returns Success = true if OK 'Subroutine tested OK If (SD_Reset_MCU()) then 'reset SD card electronics If (SD_Mode_MCU("MCU")) then 'set SD mode to MCU mode IF (SD_Baud_MCU(9600)) then 'set baud for 9600 (default) (8 N 1) Debug.Print "SD Card Reset Success!" Success = true Else Debug.Print "SD Card Baud Failure!" Success = false End If Else Debug.Print "SD Card Mode Failure!" Success = false End if Else Debug.Print "SD Card Reset Failure!" Success = false End if End Sub '-------------------------------------------------------------------------------------------- Public Sub SD_Disk_Size() 'Displays total number of bytes left on SD card. 'Subroutine tested OK Dim RV as String RV = SD_Dsize_MCU() 'check disk space bytes Debug.print "Total SD Disk Space Left (Bytes) = ";RV End Sub '-------------------------------------------------------------------------------------------- Public Sub SD_String_Write_Read_Test(ByRef Success as Boolean) 'Subroutine tested OK dim Filename as string dim Optionx as string dim String_Data as string Dim SD_Stored_Data as string dim Flag as boolean dim i as integer dim idx as integer dim x as byte 'Checks for SD card insertion - proceed if OK 'Deletes the file (RW_Test.txt) if present. 'Creates a file (RW_Test.txt) in the root directory with zero bytes on the SD card. 'Writes one line of string data + CR + LF to the SD card file (RW_Test.txt). 'Opens the file (RW_Test.txt) for reading. 'Reads the first line of string data (CR CR LF terminated) from the file (RW_Test.txt) on 'the SD card. 'Closes the file (RW_Test.txt). 'Compares the data. 'Returns Success = True or False. 'Note: Each command clears both tx/rx buffers! 'Filenames 8 chars MAX e.g 8.3 format (xxxxxxxx.xxx) <----- look out! SD_Stored_Data = "" Filename = "RW_Test.txt" Flag = SD_Card_MCU() 'check for card insertion - proceed if OK IF (Flag) then Flag = SD_Del_MCU(Filename) 'delete previously created file (RW_Test.txt) - if present. Flag = SD_Fcreate_MCU(Filename) 'create file "RW_Test.txt" with zero bytes. If (Flag) then 'Proceed on no Fcreate failure Optionx = "w" String_Data = "This is a string test" '21 chars + CR + LF = 23 i = len(String_Data) 'get string length Debug.print "Length String Data = " ;cstr(i) Flag = SD_Fputs_MCU(Filename,Optionx,String_Data) If (Flag) then 'Proceed on no write string failure Flag = SD_Fopen_MCU(Filename, "r") If (Flag) then 'Proceed on no file open failure SD_Stored_Data = SD_Fgets_MCU()'Gets first string terminated by CR LF SD_Stored_Data = Trim(SD_Stored_Data) i = len(SD_Stored_Data) 'get string length (24) string + CR + CR + LF Debug.print "Length Of Stored String = " ;cstr(i) Debug.print "Stored Data = "; SD_Stored_Data Flag = SD_Fclose_MCU() 'close the file i = i - 3 'discard last three chars. SD_Stored_Data = Mid(SD_Stored_Data,1,i) If SD_Stored_Data = String_Data then Success = true Else Success = false End if Else Success = false End if Else Success = false End if Else Success = false End If Else Success = false End if '---------------------- 'Print bytes Hex '---------------------- 'For idx = 1 to 24 'x = Asc(Mid(SD_Stored_Data,idx,1)) 'Debug.print "Index = "; cstr(idx);" Hex Value = ";cstrhex(x) 'Next idx '---------------------- 'End of bytes Hex '---------------------- ' End Sub '-------------------------------------------------------------------------------------------- Public Sub SD_Chr_Write_Read_Test(ByRef Success as Boolean) 'Subroutine tested OK dim Filename as string dim Optionx as string dim Chr_Data as string Dim SD_Stored_Chr as string dim Flag as boolean dim i as integer dim idx as integer dim x as byte dim str as string 'Checks for SD card insertion - proceed if OK 'Deletes the file (Chr_Test.txt) if present. 'Creates a file (Chr_Test.txt) in the root directory with zero bytes on the SD card. 'Writes one chr data to the SD card file (Chr_Test.txt). 'Opens the file (Chr_Test.txt) for reading. 'Reads the first chr data from the file (Chr_Test.txt) on the SD card. 'Closes the file (Chr_Test.txt). 'Compares the data. 'Returns Success = True or False. 'Note: Each command clears both tx/rx buffers! 'Filenames 8 chars MAX e.g 8.3 format (xxxxxxxx.xxx) <----- look out! SD_Stored_Chr = "" Filename = "Chr_Test.txt" Flag = SD_Card_MCU() 'check for card insertion - proceed if OK IF (Flag) then Flag = SD_Del_MCU(Filename) 'delete previously created file (Chr_Test.txt) - if present. Flag = SD_Fcreate_MCU(Filename) 'create file "Chr_Test.txt" with zero bytes. If (Flag) then 'Proceed on no Fcreate failure Optionx = "w" chr_data = "A" Flag = SD_Fputc_MCU(Filename,Optionx,Chr_Data) If (Flag) then 'Proceed on no write string failure Flag = SD_Fopen_MCU(Filename, "r") If (Flag) then 'Proceed on no file open failure SD_Stored_Chr = SD_Fgetc_MCU(1) 'return string 1 char Debug.print "SD Stored Chr = "; Chr_Data Debug.print "SD Retreived Chr = "; SD_Stored_Chr Flag = SD_Fclose_MCU() 'close the file If SD_Stored_Chr = Chr_Data then Success = true Else Success = false End if Else Success = false End if Else Success = false End if Else Success = false End If Else Success = false End if End Sub '-------------------------------------------------------------------------------------------- '=========================================================================================== ' Application Specific SD Subroutines - disreguard! '=========================================================================================== Public Sub Super_Com_Z_SD_Init() 'Subroutine tested OK 'utility prepares sd card with "zero length files" for writing strings with the append option. 'Filenames 8 chars MAX e.g 8.3 format (xxxxxxxx.xxx) <----- look out! 'No error checking except for card insertion. dim filename as string dim optionx as string dim string_data as string dim flag as boolean If (SD_Card_MCU() AND SD_Init_MCU()) then 'check for card insertion and init. filename = "CID.txt" flag = SD_Del_MCU(Filename) 'delete previously created files - if any are present. filename = "IR.txt" flag = SD_Del_MCU(Filename) filename = "Status.txt" flag = SD_Del_MCU(Filename) filename = "CID.Txt" flag = SD_Fcreate_MCU(Filename) 'create files call delay(0.5) filename = "IR.Txt" flag = SD_Fcreate_MCU(Filename) call delay(0.5) filename = "Status.Txt" flag = SD_Fcreate_MCU(Filename) call delay(0.5) 'writes only first line using "w" option for file header information 'all other subsequent writes use the "a" append option only! filename = "CID.txt" Optionx = "w" 'use only for first write. Use append on next write String_Data = "CID Data File Created On " & cstr(Month) & "/" & cstr(Day) & "/" & cstr(Year) flag = SD_Fputs_MCU(Filename,Optionx,String_Data) call delay(0.5) filename = "IR.txt" Optionx = "w" 'use only for first write. Use append on next write String_Data = "IR Data File Created On " & cstr(Month) & "/" & cstr(Day) & "/" & cstr(Year) flag = SD_Fputs_MCU(Filename,Optionx,String_Data) call delay(0.5) filename = "Status.txt" Optionx = "w" 'use only for first write. Use append on next write String_Data = "Status File Created On " & cstr(Month) & "/" & cstr(Day) & "/" & cstr(Year) flag = SD_Fputs_MCU(Filename,Optionx,String_Data) call delay(0.5) debug.print "Super_Com_Z_SD_Init Complete!" Else debug.print "Card Not Inserted And Or Initilized! - Super_Com_Z_SD_Init Failure!" End if End Sub '------------------------------------------------------------------------------------------ Public Sub Store_CID_Data_To_SD() 'Subroutine tested OK dim filename as string dim optionx as string dim string_data as string dim flag as boolean If (SD_Card_MCU()) then 'check for card insertion before appending data filename = "CID.txt" Optionx = "a" 'append data to SD String_Data = Global_CID_Date & " " & Global_CID_Time & " " & Global_CID_Name & " " & Global_CID_Number flag = SD_Fputs_MCU(Filename,Optionx,String_Data) If (flag) then debug.print "CID Data Was Stored To SD!" Else debug.print "CID Data Storage Failure To SD!" End if End if End Sub '------------------------------------------------------------------------------------------ Public Sub Store_IR_Data_To_SD() 'Subroutine tested OK 'Note: Super_Com_Z_SD_Init() was used to init files on the SD card. Used only once. dim filename as string dim optionx as string dim string_data as string dim flag as boolean If (SD_Card_MCU()) then 'check for card insertion before appending data filename = "IR.txt" Optionx = "a" 'append data to SD String_Data = "IR Sec. Det. Trig. " & Global_Local_Time & " " & Global_Local_Date flag = SD_Fputs_MCU(Filename,Optionx,String_Data) If (flag) then debug.print "IR Data Was Stored To SD!" Else debug.print "IR Data Storage Failure To SD!" End if End if End Sub '------------------------------------------------------------------------------------------- Public Sub SD_Utility_File_Sizes() 'Displays number of bytes on each utility file on the SD card. 'Subroutine tested OK Dim RV as String Dim Filename as string Filename = "CID.txt" RV = SD_Fsize_MCU(Filename) Debug.print "CID.txt File Size (Bytes) = ";RV Filename = "IR.txt" RV = SD_Fsize_MCU(Filename) Debug.print "IR.txt File Size (Bytes) = ";RV Filename = "Status.txt" RV = SD_Fsize_MCU(Filename) Debug.print "Status.txt File Size (Bytes) = ";RV End Sub '=========================================================================================== ' SD General Functions '=========================================================================================== Private Function SD_Init_MCU() as Boolean 'Initializes (Reads) SD card for use e.g. Fat16 & 16k cluster format. 'function tested OK Dim Tx_Str as string Dim Rx_Str as string Dim TimeOutS as single Dim Num_Char as integer Dim Success as boolean ClearInputBuffer(port6) ClearOutputBuffer(port6) Tx_Str = "init" Call PutLine(Port6,Tx_Str) 'includes cr lf Call delay(1.0) Rx_str = "" TimeOutS = 1.0 ' 1 sec Num_Char = 1 Success = false Call Get_String_40(Port6,Rx_Str,Num_Char,TimeOutS, Success) If (Success) then 'No Timeout Select Case Rx_str Case "O" 'OK SD_Init_MCU = True Case Else 'Unknown debug.print "Init Failure! " ;Rx_Str SD_Init_MCU = False End Select ELSE 'Timeout Failure debug.print "Init TimeOut!" SD_Init_MCU = False END IF End Function '====================================================================================== Private Function SD_Mode_MCU(Byval Mode as string) as Boolean 'Select "MCU" or "Terminal" mode. Please use only the MCU mode! 'function tested OK Dim Tx_Str as string Dim Rx_Str as string Dim TimeOutS as single Dim Num_Char as integer Dim Success as boolean ClearInputBuffer(port6) ClearOutputBuffer(port6) Select case Mode Case "Terminal" Tx_Str = "MODE /t" 'case insensitive Case "MCU" Tx_Str = "MODE /m" Case Else debug.print "Mode Not Correct! " SD_Mode_MCU = false Exit Function End Select Call PutLine(port6,Tx_Str) 'includes cr lf Call delay(1.0) Rx_str = "" TimeOutS = 1.0 ' 1 sec Num_Char = GetQueueCount(CByteArray(Inbuf(1, port6).DataAddress)) Num_Char = Num_Char - 2 'remove CR LF Success = false Call Get_String_40(Port6,Rx_Str,Num_Char,TimeOutS, Success) If (Success) then 'No Timeout Select Case Rx_str Case ">Mode: terminal(full message)" 'debug.print Rx_Str debug.print "Terminal Mode OK!" SD_Mode_MCU = False Case ">Mode: MCU(simple message)" 'debug.print Rx_Str 'debug.print "MCU Mode OK!" used by sub SD_Mode_MCU = True Case Else 'Unknown SD_Mode_MCU = False debug.print "SD MCU Mode Unknown! " ;Rx_Str End Select ELSE 'Timeout Failure debug.print "SD MCU Mode Timeout!" SD_Mode_MCU = False END IF End Function '=========================================================================================== Private Function SD_Fsize_MCU(byref Filename as String) as String 'Displays file size 'function tested OK Dim Tx_Str as string Dim Rx_Str as string Dim TimeOutS as single Dim Num_Char as integer Dim Success as boolean ClearInputBuffer(port6) ClearOutputBuffer(port6) Tx_Str = "fsize " & Filename Call PutLine(port6,Tx_Str) Call delay(1.0) Rx_str = "" TimeOutS = 1.0 ' 1 sec Num_Char = GetQueueCount(CByteArray(Inbuf(1, port6).DataAddress)) Num_Char = Num_Char - 2 'Remove CR LF Success = false Call Get_String_40(Port6,Rx_Str,Num_Char,TimeOutS, Success) If (Success) then 'No Timeout SD_Fsize_MCU = Rx_str ELSE 'Timeout Failure debug.print "File Size TimeOut!" SD_Fsize_MCU = "UnKnown File Size!" END IF End Function '=========================================================================================== Private Function SD_Dsize_MCU() as string 'Display Total Disk Space of the SD/MMC Card. 'function tested OK Dim Tx_Str as string Dim Rx_str as string Dim TimeOutS as single Dim Num_Char as integer Dim Success as boolean ClearInputBuffer(port6) ClearOutputBuffer(port6) Tx_Str = "Dsize" Call PutLine(port6,Tx_Str) Call delay(1.0) Rx_str = "" TimeOutS = 1.0 ' 1 sec Num_Char = GetQueueCount(CByteArray(Inbuf(1, port6).DataAddress)) Num_Char = Num_Char - 2 'remove CR LF Success = false Call Get_String_40(Port6,Rx_Str,Num_Char,TimeOutS, Success) If (Success) then 'No Timeout 'Debug.print "SD Disk Size (Bytes) = "; Rx_Str -- used by sub SD_Dsize_MCU = Rx_str ELSE 'Timeout Failure debug.print "Disk Size TimeOut!" SD_Dsize_MCU = "Disk Size UnKnown?" END IF End Function '=========================================================================================== Private Function SD_CD_MCU(Byref Directory as string) as Boolean 'Changes directory 'function tested OK Dim Tx_Str as string Dim Rx_str as string Dim Temp as string Dim TimeOutS as single Dim Num_Char as integer Dim Success as boolean ClearInputBuffer(port6) ClearOutputBuffer(port6) Tx_Str = "cd " & Directory Call PutLine(port6,Tx_Str) Call delay(1.0) Rx_str = "" TimeOutS = 1.0 ' 1 sec Num_Char = 1 Success = false Call Get_String_40(Port6,Rx_Str,Num_Char,TimeOutS, Success) If (Success) then 'No Timeout Temp = Mid(Rx_str,1,1) Select Case Temp Case "O" 'OK SD_CD_MCU = True Case Else 'Unknown debug.print "Change Directory Failure! ";Temp SD_CD_MCU = False End Select ELSE 'Timeout Failure debug.print "Change Directory TimeOut!" SD_CD_MCU = False END IF End Function '========================================================================================= Private Function SD_MD_MCU(Byref Directory as string) as Boolean 'Make directory 'function tested OK Dim Tx_Str as string Dim Rx_str as string Dim Temp as string Dim TimeOutS as single Dim Num_Char as integer Dim Success as boolean ClearInputBuffer(port6) ClearOutputBuffer(port6) Tx_Str = "md " & Directory Call PutLine(port6,Tx_Str) Call delay(1.0) Rx_str = "" TimeOutS = 1.0 ' 1 sec Num_Char = 1 Success = false Call Get_String_40(Port6,Rx_Str,Num_Char,TimeOutS, Success) If (Success) then 'No Timeout Temp = Mid(Rx_str,1,1) Select Case Temp Case "O" 'OK SD_MD_MCU = True Case Else 'Unknown debug.print "Make Directory Failure! ";Temp SD_MD_MCU = False End Select ELSE 'Timeout Failure debug.print "Make Directory TimeOut!" SD_MD_MCU = False END IF End Function '========================================================================================= Private Function SD_Fcreate_MCU(Byref Filename as string) as Boolean 'Filenames 8 chars MAX e.g 8.3 format (xxxxxxxx.xxx) <----- look out! 'Create a new file with size 0. 'fcreate command will create a file with size 0. ' 'Therefore all read/append commands optionx "r" read or optionx "a" append cannot be used! 'Just use only the optionx "w" only on the first write only! On all other writes use "a" 'append. 'function tested OK Dim Tx_Str as string Dim Rx_Str as string Dim Temp as string Dim TimeOutS as single Dim Num_Char as integer Dim Success as boolean ClearInputBuffer(port6) ClearOutputBuffer(port6) Tx_Str = "fcreate " & Filename Call PutLine(port6,Tx_Str) Call delay(1.0) Rx_str = "" TimeOutS = 1.0 ' 1 sec Num_Char = 1 Success = false Call Get_String_40(Port6,Rx_Str,Num_Char,TimeOutS, Success) If (Success) then 'No Timeout Temp = Mid(Rx_str,1,1) Select Case Temp Case "O" 'OK SD_Fcreate_MCU = True Case Else 'Unknown debug.print "File Create Failure! ";Temp SD_Fcreate_MCU = False End Select ELSE 'Timeout Failure debug.print "File Create TimeOut!" SD_Fcreate_MCU = False END IF End Function '========================================================================================= Private Function SD_Del_MCU(byref Filename as String) as boolean 'Delete file 'function tested OK Dim Tx_Str as string Dim Rx_str as string Dim TimeOutS as single Dim Num_Char as integer Dim Success as boolean Dim flag as boolean Dim value as single Dim Temp as string ClearInputBuffer(port6) ClearOutputBuffer(port6) Tx_Str = "Del " & Filename Call PutLine(port6,Tx_Str) Call delay(1.0) Rx_str = "" TimeOutS = 1.0 ' 1 sec Num_Char = 1 Success = false value = 0.0 flag = false Temp = "" Call Get_String_40(Port6,Rx_Str,Num_Char,TimeOutS, Success) If (Success) then 'No Timeout Temp = Mid(Rx_str,1,1) Select Case Temp Case "O" 'OK SD_Del_MCU = True Case Else 'Unknown debug.print "File Delete Failure! ";Temp SD_Del_MCU = False End Select ELSE 'Timeout Failure debug.print "File Delete TimeOut!" SD_Del_MCU = False END IF End Function '=========================================================================================== Private Function SD_Fopen_MCU(byval Filename as String, ByVal Optionx as string) as Boolean 'Filenames 8 chars MAX e.g 8.3 format (xxxxxxxx.xxx) <----- look out! 'File Open with optionx "r" File(Read), "w" File(Write) or "a" File(Append) 'After opening a File, you must use File Close (Fclose) command to close the File. 'Only 1 File may be opened at one time! ' "r" and "a" Option cannot be used with Files of size 0. You can use "w" to write to the file! 'function tested OK Dim Tx_Str as string Dim Rx_str as string Dim TimeOutS as single Dim Num_Char as integer Dim Success as boolean Dim flag as boolean Dim value as single Dim Temp as string ClearInputBuffer(port6) ClearOutputBuffer(port6) Tx_Str = "Fopen " & Filename & " /" & Optionx Call PutLine(port6,Tx_Str) Call delay(1.0) Rx_str= "" TimeOutS = 1.0 ' 1 sec Num_Char = 1 Success = false value = 0.0 flag = false Temp = "" Call Get_String_40(Port6,Rx_Str,Num_Char,TimeOutS, Success) If (Success) then 'No Timeout Temp = Mid(Rx_str,1,1) Select Case Temp Case "O" 'OK SD_Fopen_MCU = True Case Else 'Unknown debug.print "File Open Failure! ";Temp SD_Fopen_MCU = False End Select ELSE 'Timeout Failure debug.print "File Open TimeOut!" SD_Fopen_MCU = False END IF End Function '=========================================================================================== Private Function SD_Fclose_MCU() as Boolean 'File close - Note: only one file is allowed opened at a time - this command will close the 'only file opened. 'function tested OK ' Dim Tx_Str as string Dim Rx_str as string Dim Temp as string Dim TimeOutS as single Dim Num_Char as integer Dim Success as boolean ClearInputBuffer(port6) ClearOutputBuffer(port6) Tx_Str = "fclose" Call PutLine(port6,Tx_Str) Call delay(1.0) Rx_str = "" TimeOutS = 1.0 ' 1 sec Num_Char = 1 Success = false Call Get_String_40(Port6,Rx_Str,Num_Char,TimeOutS, Success) If (Success) then 'No Timeout Temp = Mid(Rx_str,1,1) Select Case Temp Case "O" 'OK SD_Fclose_MCU = True Case Else 'Unknown debug.print "File Close Failure! ";Temp SD_Fclose_MCU = False End Select ELSE 'Timeout Failure debug.print "File Close TimeOut" SD_Fclose_MCU = False END IF End Function '========================================================================================= Private Function SD_Reset_MCU() as Boolean 'Resets SD card electronics MCU 'function tested OK Dim Tx_Str as string Dim Rx_str as string Dim Temp as string Dim TimeOutS as single Dim Num_Char as integer Dim Success as boolean ClearInputBuffer(port6) ClearOutputBuffer(port6) Tx_Str = "reset" Call PutLine(port6,Tx_Str) 'cr lf added Call delay(1.0) Rx_str = "" TimeOutS = 1.0 ' 1 sec Num_Char = 1 'debug.print "queue count = "; cstr(num_char) Success = false Call Get_String_40(Port6,Rx_Str,Num_Char,TimeOutS, Success) If (Success) then 'No Timeout Temp = Mid(Rx_str,1,1) Select Case Temp Case "O" 'OK SD_Reset_MCU = True debug.print "MCU Reset OK! " Case Else 'Unknown debug.print "MCU Reset Failure! "; Temp SD_Reset_MCU = False End Select ELSE 'Timeout Failure debug.print "MCU Reset TimeOut!" SD_Reset_MCU = False END IF End Function '========================================================================================= Private Function SD_Baud_MCU(Byval SD_Baud as long) as Boolean 'Changes SD baud rate 'Baud rates supported (bps) : 4800, 9600, 19200, 38400, 57600, 115200 (Default: 9600bps 8 N 1) 'Baud rate should match the ZBasic serial interface setup! 'function tested OK Dim Tx_Str as string Dim Rx_str as string Dim Temp as string Dim TimeOutS as single Dim Num_Char as integer Dim Success as boolean ClearInputBuffer(port6) ClearOutputBuffer(port6) Temp = Cstr(SD_Baud) 'convert to string Tx_Str = "baud" & " /" & Temp Call PutLine(port6,Tx_Str) Call delay(1.0) Rx_str = "" TimeOutS = 1.0 ' 1 sec Num_Char = 1 Success = false Call Get_String_40(Port6,Rx_Str,Num_Char,TimeOutS, Success) If (Success) then 'No Timeout Select Case Rx_str Case "O" 'Ok SD_Baud_MCU = True debug.print "Set SD Baudrate OK!" Case Else 'Unknown debug.print "SD Error / Baud Rate Unknown! " ; Rx_str SD_Baud_MCU = False End Select ELSE 'Timeout Failure debug.print "Set SD Baud TimeOut!" SD_Baud_MCU = False END IF End Function '========================================================================================= Private Function SD_Card_MCU() as Boolean 'Check SD card for insertion 'When the card is first inserted the response is ID (inserted/detected) 'When the card is checked for insertion with the command the response is D detected or Exx Error xx. 'function tested OK Dim Tx_Str as string Dim Rx_str as string Dim TimeOutS as single Dim Num_Char as integer Dim Success as boolean ClearInputBuffer(port6) ClearOutputBuffer(port6) Tx_Str = "card" Call PutLine(port6,Tx_Str) 'with cr lf Call delay(1.0) Rx_str = "" TimeOutS = 1.0 ' 1 sec Num_Char = 1 Success = false Call Get_String_40(Port6,Rx_Str,Num_Char,TimeOutS, Success) If (Success) then 'No Timeout Select Case Rx_str Case "D" 'detected card SD_Card_MCU = True 'debug.print "SD Card Inserted!" used by sub Case Else 'unknown 'debug.print "SD Card Not Inserted! " ;Rx_Str used by sub SD_Card_MCU = False End Select ELSE 'Timeout Failure debug.print "SD Card TimeOut" SD_Card_MCU = False END IF End Function '========================================================================================= Private Function SD_Fputc_MCU(ByRef Filename as string, Byref Optionx as string, _ Byref Chr_Data as string ) as Boolean 'Filenames 8 chars MAX e.g 8.3 format (xxxxxxxx.xxx) <----- look out! 'Optionx = "w" write, "a" append. 'Use "w" on zero Fcreate then afterwards use "a" for append 'Write 1 byte to a file 'Use Fcreate to make file with zero bytes before using SD_Fputc_MCU. Dim Tx_Str as string Dim Rx_str as string Dim TimeOutS as single Dim Num_Char as integer Dim Success as boolean Dim Temp as string ClearInputBuffer(port6) ClearOutputBuffer(port6) Temp = Cstr(Chr_Data) Select case Optionx Case "w" Tx_Str = "Fputc " & Filename & " /" & Optionx & " " & Temp 'write 'debug.print "tx_Str write = "; tx_str Case "a" Tx_Str = "Fputc " & Filename & " /" & Optionx & " " & Temp 'append Case Else debug.print "File Put Char Optionx Not Correct!" SD_Fputc_MCU = false Exit Function End Select Call PutLine(port6,Tx_Str) Call delay(1.0) Rx_str = "" TimeOutS = 1.0 ' 1 sec Num_Char = 1 Success = false Call Get_String_40(Port6,Rx_Str,Num_Char,TimeOutS, Success) If (Success) then 'No Timeout Select Case Rx_str Case "O" 'Put char Ok SD_Fputc_MCU = True Case Else 'unknown debug.print "File Put Char Failure! " ;Rx_Str SD_Fputc_MCU = True End Select ELSE 'Timeout Failure debug.print "File put char TimeOut!" SD_Fputc_MCU = False END IF End Function '========================================================================================= Private Function SD_Fputs_MCU(ByRef Filename as String, ByVal Optionx as string, _ Byref String_Data as string ) as Boolean 'Filenames 8 chars MAX e.g 8.3 format (xxxxxxxx.xxx) <----- look out! 'Can Write 255 bytes to a file 'Keep in mind the transmit queue buffer size programmed into the ZBasic controller. 'In my case it is 41 - do not make the string longer than 40 chars! 'function tested OK Dim Tx_Str as string '40 chars max for Super Com Z Dim Rx_str as string Dim TimeOutS as single Dim Num_Char as integer Dim Success as boolean ClearInputBuffer(port6) ClearOutputBuffer(port6) Select case Optionx Case "w" Tx_Str = "Fputs " & Filename & " /" & Optionx & " " & String_Data 'write Case "a" Tx_Str = "Fputs " & Filename & " /" & Optionx & " " & String_Data 'append Case Else debug.print "File put string Optionx Not Correct!" SD_Fputs_MCU = false Exit Function End Select Call PutLine(port6,Tx_Str) Call delay(1.0) Rx_str = "" TimeOutS = 1.0 ' 1 sec Num_Char = 1 Success = false Call Get_String_40(Port6,Rx_Str,Num_Char,TimeOutS, Success) If (Success) then 'No Timeout Select Case Rx_str Case "O" 'Put string Ok SD_Fputs_MCU = True Case Else 'unknown debug.print "File Put String Failure! "; Rx_Str SD_Fputs_MCU = True End Select ELSE 'Timeout Failure debug.print "File Put String TimeOut!" SD_Fputs_MCU = False END IF End Function '========================================================================================= Private Function SD_Fgetc_MCU(Byval Number_Bytes as byte) as string '0 - 255 bytes max 'Read 0-255 bytes from a file 'Fopen command must be used before using Fgetc command. 'Be sure to close the file after use! 'Note my application has a input buffer queue of 40 chars. 'Filenames 8 chars MAX e.g 8.3 format (xxxxxxxx.xxx) <----- look out! Dim Tx_Str as string Dim Rx_str as string Dim TimeOutS as single Dim Num_Char as integer Dim Success as boolean Dim Temp as string ClearInputBuffer(port6) ClearOutputBuffer(port6) Temp = Cstr(Number_Bytes) Tx_Str = "fgetc /" & Temp Call PutLine(port6,Tx_Str) Call delay(1.0) Rx_str = "" TimeOutS = 1.0 ' 1 sec Num_Char = GetQueueCount(CByteArray(Inbuf(1, port6).DataAddress)) Success = false Call Get_String_40(Port6,Rx_Str,Num_Char,TimeOutS, Success) If (Success) then 'No Timeout If Rx_Str = "" Then debug.print "Null String Returned!" End if SD_Fgetc_MCU = Rx_Str ELSE 'Timeout Failure SD_Fgetc_MCU = "" debug.print "File Get Char TimeOut!" END IF End Function '========================================================================================= Private Function SD_Fgets_MCU() as string '0 - 255 bytes max 'Read 1 line of characters terminated by a CR LF from a file. 'Fopen command must be used before using Fgets command. 'Be sure to close the file after use. 'My input buffer queue has 40 chars max! 'Filenames 8 chars MAX e.g 8.3 format (xxxxxxxx.xxx) <----- look out! Dim Tx_Str as string Dim Rx_str as string Dim TimeOutS as single Dim Num_Char as integer Dim Success as boolean Dim Temp as string ClearInputBuffer(port6) ClearOutputBuffer(port6) Tx_Str = "fgets" Call PutLine(port6,Tx_Str) Call delay(1.0) Rx_str = "" TimeOutS = 1.0 ' 1 sec Num_Char = GetQueueCount(CByteArray(Inbuf(1, port6).DataAddress)) Success = false Call Get_String_40(Port6,Rx_Str,Num_Char,TimeOutS, Success) If (Success) then 'No Timeout If rx_str = "" Then debug.print "Null String Returned!" SD_Fgets_MCU = "" End if SD_Fgets_MCU = rx_str ELSE 'Timeout Failure debug.print "File Get String TimeOut!" SD_Fgets_MCU = "" END IF End Function '=========================================================================================