Page 1 of 1

How to check if USB or SD are actually inserted and OK

Posted: Wed Sep 12, 2012 2:59 am
by wlederer
Before writing something into external memory we, probably, must be sure that they are inserted and ready. How to do it by script?
Regards, Waldemar

Re: How to check if USB or SD are actually inserted and OK

Posted: Wed Sep 12, 2012 8:35 am
by mark.monroe
You can try and write your data to the USB card, and then use a try catch block to catch the error that will occur if the USB card is not inserted.

C# try-catch

Re: How to check if USB or SD are actually inserted and OK

Posted: Wed Sep 12, 2012 8:42 am
by Ron L.
You can also use the Directory.Exists method.
http://msdn.microsoft.com/en-us/library ... s.90).aspx

But even if you use this method, you should still have a try catch block when you open the file.

Re: How to check if USB or SD are actually inserted and OK

Posted: Wed Sep 12, 2012 5:26 pm
by Patrick Hall
The Directory.Exists Method is the one I use. Make sure you add a using to the script code.

Code: Select all

using System.IO;

Code: Select all

bool UsbPresent = Directory.Exists("\\Hard Disk");
bool SDCardPresent = Directory.Exists("\\Storage Card");
Or possibly as public properties in the Tags script, or in a custom ScriptModule for global access if you like.

Code: Select all

public bool UsbPresent {
    get {
        return Directory.Exists("\\Hard Disk");
    }
}
public bool SDCardPresent {
    get {
        return Directory.Exists("\\Storage Card");
    }
}
I have not tested to see what directories are created (if any) on devices other than WinCE HMI's I know that those directories do not exist when you Simulate or Run HMI code on the developing PC so it might be worth your effort to include a check to see if the executing device is actually a WinCE target.

Code: Select all

public bool IsWinCE {
    get {
        return Environment.OSVersion.Platform == PlatformID.WinCE;
    }
}
And from that substitute paths or drives which are to be checked accordingly.

Re: How to check if USB or SD are actually inserted and OK

Posted: Thu Sep 13, 2012 1:44 am
by wlederer
Thank You very much for the help. I want to write a script which will write to an external memory (SD or USB, depending what is available) the information about finished process.
What is the difference between ("Storage Card")
and ("\\Storage Card")?
Regards, Waldemar

Re: How to check if USB or SD are actually inserted and OK

Posted: Thu Sep 13, 2012 8:50 am
by mark.monroe
You can think of \ as the root directory in Windows CE. The first "\" is required because in C# "\" is a special character and will not be interpreted as a character. So one "\" means escape the next character.

Is you were to print "\\" on a screen using C# you would only see "\" printed on the screen.

Re: How to check if USB or SD are actually inserted and OK

Posted: Fri Sep 14, 2012 6:25 am
by wlederer
Thank You very much for the help.
I tried Directory.Exists(). Works fine. Did not check othe other options. When they are preferable?
Regards, Waldemar

Re: How to check if USB or SD are actually inserted and OK

Posted: Fri Sep 14, 2012 9:05 am
by Ron L.
A try/catch block is your insurance against an attempt to open a file and have it fail. Without the tray/catch block, if you try to access a file that doesn't exist and it fails the application will display a message and close.

Re: How to check if USB or SD are actually inserted and OK

Posted: Tue Nov 13, 2012 2:35 am
by JohnCZ
Hello

i try to gain access to USB or SD through FTP but the projekt folder on SD ,USB have not show me. Could you send me projekt T4A where can I have access on USB and SD through FTP.

Many Thanks
JohnCZ

Re: How to check if USB or SD are actually inserted and OK

Posted: Tue Nov 13, 2012 8:50 am
by mark.monroe
You can not access the USB or SD card through FTP. Only the data in the project files folder is accessible through FTP.