Advertisement
Guest User

SPS C

a guest
Apr 30th, 2015
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.69 KB | None | 0 0
  1. #include <bur/plctypes.h>
  2. #include <fileio.h>
  3. /***** Variable declaration *****/
  4. _LOCAL BOOL bOK;
  5. _LOCAL USINT byStep, byErrorLevel;
  6. _LOCAL USINT byReadData[300], byWriteData[100];
  7. _LOCAL UINT wStatus, wError;
  8. _LOCAL UDINT dwIdent;
  9. _LOCAL FileOpen_typ FOpen;
  10. _LOCAL FileClose_typ FClose;
  11. _LOCAL FileCreate_typ FCreate;
  12. _LOCAL FileRead_typ FRead;
  13. _LOCAL FileWrite_typ FWrite;
  14. _LOCAL FileDelete_typ FDelete;
  15. _LOCAL STRING device[25][30];
  16. /***** Init part *****/
  17. _INIT void Init(void)
  18. {
  19. int i;
  20. /* Initialize variables */
  21. bOK = 0;
  22. byStep = 1;
  23. byErrorLevel = 0;
  24. /* Initialize read and write data */
  25. for (i = 0; i < 300; i ++)
  26. {
  27.  
  28. byReadData[i] = 0;
  29. }
  30. }
  31.  
  32. /***** Cyclic part *****/
  33. _CYCLIC void Cyclic(void)
  34.  
  35. {
  36. int s,i,j=0;
  37. for(i=0;i<=100;i++)
  38. {
  39. if(byReadData[i]==10){
  40. j=j+1;
  41. s=0;
  42.  
  43. }
  44. else
  45. {
  46. device[j][s]=(STRING)byReadData[i];
  47. s++;
  48.  
  49. }
  50. }
  51.  
  52.  
  53.  
  54.  
  55.  
  56. switch (byStep)
  57. {
  58. case 0: /**** Error step ****/
  59. bOK = 0;
  60. break;
  61. case 1: /**** Try to open existing file ****/
  62. /* Initialize file open structrue */
  63. FOpen.enable = 1;
  64. FOpen.pDevice = (UDINT) "LOGS";
  65. FOpen.pFile = (UDINT) "config.txt";
  66. FOpen.mode = FILE_RW; /* Read and write access */
  67.  
  68. /* Call FUB */
  69. FileOpen(&FOpen);
  70.  
  71. /* Get FUB output information */
  72. dwIdent = FOpen.ident;
  73. wStatus = FOpen.status;
  74. /* Verify status (20708 -> File doesn't exist) */
  75. if (wStatus == 20708)
  76. {
  77. byStep = 3;
  78. }
  79. else if (wStatus == 0)
  80. {
  81. byStep = 4;
  82. }
  83. else if (wStatus != 65535)
  84. {
  85. byErrorLevel = 1;
  86. byStep = 0;
  87. if (wStatus == 20799)
  88. {
  89. wError = FileIoGetSysError();
  90. }
  91. }
  92. break;
  93. case 2: /**** Create file ****/
  94. /* Initialize file create structure */
  95. FCreate.enable = 1;
  96. FCreate.pDevice = (UDINT) "HARDDISK";
  97. FCreate.pFile = (UDINT) "TestFile";
  98. /* Call FUB */
  99. FileCreate(&FCreate);
  100. /* Get output information of FUB */
  101. dwIdent = FCreate.ident;
  102. wStatus = FCreate.status;
  103. /* Verify status */
  104. if (wStatus == 0)
  105. {
  106. byStep = 3;
  107. }
  108. else if (wStatus != 65535)
  109. {
  110. byErrorLevel = 2;
  111. byStep = 0;
  112.  
  113. if (wStatus == 20799)
  114. {
  115. wError = FileIoGetSysError();
  116. }
  117. }
  118.  
  119. break;
  120.  
  121. case 3: /**** Write data to file ****/
  122. /* Initialize file write structure */
  123. FWrite.enable = 1;
  124. FWrite.ident = dwIdent;
  125. FWrite.offset = 0;
  126. FWrite.pSrc = (UDINT) &byWriteData[0];
  127. FWrite.len = sizeof (byWriteData);
  128.  
  129. /* Call FUB */
  130. FileWrite(&FWrite);
  131. /* Get status */
  132. wStatus = FWrite.status;
  133. /* Verify status */
  134. if (wStatus == 0)
  135. {
  136. byStep = 4;
  137. }
  138. else if (wStatus != 65535)
  139. {
  140. byErrorLevel = 3;
  141. byStep = 0;
  142.  
  143. if (wStatus == 20799)
  144. {
  145. wError = FileIoGetSysError();
  146. }
  147. }
  148. break;
  149. case 4: /**** Read data from file ****/
  150. /* Initialize file read structure */
  151. FRead.enable = 1;
  152. FRead.ident = dwIdent;
  153. FRead.offset = 0;
  154. FRead.pDest = (UDINT) &byReadData[0];
  155. FRead.len = sizeof (byReadData);
  156. /* Call FUB */
  157. FileRead(&FRead);
  158. /* Get status */
  159. wStatus = FRead.status;
  160. /* Verify status */
  161. if (wStatus == 0)
  162. {
  163. byStep = 5;
  164. }
  165. else if (wStatus != 65535)
  166. {
  167. byErrorLevel = 4;
  168. byStep = 0;
  169. if (wStatus == 20799)
  170. {
  171. wError = FileIoGetSysError();
  172. }
  173. }
  174.  
  175. break;
  176. case 5: /**** Close file ****/
  177. /* Initialize file close structure */
  178. FClose.enable = 1;
  179. FClose.ident = dwIdent;
  180.  
  181. /* Call FUB */
  182. FileClose(&FClose);
  183.  
  184. /* Get status */
  185. wStatus = FClose.status;
  186.  
  187. /* Verify status */
  188. if (wStatus == 0)
  189. {
  190. byStep = 0;
  191. }
  192. else if (wStatus != 65535)
  193. {
  194. byErrorLevel = 5;
  195. byStep = 0;
  196. if (wStatus == 20799)
  197. {
  198. wError = FileIoGetSysError();
  199. }
  200. }
  201.  
  202. break;
  203. case 6: /**** Delete file ****/
  204. /* Initialize file delete structure */
  205. FDelete.enable = 1;
  206. FDelete.pDevice = (UDINT) "HARDDISK";
  207. FDelete.pName = (UDINT) "TestFile";
  208. /* Call FUB */
  209. FileDelete(&FDelete);
  210. /* Get status */
  211. wStatus = FDelete.status;
  212. /* Verify status */
  213. if (wStatus == 0)
  214. {
  215. bOK = 1;
  216. byStep = 7;
  217. }
  218. else if (wStatus != 65535)
  219. {
  220. byErrorLevel = 6;
  221. byStep = 0;
  222. if (wStatus == 20799)
  223. {
  224. wError = FileIoGetSysError();
  225. }
  226. }
  227. break;
  228. }
  229.  
  230.  
  231. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement