QuakeGod
2024-09-02 7eb19e6024af7f05cf94c66fb843439a3509147e
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
/**
  ******************************************************************************
  * @file           : KLink.h
  * @brief          : Header for KLink.c file.
  *                   This file contains the common defines of the application.
  ******************************************************************************
    */
#ifndef __KLINK_H__
#define __KLINK_H__
typedef unsigned char uchar;
typedef unsigned char UCHAR;
typedef unsigned short USHORT;
typedef unsigned int UINT;
 
enum eKLResult
{    
        KL_ERR = -1,
        KL_OK = 0,
        KL_NG = 1,
        KL_UNKNOWN =2,
        KL_TIMEOUT = 3,
        KL_BCC_ERR = 4,
        KL_PKG_ERR = 5,
        KL_SEQ_ERR = 6,
        KL_LENTH_EXCEED,
        KL_NEED_PASS,
        KL_NOT_SUPPORT,
    
}    ;
 
enum {KLSignStart='%',    //0x25
            KLSignReply='U',    //0x55
            KLSignEnd=0x0D,
            KLMaxPacketLength=256,
};
    enum enKLCMDs
    {
        KLCmdNone = 0x00,        //Nothing
        KLCmdPing = 0x01,            //Ping
//        KLCmdPingReply = 0x02,        //PingReply
        KLCmdInfo = 0x03,        //GetInfo
 
        KLCmdRead = 0x05,                //Read
//        KLCmdReadReply = 0x06,        //ReadReply
        KLCmdWrite = 0x07,                //Write
//        KLCmdWriteReply = 0x08,    //WriteReply
        KLCmdSaveToFlash = 0x09,        //
        
        KLCmdExChgData=0x0A,                //ExChangeDate, = Wirte + Read
//        KLCmdExChgDataReply,            //ExchangeData Reply
        
        KLCmdSyncRead=0x0d,                //SyncRead
        KLCmdSyncWrite,                        //SyncWrite
        KLCmdSequenRead,                    //Sequence Read
        KLCmdSyncTime,                        //SyncTime
        
        KLCmdChgMode = 0x11,
        KLCmdGetMode = 0x12,
 
        KLCmdChkPass,
        KLCmdSetPass,
        KLCmdEraseAll,
        KLCmdReadPLCProgram,
        KLCmdStartPLCProgram,
        KLCmdWritePLCProgram,
        KLCmdFinishPLCProgram,
        KLCmdReadPLCAnno,
        KLCmdStartPLCAnno,
        KLCmdWritePLCAnno,
        KLCmdFinishPLCAnno,    
    
        KLCmdRead1Bit = 0x21,                //ReadSingleBit
        KLCmdWrite1Bit = 0x22,                //WriteSingleBit
        KLCmdReadBits = 0x23,                //ReadBits        n = 1 - 8
        KLCmdWriteBits = 0x24,                //ReadBits        n = 1 - 8
 
        KLCmdReadBitsByByte = 0x25,            //ReadBitsByByte
        KLCmdWriteBitsByByte = 0x26,        //ReadBitsByByte
 
        KLCmdReadBitsByWord = 0x27,            //ReadBitsByWord
        KLCmdWriteBitsByWord = 0x28,        //ReadBitsByWord
        
        KLCmdReadDataByte = 0x31,
        KLCmdWriteDataByte = 0x32 ,
 
        KLCmdReadDataWord = 0x33,
        KLCmdWriteDataWord = 0x34,
 
        KLCmdReadData = 0x35,
        KLCmdWriteData = 0x36 ,
        
        KLCmdStopBlinkLED = 0x4F,
        KLCmdBlinkLED = 0x50,
        
        KLCmdReadProg = 0x51,
        KLCmdWriteProg,
        KLCmdReadSysCfg,
        KLCmdWriteSysCfg,
        KLCmdSaveSysCfg,
        KLCmdSaveRunStat,
        KLCmdReadRunStat,
        KLCmdClearRunStat,
        
        KLCmdEraseFlashPage = 0x61,
        KLCmdWriteToFlash = 0x62,
        
        KLCmdGetEventLogCount,
        KLCmdGetEventLog,
        KLCmdClearEventLog,
        KLCmdGetFactoryData,
        KLCmdWriteFactoryData,
        KLCmdResetMachine,
        KLCmdGetUid,
        KLCmdSetTime,
        KLCmdGetSN,
        KLCmdReadFactData,
        KLCmdWriteFactData,
 
        KLCmdMC = 0x70,         
        KLCmdMD,
        KLCmdMG,
        
        KLCmdWriteFirmware,
        KLCmdWriteFirmInfo,
        KLCmdGetPortInfo,
        KLCmdGetPortChnInfo,
        KLCmdGetPortChildInfo,
        KLCmdPortRemoteReq,
        
        KLCmdErrRply=0xEE,                //ERRORReply
    };
 
    enum enKLInfoType
    {
        KLInfoTypeInfo = 0,
        KLInfoTypeUID = 1,
        KLInfoTypeSN = 2,
        KLInfoTypeEventLogCount = 3,
    };
    
//extern unsigned char KLPacketBuf1[256];
extern unsigned char KLPacketBuf2[256];
 
extern unsigned char KLBufferIn[16];
extern unsigned char KLBufferOut[16];
 
extern unsigned char nKLStationId;
extern unsigned char nKLSeq;
 
extern int KLThisuS;
extern int KLRecvTimeuS;
 
//PendReq 状态
    //0, 没有
    //1, 执行中
    //2, 成功完成
    //3, 失败
#pragma anon_unions
typedef union tagKLStatDef
{
    UCHAR StatByte;
    struct {
        UCHAR nSEQ : 4;
        UCHAR HasExt : 1;
        UCHAR HasErr : 1;
        UCHAR PendReqStat : 2;
    };
}unKLStat, *pKLStat;
/*
typedef struct tagKLStatDef
{
    UCHAR Confirm : 2;
    UCHAR HasExt : 1;
    UCHAR HasErr : 1;
}stKLStat,*pKLStat;
*/
extern unKLStat nKLStatus;
 
typedef struct tagKLReqPktHdr
{
    unsigned char ReqStSgn;        //
    unsigned char DstHost;        //
    unsigned char Stat;                //
    unsigned char nCMD;                //
    unsigned char nType1;            //
    unsigned char Params[1];        //
}stKLReqPktHdr,* pKLReqPktHdr;
/*
typedef struct tagKLPingReqPkt
{
    unsigned char ReqStSgn;        //
    unsigned char DstHost;        //
    unsigned char nCMD;                //
    unsigned char nSize;            //
    
}stKLPingReqPkt,* pKLPingReqPkt;
*/
typedef struct tagKLRplyPktHdr
{
    unsigned char RplyStSgn;    //
    unsigned char DstHost;        //
    unsigned char nStatus;        //    
    unsigned char nRplyCMD;        //
    unsigned char nSize1;            //
    unsigned char Datas[1];        //
}stKLRplyPktHdr,* pKLRplyPktHdr;
 
/*
typedef struct tagKLPktHdr
{
        unsigned char StSign;        //起始标记
        unsigned char SrcAddr;    //源地址
        unsigned char DstHost;    //目标地址
        unsigned char nCMD;        //命令
    
}stKLPtHdr, * pKLPktHdr;
*/
/*
typedef struct tagKLRdPkt
{
        unsigned char StSign;        //起始标记        
        unsigned char SrcAddr;    //源地址
        unsigned char DstHost;    //目标地址
        unsigned char nCMD;        //命令
        unsigned char nType;
        unsigned char nAddr;    //
}stKLRdPkt,* pKLRdKpt;
*/
/*
typedef struct tagKLPacket
    {
        unsigned char StSign;        //起始标记        
        unsigned char SrcAddr;    //源地址
        unsigned char DstHost;    //目标地址
        unsigned char nCMD;        //命令
        unsigned char nSEQ;        //序列号
        unsigned char LoadLen;    //数据载荷长度    不包括头部5个字节,不包括尾部BCC。
        unsigned char data[1];        //数据载荷,最末尾是BCC,数据长度为0时,实际也有一个数据。
    }stKLPacket,* pKLPacket;    
*/
 
int KLinkInit(int );
 
unsigned char KLBCC(const void * pData, int nSize);
//
 
    /* Make a Packet and return Packet Length */
int KLMakeReqPacket(void * pBuf1, uchar Src, uchar Dst, uchar nType, uchar nSEQ, uchar DataLen, void *pData );
int KLMakeRplyPacket(void * pBuf1, uchar Dst, uchar Status, uchar nCmd, uchar DataLen, const void *pData );
 
/*   */
int KLCheckPacket(int nChn, void * pBuf1, int len1);
 
/* */
int KLParsePacket(int nChn, void * pBuf1, int Len1);
 
typedef void *(*KLinkEvCBDef) (int nChn, int nEvent, void *, int);    //Event callback func ,prama s is  void *,void *,int; return void *;
typedef int (*KLinkSvCBDef) (int nChn, int nSvType, int ,void *, int);    //Service Req callback func ,param is  int ,int, int, void *,int; return int;
 
//int ReadData(void);
 
//int WriteData(void);
 
//int GetStat(void);
 
 
#endif    /* __KLINK_H__ */