-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSD.cs
2364 lines (1901 loc) · 83.7 KB
/
SD.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Unitronics.ComDriver.Messages.DataRequest;
using System.Text.RegularExpressions;
using System.IO;
namespace Unitronics.ComDriver
{
public class SD
{
#region public/internal Classes and Strucs
internal struct SdParams
{
public en_FileAccessMode FileAccessMode;
public ushort SeekOrigin;
public int SeekOffset;
public ushort BufferSize;
public bool LastReadWriteChunk;
public string Guid;
public string FileName;
}
public enum SdFolder
{
RootFolder = -1,
ALARMS = 0,
DT = 1,
DT_DT1 = 100,
DT_DT2 = 101,
DT_DT3 = 102,
DT_DT4 = 103,
LOG = 3,
SYSTEM = 4,
USER_APP = 5,
TRENDS = 6,
TRENDS_TRENDS1 = 600,
TRENDS_TRENDS2 = 601,
TRENDS_TRENDS3 = 602,
TRENDS_TRENDS4 = 603,
SDBLOCKS = 9,
EXCEL = 10,
EXCEL_EXCEL1 = 1000,
EXCEL_EXCEL2 = 1001,
EXCEL_EXCEL3 = 1002,
EXCEL_EXCEL4 = 1003,
WEB = 11,
}
internal enum NameType
{
FileNameNoExtension,
FileNameWithExtension,
ExtensionOnly,
Path
}
public class Folder : File
{
private List<Folder> m_Folders;
private List<File> m_Files;
public Folder()
{
m_Folders = new List<Folder>();
m_Files = new List<File>();
}
public List<Folder> Folders
{
get { return m_Folders; }
internal set { m_Folders = value; }
}
public List<File> Files
{
get { return m_Files; }
internal set { m_Files = value; }
}
}
public class File
{
private string m_Name;
private uint m_Size;
private DateTime m_CreationDate;
private DateTime m_LastModifiedDate;
private bool m_ReadOnly;
public File()
{
m_Name = "";
m_Size = 0;
m_CreationDate = new DateTime(1980, 1, 1);
m_LastModifiedDate = new DateTime(1980, 1, 1);
m_ReadOnly = false;
}
public DateTime DateCreated
{
get { return m_CreationDate; }
internal set { m_CreationDate = value; }
}
public DateTime DateModified
{
get { return m_LastModifiedDate; }
internal set { m_LastModifiedDate = value; }
}
public uint Size
{
get { return m_Size; }
internal set { m_Size = value; }
}
public string Name
{
get { return m_Name; }
internal set { m_Name = value; }
}
public bool ReadOnly
{
get { return m_ReadOnly; }
internal set { m_ReadOnly = value; }
}
}
#endregion
#region locals
private PLC m_Plc;
private PlcVersion m_Version;
bool m_BreakFlag = false;
private int m_BreakFlagCount;
private Object objectLocker = new Object();
private static char[] forbiddenChars = new char[]
{
'"', '*', '/', ':', '<', '>',
'?', '\\', '|', ' ', '\x0000', '\x0001', '\x0002', '\x0003', '\x0004', '\x0005', '\x0006', '\x0007',
'\x0008', '\x0009', '\x000a', '\x000b', '\x000c', '\x000d', '\x000e', '\x000f', '\x0010', '\x0011',
'\x0012', '\x0013', '\x0014', '\x0015', '\x0016', '\x0017', '\x0018', '\x0019', '\x001a', '\x001b',
'\x001c', '\x001e', '\x001e', '\x001f', '\x007f', '.'
};
internal bool BreakFlag
{
get { return m_BreakFlag; }
set { m_BreakFlag = value; }
}
internal int BreakFlagCount
{
get { return m_BreakFlagCount; }
set
{
m_BreakFlagCount = value;
if (m_BreakFlagCount <= 0)
{
m_BreakFlagCount = 0;
m_BreakFlag = false;
}
}
}
#endregion
#region enums
/// <summary>
/// "r" Open a file for reading. The file must exist.
/// "r+" Open a file for reading and writing. The file must exist.
/// "w" Create an empty file for writing. If a file with the same name already exists its content is erased.
/// "w+" Create an empty file for writing and reading. If a file with the same name already exists its content is erased before it is opened.
/// "a" Append to a file. Writing operations append data at the end of the file. The file is created if it doesn't exist.
/// "a+" Open a file for reading and appending. All writing operations are done at the end of the file protecting the previous
/// content from being overwritten. You can reposition (fseek) the pointer to anywhere in the file for reading, but
/// writing operations will move back to the end of file. The file is created if it doesn't exist.
///
/// </summary>
internal enum en_FileAccessMode
{
R = 0,
R_Plus = 1,
W = 2,
W_Plus = 3,
A = 4,
A_Plus = 5,
}
internal const int ACCESS_SD_COMMANDCODE = 0x2A;
internal enum en_PComSubCommand
{
DirInit = 0x87,
Dir = 0x1,
ReadFileInit = 0x82,
ReadFile = 0x2,
WriteFileInit = 0x83,
WriteFile = 0x3,
WriteFileStatus = 0x4,
CloseSdChannel = 0x5,
DeleteFileInit = 0x86,
DeleteFile = 0x6,
DirGetStatus = 0x7,
DirGetChunk = 0x8,
}
internal enum en_SdStatus
{
Error = 0xFF,
Ack = 0x80,
Busy = 0x40
}
internal enum en_SdErrorCode
{
UnknownError = 0,
TriggerError = 1,
SdDriverError = 2,
BufferOverflow = 3,
MsgKeyError = 4,
SdLocked = 5,
Conflict = 6,
VersionMismatch = 7,
PathLength = 8,
FileOpenedByOtherClient = 9,
NoError = 0xff,
}
#endregion
internal PLC Plc
{
get { return m_Plc; }
}
internal SD(PLC plc, PlcVersion version)
{
// By making the constructor Internal, I prevent the user from creating an SD class
m_Plc = plc;
m_Version = version;
}
#region public methods
/// <summary>
/// Returns a Dir of all Folders and files in the SD (Dirs the Root)
/// </summary>
public Folder Dir(ProgressStatusChangedDelegate del)
{
bool sdChannelLockInitiated = false;
string guid = "";
lock (objectLocker)
{
m_BreakFlagCount++;
}
try
{
Folder folder = new Folder();
folder.Name = "";
dirAllSubFolders("", "*.*", folder, ref sdChannelLockInitiated, out guid, del);
return folder;
}
finally
{
try
{
if (sdChannelLockInitiated)
{
closeSdChannel(guid);
}
}
finally
{
lock (objectLocker)
{
m_BreakFlagCount--;
}
}
}
}
/// <summary>
/// Requests Dir Syncrhronously
/// </summary>
public Folder Dir(SdFolder sdFolder, string filesExtension, bool scanSubFolders,
ProgressStatusChangedDelegate del)
{
string path = GetSdFolderAsString(sdFolder);
bool sdChannelLockInitiated = false;
string guid = "";
lock (objectLocker)
{
m_BreakFlagCount++;
}
try
{
if (!scanSubFolders)
{
return dir(path, filesExtension, ref sdChannelLockInitiated, out guid, del);
}
else
{
string[] dirs = path.Split('\\');
Folder folder = new Folder();
folder.Name = dirs[dirs.Length - 1];
dirAllSubFolders(path, filesExtension, folder, ref sdChannelLockInitiated, out guid, del);
return folder;
}
}
finally
{
try
{
if (sdChannelLockInitiated)
{
closeSdChannel(guid);
}
}
finally
{
lock (objectLocker)
{
m_BreakFlagCount--;
}
}
}
}
/// <summary>
/// Requests Dir Syncrhronously
/// </summary>
public Folder Dir(string folderName, string filesExtension, bool scanSubFolders,
ProgressStatusChangedDelegate del)
{
bool sdChannelLockInitiated = false;
string guid = "";
lock (objectLocker)
{
m_BreakFlagCount++;
}
try
{
if (!scanSubFolders)
{
return dir(folderName, filesExtension, ref sdChannelLockInitiated, out guid, del);
}
else
{
string[] dirs = folderName.Split('\\');
Folder folder = new Folder();
folder.Name = dirs[dirs.Length - 1];
dirAllSubFolders(folderName, filesExtension, folder, ref sdChannelLockInitiated, out guid, del);
return folder;
}
}
finally
{
try
{
if (sdChannelLockInitiated)
{
closeSdChannel(guid);
}
}
finally
{
lock (objectLocker)
{
m_BreakFlagCount--;
}
}
}
}
/// <summary>
/// Read File Syncrhronously
/// </summary>
public byte[] ReadFile(string folderName, string fileName, ProgressStatusChangedDelegate del)
{
bool sdChannelLockInitiated = false;
string guid = "";
lock (objectLocker)
{
m_BreakFlagCount++;
}
try
{
return readFile(folderName, fileName, ref sdChannelLockInitiated, out guid, del);
}
finally
{
try
{
if (sdChannelLockInitiated)
{
closeSdChannel(guid);
}
}
finally
{
lock (objectLocker)
{
m_BreakFlagCount--;
}
}
}
}
public void ReadFile(string sourceFolderName, string sourceFileName, string targetFolder, bool resumeRead,
ProgressStatusChangedDelegate del)
{
bool sdChannelLockInitiated = false;
string guid = "";
lock (objectLocker)
{
m_BreakFlagCount++;
}
try
{
readFile(sourceFolderName, sourceFileName, targetFolder, resumeRead, ref sdChannelLockInitiated,
out guid, del);
}
finally
{
try
{
if (sdChannelLockInitiated)
{
closeSdChannel(guid);
}
}
finally
{
lock (objectLocker)
{
m_BreakFlagCount--;
}
}
}
}
/// <summary>
/// Read File Syncrhronously
/// </summary>
public byte[] ReadFile(SdFolder sdFolder, string fileName, ProgressStatusChangedDelegate del)
{
string folderName = GetSdFolderAsString(sdFolder);
bool sdChannelLockInitiated = false;
string guid = "";
lock (objectLocker)
{
m_BreakFlagCount++;
}
try
{
return readFile(folderName, fileName, ref sdChannelLockInitiated, out guid, del);
}
finally
{
try
{
if (sdChannelLockInitiated)
{
closeSdChannel(guid);
}
}
finally
{
lock (objectLocker)
{
m_BreakFlagCount--;
}
}
}
}
/// <summary>
/// Write File Syncrhronously
/// </summary>
public void WriteFile(SdFolder sdFolder, string fileName, byte[] fileContent, ProgressStatusChangedDelegate del)
{
string folderName = GetSdFolderAsString(sdFolder);
bool sdChannelLockInitiated = false;
string guid = "";
lock (objectLocker)
{
m_BreakFlagCount++;
}
try
{
writeFile(folderName, fileName, fileContent, ref sdChannelLockInitiated, out guid, del);
}
finally
{
lock (objectLocker)
{
m_BreakFlagCount--;
}
if (sdChannelLockInitiated)
{
closeSdChannel(guid);
}
}
}
/// <summary>
/// Delete File Syncrhronously
/// </summary>
public void DeleteFile(string folderName, string fileName, ProgressStatusChangedDelegate del)
{
bool sdChannelLockInitiated = false;
string guid = "";
lock (objectLocker)
{
m_BreakFlagCount++;
}
try
{
deleteFile(folderName, fileName, ref sdChannelLockInitiated, out guid, del);
}
finally
{
try
{
if (sdChannelLockInitiated)
{
closeSdChannel(guid);
}
}
finally
{
lock (objectLocker)
{
m_BreakFlagCount--;
}
}
}
}
public void DeleteFile(SdFolder sdFolder, string fileName, ProgressStatusChangedDelegate del)
{
string folderName = GetSdFolderAsString(sdFolder);
bool sdChannelLockInitiated = false;
string guid = "";
lock (objectLocker)
{
m_BreakFlagCount++;
}
try
{
deleteFile(folderName, fileName, ref sdChannelLockInitiated, out guid, del);
}
finally
{
try
{
if (sdChannelLockInitiated)
{
closeSdChannel(guid);
}
}
finally
{
lock (objectLocker)
{
m_BreakFlagCount--;
}
}
}
}
public static string GetSdFolderAsString(SdFolder sdFolder)
{
switch (sdFolder)
{
case SdFolder.ALARMS:
return "ALARMS";
case SdFolder.DT:
return "DT";
case SdFolder.DT_DT1:
return @"DT\DT1";
case SdFolder.DT_DT2:
return @"DT\DT2";
case SdFolder.DT_DT3:
return @"DT\DT3";
case SdFolder.DT_DT4:
return @"DT\DT4";
case SdFolder.LOG:
return "LOG";
case SdFolder.EXCEL:
return "EXCEL";
case SdFolder.EXCEL_EXCEL1:
return @"EXCEL\EXCEL1";
case SdFolder.EXCEL_EXCEL2:
return @"EXCEL\EXCEL2";
case SdFolder.EXCEL_EXCEL3:
return @"EXCEL\EXCEL3";
case SdFolder.EXCEL_EXCEL4:
return @"EXCEL\EXCEL4";
case SdFolder.SDBLOCKS:
return "SDBLOCKS";
case SdFolder.SYSTEM:
return "SYSTEM";
case SdFolder.TRENDS:
return "TRENDS";
case SdFolder.TRENDS_TRENDS1:
return @"TRENDS\TRENDS1";
case SdFolder.TRENDS_TRENDS2:
return @"TRENDS\TRENDS2";
case SdFolder.TRENDS_TRENDS3:
return @"TRENDS\TRENDS3";
case SdFolder.TRENDS_TRENDS4:
return @"TRENDS\TRENDS4";
case SdFolder.USER_APP:
return "USER_APP";
case SdFolder.WEB:
return "WEB";
default:
return "";
}
}
public static SdFolder GetSdFolderFromString(string path)
{
switch (path.ToUpper())
{
case "ALARMS":
return SdFolder.ALARMS;
case "DT":
return SdFolder.DT;
case @"DT\DT1":
return SdFolder.DT_DT1;
case @"DT\DT2":
return SdFolder.DT_DT2;
case @"DT\DT3":
return SdFolder.DT_DT3;
case @"DT\DT4":
return SdFolder.DT_DT4;
case "LOG":
return SdFolder.LOG;
case "EXCEL":
return SdFolder.EXCEL;
case @"EXCEL\EXCEL1":
return SdFolder.EXCEL_EXCEL1;
case @"EXCEL\EXCEL2":
return SdFolder.EXCEL_EXCEL2;
case @"EXCEL\EXCEL3":
return SdFolder.EXCEL_EXCEL3;
case @"EXCEL\EXCEL4":
return SdFolder.EXCEL_EXCEL4;
case @"SDBLOCKS":
return SdFolder.SDBLOCKS;
case @"SYSTEM":
return SdFolder.SYSTEM;
case @"TRENDS":
return SdFolder.TRENDS;
case @"TRENDS\TRENDS1":
return SdFolder.TRENDS_TRENDS1;
case @"TRENDS\TRENDS2":
return SdFolder.TRENDS_TRENDS2;
case @"TRENDS\TRENDS3":
return SdFolder.TRENDS_TRENDS3;
case @"TRENDS\TRENDS4":
return SdFolder.TRENDS_TRENDS4;
case @"USER_APP":
return SdFolder.USER_APP;
case @"WEB":
return SdFolder.WEB;
default:
return SdFolder.RootFolder;
}
}
#endregion
#region private methods
private void closeSdChannel(string guid)
{
en_SdStatus sdStatus;
en_SdErrorCode sdErrorCode;
PLC plc = PLCFactory.GetPLC(m_Plc.PLCChannel, m_Plc.UnitId);
SdParams sdParams = getInitializedSdParamsObject();
sdParams.Guid = guid;
BinaryRequest br = new BinaryRequest()
{
CommandCode = ACCESS_SD_COMMANDCODE,
SubCommand = (int) en_PComSubCommand.CloseSdChannel,
Address = 0,
ElementsCount = 0,
MessageKey = 0,
OutgoingBuffer = getSdParamsAsByteArray(sdParams),
IsInternal = true,
};
ReadWriteRequest[] rw = new ReadWriteRequest[] {br};
plc.ReadWrite(ref rw);
br.MessageKey = (br.MessageKey + 1) % 256;
sdStatus = (en_SdStatus) br.IncomingBuffer[0];
sdErrorCode = (en_SdErrorCode) br.IncomingBuffer[1];
while ((sdStatus == en_SdStatus.Error) || (sdStatus == en_SdStatus.Busy))
{
if (sdStatus == en_SdStatus.Error)
{
if (sdErrorCode != en_SdErrorCode.NoError)
{
throwSdException(sdErrorCode);
}
}
br.SubCommand = (int) en_PComSubCommand.CloseSdChannel;
plc.ReadWrite(ref rw);
br.MessageKey = (br.MessageKey + 1) % 256;
sdStatus = (en_SdStatus) br.IncomingBuffer[0];
sdErrorCode = (en_SdErrorCode) br.IncomingBuffer[1];
}
}
private void dirAllSubFolders(string path, string filesExtension, Folder folder,
ref bool sdChannelLockInitiated, out string guid, ProgressStatusChangedDelegate del)
{
Folder folderContent = dir(path, filesExtension, ref sdChannelLockInitiated, out guid, del);
foreach (File file in folderContent.Files)
{
folder.Files.Add(file);
}
foreach (Folder subFolder in folderContent.Folders)
{
folder.Folders.Add(subFolder);
if (path != "")
{
dirAllSubFolders(path + "\\" + subFolder.Name, filesExtension, subFolder,
ref sdChannelLockInitiated, out guid, del);
}
else
{
dirAllSubFolders(subFolder.Name, filesExtension, subFolder, ref sdChannelLockInitiated, out guid,
del);
}
}
}
private Folder dir(string path, string filesExtension, ref bool sdChannelLockInitiated, out string guid,
ProgressStatusChangedDelegate del)
{
ushort chunkSize = (ushort) m_Version.PlcBuffer;
chunkSize = (ushort) (chunkSize - 20); // (Reduced size to prevent messages from not fitting into buffer)
Folder result = new Folder();
int messageKey = 0;
// We don't care about chunk size bigger than 512 bytes since we read from Ram and not from SD.
chunkSize -= (ushort) (chunkSize % 2); // Make the chunk size an even number
string extension = filesExtension.Replace("*.", "");
extension = extension.Replace(".", "");
if (path.Length > 0)
{
if (path.Substring(path.Length - 1) == "\\")
path = path.Substring(0, path.Length - 1);
}
checkNameLegality(path, NameType.Path);
checkNameLegality(extension, NameType.ExtensionOnly);
SdParams sdParams = getInitializedSdParamsObject();
guid = sdParams.Guid;
if (path != "")
{
sdParams.FileName = path + @"\" + "*." + extension;
}
else
{
sdParams.FileName = "*." + extension;
}
sdParams.FileAccessMode = en_FileAccessMode.R;
sdParams.BufferSize = 2048; // 2KB for 64 files max
en_SdStatus sdStatus;
en_SdErrorCode sdErrorCode;
BinaryRequest br = new BinaryRequest()
{
CommandCode = ACCESS_SD_COMMANDCODE,
SubCommand = (int) en_PComSubCommand.DirInit,
MessageKey = messageKey,
OutgoingBuffer = getSdParamsAsByteArray(sdParams),
IsInternal = true,
};
ReadWriteRequest[] rw = new ReadWriteRequest[] {br};
if (this.BreakFlag)
throw new ComDriveExceptions("Request aborted by user",
ComDriveExceptions.ComDriveException.AbortedByUser);
m_Plc.ReadWrite(ref rw);
sdStatus = (en_SdStatus) br.IncomingBuffer[0];
sdErrorCode = (en_SdErrorCode) br.IncomingBuffer[1];
RequestProgress requestProgress = new RequestProgress();
requestProgress.Minimum = 0;
requestProgress.Maximum = 100;
requestProgress.NotificationType = RequestProgress.en_NotificationType.SetMinMax;
requestProgress.Text = "";
requestProgress.Value = 0;
while ((sdStatus == en_SdStatus.Error) && (sdErrorCode == en_SdErrorCode.SdLocked))
{
// if Sd channel is locked then message key is not incremented
requestProgress.NotificationType = RequestProgress.en_NotificationType.ProgressChanged;
requestProgress.Text = "SD Card is Locked by another process... Please wait.";
requestProgress.Value = 0;
if (del != null)
del(requestProgress);
if (this.BreakFlag)
throw new ComDriveExceptions("Request aborted by user",
ComDriveExceptions.ComDriveException.AbortedByUser);
m_Plc.ReadWrite(ref rw);
sdStatus = (en_SdStatus) br.IncomingBuffer[0];
sdErrorCode = (en_SdErrorCode) br.IncomingBuffer[1];
}
br.MessageKey = (br.MessageKey + 1) % 256;
messageKey = br.MessageKey;
sdChannelLockInitiated = true;
br.SubCommand = (int) en_PComSubCommand.DirGetStatus;
if (this.BreakFlag)
throw new ComDriveExceptions("Request aborted by user",
ComDriveExceptions.ComDriveException.AbortedByUser);
m_Plc.ReadWrite(ref rw);
br.MessageKey = (br.MessageKey + 1) % 256;
messageKey = br.MessageKey;
sdStatus = (en_SdStatus) br.IncomingBuffer[0];
sdErrorCode = (en_SdErrorCode) br.IncomingBuffer[1];
while ((sdStatus == en_SdStatus.Error) || (sdStatus == en_SdStatus.Busy))
{
if (sdStatus == en_SdStatus.Error)
{
if (sdErrorCode != en_SdErrorCode.NoError)
{
throwSdException(sdErrorCode);
}
}
br.SubCommand = (int) en_PComSubCommand.DirGetStatus;
if (this.BreakFlag)
throw new ComDriveExceptions("Request aborted by user",
ComDriveExceptions.ComDriveException.AbortedByUser);
m_Plc.ReadWrite(ref rw);
br.MessageKey = (br.MessageKey + 1) % 256;
messageKey = br.MessageKey;
sdStatus = (en_SdStatus) br.IncomingBuffer[0];
sdErrorCode = (en_SdErrorCode) br.IncomingBuffer[1];
}
int relDataSize = BitConverter.ToInt32(br.IncomingBuffer, 2);
byte[] binData = new byte[relDataSize];
int totalTransfered = 0;
requestProgress.Minimum = 0;
requestProgress.Maximum = relDataSize;
requestProgress.NotificationType = RequestProgress.en_NotificationType.SetMinMax;
requestProgress.Text = "Listing Files in Directory: " + sdParams.FileName;
requestProgress.Value = 0;
if (del != null)
del(requestProgress);
requestProgress.Value = 0;
requestProgress.NotificationType = RequestProgress.en_NotificationType.ProgressChanged;
if (del != null)
del(requestProgress);
while (totalTransfered < relDataSize)
{
sdParams.BufferSize = chunkSize;
br.SubCommand = (int) en_PComSubCommand.DirGetChunk;
if (sdParams.BufferSize + totalTransfered > relDataSize)
{
sdParams.BufferSize = (ushort) (relDataSize - totalTransfered);
}
br.Address = totalTransfered;
br.ChunkSizeAlignment = 2;
br.ElementsCount = sdParams.BufferSize;
if (this.BreakFlag)
throw new ComDriveExceptions("Request aborted by user",
ComDriveExceptions.ComDriveException.AbortedByUser);
m_Plc.ReadWrite(ref rw);
br.MessageKey = (br.MessageKey + 1) % 256;
messageKey = br.MessageKey;
sdStatus = (en_SdStatus) br.IncomingBuffer[0];
sdErrorCode = (en_SdErrorCode) br.IncomingBuffer[1];
Array.Copy(br.IncomingBuffer, 2, binData, totalTransfered, br.IncomingBuffer.Length - 2);
totalTransfered += br.IncomingBuffer.Length - 2;
requestProgress.Value = totalTransfered;
if (del != null)