📄 userfunc.c
字号:
#include <stdio.h> #include <stdlib.h> #include <memory.h> #include <math.h> #include <unistd.h> #include <fcntl.h> #include <termios.h> #include <errno.h> #include <sys/types.h> #include <sys/stat.h> #include "include/UserFunc.h"#include "include/Timer.h" #include "include/libflash.h" #include "include/libmemory.h" #include "include/SubData.h"unsigned short CRC16(unsigned char *buf, int lgn) { int i,j; unsigned char bbl, ddl, bbh, ddh, tt, tt1, tt2; unsigned char parity_tab[8] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80}; lgn+=2 ; bbl = 0xff; ddl = 0xff; bbh = 0xff; ddh = 0xff; for (i = 0; i< lgn - 2; i++) { tt = buf[i]; bbl = bbl ^ bbl; ddl = ddl ^ tt; tt2 = 0; for (j = 0; j < 8; j++) { if ((ddl & parity_tab[j]) != 0) tt2 += 1; } if ((tt2 & 0x01) != 0) bbl = 0x07; bbh = ddl; if ((bbl & 0x01) != 0) tt = 1; else tt = 0; if ((bbh & 0x01) != 0) tt1 = 1; else tt1 = 0; bbh = bbh >> 1; bbl = bbl >> 1; if (tt == 1) bbh = bbh | 0x80; if (tt1 == 1) bbl = bbl | 0x80; bbh = bbh ^ ddl; if ((bbl & 0x01) != 0) tt = 1; else tt = 0; if ((bbh & 0x01) != 0) tt1 = 1; else tt1 = 0; bbh = bbh >> 1; bbl = bbl >> 1; if (tt == 1) bbh = bbh | 0x80; if (tt1 == 1) bbl = bbl | 0x80; bbl = bbl ^ ddh; ddl = bbl; ddh = bbh; } //buf[lgn - 2] = ddl; //buf[lgn - 1] = ddh; return ((ddl<<8)+ddh);}unsigned short DGT801CRC(unsigned char *puchMsg, unsigned short usDataLen) { unsigned char uchCRCHi = 0x00 ; /* 高CRC字节初始化 */ unsigned char uchCRCLo = 0x00 ; /* 低CRC 字节初始化 */ unsigned uIndex ; /* CRC循环中的索引 */ while (usDataLen--) /* 传输消息缓冲区 */ { uIndex = uchCRCHi ^ *puchMsg++ ; /* 计算CRC */ uchCRCHi = uchCRCLo ^ auchCRCHi[uIndex] ; uchCRCLo = auchCRCLo[uIndex] ; } return (uchCRCHi << 8 | uchCRCLo) ; }unsigned char CalcCRC8(unsigned char *pBuff){ int i,len = 5; int q = 0;/*字节计数器*/ int r = 0;/*字节内位的计数器*/ int crc = 0; while(q < len) { if(pBuff[q] & (0x80>>r)) /*当字节内某位为1时,在余数末尾置1,否则置0*/ crc |= 0x01; if(crc >= 0x100) crc ^= 0x107; /*异或多项余子式*/ crc <<= 1; r++; if(r == 8) /*处理下一个字节*/ { r = 0; q++; } } for(i=0; i<8; i++) /*对位流的后8位计算CRC校验码*/ { if(crc >= 0x100) crc ^= 0x107; crc <<= 1; } crc >>= 1; crc = (~crc); /*余数取反*/ return (unsigned char)crc;}unsigned short GetCheckSum(unsigned char *pData,int nLen)//for xj100bh{ int i; unsigned short CheckResult=0; for(i=0;i<nLen;i++) CheckResult+=(*(pData+i)); return CheckResult;}unsigned char GetByteCheckSum(unsigned char *pData,int nLen)//for DL645 protocol{ int i; unsigned char CheckResult=0; for(i=0;i<nLen;i++) CheckResult+=(*(pData+i)); return CheckResult;}unsigned char GetXorSum(unsigned char *pData,int nLen){ int i; unsigned char CheckResult = *pData; for(i=0;i<nLen-1;i++) CheckResult^=(*(pData+i+1)); return CheckResult;}unsigned char GetDigital(unsigned char *pData, int nIndex){ unsigned char Temp; int Offset; Temp=*(pData+(int)nIndex/8); Offset=nIndex%8; Temp>>=(Offset); return Temp&0x01;}int BitToByte(unsigned char *pByteData,unsigned char *pBitData,int nLen){ int ByteNum,i,j; unsigned char TempByte=0; if (nLen%8 == 0) ByteNum=nLen/8; else ByteNum=nLen/8+1; for (i=0;i<ByteNum;i++) { if (i==ByteNum-1) { for (j=i*8;j<nLen;j++) { TempByte |= *(pBitData+j)<<(j%8); } } else { for (j=i*8;j<i*8+8;j++) { TempByte |= *(pBitData+j)<<(j%8); } } *(pByteData+i)=TempByte; TempByte=0; } return ByteNum; }unsigned char GetBYTEFromASCII(unsigned char ASCIIHi, unsigned char ASCIILo){ unsigned char btHi,btLo; if( ASCIIHi >= 'A' ) btHi = ASCIIHi - 'A' + 10 ; else btHi = ASCIIHi - '0' ; if( ASCIILo >= 'A' ) btLo = ASCIILo - 'A' + 10 ; else btLo = ASCIILo - '0' ; return (btHi*16+btLo);}unsigned char GetASCII(int HexData){ unsigned char RetValue; if(HexData<=9) RetValue=HexData+'0'; else RetValue=HexData+'A'-10; return RetValue;}unsigned char GetHex(unsigned char ASCII_Data){ unsigned char RetValue; if(ASCII_Data <= '9') RetValue = ASCII_Data-'0'; else RetValue = ASCII_Data-'A'+10; return RetValue;}void FlashCopyOut(unsigned short *pData,int StartAddr,int nLen){ int i; for(i=0;i<nLen;i++) { *(pData+i)=AP_Read_Flash(StartAddr+i); }}void FlashCopyIn(int StartAddr,int nLen,unsigned short *pData){ int i; Erase_AP_Sector(); for(i=0;i<nLen;i++) { AP_Write_Flash(StartAddr+i,*(pData+i)); }}void ShareMemRead(unsigned char *pData,unsigned long StartAddr,int nLen){ int i; for(i=0;i<nLen;i++) { *(pData+i)=share_memory_read(StartAddr+i); }}void ShareMemWrite(unsigned long StartAddr,int nLen,unsigned char *pData){ int i,fd; while ((fd=open("/tmp/share_memory.lock", O_RDONLY | O_CREAT | O_EXCL)) < 0); for(i=0;i<nLen;i++) { share_memory_write(StartAddr+i,*(pData+i)); } close(fd); unlink("/tmp/share_memory.lock");}unsigned char HEX_TO_BCD(unsigned char TempHex){ unsigned char TempX; if (TempHex <= 9) { return TempHex; } else { TempX = (TempHex/100)*256+((TempHex%100)/10)*16+TempHex%10; return TempX; }}int Check_QUEUE_Space(unsigned long StartAddr, unsigned int uiSize, unsigned int MaxLen){ int result =1; unsigned short uiFree; unsigned short ReadPointer; unsigned short WritePointer; unsigned char TempBuff[4]; ShareMemRead(TempBuff, StartAddr, 4); ReadPointer = TempBuff[0]*256+TempBuff[1]; WritePointer = TempBuff[2]*256+TempBuff[3]; if(WritePointer >= ReadPointer) { uiFree = MaxLen - WritePointer + ReadPointer; } else { uiFree = ReadPointer - WritePointer; } if(uiFree <= (uiSize + 1)) { result = 0; } return result;}void Save_Net104Data_To_Memory(int iPort,unsigned char *pData,int nLen){ int i; unsigned char TempBuff[8]; unsigned short WritePointer; unsigned long StartAddr = iPort*100; if(!Check_QUEUE_Space(StartAddr, nLen, MAX_CMD_LEN)) return; ShareMemRead(TempBuff, StartAddr+2, 2); WritePointer = TempBuff[0]*256+TempBuff[1]; if(nLen>0) { for(i=0; i<nLen; i++) { SubDevice[iPort].NormCmd[WritePointer] = pData[i]; WritePointer = (WritePointer+1)%MAX_CMD_LEN; } TempBuff[0] = (unsigned char)(WritePointer>>8); TempBuff[1] = (unsigned char)(WritePointer&0xFF); ShareMemWrite(StartAddr+2, 2, TempBuff); }}void Save_Call104Data_To_Memory(int iPort,unsigned char *pData,int nLen){ int i; unsigned char TempBuff[8]; unsigned short WritePointer; unsigned long StartAddr = iPort*100+4; if(!Check_QUEUE_Space(StartAddr, nLen, MAX_CMD_LEN)) return; ShareMemRead(TempBuff, StartAddr+2, 2); WritePointer = TempBuff[0]*256+TempBuff[1]; if(nLen>0) { for(i=0; i<nLen; i++) { SubDevice[iPort].CallCmd[WritePointer] = pData[i]; WritePointer = (WritePointer+1)%MAX_CMD_LEN; } TempBuff[0] = (unsigned char)(WritePointer>>8); TempBuff[1] = (unsigned char)(WritePointer&0xFF); ShareMemWrite(StartAddr+2, 2, TempBuff); }}int ReadOneCommandFromMem(int iPort,unsigned short CurReadPointer ,int CmdType,unsigned char *pRead){ unsigned short i, WichAddr; unsigned char TempBuff[8]; unsigned short ReadPointer = CurReadPointer; switch(CmdType) { case NorCMD: //一般命令,优先级高 WichAddr = iPort*100; for(i=0;i<3;i++) { *(pRead+i) = SubDevice[iPort].NormCmd[ReadPointer]; ReadPointer = (ReadPointer+1)%MAX_CMD_LEN; } if(*(pRead+1)!=0x68) { ResetMemPointer(WichAddr); return 0; } if(*(pRead+2)>0) { for(i=0;i<*(pRead+2);i++) { *(pRead+3+i) = SubDevice[iPort].NormCmd[ReadPointer]; ReadPointer = (ReadPointer+1)%MAX_CMD_LEN; } TempBuff[0] = (unsigned char)(ReadPointer>>8); TempBuff[1] = (unsigned char)(ReadPointer&0xFF); ShareMemWrite(WichAddr, 2, TempBuff); return *(pRead+2)+3; } else { ResetMemPointer(WichAddr); return 0; } break; case CallCMD: //总召唤命令,优先级低 WichAddr = iPort*100+4; for(i=0;i<3;i++) { *(pRead+i) = SubDevice[iPort].CallCmd[ReadPointer]; ReadPointer = (ReadPointer+1)%MAX_CMD_LEN; } if(*(pRead+1) != 0x68) { ResetMemPointer(WichAddr); return 0; } if(*(pRead+2)>0) { for(i=0; i<*(pRead+2); i++) { *(pRead+3+i) = SubDevice[iPort].CallCmd[ReadPointer]; ReadPointer = (ReadPointer+1)%MAX_CMD_LEN; } TempBuff[0] = (unsigned char)(ReadPointer>>8); TempBuff[1] = (unsigned char)(ReadPointer&0xFF); ShareMemWrite(WichAddr, 2, TempBuff); return *(pRead+2)+3; } else { ResetMemPointer(WichAddr); return 0; } break; } return 0;}int Read_104Data_From_Memory(int iPort,unsigned char *pRead){ unsigned char TempBuff[8]; unsigned short i,StartAddr,nRead=0; unsigned short ReadPointer,WritePointer; StartAddr=iPort*100; ShareMemRead(TempBuff, StartAddr, 4); ReadPointer = TempBuff[0]*256+TempBuff[1]; WritePointer = TempBuff[2]*256+TempBuff[3]; if(ReadPointer != WritePointer) { nRead = ReadOneCommandFromMem(iPort, ReadPointer, NorCMD, pRead); return nRead; } StartAddr = iPort*100+4; ShareMemRead(TempBuff, StartAddr, 4); ReadPointer = TempBuff[0]*256+TempBuff[1]; WritePointer = TempBuff[2]*256+TempBuff[3]; if(ReadPointer != WritePointer) { nRead = ReadOneCommandFromMem(iPort, ReadPointer, CallCMD, pRead); return nRead; } return 0;}void ResetMemPointer(unsigned long PointerAddr){ unsigned char TempBuff[4]; memset(TempBuff, 0, sizeof(TempBuff)); ShareMemWrite(PointerAddr, 4, TempBuff);}void WriteMsgToShareMem(int iPort,int iData,unsigned char *pData,int StationType,int MsgTpye){ int i; unsigned short StartAddr ; unsigned char TempBuff[8]; unsigned short WritePointer=0; int nLen = *(pData+1)+2;//报文长度 if((!CONN_COMMU_Enable()) || (nLen <= MIN_IEC104_FRAME)) return; if(StationType == EngineerStation) { StartAddr = iPort*100+16; if(!Check_QUEUE_Space(StartAddr, nLen, MAX_MSG_LEN)) return; ShareMemRead(TempBuff, StartAddr+2, 2); WritePointer = TempBuff[0]*256+TempBuff[1]; for(i=0; i<nLen; i++) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -