/** ****************************************************************************** * @file : KBus.h * @brief : Header for KBus.c file. * This file contains the common defines of the application. ****************************************************************************** */ #include "KMachine.h" #ifndef __KBUS_H__ #define __KBUS_H__ typedef unsigned char uchar; extern unsigned char bKBusMaster,bKBusSlave,bKBusRepeater;; enum enCMDs { cmdNone = '0', //Nothing cmdPing = '1', //Ping cmdPingReply = '2', //PingReply cmdRead = '3', //Read cmdReadReply = '4', //ReadReply cmdWrite = '5', //Write cmdWriteReply = '6', //WriteReply cmdGetVersion = '7', //GetVersion cmdVerInfo = '8', //VersionReply cmdExChgData='A', //ExChangeDate, = Wirte + Read cmdExChgDataReply, //ExchangeData Reply cmdSyncRead='a', //SyncRead cmdSyncWrite, //SyncWrite cmdSequenRead, //Sequence Read cmdSyncTime, //SyncTime }; enum {StartSign='%', EndSign=0x0D, MaxPacketLength=128, }; extern unsigned char PacketBuf1[128]; extern unsigned char PacketBuf2[128]; extern unsigned char BufferIn[16]; extern unsigned char BufferOut[16]; extern unsigned char nAddr; extern unsigned char nChilds; extern int ChildStat[16]; extern unsigned char nCurPollId; extern unsigned char nSeq; extern volatile unsigned char MasterRecved; extern volatile unsigned char MasterRecvOK; extern volatile unsigned char SlaveRecved; extern unsigned int SendTimeuS; extern volatile int RecvTimeuS; extern int DelayuS; extern int MaxDelayuS; extern volatile unsigned int nSlaveTick; typedef struct tagVerInfo { unsigned char nMainVer; unsigned char nSubVer; }stVerInfo; typedef struct tagMachineState { unsigned char Addr; unsigned char state; unsigned char InStartAddrBit; unsigned char OutStartAddrBit; unsigned char InDWStartAddrByte; unsigned char OutDWStartAddrByte; }ClientState; typedef struct tagMasterStat { unsigned int CycleTime; } stMasterStat; 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 tagClientCfg { unsigned int KeepOut; //Keep Output when comm err; }stClientCfg; struct ClientInfo { stVerInfo ClientVer; unsigned char nType; unsigned char nGroup; unsigned char InPutCount; unsigned char OutPutCount; unsigned char InDWCount; unsigned char OutDWCount; unsigned char Online; }; #pragma anon_unions typedef struct tagChnStat { unsigned 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; // }; }; } stChnStat; extern stChnStat ChnStats[8]; 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 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 void *(*CBFuncDef) (void*, void *, int); //callback func ,prama s is void *,void *,int; return void *; enum enumCallBackEvent { evNone = 0, evCreate = 1, evConnected = 2, evDisConnected = 3, evClosed = 4, }; typedef struct tagPacket { unsigned char Sign; //Æðʼ±ê¼Ç unsigned char DstHost; //Ä¿±êµØÖ· unsigned char SrcAddr; //Ô´µØÖ· unsigned char nCMD; //ÃüÁî unsigned char nSEQ; //ÐòÁкŠunsigned char PacketLen; //Êý¾ÝÔغɳ¤¶È ²»°üÀ¨Í·²¿5¸ö×Ö½Ú£¬²»°üÀ¨Î²²¿BCC¡£ unsigned char data[1]; //Êý¾ÝÔغÉ,×îĩβÊÇBCC£¬Êý¾Ý³¤¶ÈΪ0ʱ£¬Êµ¼ÊÒ²ÓÐÒ»¸öÊý¾Ý¡£ }stPacket,* pPacket; enum eResult { S_OK =0, S_ERR=1, S_TIMEOUT=2 } ; unsigned char BCC(void * pData, int nSize); // /* Make a Packet and return Packet Length */ int MakePacket(pPacket p1,uchar Src, uchar Dst, uchar nType, uchar nSEQ, uchar DataLen, void *pData ); /* */ int CheckPacket(int nChn, pPacket p1, int len1); /* */ int ParsePacket(int nChn, pPacket p1, int Len1); typedef struct tagMachineConfig { int bKBusMaster; int nAddr; }stMachineConfig; 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); int KBusRepeaterFunc(int nChn); int KBusMasterFunc(int nChn); int KBusSlaveFunc(int nChn); #endif /* __KBUS_H__ */