QuakeGod
2024-10-14 005755edcdc332315ba077598d4746ac195b069e
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
/**
  ******************************************************************************
  * @file           : KBus.h
  * @brief          : Header for KBus.c file.
  *                   This file contains the common defines of the application.
  ******************************************************************************
    */
#include "KBusDefine.h"
 
#include "KMachine.h"
 
#ifndef __KBUS_H__
#define __KBUS_H__
 
#define KBUS_VER    (0x104)
 
#define MAX_CLIENT 16                //×î´óÖ§³Ö×Ó»úÊýÁ¿
 
#define     KBUS_WAIT_TIME_OUT1         5            // 0.1mS ticks.
#define     KBUS_WAIT_TIME_OUT2         8            // 0.1mS ticks.
 
#define        KBUS_SLAVE_TIME_OUT 500        // 0.1ms ticks
 
//#define SYN_PREFIX 1
typedef unsigned char uchar;
typedef volatile unsigned char vuchar;
typedef unsigned short ushort;
 
enum enKBusStationType                 // KBus¹¤×÷ÀàÐÍ
{
    KBNone = 0,                                    // 0,䶨Òå
    KBusMaster = 1,                            // 1,Ö÷»ú
    KBusSlave = 2,                            // 2,×Ó»ú
    KBusRepeater = 3,                        // 3,ת·¢Æ÷,ÖмÌÆ÷
};
 
// diagnosis
enum enKBusStats
{
        KBusStatUnInited,                    // Î´³õʼ»¯×´Ì¬
        KBusStatInited,                        // Òѳõʼ»¯×´Ì¬
        KBusStatConfig,                        // ÒÑÅäÖÃ״̬
        KBusStatReady,                        // ¾ÍÐ÷״̬
        KBusStatRunning,                    // ÔËÐÐ״̬
        KBusStatIdel,                            // ÔÝͣ״̬
        KBusStatSafe,                            // °²È«×´Ì¬
        KBusStatError,                        // ´íÎó״̬
};
/*
enum enKBusStage
{
    KBusStageInit,
    KBusStageFindChild,
    KBusStageConfigChild,
    KBusStagePoll,
    
};
*/
typedef enum 
{
    KBusRunStepBroadCastCfg,
    KBusRunStepTimeSync,
    KBusRunStepTimeSyncWait,
    KBusRunStepMultiDataPoll,
    KBusRunStepMultiDataPollWait,
    KBusRunStepUniDataPoll,
    KBusRunStepUniDataPollWait,
    KBusRunStepUniDataTranster,
    KBusRunStepUniDataTransterWait,
    
}enKBusRunStep;
 
enum {
            KB_SYN = 0x55,
            KBStartSign='%',
            KBEndSign=0x0D,
            KBMaxPacketLength=148,
};
 
enum enCMDs
{
    
    cmdNone = 0x00,            //Nothing
 
    cmdQuery = 0x01,        //Query basic Info
    cmdQueryRply = 0x81,   //Query Info Reply
    
    cmdSetCfg = 0x02,                // Set Configuration
    cmdSetCfgRply = 0x82,        // Set Configuration Response
 
    cmdToRunMode = 0x03,
    cmdToRunModeRply = 0x83,
 
    cmdBroadCastCfg = 0x04,            //
 
    cmdMuExchgData = 0x05,            //
    cmdMuExchgDataRply = 0x85,    //
 
    cmdToSafeMode = 0x06,
    
    cmdHeartBeat = 0x07,            //
    cmdReHeartBeat = 0x87,        //
    
    
    cmdRemoteReq    = 0x1B,
    cmdRemoteReqReply = cmdRemoteReq|0x80,    //
    
    cmdPing = '1',            //Ping
    cmdPingReply = '2',        //PingReply
    cmdRead = '3',                //Read
    cmdReadReply = '4',        //ReadReply
    cmdWrite = '5',                //Write
    cmdWriteReply = '6',    //WriteReply
    cmdGetVersion = '7',        //GetVersion
    cmdVerInfo = '8',                //VersionReply
    
    cmdUniExChgData='A',                //ExChangeData, = Wirte + Read
    cmdExChgDataReply,            //ExchangeData Reply
    
    cmdSyncRead='a',                //SyncRead
    cmdSyncWrite,                        //SyncWrite
    cmdSequenRead,                    //Sequence Read
    cmdSyncTime,                        //SyncTime
};
 
 
typedef struct tagVerInfo                // KBus °æ±¾ÐÅÏ¢
{
    unsigned char nMainVer;
    unsigned char nSubVer;
}stVerInfo;
 
typedef struct tagMasterStat        // KBus Ö÷»ú״̬£¿
{
    unsigned int CycleTime;
} stMasterStat;
 
typedef struct tagDeviceInfo        // KBus ×Ó»ú»ù±¾ÐÅÏ¢        
{
    unsigned short DeviceType;        // ×Ó»úÀàÐÍ
    unsigned short DeviceVer;            // ×Ó»ú°æ±¾
    unsigned char InBitCount;            // ÊäÈ뿪¹ØÁ¿ÊýÁ¿
    unsigned char OutBitCount;        // Êä³ö¿ª¹ØÁ¿ÊýÁ¿
    unsigned char ExInBitCount;        // À©Õ¹µÄÊäÈ뿪¹ØÁ¿ÊýÁ¿
    unsigned char ExOutBitCount;    // À©Õ¹µÄÊä³ö¿ª¹ØÁ¿ÊýÁ¿
    unsigned char DWStartAddr;        // Êä³öÊý¾Ý×ÖÊý
    unsigned char OutDWCount;            // Êä³öÊý¾Ý×ÖÊý
    unsigned char AIWCount;                // ÊäÈëÄ£ÄâÁ¿Í¨µÀ(×Ö)Êý    // 16λΪһ¸ö×Ö(ͨµÀ)
    unsigned char AQWCount;                // Êä³öÄ£ÄâÁ¿Í¨µÀ(×Ö)Êý    // 16λΪһ¸ö×Ö(ͨµÀ)
//    unsigned char AIBits;                    //  Ã¿Í¨µÀλÊý        // 16λÒÔÏÂ
//    unsigned char AQbits;                    //    Ã¿Í¨µÀλÊý        // 16λÒÔÏÂ
 
 
}stDeviceInfo;
 
typedef struct tagExDeviceInfo        // ×Ó»úÀ©Õ¹ÐÅÏ¢
{
    unsigned short DeviceType;        // ×Ó»úÀàÐÍ
    unsigned short DeviceVer;            // ×Ó»ú°æ±¾
 
    unsigned char UID[12];                // 12×Ö½Ú UUID
    unsigned char Name[16];                // 16×Ö½Ú Ãû³Æ
//    stVerInfo ClientVer;
    unsigned char InBitCount;            
    unsigned char OutBitCount;
    unsigned char ExInBitCount;        // À©Õ¹µÄÊäÈ뿪¹ØÁ¿ÊýÁ¿
    unsigned char ExOutBitCount;    // À©Õ¹µÄÊä³ö¿ª¹ØÁ¿ÊýÁ¿
    unsigned char DWStartAddr;
    unsigned char OutDWCount;
    unsigned char AIWCount;
    unsigned char AQWCount;
//    unsigned char AIBits;
//    unsigned char AQbits;
 
 
}stExDeviceInfo;
 
enum enKBusConfigStat
{
    KBusUnConfiged = 0,
    KBusDefaultConfiged =1,
    KBusConfiged = 2,
};
 
typedef struct tagClientCfg                // ×Ó»úÅäÖÃ
{
    unsigned char Addr;                            // ×Ó»úµØÖ·
    unsigned char Configed;                        // ÊÇ·ñÒÑÅäÖÃ
    unsigned char bOnline;                    //µ±Ç°ÔÚÏß״̬
    unsigned char InStartAddrBit;            //ÊäÈëÆðʼλµØÖ·
    unsigned char OutStartAddrBit;        //Êä³öÆðʼλµØÖ·
    unsigned char AIWStartAddrByte;        //Ä£ÄâÈëÆðʼ×ÖµØÖ·
    unsigned char AQWStartAddrByte;        //Ä£Äâ³öÆðʼ×ÖµØÖ·
    unsigned char DIWStartAddrByte;        //Êý¾ÝÈëÆðʼ×ÖµØÖ·
    unsigned char DOWStartAddrByte;        //Êý¾Ý³öÆðʼ×ÖµØÖ·
    unsigned char KeepOut;                        //Keep Output when comm err;
}stClientCfg;
 
/*
typedef struct ServerStatus
{
    unsigned char Addr;
    unsigned char Status;
    unsigned short Input[16];
    unsigned short DT[128];
    unsigned short output[16];    
    
}stServerStatus,*pServerStatus;
*/
typedef struct tagMachineState            // ±¾»ú״̬
{
    unsigned char Addr;                                // µØÖ·
    unsigned char state;                            //״̬
    unsigned char InStartAddrBit;                //ÊäÈëÆðʼλµØÖ·
    unsigned char OutStartAddrBit;            //Êä³öÆðʼλµØÖ·
    unsigned char InDWStartAddrByte;        //×ÖÊäÈëÆðʼ×ÖµØÖ·
    unsigned char OutDWStartAddrByte;        //×ÖÊä³öÆðʼ×ÖµØÖ·
}ClientState;
 
 
 
#pragma anon_unions
typedef struct tagChnStat
{
    union{
        unsigned char MStat; 
        struct{
            unsigned char bOnline:1;
            unsigned char bErr1:1;
            unsigned char bErr2:1;
            unsigned char bReq:1;
        };
    };
    unsigned char SStat;
    unsigned short SendPackets;
    unsigned short RecvPackets;
    unsigned short LastSentTimeTick;
    unsigned short LostPackets;
    unsigned short CtnLstPkts;
    unsigned short MaxCtnLstPkts;
    unsigned short NotPkgErr;
    unsigned short PkgLenErr;
    unsigned short BCCErr;
    unsigned short TimeOutErr;
    unsigned short Delay;
    unsigned short MaxDelay;
    unsigned short SendTimeInterval;
    union
    {
        unsigned short ClientDatas[10];
        struct {
        unsigned short ClientRecvPkts;    //
        unsigned short ClientSendPkts;    //
        unsigned short ClientNotPktErr;    //
        unsigned short ClientMisIdPkts;    //
//        unsigned int ClientNoEndErr;    //
        unsigned short ClientPkgLenErr;    //
        unsigned short ClientBccErr;        //
        unsigned short ClientTimeOutErr;    //
        };
    };
} stChnStat;
 
 
typedef struct tagSlaveStat
{
    unsigned int nSlaveTick;
} stSlaveStat;
 
typedef struct ServerClientListElement
{
    unsigned char Addr;
    unsigned char Status;
    unsigned char Father;
    unsigned char Childs;
    
}    stServerClientListElement;
 
typedef struct ServerClientList
{
    unsigned char Addr;
}stServerClientList,*pServerClientList;
 
typedef struct tagMachineConfig
{
    int bKBusMaster;
    int nAddr;
}stMachineConfig;
 
/*
typedef struct ClientStatus
{
    unsigned char Addr;
    unsigned char Status;
    unsigned char Father;
    unsigned char Childs;
    unsigned short Input[16];
    unsigned short DT[128];
    unsigned short output[16];
    
}stChnStatus,*pClientStatus;
*/
 
typedef struct tagStatusDef
{
        unsigned char nSeq:2;        //ÐòÁкÅ
        unsigned char :2;
        unsigned char nErr1:1;
        unsigned char nErr2:1;
        unsigned char bReq:1;
    
}stStatus;    
 
typedef union tagUnStatusDef
{
    unsigned char nStatus;
 stStatus;
}unStatus;
 
typedef struct tagKBPacket
{
    unsigned char Sign;        //Æðʼ±ê¼Ç        
    unsigned char DstHost;    //Ä¿±êµØÖ·
    unsigned char SrcAddr;    //Ô´µØÖ·
    unsigned char nCMD;        //ÃüÁî
    union{
        unsigned char nStatus;        //״̬+ÐòÁкÅ
        struct{
            unsigned char nSeq:2;        //ÐòÁкÅ
            unsigned char :2;
            unsigned char nErr1:1;    //´íÎó1
            unsigned char nErr2:1;    //´íÎó2
            unsigned char bReq:1;        //
        };
    };
    unsigned char DataLen;    //Êý¾ÝÔغɳ¤¶È    ²»°üÀ¨Í·²¿5¸ö×Ö½Ú£¬²»°üÀ¨Î²²¿BCC¡£
    unsigned char data[1];        //Êý¾ÝÔغÉ,×îĩβÊÇBCC£¬Êý¾Ý³¤¶ÈΪ0ʱ£¬Êµ¼ÊÒ²ÓÐÒ»¸öÊý¾Ý¡£
}stKBPacket,* pKBPacket;    
 
typedef struct tagKBInfoBlockV1
{
    unsigned short nDeviceType;
    unsigned short nProgVer;
    
    unsigned char InBitCount;
    unsigned char OutBitCount;
    
    unsigned char AICount;
    unsigned char AIBits;
    unsigned char AQCount;
    unsigned char AQbits;
    
    unsigned char InDWCount;
    unsigned char OutDWCount;    
    
}stKBInfoBlockV1, *pKBInfoBlockV1;
 
enum eResult
{    
    S_OK =0,
    S_ERR=1,
    S_TIMEOUT=2
 
}    ;
 
/*
typedef struct tagKBPacketV10
{
    unsigned char Sign;        //Æðʼ±ê¼Ç        
    unsigned char DstHost;    //Ä¿±êµØÖ·
    unsigned char SrcAddr;    //Ô´µØÖ·
    unsigned char nCMD;        //ÃüÁî
    union{
        unsigned char nStatus;        //״̬+ÐòÁкÅ
        struct{
            unsigned char nSeq:2;        //ÐòÁкÅ
            unsigned char :2;
            unsigned char nErr1:1;
            unsigned char nErr2:1;
            
        };
    };
    unsigned char DataLen;    //Êý¾ÝÔغɳ¤¶È    ²»°üÀ¨Í·²¿5¸ö×Ö½Ú£¬²»°üÀ¨Î²²¿BCC¡£
    unsigned char data[1];        //Êý¾ÝÔغÉ,×îĩβÊÇBCC£¬Êý¾Ý³¤¶ÈΪ0ʱ£¬Êµ¼ÊÒ²ÓÐÒ»¸öÊý¾Ý¡£
}stKBPacketV10,* pKBPacketV10;    
 
typedef struct tagKBPacketV20
{
    unsigned char Sign;        //Æðʼ±ê¼Ç        
    unsigned char DstHost;    //Ä¿±êµØÖ·
    unsigned char SrcAddr;    //Ô´µØÖ·
    unsigned char nCMD;        //ÃüÁî
    union{
        unsigned char nStatus;        //״̬+ÐòÁкÅ
        struct{
            unsigned char nSeq:2;        //ÐòÁкÅ
            unsigned char :2;
            unsigned char nErr1:1;
            unsigned char nErr2:1;
            
        };
    };
    unsigned char DataLen;    //Êý¾ÝÔغɳ¤¶È    ²»°üÀ¨Í·²¿5¸ö×Ö½Ú£¬²»°üÀ¨Î²²¿BCC¡£
    unsigned char data[1];        //Êý¾ÝÔغÉ,×îĩβÊÇBCC£¬Êý¾Ý³¤¶ÈΪ0ʱ£¬Êµ¼ÊÒ²ÓÐÒ»¸öÊý¾Ý¡£
}stKBPacketV20,* pKBPacketV20;    
*/
 
enum enumCallBackEvent
{
    KBusEvNone = 0,
    KBusEvCreate = 1,
    KBusEvConnected = 2,
    KBusEvDisConnected = 3,
    KBusEvClosed = 4,
    KBusEvStateChange =5,
    KBusEvTimeSync=6,
    KBusEvDataUpdate =7,
    KBusEvCmdResponse = 8,
    
};
 
typedef int (*KBusSendPktFuncDef) (uchar * pBuf, int nLen);                    // SendPacket CallBack func for KBus 
 
typedef void *(*KBusEvCBFuncDef) (void * , int nEvent, void *, int);    //callback func ,prama s is  void *,void *,int; return void *;
 
typedef int (*KBusSvCBFuncDef) (void *, int nSvType, int ,void *, int);    //Service Req callback func ,param is  int ,int, int, void *,int; return int;
 
/*
typedef struct tagKBusRunStat
{
    unsigned char bMaster;                // ÊÇ·ñÖ÷»ú
    unsigned char nStationID;            // ×Ó»úµØÖ·
    unsigned char nChildren;            // ×Ó»úÊýÁ¿
    unsigned char nCurPollId;            // µ±Ç°ÂÖѯµ½µÄ×Ó»ú µØÖ·¡£
    unsigned char nSeq;                        // °üÐòÁкÅ
 
    unsigned char bMasterRecved;        // ×´Ì¬£¬ Ö÷»úÒѽÓÊÕµ½·µ»ØµÄÊý¾Ý°ü
    unsigned char bMasterRecvOK;        // ×´Ì¬£¬ Ö÷»ú½ÓÊÕµ½µÄ·µ»ØµÄÊý¾Ý°üÕýÈ·
    unsigned char bSlaveRecved;            // ×´Ì¬£¬ ×Ó»ú½ÓÊܵ½Êý¾Ý°ü¡£
    unsigned int nSendTimeuS;                // Êý¾Ý°ü·¢³öʱ¿Ì¡£
 
}stKBusRunStat , * pstKBusRunStat;
*/
 
#pragma anon_unions
typedef struct tagKBusDef                // KBus ¹¤×÷ÄÚ´æ
{
///*
    union {
        uchar nConfig;
        struct {
            uchar bMaster:1;    //KBus, ÊÇ·ñÖ÷»ú
            uchar bSlave:1;        //KBus, ÊÇ·ñ×Ó»ú
            uchar bRepeater:1;    //KBus, ÊÇ·ñת·¢Æ÷,ÖмÌÆ÷
        };
    };
// */ 
/*    
    uchar bMaster;                    //KBus, ÊÇ·ñÖ÷»ú
    uchar bSlave;                        //KBus, ÊÇ·ñ×Ó»ú
    uchar bRepeater;                //KBus, ÊÇ·ñת·¢Æ÷,ÖмÌÆ÷
*/
 
    uchar nStationId;                //KBus  Õ¾ºÅ
    vuchar MyStat;                        //KBus, ×´Ì¬»ú״̬
    vuchar nRunStep;                    //KBus, ÔËÐв½
    vuchar nSeq;                            //KBus, °üÐòÁкÅ
    
    vuchar bMasterSent;                //  ×´Ì¬ Ö÷»úÒÑ·¢ËÍ
    vuchar bMasterRecved;            //  ×´Ì¬ Ö÷»úÒѽÓÊÕ »Ø°ü
    vuchar bMasterRecvOK;            //  ×´Ì¬ Ö÷»ú½ÓÊջذüÕýÈ·
    vuchar bSlaveRecved;                //  ×´Ì¬ ×Ó»úÒѽÓÊÕ°ü
    
    vuchar nChildCount;                //  ×Ó»úÊýÁ¿
    uchar nCurPollId;                        //  µ±Ç°ÂÖѯ×Ó»ú ºÅ
    ushort ErrStat;
    ushort RunStat;
 
    uchar bReq;                            //ÔËÐÐϵͳÇëÇóÖÐ
    uchar nReqSvrId;                //ÇëÇóºÅ
    uchar nReqChildId;            //ÇëÇóµÄ×Ó»úºÅ
    uchar nReqParam;                //ÇëÇóµÄ²ÎÊý
    ushort nReqReCount;                //
 
    uchar nReqDataHdr[4];
    uchar pReqDatas[140];
    uchar nReqDataOff;
    uchar nReqDataLen;
    
 
 
    int SendTimeTick;                        // ·¢°üʱ¿Ì
    int RecvTimeTick;                        // ÊÕ°üʱ¿Ì
    int DelayTick;                            // °üÑ­»·ÑÓ³Ù
    int nMaxDelayTick;                    // ×î´ó°üÑ­»·ÑÓ³Ù
    int LastCircleStartTime;                            //=0;
    int CircleTime;                                                //=0;    
 
    int nSlaveTick;                        // ×Ó»úʱ¼ä tick
 
//    int ThisuS;
    //volatile unsigned int nSlaveTick;            //=0;
 
    int Clk3;                                                            //=0;
    int SendTime,Latancy,LatancyClk,SendClk;    
 
    //volatile int nCount2;                                    //=0;
    int TimeOutCount;                                            //=0;
 
 
    stPortDef KBusPort;
    KBusSendPktFuncDef KBusSendPacket;            //    ·¢ËÍÊý¾Ý°üº¯ÊýÖ¸Õë    
    KBusEvCBFuncDef KBusEvCBFunc;                        //        Ê¼þ»Øµ÷º¯ÊýÖ¸Õë
    KMSvCBDef KBusSvCBFunc;                                    //        ·þÎñÇëÇó»Øµ÷Ö¸Õë
 
    stDeviceInfo* pMyDeviceInfo;
 
    uchar RetryCount;                        //  ÖØÊÔ´ÎÊý    
    uchar nTimeOutCount;                //  ³¬Ê±¼ÆÊý
    vuchar step;                                //  µ±Ç°²½Öè
    uchar ChildStat[MAX_CLIENT];            //  ×Ó»ú״̬(ÃÇ)
    
 
    uchar CurXBitAddr;                //  ÒÑ·ÖÅä X Î» µØÖ· 
    uchar CurYBitAddr;                //  ÒÑ·ÖÅä Y Î» µØÖ· 
    uchar CurAIWAddr;                    //  ÒÑ·ÖÅä Ä£ÄâÁ¿Èë ×Ö µØÖ·
    uchar CurAQWAddr;                    //  ÒÑ·ÖÅä Ä£ÄâÁ¿³ö ×Ö µØÖ·
    uchar CurDIWAddr;                    //  ÒÑ·ÖÅä Êý¾ÝÈë ×Ö µØÖ·
    uchar CurDOWAddr;                    //  ÒÑ·ÖÅä Êý¾Ý³ö ×Ö µØÖ·
    
    uchar MyYStartBitAddress;                //  ±¾»ú Æðʼ Y Î»µØÖ· 
    uchar MyAQWStartWordAddress;        //  ±¾»ú Æðʼ Ä£ÄâÁ¿ ³ö ×Ö µØÖ·
    uchar MyDWStartWordAddress;            //  ±¾»ú Æðʼ Êý¾Ý³ö ×Ö µØÖ· 
    
    stDeviceInfo DeviceInfos[MAX_CLIENT];        //        ×Ó»úÐÅÏ¢(ÃÇ)
    stClientCfg ClientCfg[MAX_CLIENT];            //    ×Ó»úÅäÖÃ(ÃÇ)
    stChnStat KBusChnStats[MAX_CLIENT];
    
 
//    uchar bKBusEvCBFuncSet;                                //    Ê¼þ»Øµ÷º¯ÊýÊÇ·ñÒѾ­ÉèÖÃ
//    uchar bKBusSvCBFuncSet;                                //    Ê¼þ»Øµ÷º¯ÊýÊÇ·ñÒѾ­ÉèÖÃ
 
//    uchar PacketBuf1[KBMaxPacketLength];
    uchar PacketBuf2[KBMaxPacketLength];
 
 
    unsigned char Datas[128];        
    volatile int PacketLength;                        // = 0;
 
    unsigned char KBusDiagnosis;                // =1;        // Kbus Õï¶Ïģʽ
    unsigned char KBusSnapPos;                        // = 0;        // ×¥È¡ Kbus °ü
 
//    unsigned char nStationID;                            //=0;
//    unsigned char nChilds;
    //int ChildStat[16];
    // unsigned char nCurPollId;                            //=0;
    // unsigned char nSeq;                                        //=0;
    unsigned char nClientDataIndex;            //=0;
 
}stKBusDef,*pstKBusDef;
 
 
typedef struct tagKBusMem
{
    union{
    ushort WLX[16];
    uchar WLXB[32];
    };
    union{
    ushort WLY[16];
    uchar WLYB[32];
    };
    ushort AIW[16];
    ushort AOW[16];
    ushort DIW[16];
    ushort DOW[16];    
}stKBusMem, *pstKBusMem;
 
extern stKBusMem KBusMem;
 
/*
typedef struct tagKBusDiag
{
    
    int Stat;
    unsigned int SendPackets;
    unsigned int RecvPackets;
    unsigned int LastSentTimeuS;
    unsigned int LostPackets;
    unsigned int CtnLstPkts;
    unsigned int MaxCtnLstPkts;
    unsigned int NotPkgErr;
    unsigned int PkgLenErr;
    unsigned int BCCErr;
    unsigned int TimeOutErr;
    unsigned int Delay;
    unsigned int MaxDelay;
    unsigned int SendTimeInterval;
    union
    {
        unsigned int ClientDatas[10];
        struct {
        unsigned int ClientRecvPkts;    //
        unsigned int ClientSendPkts;    //
        unsigned int ClientNotPktErr;    //
        unsigned int ClientMisIdPkts;    //
//        unsigned int ClientNoEndErr;    //
        unsigned int ClientPkgLenErr;    //
        unsigned int ClientBccErr;        //
        unsigned int ClientTimeOutErr;    //
        };
    };    
    
}stKBusDiag, *pstKBusDiag;
*/
 
 
int KBusInit(stKBusDef * pKBus, KBusSendPktFuncDef KBusSendPacket, int bMaster, int nChildCount);
 
int KBusInitMaster(stKBusDef * pKBus, KBusSendPktFuncDef KBusSendPacket, int nChildCount);
int KBusInitSlave(stKBusDef * pKBus, KBusSendPktFuncDef KBusSendPacket, int nStationId, stDeviceInfo *pClientInfo);
 
int KBusLoadSavedConfig(stKBusDef * pKBus);
int KBusSetEvCallBackFunc(stKBusDef * pKBus, KBusEvCBFuncDef CBFunc);            // Set Event CallBack function
int KBusSetSvCallBackFunc(stKBusDef * pKBus, KMSvCBDef CBFunc);            // Set ServiceReq CallBack Function
int KBusMakeDefaultClildInfo(stKBusDef * pKBus, int nChild);
int KBusReqService (stKBusDef * pKBus, int ReqId, int nParam1, int nParam2, void **pData, unsigned short * len1);
 
int KBusStart(stKBusDef * pKBus) ;
int KBusStop(stKBusDef * pKBus) ;
int KBusDataPoll(stKBusDef * pKBus);
 
int KBusLoopProcess(stKBusDef * pKBus);
int KBusMasterFunc(stKBusDef * pKBus);
int KBusSlaveFunc(stKBusDef * pKBus);
int KBusRepeaterFunc(stKBusDef * pKBus);
 
    /* Make a Packet and return Packet Length */
int KBusMakePacket(pKBPacket p1,uchar Src, uchar Dst, uchar nType, uchar nStatus, uchar DataLen, void *pData );
 
/*   */
int KBusCheckPacket(stKBusDef * pKBus, pKBPacket p1, int len1);
 
/* */
int KBusParsePacket(stKBusDef * pKBus, pKBPacket p1, int Len1);
 
int KBusPacketSendDone(stKBusDef * pKBus);
/*
static inline char KBGetClientNum(int nChn) { };
static inline char KBGetClientInfo(int nChn, int nClientId) { };
static inline char KBGetClientIOCount(int nChn,int nClientId) {};
int KBGetClientStatus(int nChn);
    
static inline char KBisSafeState(int nPortNum) { };
static inline char KBisRunState(int nPortNum) { };
static inline char KBGetState(int nPortNum) { };
static inline char KBChangeState(int nPortNum, int nNewState){ };
*/
 
unsigned char KBusBCC(void * pData, int nSize);
//
int KBusSlaveRunRemoteReq(stKBusDef * pKBus, int nReqSvrId, unsigned char * pData , int Len1);
 
/*
int InitMachine(stMachineConfig * pConfig);
 
int SetConfig(void );
 
int StartConfig(void);
    
int SetMasterConfig(void);
 
int StartPolling(void);
    
int ReadData(void);
 
int WriteData(void);
 
int GetStat(void);
*/
 
 
#endif    /* __KBUS_H__ */