Hmmm. I'm testing it on an embedded windows PLC so I changed /media to /UsbStorage and changed it so that DirEntry.sEntry checks for 'System Volume Information' and it correctly detects whether there is a USB drive or not.
However it seems to get stuck sometimes with DirOpen and doesn't reset if it hits an error.
Last edit: Morberis 2020-09-25
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
What Error is indicated? eErrorOpen should show some clear text.
I think in principle is better to program a small state machine e.g. with case function.
Than it's better to handle errors and so on.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Inside Linux yes. You can see it with lsusb command.
Inside CoDeSys I don't think that there is implemented something like this because this depends on the device.
May your supplier of your sps has already a library for that.
At the moment I'm trying to program code with "lsusb" to detect usb stick and sd-card.
When I'm finished, I can share it if you want. (I think with lsusb is the safe way)
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I tested the following with a Linux-System. I used not lsusb but lsblk because I also want to see my sd-card.
If you need you can additionally include then an error handling. May you can use this.
codeFUNCTION_BLOCKDetectUSB_SD_31VAR_INPUTxEnable: BOOL;tWaitTime:TIME:=T#1S;END_VARVAR_OUTPUTxUSBisMounted: BOOL;xSDCardMounted: BOOL;sFilePathUSB:STRING;sFilePathSD:STRING;END_VARVARscmd:STRING:='lsusb | grep "Flash Drive"'; //for sd-Card = lsblk | grep "/media/sd" | grep -v "sda"sStdOut: STRING(1000);Result: SysProcess_Implementation.RTS_IEC_RESULT;iFindUSB: INT;xError: BOOL;xWait:BOOL;TP_Wait:TP;iState:INT:=10;iFindtemp1: INT;iFindtemp2: INT;iFindUsbPath: INT;iFindSD: INT;iRetry: INT;END_VAR//IfDetectionisfinished, waittillnewdetectionstartsTP_Wait(IN:=xWait, PT:=tWaitTime);xWait:=FALSE;IFxEnableANDNOTTP_Wait.QTHENCASEiStateOF10: //CmdforUSB-Stickmounted?
scmd:='lsblk | grep "/media/sda" ; echo OK';(*'lsusb | grep "Flash Drive" ; echo OK';*) //echo ok must be done because when no string is at the output, this function get an errorSysProcess.SysProcessExecuteCommand2(pszCommand:=scmd, pszStdOut:=sStdOut, udiStdOutLen:=SIZEOF(sStdOut), pResult:=ADR(Result));IFResult=0THENiState:=20;xError:=FALSE;ELSExError:=TRUE;sStdOut:='';iState:=10;iRetry:=iRetry+1;END_IFIFiRetry>10THENxError:=TRUE;iState:=999;END_IF20: //Checkreturnstring. Is"/media/sda"insidethestring? Min. 1USBdevicemounted?
IFsStdOut<>''THENiFindUSB:=FIND(str1:=sStdOut, STR2:='/media/sda');(*iFindUSB:=FIND(str1:=sStdOut, STR2:='Flash Drive');*)IFiFindUSB<>0THENxUSBisMounted:=TRUE;iState:=30;ELSExUSBisMounted:=FALSE;iState:=30;END_IFELSExUSBisMounted:=FALSE;iState:=30;END_IF30: //CheckpathofUSB-Stickscmd:='lsblk | grep "/media/sda" ; echo OK';SysProcess.SysProcessExecuteCommand2(pszCommand:=scmd, pszStdOut:=sStdOut, udiStdOutLen:=SIZEOF(sStdOut), pResult:=ADR(Result));IFResult=0THEN//CheckifUSBismountedassdaorsda1orsda2iFindUsbPath:=FIND(STR1:=sStdOut, STR2:='/media/');iFindtemp1:=FIND(STR1:=sStdOut, STR2:='sda1');iFindtemp2:=FIND(STR1:=sStdOut, STR2:='sda2');IF(iFindtemp1<>0)OR(iFindtemp2<>0)THENiFindUsbPath:=FIND(STR1:=sStdOut, STR2:='/media/');//saveUSB-PathsFilePathUSB:=MID(STR:=sStdOut, LEN:=11, POS:=iFindUsbPath);iState:=40;ELSE//saveUSB-PathsFilePathUSB:=MID(STR:=sStdOut, LEN:=10, POS:=iFindUsbPath);iState:=40;END_IFELSExError:=TRUE;sStdOut:='';iState:=10;iRetry:=iRetry+1;END_IFIFiRetry>10THENxError:=TRUE;iState:=999;END_IF40: //cmdforSD-Cardmountedscmd:='lsblk | grep "/media/sd" | grep -v "sda" ; echo OK';SysProcess.SysProcessExecuteCommand2(pszCommand:=scmd, pszStdOut:=sStdOut, udiStdOutLen:=SIZEOF(sStdOut), pResult:=ADR(Result));IFResult=0THENiState:=50;xError:=FALSE;ELSExError:=TRUE;sStdOut:='';iState:=10;iRetry:=iRetry+1;END_IFIFiRetry>10THENxError:=TRUE;iState:=999;END_IF50: //Checkreturnstring. Is"/media/sd"insidethestring? SD-cardmounted?
IFsStdOut<>''THENiFindSD:=FIND(str1:=sStdOut, STR2:='/media/sd');IFiFindSD<>0THENxSDCardMounted:=TRUE;iState:=60;ELSExSDCardMounted:=FALSE;iState:=60;END_IFELSExSDCardMounted:=FALSE;iState:=60;END_IF60: //CheckpathofSD-Card//Pathisallways/medaia/sdIFxSDCardMountedTHENsFilePathSD:='/media/sd';ELSEsFilePathSD:='';END_IFiState:=100;100: //EndiState:=10;xWait:=TRUE;iRetry:=0;999: //ErroratwritingcommandtolinuxconsolexWait:=TRUE;iState:=10;xSDCardMounted:=FALSE;xUSBisMounted:=FALSE;sFilePathSD:='';sFilePathUSB:='';iRetry:=0;END_CASEEND_IF
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Your new code is using command prompt it seems? After talking with tech support I might not have equivalent commands available to me but they're going to get back to me on that.
I'm using an Eaton XV303 and they do have a Library called EA_Service which looks like it might have some things that would work. For instance a Function called 'Service_GetUsbState' but it doesn't actually change when a usb device is present. Just it indicates whether the usb port has been enabled or disabled.
I'll post back here when I have an answer.
Last edit: Morberis 2020-09-29
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
@ Reinier Geers: You Wrote:Thanks. But it looks like it doesn't know lsblk.
Did you try the command directly inside a terminal e.g. Putty? If the command works there you should have a look if inside the CODESYSControl.cfg your command is allowed.
Search at [SysProcess]
Command=AllowAll
or specific command: [SysProcess]
Command.0=lsblk
Command.1=echo
@ Morberis:
I think wrong parameter could be an error because of a wrong path. Invalid Parameter should then an error of DirList. DirOpen only creates an handle and DirList use this handle. But if you have a wrong path also the handle will be not valid.
In my example above I added no error handling. You can add it on your own. May do the whole program with a state machine to be sure in which state you got the error.
BTW I tried this with DirOpen and DirList on my SD-Card.. after a while it seems that the card is corrupted. Then the card needs to be formatted. May the command prompt is the better way.
Hope this will help you.
BR Chris
Last edit: Chris.O 2020-09-30
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
1: // check for USB Stick
udiDirHandle := SysDirOpen(szDir:= sUSB_STICK, szDirEntry:= sDirEntry, diMaxDirEntry:= SIZEOF(sDirEntry),
pDirInfo:= ADR(stDirInfo), pResult:= Fehler_Dir);
b_Pugged:= udiDirHandle <> 16#FFFFFFFF; //Fehler_Dir = 0;
udiCloseError := SysDirClose(hDir:= udiDirHandle);
hmh I'm not sure if SysDirOpen function create his own file when the file is not there...
Try CAA FIle library... I tried.. .it works!
I programmed a quick and dirty solution for SD-Card and USB-Stick....may ther some bugs inside but it works
Hmmm. I'm testing it on an embedded windows PLC so I changed /media to /UsbStorage and changed it so that DirEntry.sEntry checks for 'System Volume Information' and it correctly detects whether there is a USB drive or not.
However it seems to get stuck sometimes with DirOpen and doesn't reset if it hits an error.
Last edit: Morberis 2020-09-25
What Error is indicated? eErrorOpen should show some clear text.
I think in principle is better to program a small state machine e.g. with case function.
Than it's better to handle errors and so on.
Isn't there a normal way to detect an USB stick ? If would expect that the hardware the stick detects and set a status .
Inside Linux yes. You can see it with lsusb command.
Inside CoDeSys I don't think that there is implemented something like this because this depends on the device.
May your supplier of your sps has already a library for that.
At the moment I'm trying to program code with "lsusb" to detect usb stick and sd-card.
When I'm finished, I can share it if you want. (I think with lsusb is the safe way)
I tested the following with a Linux-System. I used not lsusb but lsblk because I also want to see my sd-card.
If you need you can additionally include then an error handling. May you can use this.
Thanks. But it looks like it doesn't know lsblk.
Sorry I can't believe that I didn't post that info.
I've seen 2 errors
5109 - wrong parameter
5103 - invalid handle
Your new code is using command prompt it seems? After talking with tech support I might not have equivalent commands available to me but they're going to get back to me on that.
I'm using an Eaton XV303 and they do have a Library called EA_Service which looks like it might have some things that would work. For instance a Function called 'Service_GetUsbState' but it doesn't actually change when a usb device is present. Just it indicates whether the usb port has been enabled or disabled.
I'll post back here when I have an answer.
Last edit: Morberis 2020-09-29
@ Reinier Geers: You Wrote:Thanks. But it looks like it doesn't know lsblk.
Did you try the command directly inside a terminal e.g. Putty? If the command works there you should have a look if inside the CODESYSControl.cfg your command is allowed.
Search at
[SysProcess]
Command=AllowAll
or specific command:
[SysProcess]
Command.0=lsblk
Command.1=echo
@ Morberis:
I think wrong parameter could be an error because of a wrong path. Invalid Parameter should then an error of DirList. DirOpen only creates an handle and DirList use this handle. But if you have a wrong path also the handle will be not valid.
In my example above I added no error handling. You can add it on your own. May do the whole program with a state machine to be sure in which state you got the error.
BTW I tried this with DirOpen and DirList on my SD-Card.. after a while it seems that the card is corrupted. Then the card needs to be formatted. May the command prompt is the better way.
Hope this will help you.
BR Chris
Last edit: Chris.O 2020-09-30
Nah the path is definately correct. At least it's correct when the usb drive is inserted.
For now I'm forgoing detection of a USB drive. If the copy operation fails or gets stuck that's all the feedback I need and it works consistently.
If you need detection of USB drive I recommend to do it with the command lsblk (Like Reinier Geers) :-) It seems that it work very well.
I don't really need it thankfully. I do thank you for your help, as well everyone in this thread has been great. I'm just out of time for now.