Build your own SD Card Explorer
Did you find this code helpful?
3 members have voted
People who see the SD Card Explorer in action usually sometimes wish to have the same functionality in their own application.Some of them contact Unitronics in order to get some details on the protocol, or ways that they can do the same, while others fear from the worst: that it's a lot of work.I know very few people that will be happy to write the whole protocol by themselves.The only guy I can think of in that matter is Ash Nielson... I have no idea where he gets his strength and energy from, but the solution for this problem is actually simpler than you think.You don't have to be energetic as Ash in order to write your own SD Card Explorer because the .Net communication driver actually contains all the logic If you don't know what the .Net communication driver, then please refer to the Downloads section on the main website (not the forum) it has some examples + the source code of the .Net communication driver.For those of you who does know how to work with it, I can assume that you are already familiar with the all mighty and magical PLC object.Within the PLC object, hides a small sub class called SD.This class gives all the capabilities of reading and writing files and also listing directories and deleting files.So how do we get started:Lets assume that we have a Serial port called serialLets create a PLC object:PLC plc = PLCFactory.GetPLC(serial, 0);All the code that I will write will be in C#, but the syntax in VB.net is not much different.Now that we have a PLC object called plc, we can access it's SD Class.Please note that SD capabilities are only available to the Enhanced PLCs (V570/V130/V350).Before I give any more code examples, I'll list the Public functions inside the SD Class:public Folder Dir(ProgressStatusChangedDelegate del)Returns a Dir of all Folders and files in the SD (Dirs the Root). ProgressStatusChanged is being used to return the Status, Pecentage and Action of the current Statuspublic Folder Dir(SdFolder sdFolder, string filesExtension, bool scanSubFolders, ProgressStatusChangedDelegate del)Requests Dir Syncrhronously. Accepts SdFolder enum as the path to list, extension for example "*.*", and some other paremeterspublic Folder Dir(string folderName, string filesExtension, bool scanSubFolders, ProgressStatusChangedDelegate del)Requests Dir Syncrhronously. Same as above, but path is string and not enumpublic byte ReadFile(string folderName, string fileName, ProgressStatusChangedDelegate del)Read File Syncrhronously. Reads the whole file from SD and then returns it's content as Byte Arraypublic byte ReadFile(SdFolder sdFolder, string fileName, ProgressStatusChangedDelegate del)Read File Syncrhronously. Reads the whole file from SD and then returns it's content as Byte Array.Gets SdFolder as path instead of stringpublic void ReadFile(string sourceFolderName, string sourceFileName, string targetFolder, bool resumeRead, ProgressStatusChangedDelegate del)Read File Syncrhronously. Reads the file and each chunk that is being read is instantly written into diskpublic void WriteFile(SdFolder sdFolder, string fileName, byte fileContent, ProgressStatusChangedDelegate del)Write File Syncrhronously. Since we only allow writing files into Authorized folders only then this function only accepts SdFolder enum as path and not as stringpublic void DeleteFile(string folderName, string fileName, ProgressStatusChangedDelegate del)Deletes a file... DUHH!!! (Sorry, I had to do that)public void DeleteFile(SdFolder sdFolder, string fileName, ProgressStatusChangedDelegate del)Same as above, but with SdFolder enum as pathAlso consider using:public static string GetSdFolderAsString(SdFolder sdFolder)and the opposite function:public static SdFolder GetSdFolderFromString(string path)Coming up next... Real code. SD Card Explorer Code revealed.Come back to our next episode next week