Host Engineering Forum

General Category => Do-more CPUs and Do-more Designer Software => Topic started by: ksasikumar on June 16, 2022, 09:37:47 PM

Title: Removing SD Card
Post by: ksasikumar on June 16, 2022, 09:37:47 PM
I am working on a program that will read setpoints from a file on the PLC's SD Card. I am using $SDCardFS.MediaMounted and $SDCardFS.MediaInstalled to check if the SD card is inserted, and then running a sub-program which parses for the setpoints on the file. My question relates to inserting & removing the SD card while the PLC is running:
1. Will physically removing the SD card cause any issues/crashes in the program?
2. Upon inserting the SD card, does the PLC need to be restarted for it to detect the SD card?

Best case scenario is that a tech would be able to remove the SD card, change the file contents, and re-insert it without the program freezing/crashing.

Also bonus question - is there any difference between $SDCardFS.MediaMounted and $SDCardFS.MediaInstalled? Or any instance when these bits will not have the same status?
Title: Re: Removing SD Card
Post by: franji1 on June 17, 2022, 09:16:38 AM
I am working on a program that will read setpoints from a file on the PLC's SD Card. I am using $SDCardFS.MediaMounted and $SDCardFS.MediaInstalled to check if the SD card is inserted, and then running a sub-program which parses for the setpoints on the file. My question relates to inserting & removing the SD card while the PLC is running:
1. Will physically removing the SD card cause any issues/crashes in the program?
2. Upon inserting the SD card, does the PLC need to be restarted for it to detect the SD card?

Best case scenario is that a tech would be able to remove the SD card, change the file contents, and re-insert it without the program freezing/crashing.

Also bonus question - is there any difference between $SDCardFS.MediaMounted and $SDCardFS.MediaInstalled? Or any instance when these bits will not have the same status?

Regarding the .MediaMounted and .MediaInstalled File System structure bits being different...
As you may know, PC's cache up write to flash.  Not sure if the PLC does, but the media supports that concept.  Hence, you should "dismount" the media before you "remove" it.  I don't think the PLC caches writes, so this is moot, but the point being, is that you CAN use the FILESYSCMD to Dismount (and Mount and Format) SD cards.  Also, those bits will also be different when inserting the card.  It will detect "insertion" immediately, but then it does take some amount of time (less than a second) to "mount" it.

Realize that the operator needs to remove the card when the PLC is NOT writing to the drive.  There is an "access" LED on the SD card slot, but that doesn't mean it won't try to come on shortly before operator tries to remove it.

When does your logic write to the SD card?  Is the operator aware of this behavior and understands when it is SAFE and when it is NOT SAFE to remove the SD card?
Title: Re: Removing SD Card
Post by: franji1 on June 17, 2022, 09:34:19 AM
Also, I forgot this point...

Does your logic leave files open?  Or do they do the OPEN/WRITE/CLOSE ALWAYS?

Note that the FILELOG behavior is exactly that - it internally does OPEN/APPEND/CLOSE every time it does a log, making this instruction GREAT for PLC logging mechanism and operator removal of the media.

HOWEVER, if you are writing your own FILEOPEN, FILEWRITE, FILECLOSE behavior, but you are keeping the file OPEN (i.e. logic that does MULTILE FILEWRITEs over a period of time but with single FILEOPEN/FILECLOSE instructions), you definitely need to provide control to the operator and write logic to CLOSE the file based on that operator request.  After doing a FILECLOSE, you can even do the FILESYSCMD and Dismount to ensure the media can be removed properly.  I think the LED turns RED when it is dismounted, providing feedback to the operator that it is safe to remove the media.  It auto-mounts when you re-insert it.
Title: Re: Removing SD Card
Post by: ksasikumar on June 17, 2022, 09:48:54 AM
Thanks for the information on those bits.

Let me clarify - the PLC never writes To the SD Card, it only ever reads From it. Once the SD card is detected, I use FileCOPY to move the file to Ram. The card is not used/accessed again until its time to remove it and make changes on the file.
I use FileOpen and then parse the file (in RAM) and then  use FileClose when the parsing is completed.
Title: Re: Removing SD Card
Post by: franji1 on June 17, 2022, 10:03:05 AM
Good!
Title: Re: Removing SD Card
Post by: ksasikumar on June 17, 2022, 10:19:18 AM
So to confirm - its safe to remove the SD card as long as i use FILESYSCMD and Dismount in the program. And reinserting it while the program is running is always safe?
Title: Re: Removing SD Card
Post by: franji1 on June 17, 2022, 10:25:35 AM
It's always safe.  Since you make a copy of the file, as long as the ROM LED is not ON (i.e. it's in the middle of the FILEOCOPY), everything should be fine.

No need to dismount.  The PLC hardware will be safe regardless.  The FILECOPY instruction may fail if you catch it in that "pregnant moment".  Does your ladder logic handle that failure to your satisfaction if that is even possible (not sure when your FILECOPY instruction executes relative to the insertion and removal of the media?).
Title: Re: Removing SD Card
Post by: ksasikumar on June 21, 2022, 04:53:14 PM
I appreciate it, thanks!