From 9aed5d7e7b3c7bf09da712e9c272ece401a7acc9 Mon Sep 17 00:00:00 2001 From: QuakeGod <QuakeGod@sina.com> Date: 星期一, 25 十一月 2024 14:51:23 +0800 Subject: [PATCH] add UltraSonic and MultiWireLess --- ComLib/Inc/KBus.h | 526 +++++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 files changed, 479 insertions(+), 47 deletions(-) diff --git a/ComLib/Inc/KBus.h b/ComLib/Inc/KBus.h index b4aabbc..0849ced 100644 --- a/ComLib/Inc/KBus.h +++ b/ComLib/Inc/KBus.h @@ -16,39 +16,489 @@ #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 SendTimeuS; // 发包时刻 - int RecvTimeuS; // 收包时刻 - int DelayuS; // 包循环延迟 - int nMaxDelayuS; // 最大包循环延迟 + + + int SendTimeTick; // 发包时刻 + int RecvTimeTick; // 收包时刻 + int DelayTick; // 包循环延迟 + int nMaxDelayTick; // 最大包循环延迟 + int LastCircleStartTime; //=0; + int CircleTime; //=0; int nSlaveTick; // 子机时间 tick - int ThisuS; +// int ThisuS; //volatile unsigned int nSlaveTick; //=0; int Clk3; //=0; @@ -56,10 +506,9 @@ //volatile int nCount2; //=0; int TimeOutCount; //=0; - int LastCircleStartTime; //=0; - int CircleTime; //=0; + stPortDef KBusPort; KBusSendPktFuncDef KBusSendPacket; // 发送数据包函数指针 KBusEvCBFuncDef KBusEvCBFunc; // 事件回调函数指针 KMSvCBDef KBusSvCBFunc; // 服务请求回调指针 @@ -91,7 +540,7 @@ // uchar bKBusEvCBFuncSet; // 事件回调函数是否已经设置 // uchar bKBusSvCBFuncSet; // 事件回调函数是否已经设置 - uchar PacketBuf1[KBMaxPacketLength]; +// uchar PacketBuf1[KBMaxPacketLength]; uchar PacketBuf2[KBMaxPacketLength]; @@ -127,9 +576,9 @@ ushort DOW[16]; }stKBusMem, *pstKBusMem; -//extern stKBus KBus1; extern stKBusMem KBusMem; +/* typedef struct tagKBusDiag { @@ -163,54 +612,39 @@ }; }stKBusDiag, *pstKBusDiag; - -//extern unsigned char bKBusMaster,bKBusSlave,bKBusRepeater;; -//extern unsigned char PacketBuf1[KBMaxPacketLength]; -//extern unsigned char PacketBuf2[KBMaxPacketLength]; - -//extern unsigned char nStationID; -//extern unsigned char nChilds; -//extern unsigned char nCurPollId; -//extern unsigned char nSeq; - -//extern volatile unsigned char KBusMasterRecved; -//extern volatile unsigned char KBusMasterRecvOK; -//extern volatile unsigned char KBusSlaveRecved; -//extern unsigned int KBusSendTimeuS; -//extern volatile int KBusRecvTimeuS; -//extern int KBusDelayuS; -//extern int KBusMaxDelayuS; +*/ -int KBusInit(stKBusDef * pKbusDef, KBusSendPktFuncDef KBusSendPacket, int bMaster, int nChildCount); +int KBusInit(stKBusDef * pKBus, KBusSendPktFuncDef KBusSendPacket, int bMaster, int nChildCount); -int KBusInitMaster(stKBusDef * pKbusDef, KBusSendPktFuncDef KBusSendPacket, int nChildCount); -int KBusInitSlave(stKBusDef * pKbusDef, KBusSendPktFuncDef KBusSendPacket, int nStationId, stDeviceInfo *pClientInfo); +int KBusInitMaster(stKBusDef * pKBus, KBusSendPktFuncDef KBusSendPacket, int nChildCount); +int KBusInitSlave(stKBusDef * pKBus, KBusSendPktFuncDef KBusSendPacket, int nStationId, stDeviceInfo *pClientInfo); -int KBusLoadSavedConfig(stKBusDef * pKbusDef); -int KBusSetEvCallBackFunc(stKBusDef * pKbusDef, KBusEvCBFuncDef CBFunc); // Set Event CallBack function -int KBusSetSvCallBackFunc(stKBusDef * pKbusDef, KMSvCBDef CBFunc); // Set ServiceReq CallBack Function +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 KBusStart(stKBusDef * pKbusDef) ; -int KBusStop(stKBusDef * pKbusDef) ; -int KBusDataPoll(stKBusDef * pKbusDef); - -int KBusLoopProcess(stKBusDef * pKbusDef); -int KBusMasterFunc(stKBusDef * pKbusDef); -int KBusSlaveFunc(stKBusDef * pKbusDef); -int KBusRepeaterFunc(stKBusDef * pKbusDef); +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 * pKbusDef, pKBPacket p1, int len1); +int KBusCheckPacket(stKBusDef * pKBus, pKBPacket p1, int len1); /* */ -int KBusParsePacket(stKBusDef * pKbusDef, 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) { }; @@ -223,11 +657,9 @@ 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); -- Gitblit v1.9.1