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

A forum devoted to the discussion of all topics having to do with scripting and other advanced programming using iX Developer.
Post Reply
wlederer
Posts: 175
Joined: Fri Jan 27, 2012 4:20 am

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

Post 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

mark.monroe
Posts: 824
Joined: Tue Mar 13, 2012 9:53 am

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

Post 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
Best Regards,
Mark Monroe

Beijer Electronics, Inc. | Applications Engineer

User avatar
Ron L.
Posts: 214
Joined: Fri Jul 15, 2011 3:21 pm

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

Post 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.
Best Regards,

Beijer Electronics, Inc.
Ron Lloyd | Applications Engineer

Patrick Hall
Posts: 22
Joined: Fri May 25, 2012 7:44 am
Location: Charlotte, NC. USA

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

Post 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.
Best Regards,
Patrick Hall

wlederer
Posts: 175
Joined: Fri Jan 27, 2012 4:20 am

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

Post 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

mark.monroe
Posts: 824
Joined: Tue Mar 13, 2012 9:53 am

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

Post 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.
Best Regards,
Mark Monroe

Beijer Electronics, Inc. | Applications Engineer

wlederer
Posts: 175
Joined: Fri Jan 27, 2012 4:20 am

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

Post 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

User avatar
Ron L.
Posts: 214
Joined: Fri Jul 15, 2011 3:21 pm

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

Post 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.
Best Regards,

Beijer Electronics, Inc.
Ron Lloyd | Applications Engineer

JohnCZ
Posts: 73
Joined: Wed Jun 27, 2012 1:17 am
Location: CZ
Contact:

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

Post 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

mark.monroe
Posts: 824
Joined: Tue Mar 13, 2012 9:53 am

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

Post 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.
Best Regards,
Mark Monroe

Beijer Electronics, Inc. | Applications Engineer

Post Reply