Yao Chunli
2022-06-21 e00d5a1575d26f8fec1be6fa8a844203cd66a24c
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
#pragma once
/************************************************************************************
FPX PLC Í¨Ñ¶Í·Îļþ
Version: V1.02
Date:    2015-06-30
Description;
2015-08-30 Ôö¼ÓÁËÄ£Ä⹦ÄÜ
1.03 2016-05-23
Ôö¼ÓÁÙ½çÇø
 
************************************************************************************/
#include "omp.h"
#include "../SerialCom/SerialCom.hpp"
const unsigned short HexMask[16]=
{
    0x1,0x2,0x4,0x8,0x10,0x20,0x40,0x80,
    0x100,0x200,0x400,0x800,0x1000,0x2000,0x4000,0x8000
};
static inline int xtoi(const char * hexstr, int len=0)
{
    int i,j,k;
    unsigned char ch;
    k=0;j=0;
    int len1=(int)strlen(hexstr);
    if (len>0) len1=len;
    for (i=0;i<len1;i++)
    {
        ch=hexstr[i];
        if (ch>='0'&&ch<='9')
        {
            k=ch-'0';
            j=j*16+k;
            continue;
        }
        if (ch>='A'&&ch<='F')
        {
            k=ch-'A'+10;
            j=j*16+k;
            continue;
        }
        if (ch>='a'&&ch<='f')
        {
            k=ch-'a'+10;
            j=j*16+k;
            continue;
        }
        if (ch==' '||ch=='    ')
        {
            continue;
        }
        break;
    }
    return j;
}
#pragma pack(push)
#pragma pack(2)
struct bitfield16 
{
    short int b0:1;
    short int b1:1;
    short int b2:1;
    short int b3:1;
    short int b4:1;
    short int b5:1;
    short int b6:1;
    short int b7:1;
    short int b8:1;
    short int b9:1;
    short int ba:1;
    short int bb:1;
    short int bc:1;
    short int bd:1;
    short int be:1;
    short int bf:1;
};
#pragma pack()
#pragma pack(pop)
class FPXPLC
{
public:
    int Connected;
    CSerialCom SerialPlc1;
    int Station;
    char buffer1[4000];
    char buffer2[4000];
    int SimFunc;        //Ä£Ä⹦ÄÜ
    int simresult1;        //Ä£Ä⹦ÄÜÊýÖµ
    CStringA LastRecvString;
 
    double SendTimeMin;    //·¢ËÍʱ¼ä×îС
    double SendTimeMax;    //·¢ËÍʱ¼ä×î´ó
    double SendTimeCur;    //·¢ËÍʱ¼äµ±Ç°
    double SendTimeAvg; //·¢ËÍʱ¼äƽ¾ù
 
    double RecvTimeMin; //½ÓÊÕʱ¼ä×îС
    double RecvTimeMax;    //½ÓÊÕʱ¼ä×î´ó
    double RecvTimeCur;    //½ÓÊÕʱ¼äµ±Ç°
    double RecvTimeAvg;    //½ÓÊÕʱ¼äƽ¾ù
 
    double TransTimeMin;    //´«Êäʱ¼ä£¬×îС
    double TransTimeMax;    //´«Êäʱ¼ä×î´ó
    double TransTimeCur;    //´«Êäʱ¼äµ±Ç°
    double TranstimeAvg;    //´«Êäʱ¼äƽ¾ù
 
#pragma pack(push)
#pragma pack(2)
    union uPLCINFO
    {
        struct //stPLCINFO
        {
            char PlcType[2];
            char Version[2];
            char PrgSize[2];
            char RunMode[2];
            char LinkInfo[2];
            char ErrMark[2];
            char SelfTestInfo[4];
        };
        unsigned char bytes[16];
    }InfoBytes;
#pragma pack()
#pragma pack(1)
    struct stPLCINFO
    {
        unsigned char PlcType;
        unsigned char Version;
        unsigned char PrgSize;
        union{
            unsigned char RunMode;
            struct 
            {
                unsigned char Running:1;
                unsigned char Test:1;
                unsigned char BRK:1;
                unsigned char BRKALW:1;
                unsigned char ReFresh:1;
                unsigned char SigleStep:1;
                unsigned char MSG:1;
                unsigned char REM:1;
            }stRunMode;
        };
        unsigned char LinkInfo;
        union{
            unsigned char Errs;
            struct{
                unsigned char SelfTestErr:1;
                unsigned char PowerDown:1;
                unsigned char Fuse:1;
                unsigned char UnitErr:1;
                unsigned char IOChkErr:1;
                unsigned char BetteryErr:1;
                unsigned char BetteryErrKeep:1;
                unsigned char RunErr:1;
            }stErrs;
        };
        unsigned short SelfTestInfo;
    }PlcInfo;
#pragma pack()
#pragma pack(pop)
    volatile bitfield16 * R;        //λ·ÃÎÊ
    volatile short WX[110];        //WX0-WX109;
    volatile short WY[110];        //WY0-WY109;
    volatile short WL[128];        //WL0-WL127;
    volatile short WR[256];        //WR0-WR255;
    volatile short SWR[15];        //WR900-WR915;
    volatile short DT[32768];    //DT0-DT32767;
    volatile short SDT[512];    //DT90000-DT90499;
    volatile short LD[256];        //LD0-LD255;
 
    CCriticalSection myCriticalSection1;        //ÁÙ½çÇø
 
    FPXPLC()
    {
        SimFunc=0;
        R=(bitfield16 *)WR;
        RecvTimeAvg=RecvTimeCur=RecvTimeMin=RecvTimeMax=0;
        SendTimeAvg=SendTimeCur=SendTimeMin=SendTimeMax=0;
        TranstimeAvg=TransTimeCur=TransTimeMin=TransTimeMax=0;
 
    };
    ~FPXPLC(){};
    
    int BCC(const char * value)
    {
        int i,j;
        char k;
        k=0;
        j=(int)strlen(value);
        for (i=0;i<j;i++)
        {
            k^=value[i];
        }
        return k;
    }
 
    int SendPLCData(const char * buffer, int n)
    {
        double time1=GetTimeMs();
        SerialPlc1.Purge();
        int j=SerialPlc1.Send(buffer,n);
        double time2=GetTimeMs();
        double dif=time2-time1;
        if (SendTimeCur==0)
        {
            SendTimeCur=SendTimeMin=SendTimeMax=SendTimeAvg=dif;
        }else
        {
            SendTimeCur=dif;
            if (dif>SendTimeMax) {SendTimeMax=dif;}
            if (dif<SendTimeMin) {SendTimeMin=dif;}
            SendTimeAvg=SendTimeAvg*0.9+dif*0.1;
        }
        return j;
    }
 
    int ReadPLCCom(char * buffer, int n)
    {
        double time1=GetTimeMs();
        int j=SerialPlc1.Read(buffer,n);
        double time2=GetTimeMs();
        double dif=time2-time1;
        if (j>0)
        {
            buffer[j]=0;
            LastRecvString=buffer;
        }
        if (RecvTimeCur==0)
        {
            RecvTimeCur=RecvTimeAvg=RecvTimeMax=RecvTimeMin=dif;
        }else
        {
            RecvTimeCur=dif;
            if (dif>RecvTimeMax) {RecvTimeMax=dif;}
            if (dif<RecvTimeMin) {RecvTimeMin=dif;}
            RecvTimeAvg=RecvTimeAvg*0.9+dif*0.1;
        }
        return j;
    }
    int Connect(int Station2=0)
    {
        int j;
        j=SerialPlc1.Open();
        if (j==1)
        {//    RT(Station2);
            Connected=1;
            RecvTimeAvg=RecvTimeCur=RecvTimeMin=RecvTimeMax=0;
            SendTimeAvg=SendTimeCur=SendTimeMin=SendTimeMax=0;
            TranstimeAvg=TransTimeCur=TransTimeMin=TransTimeMax=0;
        }
        return j;
    }
    int DisConnect()
    {
        if (Connected)
        {
            SerialPlc1.Close();
            Connected=0;
        }
        return 0;
    }
 
//     /*
//     Ö¸Áî ¹¦ÄÜÃèÊö
//*    RCS Read single point of contact information ¶ÁÈ¡µ¥¸ö´¥µãµÄ״̬ÐÅÏ¢        X Y R L T C
//*    WCS Write single point of contact information Ð´Èëµ¥¸ö´¥µãµÄ״̬ÐÅÏ¢          Y R L
//     RCP Read plural point of contact information ¶ÁÈ¡¶à¸ö´¥µãµÄ״̬ÐÅÏ¢
//     WCP Write plural point of contact information Ð´Èë¶à¸ö´¥µãµÄ״̬ÐÅÏ¢
//*    RCC Read word unit of contact information ¶ÁÈ¡×Öµ¥Î»µÄ´¥µãµÄ״̬ÐÅÏ¢        X Y R L T C
//*    WCC Write word unit of contact information Ð´Èë×Öµ¥Î»µÄ´¥µãµÄ״̬ÐÅÏ¢          Y R L
//     SC Preset word unit in contact area Ô¤ÖÃ×Öµ¥Î»µÄ´¥µã
//
//*    RD Read data area ¶ÁÈ¡Êý¾Ý¼Ä´æÆ÷Öµ    D L F
//*    WD Write data area Ð´ÈëÊý¾Ý¼Ä´æÆ÷Öµ    D L F
//     SD Preset of data area Ô¤ÖÃÊý¾Ý¼Ä´æÆ÷Öµ
// 
//     RS Read timer and counter set value area ¶ÁÈ¡¶¨Ê±Æ÷/¼ÆÊýÆ÷Ä¿±êÖµ
//     WS Write timer and counter set value area Ð´È붨ʱÆ÷/¼ÆÊýÆ÷Ä¿±êÖµ
//     RK Read timer and counter elapsed value area ¶ÁÈ¡¶¨Ê±Æ÷/¼ÆÊýÆ÷¾­¹ýÖµ
//     WK Write timer and counter elapsed value area Ð´È붨ʱÆ÷/¼ÆÊýÆ÷¾­¹ýÖµ
// 
//     MC Registration and reset of monitor contact µÇ¼¼°¸´Î»¼à¿Ø´¥µã
//     MD Registration and reset of monitor data µÇ¼¼°¸´Î»¼à¿ØÊý¾Ý
//     MG Monitor execution Ö´Ðмà¿Ø
// 
//     RR Read system register ¶Áȡϵͳ¼Ä´æÆ÷
//     WR Write system register Ð´Èëϵͳ¼Ä´æÆ÷
// 
//*    RT Read Programmable Controller (PC) status ¶ÁÈ¡PLC µ±Ç°×´Ì¬
//     RP Read program ¶ÁÈ¡³ÌÐò
//     WP Write program Ð´Èë³ÌÐò
//     RM Remote control (RUN/PROGRAM mode switching) Ò£¿Ø(ÔËÐÐ/±à³ÌģʽÇл»)
//     AB Transmission abort command ´«ÊäÖÕÖ¹Ö¸Áî
//     */
    int RT(int Station2=0)
    {
        if (SimFunc)
        {
 
        }
        if (!SerialPlc1.IsOpened) return 0;
        int len;
        CStringA s1;
        int station3;
        if (Station2==0) station3=Station;
        else station3=Station2;
        s1.Format("%%%02X#RT",station3);
        s1.AppendFormat("%02X\r",(unsigned char)(BCC(s1)));
        len=s1.GetLength();
        myCriticalSection1.Lock(INFINITE);
        SendPLCData(s1,len);
        int j;
        j=ReadPLCCom(buffer2,9+16);
        myCriticalSection1.Unlock();
        if (j>=9) 
        {
            buffer2[j]=0;
            s1=buffer2;
            for (int i=0;i<16;i++)
            {
                InfoBytes.bytes[i]=buffer2[6+i];
            }
            PlcInfo.PlcType=xtoi(InfoBytes.PlcType,2);
            PlcInfo.Version=xtoi(InfoBytes.Version,2);
            PlcInfo.PrgSize=xtoi(InfoBytes.PrgSize,2);
            PlcInfo.RunMode=xtoi(InfoBytes.RunMode,2);
            PlcInfo.LinkInfo=xtoi(InfoBytes.LinkInfo,2);
            PlcInfo.Errs=xtoi(InfoBytes.ErrMark,2);
            PlcInfo.SelfTestInfo=xtoi(InfoBytes.SelfTestInfo,4);
 
            return 1;
        }
        else
        {
            return 0;
        }
    }
    int RCS(const char * typeaddr,int &value, int Station2=0) //RCS Read single point of contact information ¶ÁÈ¡µ¥¸ö´¥µãµÄ״̬ÐÅÏ¢ X Y R L T C
 
    {
        if (SimFunc) 
        {
            myCriticalSection1.Lock(INFINITE);
            CStringA stypeaddr;
            stypeaddr=typeaddr;
            CStringA type;
            CStringA addr,Laddr,Raddr;
            type=typeaddr[0];
            addr="0000"+stypeaddr.Mid(1);
            addr=addr.Right(4);
            Laddr=addr.Left(3);
            Raddr=addr.Right(1);
            int Laddrv=atoi(Laddr);
            int Raddrv=xtoi(Raddr);
            if (type.MakeUpper()=="R")
            {
                value=(WR[Laddrv]&HexMask[Raddrv]);
                simresult1=1;
            }else {simresult1=0;}
            myCriticalSection1.Unlock();
            return simresult1;
        }
        if (!Connected) {return 0;}
 
        CStringA stypeaddr;
        stypeaddr=typeaddr;
        CStringA type;
        CStringA addr;
        type=typeaddr[0];
        addr="0000"+stypeaddr.Mid(1);
        addr=addr.Right(4);
        int len;
        CStringA s1;
        int station3;
        if (Station2==0) station3=Station;
        else station3=Station2;
        s1.Format("%%%02X#RCS%c%s",station3,type[0],addr);
        s1.AppendFormat("%02X\r",(unsigned char)(BCC(s1)));
        len=s1.GetLength();
        myCriticalSection1.Lock(INFINITE);
        SendPLCData(s1,len);
        int j;
        j=ReadPLCCom(buffer2,9+2);
        myCriticalSection1.Unlock();
        if (j>=9) 
        {
            value=buffer2[6]-48;
            return 1;
        }
        else
        {
            return 0;
        }
    }
 
    int WCS(const char * typeaddr, int value)
    {
        if (SimFunc) 
        {
            myCriticalSection1.Lock(INFINITE);
            CStringA stypeaddr;
            stypeaddr=typeaddr;
            CStringA type;
            CStringA addr,Laddr,Raddr;
            type=typeaddr[0];
            addr="0000"+stypeaddr.Mid(1);
            addr=addr.Right(4);
            Laddr=addr.Left(3);
            Raddr=addr.Right(1);
            int Laddrv=atoi(Laddr);
            int Raddrv=xtoi(Raddr);
            if (type.MakeUpper()=="R")
            {
                if (value)
                {
                    WR[Laddrv]|=HexMask[Raddrv];
 
                }
                else
                {
                    WR[Laddrv]&=~HexMask[Raddrv];
                }
                simresult1=1;
            }else simresult1=0;
            myCriticalSection1.Unlock();
            return simresult1;
        }
 
        if (!Connected) {return 0;}
        CStringA s1;
        CStringA type;
        CStringA addr;
        CStringA stypeaddr;
        stypeaddr=typeaddr;
        type=typeaddr[0];
        addr="0000"+stypeaddr.Mid(1);
        addr=addr.Right(4);
        s1.Format("%%%02X#WCS%C%s%1X",Station,type[0],addr,value);
        s1.AppendFormat("%02X\r",(unsigned char)(BCC(s1)));
        int len;
        len=s1.GetLength();
        myCriticalSection1.Lock(INFINITE);
        SendPLCData(s1,len);
        len=ReadPLCCom(buffer2,9+2);
        myCriticalSection1.Unlock();
        CString s1T;
        if (len<9)
        {
            s1T.Format(_T("WCS faild\r\n"),type);
            return 0;
        }
        return 1;
    }
 
 
    int RCC(const char * typeaddr,int count,short int value[])    //count ×î´ó32
    {
        if (SimFunc)
        {
            myCriticalSection1.Lock(INFINITE);
            CStringA stypeaddr,type,addr;
            stypeaddr=typeaddr;
            type=typeaddr[0];
            addr=stypeaddr.Mid(1);
            int addrv=atoi(addr);
            if (type.MakeUpper()=="R")
            {
                for (int i=0;i<count;i++)    {    value[i]=WR[i+addrv];    }
            }else if (type.MakeUpper()=="X")
            {
                for (int i=0;i<count;i++)    {    value[i]=WX[i+addrv];    }
            }else if (type.MakeUpper()=="Y")
            {
                for (int i=0;i<count;i++)    {    value[i]=WY[i+addrv];    }
            }else if (type.MakeUpper()=="L")
            {
                for (int i=0;i<count;i++)    {    value[i]=WL[i+addrv];    }
            }
            myCriticalSection1.Unlock();
            return 1;
        }
        if (!Connected) {return 0;}
        CStringA type;
        int addr;
        CStringA s1,s2;
        type=typeaddr[0];
        s1=typeaddr;
        s1=s1.Mid(1);
        CString s1T;
        addr=atoi(s1);
        if (count>32) {count=32;}
 
        s1.Format("%%%02X#RCC%c%04d%04d",Station,type[0],addr,addr+count-1);
        s1.AppendFormat("%02X\r",(unsigned char)(BCC(s1)));
        int len=s1.GetLength();
        myCriticalSection1.Lock(INFINITE);
        SendPLCData(s1,len);
        len=ReadPLCCom(s1.GetBuffer(512),9+count*4+1);
        myCriticalSection1.Unlock();
        s1.ReleaseBuffer(len);
        int i;
        if (len>9)
        {
            for (i=0;i<count;i++)
            {
                value[i]=xtoi(s1.Mid(6+i*4,2))+xtoi(s1.Mid(8+i*4,2))*256;
            }
            s1T.Format(_T("RCC %c%d %d OK %dB \r\n"),type[0],addr,count,len);
        }else
        {
            for (i=0;i<count;i++)
            {
                value[i]=0;
            }
            return 0;
        }
        return    1;
    }
    int WCC(const char * typeaddr,int count,short int value[])
    {
        if (SimFunc)
        {
            myCriticalSection1.Lock(INFINITE);
            CStringA stypeaddr,type,addr;
            stypeaddr=typeaddr;
            type=typeaddr[0];
            addr=stypeaddr.Mid(1);
            int addrv=atoi(addr);
            if (type.MakeUpper()=="R")
            {
                for (int i=0;i<count;i++)    {    WR[i+addrv]=value[i];    }
            }else if (type.MakeUpper()=="X")
            {
                for (int i=0;i<count;i++)    {    WX[i+addrv]=value[i];    }
            }else if (type.MakeUpper()=="Y")
            {
                for (int i=0;i<count;i++)    {    WY[i+addrv]=value[i];    }
            }else if (type.MakeUpper()=="L")
            {
                for (int i=0;i<count;i++)    {WL[i+addrv]=value[i];    }
            }
            myCriticalSection1.Unlock();
            return 1;
        }
        if (!Connected) {return 0;}
        CStringA type;
        int addr;
        CStringA s1,s2;
        type=typeaddr[0];
        s1=typeaddr;
        s1=s1.Mid(1);
        addr=atoi(s1);
        int i;
        s1.Empty();
        unsigned char low,high;
        CString s1T;
        for (i=0;i<count;i++)
        {
            low=value[i]%256;high=((unsigned)value[i])/256;
            s1.AppendFormat("%02X%02X",low,high);
        }
 
        s2.Format("%%%02X#WCC%c%04d%04d%s",Station,type[0],addr,addr+count-1,s1);
        s2.AppendFormat("%02X\r",((unsigned char) (BCC(s2))));
 
        int len=s2.GetLength();
        myCriticalSection1.Lock(INFINITE);
        SendPLCData(s2,len);
        len=ReadPLCCom(s1.GetBuffer(512),9+2);
        myCriticalSection1.Unlock();
        s1.ReleaseBuffer(len);
        if (len>0)
        {
        }else
        {
            s1T.Format(_T("WCC %c%d %d failed \r\n"),type[0],addr,count);
        }
        return    1;
    }
    int RD(const char * typeaddr,int count,short int value[])
    {
        if (SimFunc)
        {
            myCriticalSection1.Lock(INFINITE);
            CStringA stypeaddr,type,addr;
            stypeaddr=typeaddr;
            type=typeaddr[0];
            addr=stypeaddr.Mid(1);
            int addrv=atoi(addr);
            if (type.MakeUpper()=="D")
            {
                if (addrv+count<32768)
                {
                    for (int i=0;i<count;i++)    {    value[i]=DT[i+addrv];    }
                }
                else if (addrv>=90000)
                {
                    for (int i=0;i<count;i++)    {value[i]=SDT[i+addrv-90000];}
                }
                else    {        }
            }else if (type.MakeUpper()=="L")
            {
                for (int i=0;i<count;i++){    value[i]=LD[i+addrv];}
            }
            myCriticalSection1.Unlock();
            return 1;
        }
        if (!Connected) {return 0;}
        CStringA type;
        int addr;
        CStringA s1,s2;
        CString s1T;
        type=typeaddr[0];
        s1=typeaddr;
        s1=s1.Mid(1);
 
        addr=atoi(s1);
        if (count>32) {count=32;}
 
        s1.Format("%%%02X#RD%c%05d%05d",Station,type[0],addr,addr+count-1);
        s1.AppendFormat("%02X\r",(unsigned char)BCC(s1));
        int len=s1.GetLength();
        myCriticalSection1.Lock(INFINITE);
        SendPLCData(s1,len);
        len=ReadPLCCom(s1.GetBuffer(512),9+count*4+1);
        myCriticalSection1.Unlock();
        s1.ReleaseBuffer(len);
        if (len>9)
        {
            int i;
            for (i=0;i<count&&i*4+9<=len;i++)
            {
                value[i]=xtoi(s1.Mid(6+i*4,2))+xtoi(s1.Mid(8+i*4,2))*256;
            }
            for (;i<count;i++)    //Ê£ÓàµÄ¡£
            {    value[i]=0;    }
        }
        else
        {
            for (int i=0;i<count;i++)    {    value[i]=0;    }
            s1T.Format(_T("RD %c%d %d failed \r\n"),type[0],addr,count);
            return 0;
        }
        return    1;
    }
    int WD(const char * typeaddr,int count,short int value[])
    {
        if (SimFunc)
        {
            myCriticalSection1.Lock(INFINITE);
            CStringA stypeaddr,type,addr;
            stypeaddr=typeaddr;
            type=typeaddr[0];
            addr=stypeaddr.Mid(1);
            int addrv=atoi(addr);
            if (type.MakeUpper()=="D")
            {
                if (addrv+count<32768)
                {
                    for (int i=0;i<count;i++){DT[i+addrv]=value[i];    }
                }else if (addrv>=90000)
                {
                    for (int i=0;i<count;i++){    SDT[i+addrv-90000]=value[i];}
                }else{        }
            } else if (type.MakeUpper()=="L")
            {
                for (int i=0;i<count;i++){LD[i+addrv]=value[i];    }
            }
            myCriticalSection1.Unlock();
            return 1;
        }
        if (!Connected) {return 0;}
        CStringA type;
        int addr;
        CStringA s1,s2;
        CString s1T;    
        type=typeaddr[0];
        s1=typeaddr;
        s1=s1.Mid(1);
        addr=atoi(s1);
        int i;
        s1.Empty();
        unsigned char low,high;
 
        for (i=0;i<count;i++)
        {
            low=value[i]%256;high=((unsigned)value[i])/256;
            s1.AppendFormat("%02X%02X",low,high);
        }
 
        s2.Format("%%%02X#WD%c%05d%05d%s",Station,type[0],addr,addr+count-1,s1);
        s2.AppendFormat("%02X\r",((unsigned char) (BCC(s2))));
 
        int len=s2.GetLength();
        myCriticalSection1.Lock(INFINITE);
        SendPLCData(s2,len);
        len=ReadPLCCom(s1.GetBuffer(512),9+2);
        myCriticalSection1.Unlock();
        s1.ReleaseBuffer(len);
        if (len>0)    {    return 1;    }
        else{
            s1T.Format(_T("WD %c%d %d failed \r\n"),type[0],addr,count);
            return 0;
        }
        return    1;
    }
};