You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

4507 lines
112 KiB
C++

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/*****************************************************************************
* FileName : commport.c *
* Programmer : Li Liangchu *
* Writen at : 2004.07.22 *
* Version : *
* Description: *
* Last modify: 2002.11.6 *
*****************************************************************************/
//#include "stdafx.h"
//#include "resource.h"
#include "commport.h"
#include "common.h"
#include "udpcomm.h"
#include "udpping.h"
#include "tcphost.h"
#include "netport.h"
#ifdef _WIN32
#include <winsock.h>
#else
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <math.h>
#include "inifile.h"
#endif
//#include "..\confrwlib\confrw.h"
#define MAX_STRING_LEN 512
extern int CurPort;
extern int RealDataDispFlag;
extern int iCurDevIdx;
// extern DEV_DEF DevParam[];
// extern UDP_SET_DEF UdpParam;
extern int RealDataDispFlag;
FUNCTION_CALL *FunCallPtr;
SIO_PARAM_DEF SioParam[DEFAULT_MAX_PORT_NUM];
int ProvPortCnt = 0;
u_short UdpCommPort;
u_short MaxSerialPortNum;
u_short MaxNetPortNum;
u_short MaxPortNum;
void * pvconf_handle = NULL;
void * pvportconf_handle = NULL;
void * pvdataconf_handle = NULL;
char pvDataFileName[256];
char DeviceName[256];
char Version[256];
char VersionDate[256];
char Manufacturer[256];
char IniFilePath[256];
DAY_TIME DayTime;
u_long GetPrivateProString(const char *section, const char *entry, const char *defaultstr, char *result, int len, char *inifile)
{
int ret = SUCCESS;
char szConfig[256], szPortConfig[256];
//GetCurrentDirectory(sizeof(szDir), szDir);
sprintf(szConfig, "%s/config.ini", IniFilePath);
sprintf(szPortConfig, "%s/portconfig.ini", IniFilePath);
//GetPrivateProfileString(section, entry, defaultstr, result, len, inifile);
if( (strcmp(inifile, szConfig) == 0) && (pvconf_handle != NULL) ) //config.ini
{
ret = conf_read_key ( pvconf_handle,
section,
entry,
defaultstr,
result,
len);
}
else if( (strcmp(inifile, szPortConfig) == 0) && (pvportconf_handle != NULL) ) //portconfig.ini
{
ret = conf_read_key ( pvportconf_handle,
section,
entry,
defaultstr,
result,
len);
}
else //其它情况使用老办法读配置
{
#ifdef _WIN32
GetPrivateProfileString((LPWSTR)section, (LPWSTR)entry, (LPWSTR)defaultstr, (LPWSTR)result, len, (LPWSTR)inifile);
#else
GetPrivateProfileString(section, entry, defaultstr, result, len, inifile);
#endif
printf("\nTIP_(%04d): GetPrivateProString used GetPrivateProfileString(inifile=%s,section=%s, item=%s,result=%s)\n", inifile, section, entry, result);
result[0] = 0;
}
if( SUCCESS == ret )
StringCutSpace(result);
else
result[0] = 0;
return (u_long)strlen(result);
}
int GetPrivateProInt(const char *section, const char *entry, int idefault, char *inifile)
{
int i, ilen, iRetVal;
char szResult[128];
memset(szResult, 0, sizeof(szResult));
ilen = GetPrivateProString(section, entry, "", szResult, sizeof(szResult)-1, inifile);
if(ilen <= 0)
{
iRetVal = idefault;
}
else
{
if(ilen > 2)
{
if((szResult[0]=='0') && ((szResult[1]=='x') || (szResult[1]=='X')))
{
iRetVal = 0;
for(i=2; i<ilen; i++)
{
iRetVal = (iRetVal << 4) + HexCharToInt(szResult[i]);
}
}
else
{
//iRetVal = atoul(szResult);
iRetVal = atoi(szResult);
}
}
else
{
//iRetVal = atoul(szResult);
iRetVal = atoi(szResult);
}
}
return iRetVal;
}
u_long GetPrivateProString2(const char *section, const char *entry, const char *defaultstr, char *result, int len, HDSFILE hFile)
{
int end, rlen, SectionOk;
int ilinelen, iReadLines;
char *ptr;
char fstring[MAX_STRING_LEN];
char istring[MAX_STRING_LEN];
char tmp[MAX_STRING_LEN];
char retstr[MAX_STRING_LEN];
#if 0
FILE *fp;
char szfilename[128];
#ifdef OS_LINUX
strcpy(szfilename, "/log/dsdebug.txt");
#else
strcpy(szfilename, "dsdebug.txt");
#endif
fp = fopen(szfilename, "ab");
if(fp)
{
fwrite("#####begin#####\r\n", 1, strlen("#####begin#####\r\n"), fp);
fwrite(section, 1, strlen(section), fp);
fwrite("-------", 1, 7, fp);
fwrite(entry, 1, strlen(entry), fp);
fwrite("\r\n", 1, strlen("\r\n"), fp);
fclose(fp);
}
#endif
rlen = (len < MAX_STRING_LEN) ? len : MAX_STRING_LEN;
rlen--;
// 如果缺省值的长度大于返回的最大长度
if(!defaultstr)
{
retstr[0] = 0;
}
else
{
ilinelen = (int)strlen(defaultstr);
if(ilinelen == 0)
{
retstr[0] = 0;
}
else if(ilinelen > rlen)
{
memcpy(retstr, defaultstr, rlen-1);
retstr[rlen] = 0;
}
else
{
strcpy(retstr, defaultstr);
}
}
// 首先将返回值赋值为缺省值,有且仅有在此赋值缺省值
strcpy(result, retstr);
if(NULL == hFile)
{
goto ExitFlag2;
}
sprintf(istring, "[%s]", section);
// 清除当前文件数据读取计数
DSfseek(hFile, 0, SEEK_CLR_RCNT);
SectionOk = DSHaveSection(istring, hFile);
if(DS_SECTION_SEARCH == SectionOk)
{
while(1)
{
ilinelen = DSfgetreadcount(hFile);
if(DSfgets(fstring, sizeof(fstring)-1, hFile) == NULL)
{
if(DSfeob(hFile) == 0)
{
break;
}
else
{
DSfseek(hFile, 0, SEEK_SET);
continue;
}
}
if(StringCutSpace(fstring) > 0)
{
fstring[strlen(istring)] = 0;
// if remark
if(IsRemarkLine(fstring) == TRUE)
{
continue;
}
//if find the section
if(strstr(istring, fstring) != NULL)
{
SectionOk = DS_SECTION_EXIST;
DSSaveSectionOffset(hFile);
break;
}
}
}
}
if(DS_SECTION_SEARCH == SectionOk)
{
DSRestoreSectionOffset(hFile);
SectionOk = DS_SECTION_NOEXIST;
}
if(SectionOk <= DS_SECTION_NOEXIST)
{
goto ExitFlag2;
}
iReadLines = 0;
//after find the section, continue find the entry
sprintf(istring, "%s=", entry);
while(1)
{
if(DSSectionisOver(iReadLines, hFile))
{
break;
}
if(DSfgets(fstring, sizeof(fstring)-1, hFile) == NULL)
{
// 最后一个配置段,不存在[]的边界
if(DSfeob(hFile) == 0)
{
break;
}
else
{
DSSetSectionLinesAddFlag(0, hFile);
if(DSSectionisOver(iReadLines, hFile))
{
//goto ExitFlag2;
break;
}
else
{
DSRestoreSectionOffset(hFile);
continue;
}
}
}
iReadLines++;
DSAddSectionLines(1, hFile);
if(StringCutSpace(fstring) == 0)
{
continue;
}
//just look for the string in current section
if((fstring[0] == '[') && (strstr(fstring,"]") != NULL))
{
iReadLines--;
DSSubSectionLines(1, hFile);
DSSetSectionLinesAddFlag(0, hFile);
if(DSSectionisOver(iReadLines, hFile))
{
break;
}
else
{
DSRestoreSectionOffset(hFile);
continue;
}
}
// if remark
if(IsRemarkLine(fstring) == TRUE)
{
continue;
}
strcpy(tmp, fstring);
tmp[strlen(istring)+1] = 0;
if(strstr(tmp, istring) != 0)
{
ptr = strstr(fstring, "=");
strcpy(retstr, ptr + 1);
end = strlen(retstr) - 1;
if(retstr[end] == '\\')
{
retstr[end] = 0;
for(; ;)
{
if(DSfgets(fstring, sizeof(fstring)-1, hFile) == NULL)
{
break;
}
iReadLines++;
DSAddSectionLines(1, hFile);
if(StringCutSpace(fstring) == 0)
{
break;
}
if((fstring[0] == '[') && (strstr(fstring,"]") != NULL))
{
iReadLines--;
DSSubSectionLines(1, hFile);
DSSetSectionLinesAddFlag(0, hFile);
if(!DSSectionisOver(iReadLines, hFile))
{
DSRestoreSectionOffset(hFile);
}
break;
}
end = strlen(fstring) - 1;
if(fstring[end] != '\\')
{
if((strlen(retstr)+strlen(fstring)) < sizeof(retstr))
{
strcat(retstr, fstring);
}
break;
}
else
{
fstring[end] = 0;
if((strlen(retstr)+strlen(fstring)) < sizeof(retstr))
{
strcat(retstr, fstring);
}
else
{
break;
}
}
if((int)strlen(retstr) >= rlen)
{
break;
}
}
}
// 找到合适的匹配项,直接退出
retstr[rlen] = 0;
strcpy(result, retstr);
goto ExitFlag2;
}
}
// 没有找到合适的项目,使用缺省值
ExitFlag2:
#if 0
fp = fopen(szfilename, "ab");
if(fp)
{
fwrite("#####end#####\r\n", 1, strlen("#####end#####\r\n"), fp);
fclose(fp);
}
#endif
return strlen(result);
}
int GetPrivateProInt2(const char *section, const char *entry, int idefault, HDSFILE hFile)
{
int i, ilen, iRetVal;
char szResult[128];
memset(szResult, 0, sizeof(szResult));
ilen = GetPrivateProString2(section, entry, "", szResult, sizeof(szResult)-1, hFile);
if(ilen <= 0)
{
iRetVal = idefault;
}
else
{
if(ilen > 2)
{
if((szResult[0]=='0') && ((szResult[1]=='x') || (szResult[1]=='X')))
{
iRetVal = 0;
for(i=2; i<ilen; i++)
{
iRetVal = (iRetVal << 4) + HexCharToInt(szResult[i]);
}
}
else
{
//iRetVal = atoul(szResult);
iRetVal = atoi(szResult);
}
}
else
{
//iRetVal = atoul(szResult);
iRetVal = atoi(szResult);
}
}
return iRetVal;
}
int PutDataToBuf(DATA_BUF *buf, u_char *msg, int len)
{
int j;
if (len == 0)
return 0;
if ((buf->BufSize - buf->MsgCnt) < len)
return 0;
if (buf->MsgCnt == 0)
{
buf->Front = 0;
buf->Rear = 0;
}
j = buf->BufSize - buf->Rear;
if (j >= len)
{
memmove((char*)&buf->MsgData[buf->Rear], msg, len);
buf->Rear += len;
}
else
{
memmove((char*)&buf->MsgData[buf->Rear], msg, j);
buf->Rear = (buf->Rear + j) % buf->BufSize;
memmove((char*)&buf->MsgData[buf->Rear], (char*)&msg[j], len - j);
buf->Rear = (buf->Rear + len - j) % buf->BufSize;
}
buf->MsgCnt += len;
return len;
}
int GetDataFormBuf(DATA_BUF *databuf, u_char *buf, int len, int clrflag)
{
int tmp, rdcnt;
buf[0] = 0;
if (databuf->MsgCnt == 0)
return 0;
rdcnt = min(databuf->MsgCnt, len);
if((databuf->BufSize - databuf->Front) >= rdcnt)
{
memmove((char *)buf, (char *)&databuf->MsgData[databuf->Front], rdcnt);
}
else
{
tmp = databuf->BufSize - databuf->Front;
memmove((char *)buf, (char *)&databuf->MsgData[databuf->Front], tmp);
memmove((char *)&buf[tmp], (char *)&databuf->MsgData[0], rdcnt - tmp);
}
if (clrflag)
{
databuf->Front = (databuf->Front + rdcnt) % databuf->BufSize;
databuf->MsgCnt -= rdcnt;
}
return rdcnt;
}
void GetCommMode(int port, u_char *str)
{
char *ptr;
StringCutSpace((char*)str);
if (strlen((char*)str) == 0)
return;
SioParam[port].m_psSerial->Baud = atoi((char*)str);
ptr = strstr((char*)str, ",");
ptr++;
switch(*ptr)
{
case 'o':
case 'O': // 奇校验
SioParam[port].m_psSerial->CommMode &= ~PARITYMASK;
SioParam[port].m_psSerial->CommMode |= PARITYODD;
break;
case 'e':
case 'E': // 奇校验
SioParam[port].m_psSerial->CommMode &= ~PARITYMASK;
SioParam[port].m_psSerial->CommMode |= PARITYEVE;
break;
default: // 无校验
SioParam[port].m_psSerial->CommMode &= ~PARITYMASK;
SioParam[port].m_psSerial->CommMode |= PARITYNONE;
break;
}
ptr = strstr(ptr, ",");
ptr++;
switch(*ptr)
{
case '5':
SioParam[port].m_psSerial->CommMode &= ~BITMASK;
SioParam[port].m_psSerial->CommMode |= BIT5;
break;
case '6':
SioParam[port].m_psSerial->CommMode &= ~BITMASK;
SioParam[port].m_psSerial->CommMode |= BIT6;
break;
case '7':
SioParam[port].m_psSerial->CommMode &= ~BITMASK;
SioParam[port].m_psSerial->CommMode |= BIT7;
break;
default:
SioParam[port].m_psSerial->CommMode &= ~BITMASK;
SioParam[port].m_psSerial->CommMode |= BIT8;
break;
}
switch(str[strlen((char*)str)-1])
{
case '1':
SioParam[port].m_psSerial->CommMode &= ~STOPMASK;
SioParam[port].m_psSerial->CommMode |= STOP1;
break;
case '5':
SioParam[port].m_psSerial->CommMode &= ~STOPMASK;
SioParam[port].m_psSerial->CommMode |= STOP15;
break;
case '2':
SioParam[port].m_psSerial->CommMode &= ~STOPMASK;
SioParam[port].m_psSerial->CommMode |= STOP2;
break;
}
}
int GetPortProtocolIdx(char *name)
{
int i;
for (i = 0; ; i++)
{
if (FunCallPtr[i].ProtocolName == 0)
break;
if (CmpString((u_char*)FunCallPtr[i].ProtocolName, (u_char*)name))
{
return i;
}
}
return 0;
}
char *GetPortProtocolName(int Idx)
{
int i;
for(i=0; i<=Idx; i++)
{
if(FunCallPtr[i].ProtocolName == 0)
break;
if(i == Idx)
{
break;
}
}
if(FunCallPtr[i].ProtocolName == 0)
{
return NULL;
}
return FunCallPtr[i].ProtocolName;
}
void ReadPortConfigInfo(u_short netport)
{
int i, j;
char *ptr;
char entry[256], szbuf[128];
MaxSerialPortNum = DEFAULT_MAX_SERIAL_PORT_NUM;
MaxNetPortNum = netport;
if (MaxNetPortNum > DEFAULT_MAX_NET_PORT_NUM)
MaxNetPortNum = DEFAULT_MAX_NET_PORT_NUM;
MaxPortNum = MaxSerialPortNum + MaxNetPortNum;
char szConfig[256], szPortConfig[256];
//GetCurrentDirectory(sizeof(szDir), szDir);
sprintf(szConfig, "%s/config.ini", IniFilePath);
sprintf(szPortConfig, "%s/portconfig.ini", IniFilePath);
//GetPrivateProString("NetCommPort", "UdpCommPort", "", szbuf, 120, "./config.ini");
GetPrivateProString("NetCommPort", "UdpCommPort", "", szbuf, 120, szConfig);
if (strlen(szbuf))
UdpCommPort = (u_short)atoul(szbuf);
else
UdpCommPort = DEFAULT_UDP_COMM_PORT;
#ifdef _OS_WINDOWS_DEBUG_
SYSTEMTIME sm;
GetLocalTime(&sm);
#endif
memset((char*)SioParam, 0, sizeof(SIO_PARAM_DEF)*MaxPortNum);
// 读端口信息
for (i = 0; i < MaxPortNum; i++)
{
// wen test debug
#ifdef _OS_WINDOWS_DEBUG_
//sprintf(szbuf, "初始化端口%d... ...\n", i+1);
sprintf(szbuf, "%02d_%02d:%02d:%02d.%03d 初始化端口%d... ...\n", sm.wDay, sm.wHour,
sm.wMinute, sm.wSecond, sm.wMilliseconds, i+1);
OutputDebugString((LPCWSTR)szbuf);
#elif _DEBUG
printf("初始化端口%d... ...\n", i+1);
#endif
SioParam[i].m_psBaoHu = (SIO_PARAM_BAOHU_DEF *)malloc(sizeof(SIO_PARAM_BAOHU_DEF));
memset((void *)SioParam[i].m_psBaoHu, 0, sizeof(SIO_PARAM_BAOHU_DEF));
SioParam[i].m_psSerial = (SIO_PARAM_SERIAL_DEF *)malloc(sizeof(SIO_PARAM_SERIAL_DEF));
memset((void *)SioParam[i].m_psSerial, 0, sizeof(SIO_PARAM_SERIAL_DEF));
SioParam[i].RecvBuf.BufSize = MAX_MSG_BUF_SIZE;
for (j = 0; j < POLL_CMD_TYPE_NUM; j++)
SioParam[i].m_psBaoHu->PollCmd[j].BufSize = MAX_MSG_BUF_SIZE;
sprintf(entry, "Port%d", i+1);
GetPrivateProString(entry, "Protocol", "", szbuf, 120, szPortConfig);
SioParam[i].ProtocolIdx = GetPortProtocolIdx(szbuf);
StringToUpper(szbuf);
SioParam[i].ByPassFlag = CmpString((u_char*)szbuf, (u_char*)"bypass");
// 串行端口
if (i < MaxSerialPortNum)
{
SioParam[i].m_psSerial->Baud = DEFAULT_BAUD;
SioParam[i].m_psSerial->CommMode = BIT8 | STOP1 | PARITYNONE;
SioParam[i].m_psSerial->WorkMode = RS232_COMM_MODE;
SioParam[i].m_psSerial->HandshakeMode = NO_HANDSHAKE_MODE;
SioParam[i].m_psSerial->ChSelect = 0;
SioParam[i].m_psSerial->FreqSelect = 0;
SioParam[i].m_psSerial->ChipCheckOk = 1;
GetPrivateProString("PortConfig", entry, "", szbuf, 16, szConfig);
if (strlen(szbuf))
{
SioParam[i].m_psSerial->ChSelect = atoi(szbuf);
ptr = strstr(szbuf, ",");
if (ptr)
{
ptr++;
SioParam[i].m_psSerial->FreqSelect = atoi(ptr);
}
}
GetPrivateProString(entry, "CommMode", "", szbuf, 120, szPortConfig);
GetCommMode(i, (u_char*)szbuf);
GetPrivateProString(entry, "WorkMode", "", szbuf, 120, szPortConfig);
StringCutSpace(szbuf);
if (strstr(szbuf, "422"))
SioParam[i].m_psSerial->WorkMode = RS422_COMM_MODE;
if (strstr(szbuf, "485"))
SioParam[i].m_psSerial->WorkMode = RS485_COMM_MODE;
GetPrivateProString(entry, "HandshakeMode", "", szbuf, 120, szPortConfig);
StringCutSpace(szbuf);
StringToLower(szbuf);
if (strstr(szbuf, "HARDWARE"))
SioParam[i].m_psSerial->HandshakeMode = HARDWARE_MODE;
if (strstr(szbuf, "SOFTWARE"))
SioParam[i].m_psSerial->HandshakeMode = SOFTWARE_MODE;
GetPrivateProString(entry, "XonXoff", "",szbuf, 120, szPortConfig);
SioParam[i].m_psSerial->XonChar1 = HexCharToInt(szbuf[0]) * 16 + HexCharToInt(szbuf[1]);
SioParam[i].m_psSerial->XoffChar1 = HexCharToInt(szbuf[2]) * 16 + HexCharToInt(szbuf[3]);
SioParam[i].m_psSerial->XonChar2 = SioParam[i].m_psSerial->XonChar1;
SioParam[i].m_psSerial->XoffChar2 = SioParam[i].m_psSerial->XoffChar1;
// 在嵌入式系统中,读、写、数据处理是分离的进程,但是数据处理时会
// 用到波特率,而波特率只有在串口通讯中存在,数据处理进程不含有串
// 口通讯指针信息,所以需要在此重新赋值一次。
SioParam[i].m_psBaoHu->Baud = SioParam[i].m_psSerial->Baud;
}
else
{
SioParam[i].NetType = NO_COMM;
SioParam[i].m_psBaoHu->Baud = DEFAULT_BAUD;
GetPrivateProString(entry, "NetPort", "", szbuf, 120, szPortConfig);
StringToLower(szbuf);
if ((szbuf[0] == '0') && (szbuf[1] == 'X'))
{
SioParam[i].NetPort = (HexCharToInt(szbuf[2]) << 12)
| (HexCharToInt(szbuf[3]) << 8)
| (HexCharToInt(szbuf[4]) << 4)
| HexCharToInt(szbuf[5]);
}
else
{
SioParam[i].NetPort = atoi(szbuf);
}
if (SioParam[i].NetPort == 0)
continue;
GetPrivateProString(entry, "NetCommAddr", "", szbuf, 120, szPortConfig);
SioParam[i].NetCommIpAddr = inet_addr(szbuf);
if ((SioParam[i].NetPort == 0) || (SioParam[i].NetCommIpAddr == 0))
{
SioParam[i].NetPort = 0;
SioParam[i].NetCommIpAddr = 0;
continue;
}
GetPrivateProString(entry, "NetCommMode", "", szbuf, 120, szPortConfig);
StringCutSpace(szbuf);
if (strlen(szbuf) == 0)
continue;
StringToLower(szbuf);
if (strstr(szbuf, "UDP"))
SioParam[i].NetType = UDP_COMM;
if (strstr(szbuf, "TCP,SERVER"))
SioParam[i].NetType = TCP_S_COMM;
if (strstr(szbuf, "TCP,CLIENT"))
SioParam[i].NetType = TCP_C_COMM;
}
// 读端口测点数量
GetPrivateProString(entry, "AiNum", "0", szbuf, 120, szPortConfig);
SioParam[i].m_psBaoHu->AiNum = atoi(szbuf);
GetPrivateProString(entry, "DiNum", "0", szbuf, 120, szPortConfig);
SioParam[i].m_psBaoHu->DiNum = atoi(szbuf);
GetPrivateProString(entry, "PiNum", "0", szbuf, 120, szPortConfig);
SioParam[i].m_psBaoHu->PiNum = atoi(szbuf);
// 读 Poll Command 时间常数
GetPrivateProString(entry, "Retry", "", szbuf, 120, szPortConfig);
SioParam[i].m_psBaoHu->Retry = (u_long)atoi(szbuf);
GetPrivateProString(entry, "RetryTime", "3", szbuf, 120, szPortConfig);
SioParam[i].m_psBaoHu->RetryTime = (u_long)((atof(szbuf) * 1000) / TIMER_CNT);
GetPrivateProString(entry, "WaitTime", "", szbuf, 120, szPortConfig);
SioParam[i].m_psBaoHu->WaitTime = (u_long)((atof(szbuf) * 1000) / TIMER_CNT);
// wen 2004.10.26 增加数据点的内存分配(先分配成接收规约内存区)
//PortMemoryMalloc(i, RECV_PROTOCOL);
GetPrivateProString(entry, "PortType", "0", szbuf, 120, szPortConfig);
SioParam[i].m_psBaoHu->PortType = (u_char)atoi(szbuf);
GetPrivateProString(entry, "CheckTime", "no", szbuf, 120, szPortConfig);
if(_stricmp(szbuf, "up") == 0)
SioParam[i].m_psBaoHu->CheckTime = 1;
else if(_stricmp(szbuf, "down") == 0)
SioParam[i].m_psBaoHu->CheckTime = 2;
else
SioParam[i].m_psBaoHu->CheckTime = 0;
// wen 2005.03.01 增加遥控遥调转发处理
//"ProvYkYtTimeOut"="转发遥控超时时间"
#ifdef _WIN32
GetPrivateProfileString((LPWSTR)entry, (LPWSTR)"ProvYkYtTimeOut", (LPWSTR)"10", (LPWSTR)szbuf, 120, (LPWSTR)szPortConfig);
#else
GetPrivateProfileString(entry, "ProvYkYtTimeOut", "10", szbuf, 120, szPortConfig);
#endif
SioParam[i].m_psBaoHu->ProvYkYtMsg.m_iTimeOut = atoi(szbuf);
#ifdef _OS_WINDOWS_DEBUG_
GetLocalTime(&sm);
sprintf(szbuf, "%02d_%02d:%02d:%02d.%03d 内存分配%d开始\n", sm.wDay, sm.wHour,
sm.wMinute, sm.wSecond, sm.wMilliseconds, i+1);
OutputDebugString((LPCWSTR)szbuf);
#endif
// 数据库点内存分配
PortMemoryMalloc(i, SioParam[i].m_psBaoHu->PortType, SioParam[i].m_psBaoHu);
// 读规约配置
if (FunCallPtr[SioParam[i].ProtocolIdx].ReadConfig)
{
FunCallPtr[SioParam[i].ProtocolIdx].ReadConfig(i);
}
#ifdef _OS_WINDOWS_DEBUG_
GetLocalTime(&sm);
// wen test debug
//sprintf(szbuf, "初始化端口%d成功\n", i+1);
sprintf(szbuf, "%02d_%02d:%02d:%02d.%03d 初始化端口%d成功\n", sm.wDay, sm.wHour,
sm.wMinute, sm.wSecond, sm.wMilliseconds, i+1);
OutputDebugString((LPCWSTR)szbuf);
OutputDebugString((LPCWSTR)"\n");
#elif _OS_WINDOWS_DEBUG_
printf("初始化端口%d成功\n", i+1);
#endif
}
}
void ReadPortConfigInfoEx(u_short netport)
{
int i, j;
char *ptr;
char entry[256], szbuf[128];
MaxSerialPortNum = DEFAULT_MAX_SERIAL_PORT_NUM;
MaxNetPortNum = netport;
if (MaxNetPortNum > DEFAULT_MAX_NET_PORT_NUM)
MaxNetPortNum = DEFAULT_MAX_NET_PORT_NUM;
MaxPortNum = MaxSerialPortNum + MaxNetPortNum;
char szConfig[256], szPortConfig[256];
//GetCurrentDirectory(sizeof(szDir), szDir);
sprintf(szConfig, "%s/config.ini", IniFilePath);
sprintf(szPortConfig, "%s/portconfig.ini", IniFilePath);
//GetPrivateProString("NetCommPort", "UdpCommPort", "", szbuf, 120, "./config.ini");
GetPrivateProString("NetCommPort", "UdpCommPort", "", szbuf, 120, szConfig);
if (strlen(szbuf))
UdpCommPort = (u_short)atoul(szbuf);
else
UdpCommPort = DEFAULT_UDP_COMM_PORT;
#ifdef _OS_WINDOWS_DEBUG_
SYSTEMTIME sm;
GetLocalTime(&sm);
#endif
memset((char*)SioParam, 0, sizeof(SIO_PARAM_DEF)*MaxPortNum);
// 读端口信息
for (i = 0; i < MaxPortNum; i++)
{
// wen test debug
#ifdef _OS_WINDOWS_DEBUG_
//sprintf(szbuf, "初始化端口%d... ...\n", i+1);
sprintf(szbuf, "%02d_%02d:%02d:%02d.%03d 初始化端口%d... ...\n", sm.wDay, sm.wHour,
sm.wMinute, sm.wSecond, sm.wMilliseconds, i+1);
OutputDebugString((LPCWSTR)szbuf);
#elif _OS_WINDOWS_DEBUG_
printf("初始化端口%d... ...\n", i+1);
#endif
SioParam[i].m_psBaoHu = (SIO_PARAM_BAOHU_DEF *)malloc(sizeof(SIO_PARAM_BAOHU_DEF));
memset((void *)SioParam[i].m_psBaoHu, 0, sizeof(SIO_PARAM_BAOHU_DEF));
SioParam[i].m_psSerial = (SIO_PARAM_SERIAL_DEF *)malloc(sizeof(SIO_PARAM_SERIAL_DEF));
memset((void *)SioParam[i].m_psSerial, 0, sizeof(SIO_PARAM_SERIAL_DEF));
SioParam[i].RecvBuf.BufSize = MAX_MSG_BUF_SIZE;
for (j = 0; j < POLL_CMD_TYPE_NUM; j++)
SioParam[i].m_psBaoHu->PollCmd[j].BufSize = MAX_MSG_BUF_SIZE;
sprintf(entry, "Port%d", i+1);
GetPrivateProString(entry, "Protocol", "", szbuf, 120, szPortConfig);
SioParam[i].ProtocolIdx = GetPortProtocolIdx(szbuf);
StringToUpper(szbuf);
SioParam[i].ByPassFlag = CmpString((u_char*)szbuf, (u_char*)"bypass");
// 串行端口
if (i < MaxSerialPortNum)
{
SioParam[i].m_psSerial->Baud = DEFAULT_BAUD;
SioParam[i].m_psSerial->CommMode = BIT8 | STOP1 | PARITYNONE;
SioParam[i].m_psSerial->WorkMode = RS232_COMM_MODE;
SioParam[i].m_psSerial->HandshakeMode = NO_HANDSHAKE_MODE;
SioParam[i].m_psSerial->ChSelect = 0;
SioParam[i].m_psSerial->FreqSelect = 0;
SioParam[i].m_psSerial->ChipCheckOk = 1;
GetPrivateProString("PortConfig", entry, "", szbuf, 16, szConfig);
if (strlen(szbuf))
{
SioParam[i].m_psSerial->ChSelect = atoi(szbuf);
ptr = strstr(szbuf, ",");
if (ptr)
{
ptr++;
SioParam[i].m_psSerial->FreqSelect = atoi(ptr);
}
}
GetPrivateProString(entry, "CommMode", "", szbuf, 120, szPortConfig);
GetCommMode(i, (u_char*)szbuf);
GetPrivateProString(entry, "WorkMode", "", szbuf, 120, szPortConfig);
StringCutSpace(szbuf);
if (strstr(szbuf, "422"))
SioParam[i].m_psSerial->WorkMode = RS422_COMM_MODE;
if (strstr(szbuf, "485"))
SioParam[i].m_psSerial->WorkMode = RS485_COMM_MODE;
GetPrivateProString(entry, "HandshakeMode", "", szbuf, 120, szPortConfig);
StringCutSpace(szbuf);
StringToLower(szbuf);
if (strstr(szbuf, "HARDWARE"))
SioParam[i].m_psSerial->HandshakeMode = HARDWARE_MODE;
if (strstr(szbuf, "SOFTWARE"))
SioParam[i].m_psSerial->HandshakeMode = SOFTWARE_MODE;
GetPrivateProString(entry, "XonXoff", "",szbuf, 120, szPortConfig);
SioParam[i].m_psSerial->XonChar1 = HexCharToInt(szbuf[0]) * 16 + HexCharToInt(szbuf[1]);
SioParam[i].m_psSerial->XoffChar1 = HexCharToInt(szbuf[2]) * 16 + HexCharToInt(szbuf[3]);
SioParam[i].m_psSerial->XonChar2 = SioParam[i].m_psSerial->XonChar1;
SioParam[i].m_psSerial->XoffChar2 = SioParam[i].m_psSerial->XoffChar1;
// 在嵌入式系统中,读、写、数据处理是分离的进程,但是数据处理时会
// 用到波特率,而波特率只有在串口通讯中存在,数据处理进程不含有串
// 口通讯指针信息,所以需要在此重新赋值一次。
SioParam[i].m_psBaoHu->Baud = SioParam[i].m_psSerial->Baud;
}
else
{
SioParam[i].NetType = NO_COMM;
SioParam[i].m_psBaoHu->Baud = DEFAULT_BAUD;
SioParam[i].CommSock = -1;
SioParam[i].LiSock = -1;
GetPrivateProString(entry, "NetPort", "", szbuf, 120, szPortConfig);
StringToLower(szbuf);
if ((szbuf[0] == '0') && (szbuf[1] == 'X'))
{
SioParam[i].NetPort = (HexCharToInt(szbuf[2]) << 12)
| (HexCharToInt(szbuf[3]) << 8)
| (HexCharToInt(szbuf[4]) << 4)
| HexCharToInt(szbuf[5]);
}
else
{
SioParam[i].NetPort = atoi(szbuf);
}
if (SioParam[i].NetPort == 0)
{
//continue;
goto mallocdbflag;
}
GetPrivateProString(entry, "NetCommAddr", "", szbuf, 120, szPortConfig);
SioParam[i].NetCommIpAddr = inet_addr(szbuf);
if ((SioParam[i].NetPort == 0) || (SioParam[i].NetCommIpAddr == 0))
{
SioParam[i].NetPort = 0;
SioParam[i].NetCommIpAddr = 0;
//continue;
goto mallocdbflag;
}
GetPrivateProString(entry, "NetCommMode", "", szbuf, 120, szPortConfig);
StringCutSpace(szbuf);
if(strlen(szbuf) == 0)
{
//continue;
goto mallocdbflag;
}
StringToUpper(szbuf);
if (strstr(szbuf, "UDP"))
SioParam[i].NetType = UDP_COMM;
if (strstr(szbuf, "TCP,SERVER"))
SioParam[i].NetType = TCP_S_COMM;
if (strstr(szbuf, "TCP,CLIENT"))
SioParam[i].NetType = TCP_C_COMM;
}
mallocdbflag:
// 读端口测点数量
GetPrivateProString(entry, "AiNum", "0", szbuf, 120, szPortConfig);
SioParam[i].m_psBaoHu->AiNum = atoi(szbuf);
GetPrivateProString(entry, "DiNum", "0", szbuf, 120, szPortConfig);
SioParam[i].m_psBaoHu->DiNum = atoi(szbuf);
GetPrivateProString(entry, "PiNum", "0", szbuf, 120, szPortConfig);
SioParam[i].m_psBaoHu->PiNum = atoi(szbuf);
// 读 Poll Command 时间常数
GetPrivateProString(entry, "Retry", "", szbuf, 120, szPortConfig);
SioParam[i].m_psBaoHu->Retry = (u_long)atoi(szbuf);
GetPrivateProString(entry, "RetryTime", "3", szbuf, 120, szPortConfig);
SioParam[i].m_psBaoHu->RetryTime = (u_long)((atof(szbuf) * 1000) / TIMER_CNT);
GetPrivateProString(entry, "WaitTime", "", szbuf, 120, szPortConfig);
SioParam[i].m_psBaoHu->WaitTime = (u_long)((atof(szbuf) * 1000) / TIMER_CNT);
// wen 2004.10.26 增加数据点的内存分配(先分配成接收规约内存区)
//PortMemoryMalloc(i, RECV_PROTOCOL);
GetPrivateProString(entry, "PortType", "0", szbuf, 120, szPortConfig);
SioParam[i].m_psBaoHu->PortType = (u_char)atoi(szbuf);
GetPrivateProString(entry, "CheckTime", "no", szbuf, 120, szPortConfig);
if(_stricmp(szbuf, "up") == 0)
SioParam[i].m_psBaoHu->CheckTime = 1;
else if(_stricmp((const char*)szbuf, "down") == 0)
SioParam[i].m_psBaoHu->CheckTime = 2;
else
SioParam[i].m_psBaoHu->CheckTime = 0;
// wen 2005.03.01 增加遥控遥调转发处理
//"ProvYkYtTimeOut"="转发遥控超时时间"
#ifdef _WIN32
GetPrivateProfileString((LPWSTR)entry, (LPWSTR)"ProvYkYtTimeOut", (LPWSTR)"10", (LPWSTR)szbuf, 120, (LPWSTR)szPortConfig);
#else
GetPrivateProfileString(entry, "ProvYkYtTimeOut", "10", szbuf, 120, szPortConfig);
#endif
SioParam[i].m_psBaoHu->ProvYkYtMsg.m_iTimeOut = atoi(szbuf);
#ifdef _OS_WINDOWS_DEBUG_
GetLocalTime(&sm);
sprintf(szbuf, "%02d_%02d:%02d:%02d.%03d 内存分配%d开始\n", sm.wDay, sm.wHour,
sm.wMinute, sm.wSecond, sm.wMilliseconds, i+1);
OutputDebugString((LPCWSTR)szbuf);
#endif
// 数据库点内存分配
PortMemoryMallocEx(i, SioParam[i].m_psBaoHu->PortType, SioParam[i].m_psBaoHu);
// 读规约配置
if (FunCallPtr[SioParam[i].ProtocolIdx].ReadConfig)
{
FunCallPtr[SioParam[i].ProtocolIdx].ReadConfig(i);
}
#ifdef _OS_WINDOWS_DEBUG_
GetLocalTime(&sm);
// wen test debug
//sprintf(szbuf, "初始化端口%d成功\n", i+1);
sprintf(szbuf, "%02d_%02d:%02d:%02d.%03d 初始化端口%d成功\n", sm.wDay, sm.wHour,
sm.wMinute, sm.wSecond, sm.wMilliseconds, i+1);
OutputDebugString((LPCWSTR)szbuf);
OutputDebugString((LPCWSTR)"\n");
#elif _OS_WINDOWS_DEBUG_
printf("初始化端口%d成功\n", i+1);
#endif
}
for(i = 0; i < MaxPortNum; i++)
{
#ifdef _OS_WINDOWS_DEBUG_
//sprintf(szbuf, "初始化端口%d... ...\n", i+1);
sprintf(szbuf, "%02d_%02d:%02d:%02d.%03d 初始化端口%d数据库... ...\n", sm.wDay, sm.wHour,
sm.wMinute, sm.wSecond, sm.wMilliseconds, i+1);
OutputDebugString((LPCWSTR)szbuf);
#elif _OS_WINDOWS_DEBUG_
printf("初始化端口%d数据库... ...\n", i+1);
#endif
PortDataPntInit(i, SioParam[i].m_psBaoHu->PortType, SioParam[i].m_psBaoHu);
#ifdef _OS_WINDOWS_DEBUG_
GetLocalTime(&sm);
// wen test debug
//sprintf(szbuf, "初始化端口%d成功\n", i+1);
sprintf(szbuf, "%02d_%02d:%02d:%02d.%03d 初始化端口%d数据库成功\n", sm.wDay, sm.wHour,
sm.wMinute, sm.wSecond, sm.wMilliseconds, i+1);
OutputDebugString((LPCWSTR)szbuf);
OutputDebugString((LPCWSTR)"\n");
#elif _OS_WINDOWS_DEBUG_
printf("初始化端口%d数据库成功\n", i+1);
#endif
}
}
/*void OpenAllPort(void)
{
int i;
for (i = 0; i < MaxPortNum; i++)
{
//if (SioParam[i].OpenFlag)
if(ShmGetPortFlag(i, FLAG_OPEN))
continue;
if (SioParam[i].ProtocolIdx == 0)
continue;
SioParam[i].OpenFlag = 1;
}
}
*/
void OpenAllPort(void)
{
int i;
// 只打开有效使用的端口,规约为"none"的视为无效端口
for (i = 0; i < MaxPortNum; i++)
{
//commid = ValidPort[i];
if (ShmGetPortFlag(i, FLAG_OPEN))
continue;
/*if (OPEN_PORT == SioParam[commid].OpenFlag)
{
SioParam[commid].iCreateFailCnt = 0;
continue;
}*/
if (SioParam[i].ProtocolIdx == 0)
continue;
if (i < MaxSerialPortNum)
{
//OpenAllSerialPort(commid);
continue;
}
// 增加端口连续10次连接不上的处理方式如果10次没有连接上则等两分钟之后重新连接
/*if ((SioParam[commid].iCreateFailCnt > 10) && (SioParam[commid].iCreateFailCnt < 120))
{
SioParam[commid].iCreateFailCnt++;
continue;
}
if (SioParam[commid].iCreateFailCnt >= 120)
SioParam[commid].iCreateFailCnt = 0;*/
OpenNetPort(i);
//SioParam[commid].iCreateFailCnt++;
}
}
void CloseOnePort(int commid)
{
SioParam[commid].OpenFlag = 0;
// wen 2004.10.26 删除分配的内存空间
//PortMemoryFree(commid);
PortMemoryFree(commid, &SioParam[commid]);
}
void CloseAllPort(void)
{
int i;
for (i = 0; i < MaxPortNum; i++)
CloseOnePort(i);
}
//*******************************************************************
// 通讯程序 *
//*******************************************************************
int SetPntMsg(int port, int point, void *pntmsg, BYTE type, BYTE attr)
{
int i, j, k;
AI_DEF *aiptr, *aipntmsg;
DI_DEF *diptr, *dipntmsg;
PI_DEF *piptr, *pipntmsg;
PROV_AI_PNT *aiprovptr;
PROV_DI_PNT *diprovptr;
PROV_PI_PNT *piprovptr;
SOE_DEF SoeData, *pSoeMsg;
BOOL bRetVal;
bRetVal = false;
if ((port < 0) || (port >= MaxPortNum))
return bRetVal;
if (!IsBaoHuPtr(port))
return bRetVal;
if (RECV_PROTOCOL != SioParam[port].m_psBaoHu->PortType )
return bRetVal;
switch (type)
{
case AI_PNT_TYPE:
if(!SioParam[port].m_psBaoHu->AiPtr)
{
break;
}
if ((point < 0) || (point >= SioParam[port].m_psBaoHu->AiNum))
{
break;
}
aipntmsg = (AI_DEF*)pntmsg;
aiptr = (AI_DEF*)SioParam[port].m_psBaoHu->AiPtr;
switch (attr)
{
case PNT_ENABLE:
aiptr[point].Enable = aipntmsg->Enable;
bRetVal = true;
break;
case PNT_STATE:
aiptr[point].State = aipntmsg->State;
bRetVal = true;
break;
case PNT_POLAR:
aiptr[point].Polar = aipntmsg->Polar;
bRetVal = true;
break;
case PNT_RAWVALUE:
//SioParam[port].Status = TRUE;
if (abs((long)(aiptr[point].RawValue - aipntmsg->RawValue)) >= 1)
{
aiptr[point].RawValue = aipntmsg->RawValue;
aiptr[point].State = 1;
// 定义前置机功能
#ifdef FUNCTION_FEND
WriteChangeData(port, point, aipntmsg, AI_PNT_TYPE);
#endif
for (i = 0; i < MAX_PROV_PORT_NUM; i++)
{
if (aiptr[point].Prov[i].Enable == 0)
continue;
j = aiptr[point].Prov[i].PortNo;
k = aiptr[point].Prov[i].PntNo;
if((PROV_PROTOCOL == SioParam[j].m_psBaoHu->PortType)
&& SioParam[j].m_psBaoHu)
{
if(SioParam[j].m_psBaoHu->AiPtr)
{
aiprovptr = (PROV_AI_PNT*)SioParam[j].m_psBaoHu->AiPtr;
aiprovptr[k].ChangeFlag = 1;
// wen 2004.11.19 增加遥测变化数据的处理
SioParam[j].m_psBaoHu->AiChange = TRUE;
}
}
}
// wen 2004.11.19 增加遥测变化数据的处理
SioParam[port].m_psBaoHu->AiChange = TRUE;
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// wen 2005.12.15 增加设置点数据值时,可以激活端口状态
SioParam[port].LineCommCnt = 0;
SioParam[port].Status = TRUE;
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++
bRetVal = true;
break;
case PNT_PROV_MSG:
for (i = 0; i < MAX_PROV_PORT_NUM; i++)
{
if (aiptr[point].Prov[i].Enable == 0)
continue;
if (aiptr[point].Prov[i].PortNo == aipntmsg->Prov[0].PortNo)
{
aiptr[point].Prov[i].PntNo = aipntmsg->Prov[0].PntNo;
aiptr[point].Prov[i].Enable = 1;
bRetVal = true;
break;
}
}
if(bRetVal)
{
break;
}
for (i = 0; i < MAX_PROV_PORT_NUM; i++)
{
if (aiptr[point].Prov[i].Enable)
continue;
aiptr[point].Prov[i].PortNo = aipntmsg->Prov[0].PortNo;
aiptr[point].Prov[i].PntNo = aipntmsg->Prov[0].PntNo;
aiptr[point].Prov[i].Enable = 1;
bRetVal = true;
break;
}
break;
}
break;
case DI_PNT_TYPE:
if (!SioParam[port].m_psBaoHu->DiPtr)
break;
// wen 2005.09.19
if((point < 0) || (point >= SioParam[port].m_psBaoHu->DiNum))
{
break;
}
dipntmsg = (DI_DEF*)pntmsg;
diptr = (DI_DEF*)SioParam[port].m_psBaoHu->DiPtr;
switch (attr)
{
case PNT_ENABLE:
diptr[point].Enable = dipntmsg->Enable;
bRetVal = true;
break;
case PNT_STATE:
diptr[point].State = dipntmsg->State;
bRetVal = true;
break;
case PNT_SOE_TIME:
memmove((char*)&diptr[point].SoeTime, (char*)&dipntmsg->SoeTime,
sizeof(DAY_TIME));
// wen 2004.11.16 增加soe数据的转发存储(是否增加soe点的配置???)
memset((void *)&SoeData, 0, sizeof(SOE_DEF));
memcpy((void *)&SoeData.SoeTime, (void*)&dipntmsg->SoeTime,
sizeof(DAY_TIME));
SoeData.bStatus = dipntmsg->Status;
// 一般为ASDU1, 即扩展字节无意义
SoeData.u8Type = 1;
for(i=0; i<MAX_PROV_PORT_NUM; i++)
{
if(diptr[point].Prov[i].Enable == 0)
continue;
SoeData.iPntNo = diptr[point].Prov[i].PntNo;
ProvWriteSoeData(diptr[point].Prov[i].PortNo, &SoeData);
}
// 定义前置机功能
#ifdef FUNCTION_FEND
SoeData.iPntNo = point;
SetSoeData(port, &SoeData);
#endif
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// wen 2005.12.15 增加设置点数据值时,可以激活端口状态
SioParam[port].LineCommCnt = 0;
SioParam[port].Status = TRUE;
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++
bRetVal = true;
break;
case PNT_SOE_TIME_EX:
pSoeMsg = (SOE_DEF*)pntmsg;
// test
if(pSoeMsg->SoeTime.Hour == 6)
{
pSoeMsg->SoeTime.Hour = 6;
}
memmove((char *)&diptr[point].SoeTime, (char *)&pSoeMsg->SoeTime,
sizeof(DAY_TIME));
memcpy((void *)&SoeData, (void*)pSoeMsg, sizeof(SOE_DEF));
for(i=0; i<MAX_PROV_PORT_NUM; i++)
{
if(diptr[point].Prov[i].Enable == 0)
continue;
SoeData.iPntNo = diptr[point].Prov[i].PntNo;
ProvWriteSoeData(diptr[point].Prov[i].PortNo, &SoeData);
}
// 定义前置机功能
#ifdef FUNCTION_FEND
SoeData.iPntNo = point;
SetSoeData(port, &SoeData);
#endif
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// wen 2005.12.15 增加设置点数据值时,可以激活端口状态
SioParam[port].LineCommCnt = 0;
SioParam[port].Status = TRUE;
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++
bRetVal = true;
break;
case PNT_STATUS:
//SioParam[port].Status = TRUE;
dipntmsg->Status &= 0x01;
if (diptr[point].Status != dipntmsg->Status)
{
for (i = 0; i < MAX_PROV_PORT_NUM; i++)
{
if (diptr[point].Prov[i].Enable == 0)
continue;
j = diptr[point].Prov[i].PortNo;
k = diptr[point].Prov[i].PntNo;
if((SioParam[j].m_psBaoHu->PortType == PROV_PROTOCOL)
&& SioParam[j].m_psBaoHu)
{
if(SioParam[j].m_psBaoHu->DiPtr)
{
diprovptr = (PROV_DI_PNT*)SioParam[j].m_psBaoHu->DiPtr;
diprovptr[k].ChangeFlag = 1;
// wen 2004.11.19 增加遥信变化数据的处理
SioParam[j].m_psBaoHu->DiChange = TRUE;
}
}
}
// wen 2004.11.19 增加遥信变化数据的处理
SioParam[port].m_psBaoHu->DiChange = TRUE;
// 定义前置机功能
#ifdef FUNCTION_FEND
WriteChangeData(port, point, dipntmsg, DI_PNT_TYPE);
#endif
}
diptr[point].Status = dipntmsg->Status;
diptr[point].State = 1;
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// wen 2005.12.15 增加设置点数据值时,可以激活端口状态
SioParam[port].LineCommCnt = 0;
SioParam[port].Status = TRUE;
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++
bRetVal = true;
break;
case PNT_CTRL_NO:
diptr[point].ControlNo = dipntmsg->ControlNo;
diptr[point].CtrlEnable = true;
bRetVal = true;
break;
case PNT_PROV_MSG:
for (i = 0; i < MAX_PROV_PORT_NUM; i++)
{
if (diptr[point].Prov[i].Enable == 0)
continue;
if (diptr[point].Prov[i].PortNo == dipntmsg->Prov[0].PortNo)
{
diptr[point].Prov[i].PntNo = dipntmsg->Prov[0].PntNo;
diptr[point].Prov[i].Enable = 1;
bRetVal = true;
break;
}
}
if(bRetVal)
{
break;
}
for (i = 0; i < MAX_PROV_PORT_NUM; i++)
{
if (diptr[point].Prov[i].Enable)
continue;
diptr[point].Prov[i].PortNo = dipntmsg->Prov[0].PortNo;
diptr[point].Prov[i].PntNo = dipntmsg->Prov[0].PntNo;
diptr[point].Prov[i].Enable = 1;
bRetVal = true;
break;
}
break;
}
break;
case PI_PNT_TYPE:
if (!SioParam[port].m_psBaoHu->PiPtr)
break;
// wen 2005.09.19
if((point < 0) || (point >= SioParam[port].m_psBaoHu->PiNum))
{
break;
}
pipntmsg = (PI_DEF*)pntmsg;
piptr = (PI_DEF*)SioParam[port].m_psBaoHu->PiPtr;
switch (attr)
{
case PNT_ENABLE:
piptr[point].Enable = pipntmsg->Enable;
bRetVal = true;
break;
case PNT_STATE:
piptr[point].State = pipntmsg->State;
bRetVal = true;
break;
case PNT_RAWVALUE:
//SioParam[port].Status = TRUE;
if (abs((long)(piptr[point].RawValue - pipntmsg->RawValue)) > 2)
{
piptr[point].RawValue = pipntmsg->RawValue;
piptr[point].State = 1;
for (i = 0; i < MAX_PROV_PORT_NUM; i++)
{
if (piptr[point].Prov[i].Enable == 0)
continue;
j = piptr[point].Prov[i].PortNo;
k = piptr[point].Prov[i].PntNo;
if(SioParam[j].m_psBaoHu)
{
if((PROV_PROTOCOL == SioParam[j].m_psBaoHu->PortType)
&& SioParam[j].m_psBaoHu->PiPtr)
{
piprovptr = (PROV_PI_PNT*)SioParam[j].m_psBaoHu->PiPtr;
piprovptr[k].ChangeFlag = 1;
// wen 2004.11.19 增加电度变化数据的处理
// SioParam[j].m_psBaoHu->PiChange = TRUE;
}
}
}
// 定义前置机功能
#ifdef FUNCTION_FEND
WriteChangeData(port, point, pipntmsg, PI_PNT_TYPE);
#endif
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// wen 2005.12.15 增加设置点数据值时,可以激活端口状态
SioParam[port].LineCommCnt = 0;
SioParam[port].Status = TRUE;
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++
bRetVal = true;
break;
case PNT_PROV_MSG:
for (i = 0; i < MAX_PROV_PORT_NUM; i++)
{
if (piptr[point].Prov[i].Enable == 0)
continue;
if (piptr[point].Prov[i].PortNo == pipntmsg->Prov[0].PortNo)
{
piptr[point].Prov[i].PntNo = pipntmsg->Prov[0].PntNo;
piptr[point].Prov[i].Enable = 1;
bRetVal = true;
break;
}
}
if(bRetVal)
{
break;
}
for (i = 0; i < MAX_PROV_PORT_NUM; i++)
{
if (piptr[point].Prov[i].Enable)
continue;
piptr[point].Prov[i].PortNo = pipntmsg->Prov[0].PortNo;
piptr[point].Prov[i].PntNo = pipntmsg->Prov[0].PntNo;
piptr[point].Prov[i].Enable = 1;
bRetVal = true;
break;
}
break;
}
break;
}
return bRetVal;
}
int GetPntMsg(int port, int point, void *pntmsg, BYTE type, BYTE attr)
{
AI_DEF *aiptr, *aipntmsg;
DI_DEF *diptr, *dipntmsg;
PI_DEF *piptr, *pipntmsg;
BOOL bRetVal;
bRetVal = false;
if ((port < 0) || (port >= MaxPortNum))
return bRetVal;
if (!IsBaoHuPtr(port))
return bRetVal;
if (RECV_PROTOCOL != SioParam[port].m_psBaoHu->PortType)
return bRetVal;
switch (type)
{
case AI_PNT_TYPE:
if (!SioParam[port].m_psBaoHu->AiPtr)
break;
if ((point < 0) || (point >= SioParam[port].m_psBaoHu->AiNum))
break;
aipntmsg = (AI_DEF*)pntmsg;
aiptr = (AI_DEF*)SioParam[port].m_psBaoHu->AiPtr;
bRetVal = true;
switch (attr)
{
case PNT_ENABLE:
aipntmsg->Enable = aiptr[point].Enable;
break;
case PNT_STATE:
aipntmsg->State = aiptr[point].State;
break;
case PNT_POLAR:
aipntmsg->Polar = aiptr[point].Polar;
break;
case PNT_RAWVALUE:
aipntmsg->RawValue = aiptr[point].RawValue;
break;
case PNT_PROV_MSG:
memmove((char*)&aipntmsg->Prov[0],
(char*)&aiptr[point].Prov[0],
sizeof(PROV_PNT)*MAX_PROV_PORT_NUM);
break;
case PNT_ALL_MSG:
memmove((char*)aipntmsg, (char*)&aiptr[point], sizeof(AI_DEF));
break;
default:
bRetVal = false;
break;
}
break;
case DI_PNT_TYPE:
if (!SioParam[port].m_psBaoHu->DiPtr)
break;
if ((point < 0) || (point >= SioParam[port].m_psBaoHu->DiNum))
break;
dipntmsg = (DI_DEF*)pntmsg;
diptr = (DI_DEF*)SioParam[port].m_psBaoHu->DiPtr;
bRetVal = true;
switch (attr)
{
case PNT_ENABLE:
dipntmsg->Enable = diptr[point].Enable;
break;
case PNT_STATE:
dipntmsg->State = diptr[point].State;
break;
case PNT_SOE_TIME:
memmove((char*)&dipntmsg->SoeTime,
(char*)&diptr[point].SoeTime,
sizeof(DAY_TIME));
break;
case PNT_STATUS:
dipntmsg->Status = diptr[point].Status;
break;
case PNT_CTRL_NO:
dipntmsg->ControlNo = diptr[point].ControlNo;
dipntmsg->CtrlEnable = diptr[point].CtrlEnable;
break;
case PNT_PROV_MSG:
memmove((char*)&dipntmsg->Prov[0],
(char*)&diptr[point].Prov[0],
sizeof(PROV_PNT)*MAX_PROV_PORT_NUM);
break;
case PNT_ALL_MSG:
memmove((char*)dipntmsg, (char*)&diptr[point], sizeof(DI_DEF));
break;
default:
bRetVal = false;
break;
}
break;
case PI_PNT_TYPE:
if (!SioParam[port].m_psBaoHu->PiPtr)
break;
if ((point < 0) || (point >= SioParam[port].m_psBaoHu->PiNum))
break;
pipntmsg = (PI_DEF*)pntmsg;
piptr = (PI_DEF*)SioParam[port].m_psBaoHu->PiPtr;
bRetVal = true;
switch (attr)
{
case PNT_ENABLE:
pipntmsg->Enable = piptr[point].Enable;
break;
case PNT_STATE:
pipntmsg->State = piptr[point].State;
break;
case PNT_RAWVALUE:
pipntmsg->RawValue = piptr[point].RawValue;
break;
case PNT_PROV_MSG:
memmove((char*)&pipntmsg->Prov[0],
(char*)&piptr[point].Prov[0],
sizeof(PROV_PNT)*MAX_PROV_PORT_NUM);
break;
case PNT_ALL_MSG:
memmove((char*)pipntmsg, (char*)&piptr[point], sizeof(PI_DEF));
break;
default:
bRetVal = false;
}
break;
}
return bRetVal;
}
int SetPorvPntMsg(int port, int point, void *pntmsg, BYTE type, BYTE attr)
{
AI_DEF aipnt;
DI_DEF dipnt;
PI_DEF pipnt;
PROV_AI_PNT *aiprovptr, *aipptr;
PROV_DI_PNT *diprovptr, *dipptr;
PROV_PI_PNT *piprovptr, *pipptr;
BOOL bRetVal;
bRetVal = false;
if((port < 0) || (port >= MaxPortNum))
return bRetVal;
if(!IsBaoHuPtr(port))
return bRetVal;
if(PROTOCOL_SLAVE != SioParam[port].m_psBaoHu->PortType)
return bRetVal;
switch (type)
{
case AI_PNT_TYPE:
if (!SioParam[port].m_psBaoHu->AiPtr)
break;
if ((point < 0) || (point >= SioParam[port].m_psBaoHu->AiNum))
break;
aiprovptr = (PROV_AI_PNT*)SioParam[port].m_psBaoHu->AiPtr;
aipptr = (PROV_AI_PNT*)pntmsg;
aiprovptr[point].PortNo = aipptr->PortNo;
aiprovptr[point].PntNo = aipptr->PntNo;
aiprovptr[point].fFactor = aipptr->fFactor;
aiprovptr[point].Enable = 1;
aiprovptr[point].ChangeFlag = 0;
aipnt.Prov[0].PortNo = port;
aipnt.Prov[0].PntNo = point;
SetPntMsg(aipptr->PortNo, aipptr->PntNo,
(void*)&aipnt, AI_PNT_TYPE, PNT_PROV_MSG);
bRetVal = true;
break;
case DI_PNT_TYPE:
if (!SioParam[port].m_psBaoHu->DiPtr)
break;
if ((point < 0) || (point >= SioParam[port].m_psBaoHu->DiNum))
break;
diprovptr = (PROV_DI_PNT*)SioParam[port].m_psBaoHu->DiPtr;
dipptr = (PROV_DI_PNT*)pntmsg;
switch(attr)
{
case PNT_PROV_MSG:
diprovptr[point].PortNo = dipptr->PortNo;
diprovptr[point].PntNo = dipptr->PntNo;
diprovptr[point].Enable = 1;
diprovptr[point].ChangeFlag = 0;
dipnt.Prov[0].PortNo = port;
dipnt.Prov[0].PntNo = point;
SetPntMsg(dipptr->PortNo, dipptr->PntNo,
(void*)&dipnt, DI_PNT_TYPE, PNT_PROV_MSG);
bRetVal = true;
break;
case PNT_CTRL_NO:
diprovptr[point].ControlNo = dipptr->ControlNo;
diprovptr[point].CtrlEnable = true;
bRetVal = true;
break;
}
break;
case PI_PNT_TYPE:
if (!SioParam[port].m_psBaoHu->PiPtr)
break;
if ((point < 0) || (point >= SioParam[port].m_psBaoHu->PiNum))
break;
piprovptr = (PROV_PI_PNT*)SioParam[port].m_psBaoHu->PiPtr;
pipptr = (PROV_PI_PNT*)pntmsg;
piprovptr[point].PortNo = pipptr->PortNo;
piprovptr[point].PntNo = pipptr->PntNo;
piprovptr[point].fFactor = pipptr->fFactor;
piprovptr[point].Enable = 1;
piprovptr[point].ChangeFlag = 0;
pipnt.Prov[0].PortNo = port;
pipnt.Prov[0].PntNo = point;
SetPntMsg(pipptr->PortNo, pipptr->PntNo,
(void*)&pipnt, PI_PNT_TYPE, PNT_PROV_MSG);
bRetVal = true;
break;
}
return bRetVal;
}
int GetPorvPntMsg(int port, int point, void *pntmsg, BYTE type)
{
PROV_AI_PNT *aiprovptr;
PROV_DI_PNT *diprovptr;
PROV_PI_PNT *piprovptr;
BOOL bRetVal;
bRetVal = false;
if ((port < 0) || (port >= MaxPortNum))
return bRetVal;
if (!IsBaoHuPtr(port))
return bRetVal;
if (RECV_PROTOCOL != SioParam[port].m_psBaoHu->PortType)
return bRetVal;
switch (type)
{
case AI_PNT_TYPE:
if (!SioParam[port].m_psBaoHu->AiPtr)
break;
if ((point < 0) || (point >= SioParam[port].m_psBaoHu->AiNum))
break;
aiprovptr = (PROV_AI_PNT*)SioParam[port].m_psBaoHu->AiPtr;
memmove((char*)pntmsg, (char*)&aiprovptr[point], sizeof(PROV_AI_PNT));
bRetVal = true;
break;
case DI_PNT_TYPE:
if (!SioParam[port].m_psBaoHu->DiPtr)
break;
if ((point < 0) || (point >= SioParam[port].m_psBaoHu->DiNum))
break;
diprovptr = (PROV_DI_PNT*)SioParam[port].m_psBaoHu->DiPtr;
memmove((char*)pntmsg, (char*)&diprovptr[point], sizeof(PROV_DI_PNT));
bRetVal = true;
break;
case PI_PNT_TYPE:
if (!SioParam[port].m_psBaoHu->PiPtr)
break;
if ((point < 0) || (point >= SioParam[port].m_psBaoHu->PiNum))
break;
piprovptr = (PROV_PI_PNT*)SioParam[port].m_psBaoHu->PiPtr;
memmove((char*)pntmsg, (char*)&piprovptr[point], sizeof(PROV_PI_PNT));
bRetVal = true;
break;
}
return bRetVal;
}
//*******************************************************************
//* Poll Cmd *
//*******************************************************************
int CheckPollCmdBufEmpty(int port)
{
int i, j;
if ((port < 0) || (port >= MaxPortNum))
return 0;
if (!IsBaoHuPtr(port))
return 0;
j = 0;
for (i = 0; i < POLL_CMD_TYPE_NUM; i++)
{
j += SioParam[port].m_psBaoHu->PollCmd[i].MsgCnt ? 1 : 0;
}
return (j ? 0 : 1);
}
int PutPollCmdToBuf(int port, int type, WORD wait, char *buf, int len)
{
int ret;
u_char szbuf[MAX_MSG_BUF_SIZE];
if ((port < 0) || (port >= MaxPortNum))
return 0;
if (len == 0)
return 0;
// wen 2004.10.25 如果端口未打开则返回0
//if(!SioParam[port].OpenFlag)
if(!ShmGetPortFlag(port, FLAG_OPEN))
return 0;
if (!IsBaoHuPtr(port))
return false;
szbuf[0] = HIBYTE(len+2);
szbuf[1] = LOBYTE(len+2);
// 将等待时间作为命令的一部分存储
szbuf[2] = HIBYTE(wait);
szbuf[3] = LOBYTE(wait);
memmove((char*)&szbuf[4], buf, len);
ret = PutDataToBuf(&SioParam[port].m_psBaoHu->PollCmd[type], szbuf, len+4);
if (ret == (len + 4))
return true;
return false;
}
// wen 2005.09.08 清空指令缓冲区中的所有指令
void ClearAllCmdFromPollCmdBuf(int port)
{
int i;
if ((port < 0) || (port >= MaxPortNum))
return;
if (!IsBaoHuPtr(port))
return;
for(i = 0; i < POLL_CMD_TYPE_NUM; i++)
{
SioParam[port].m_psBaoHu->PollCmd[i].MsgCnt=0;
}
ClearCmdAllFlag(port);
}
void ClearCmdFormPollCmdBuf(int port)
{
int len, idx;
u_char szbuf[MAX_MSG_BUF_SIZE];
if ((port < 0) || (port >= MaxPortNum))
return;
if (!IsBaoHuPtr(port))
return;
idx = SioParam[port].m_psBaoHu->LastGetCmdBuf;
GetDataFormBuf(&SioParam[port].m_psBaoHu->PollCmd[idx], szbuf, 2, true);
len = BYTE1(szbuf[0]) + BYTE0(szbuf[1]);
GetDataFormBuf(&SioParam[port].m_psBaoHu->PollCmd[idx], szbuf, len, true);
// wen 2005.09.20 修改清除指令下发标识
ClearCmdAllFlag(port);
}
void ClearCmdFormPollCmdBufEx( int port, char *file, int line )
{
int len, idx;
char szbuf[MAX_MSG_BUF_SIZE];
ClearCmdFormPollCmdBuf(port);
return;
if ((port < 0) || (port >= MaxPortNum))
return;
if (!IsBaoHuPtr(port))
return;
sprintf(szbuf, "commid=%d %s<%d>, Clear Cmd", port + 1, file, line);
DebugPrint(szbuf);
idx = SioParam[port].m_psBaoHu->LastGetCmdBuf;
GetDataFormBuf(&SioParam[port].m_psBaoHu->PollCmd[idx], (u_char *)szbuf, 2, true);
len = BYTE1(szbuf[0]) + BYTE0(szbuf[1]);
GetDataFormBuf(&SioParam[port].m_psBaoHu->PollCmd[idx], (u_char *)szbuf, len, true);
// wen 2005.09.20 修改清除指令下发标识
ClearCmdAllFlag(port);
}
void ClearCmdAllFlag(int port)
{
if (!IsBaoHuPtr(port))
return;
SioParam[port].m_psBaoHu->RetryCnt = 0;
SioParam[port].m_psBaoHu->RetryTimeCnt = 0;
SioParam[port].m_psBaoHu->WaitTimeCnt = 0;
SioParam[port].m_psBaoHu->ForceWaitFlag = 0;
SioParam[port].m_psBaoHu->ForceWaitCnt = 0;
SioParam[port].m_psBaoHu->SendCmdFlag = 0;
SioParam[port].m_psBaoHu->RevCmdFlag = 0;
SioParam[port].m_psBaoHu->ReSendCmdFlag = 0;
SioParam[port].m_psBaoHu->LastGetCmdBuf = 0;
}
int GetCmdFormPollCmdBuf(int port, u_char *buf, int len)
{
int i, tmp;
u_char szbuf[MAX_MSG_BUF_SIZE];
if ((port < 0) || (port >= MaxPortNum))
return 0;
if (!IsBaoHuPtr(port))
return 0;
for (i = POLL_CMD_TYPE_NUM - 1; i >= 0; i--)
{
if (SioParam[port].m_psBaoHu->PollCmd[i].MsgCnt < 4)
{
SioParam[port].m_psBaoHu->PollCmd[i].MsgCnt = 0;
continue;
}
GetDataFormBuf(&SioParam[port].m_psBaoHu->PollCmd[i], szbuf, 2, false);
tmp = BYTE1(szbuf[0]) + BYTE0(szbuf[1]);
GetDataFormBuf(&SioParam[port].m_psBaoHu->PollCmd[i], szbuf, tmp + 2, false);
memmove((char*)buf, (char*)&szbuf[2], tmp);
SioParam[port].m_psBaoHu->LastGetCmdBuf = i;
return tmp;
}
return 0;
}
int ReGetCmdFormPollCmdBuf(int port, u_char *buf, int len)
{
int idx, ret, tmp;
u_char szbuf[MAX_MSG_BUF_SIZE];
if ((port < 0) || (port >= MaxPortNum))
return 0;
if (!IsBaoHuPtr(port))
return false;
SioParam[port].LostSyncCnt++;
idx = SioParam[port].m_psBaoHu->LastGetCmdBuf;
if (SioParam[port].m_psBaoHu->PollCmd[idx].MsgCnt < 4)
{
SioParam[port].m_psBaoHu->PollCmd[idx].MsgCnt = 0;
return 0;
}
GetDataFormBuf(&SioParam[port].m_psBaoHu->PollCmd[idx], szbuf, 2, false);
tmp = BYTE1(szbuf[0]) + BYTE0(szbuf[1]);
ret = GetDataFormBuf(&SioParam[port].m_psBaoHu->PollCmd[idx], szbuf, tmp + 2, false);
memmove((char*)buf, (char*)&szbuf[2], tmp);
return tmp;
}
void SendCmdFormPollCmdBuf(int port)
{
int len;
u_char buf[MAX_MSG_BUF_SIZE];
if ((port < 0) || (port >= MaxPortNum))
return;
//if (SioParam[port].OpenFlag == 0)
if(!ShmGetPortFlag(port, FLAG_OPEN))
return;
if (!IsBaoHuPtr(port))
return;
if (SioParam[port].m_psBaoHu->ForceWaitFlag
&& SioParam[port].m_psBaoHu->ForceWaitCnt)
{
SioParam[port].m_psBaoHu->ForceWaitCnt--;
if (SioParam[port].m_psBaoHu->ForceWaitCnt == 0)
{
ClearCmdFormPollCmdBuf(port); // 清除当前命令
//ClearCmdAllFlag(port);
}
}
if (SioParam[port].m_psBaoHu->SendCmdFlag
&& (SioParam[port].m_psBaoHu->RevCmdFlag == 0))
{
SioParam[port].m_psBaoHu->RetryTimeCnt++;
if (SioParam[port].m_psBaoHu->RetryTimeCnt
< SioParam[port].m_psBaoHu->RetryTime)
return;
SioParam[port].m_psBaoHu->RetryTimeCnt = 0;
SioParam[port].m_psBaoHu->RetryCnt++;
if (SioParam[port].m_psBaoHu->RetryCnt
> SioParam[port].m_psBaoHu->Retry)
{
SioParam[port].m_psBaoHu->RevCmdFlag = 1;
}
else
{
SioParam[port].m_psBaoHu->SendCmdFlag = 0;
SioParam[port].m_psBaoHu->RevCmdFlag = 0;
SioParam[port].m_psBaoHu->ReSendCmdFlag = 1;
}
}
if (SioParam[port].m_psBaoHu->SendCmdFlag
&& SioParam[port].m_psBaoHu->RevCmdFlag)
{
ClearCmdFormPollCmdBuf(port); // 清除当前命令
//ClearCmdAllFlag(port);
}
if (SioParam[port].m_psBaoHu->WaitTime)
{
SioParam[port].m_psBaoHu->WaitTimeCnt++;
if (SioParam[port].m_psBaoHu->WaitTimeCnt
< SioParam[port].m_psBaoHu->WaitTime)
return;
}
SioParam[port].m_psBaoHu->WaitTimeCnt = 0;
if (SioParam[port].m_psBaoHu->ReSendCmdFlag)
{
len = ReGetCmdFormPollCmdBuf(port, buf, MAX_MSG_BUF_SIZE);
}
else
{
len = GetCmdFormPollCmdBuf(port, buf, MAX_MSG_BUF_SIZE);
}
if (len < 3)
return;
#if 0
if (UdpParam.Socket)
{
FillAndSendCmd(0, port, SDS_SIO_SEND_DATA,
(u_char*)&buf[2], len - 2);
SioParam[port].SendCharNum += (len - 2);
SioParam[port].m_psBaoHu->SendCmdFlag = 1;
SioParam[port].m_psBaoHu->ReSendCmdFlag = 0;
SioParam[port].m_psBaoHu->RevCmdFlag = 0;
}
if ((FAST_CMD_TYPE == SioParam[port].m_psBaoHu->LastGetCmdBuf)
|| (UdpParam.Socket == 0))
{
SioParam[port].m_psBaoHu->ForceWaitCnt
= BYTE1(buf[0]) + BYTE0(buf[1]) + TIMER_CNT - 1;
SioParam[port].m_psBaoHu->ForceWaitCnt /= TIMER_CNT;
if (SioParam[port].m_psBaoHu->ForceWaitCnt)
SioParam[port].m_psBaoHu->ForceWaitFlag = 1;
else
{
ClearCmdFormPollCmdBuf(port); // 清除当前命令
//ClearCmdAllFlag(port); // 清除当前命令
}
}
#endif
}
void TimerProc(void)
{
int i;
for (i = 0; i < MaxPortNum; i++)
{
//if (SioParam[i].OpenFlag == 0)
if(!ShmGetPortFlag(i, FLAG_OPEN))
{
ClearCmdFormPollCmdBuf(i);
continue;
}
// wen 2004.10.25 以便规约模块控制发送时序
if (FunCallPtr[SioParam[i].ProtocolIdx].Timer)
FunCallPtr[SioParam[i].ProtocolIdx].Timer(i);
else
SendCmdFormPollCmdBuf(i);
}
}
void YkYtCommandProcess(int port, u_char *buf, int len)
{
//if (SioParam[port].OpenFlag == 0)
if(!ShmGetPortFlag(port, FLAG_OPEN))
return;
if (SioParam[port].ProtocolIdx < 1)
return;
if (FunCallPtr[SioParam[port].ProtocolIdx].YkYtProcess)
{
FunCallPtr[SioParam[port].ProtocolIdx].YkYtProcess(port, buf, len);
}
}
void SystemRtuTime(void)
{
int i;
for (i = 0; i < MaxPortNum; i++)
{
//if (SioParam[i].OpenFlag == 0)
if(!ShmGetPortFlag(i, FLAG_OPEN))
continue;
if (!IsBaoHuPtr(i))
continue;
if (!FunCallPtr[SioParam[i].ProtocolIdx].SendSystemTime)
continue;
// 下对时
if(SioParam[i].m_psBaoHu->CheckTime == 2)
FunCallPtr[SioParam[i].ProtocolIdx].SendSystemTime(i);
}
}
int InitBaohuLibMainFlag = 0;
void PortRecvDataProcess(void)
{
int i;
for (i = 0; i < MaxPortNum; i++)
{
//if(SioParam[i].OpenFlag && (SioParam[i].RecvBuf.MsgCnt > 0))
if (!SioParam[i].m_psBaoHu)
continue;
if (FLAG_OPEN == SioParam[i].OpenFlag && (SioParam[i].RecvBuf.MsgCnt > 0))
{
// 严格判断缓冲区长度
if (SioParam[i].RecvBuf.MsgCnt > sizeof(SioParam[i].RecvBuf.MsgData))
{
printf("WARN(%04d): RecvBuf is confusional(MsgCnt=%d, MaxLen=%d).\n",
_getpid(), SioParam[i].RecvBuf.MsgCnt,
sizeof(SioParam[i].RecvBuf.MsgData));
SioParam[i].RecvBuf.MsgCnt = 0;
continue;
}
if (FunCallPtr[SioParam[i].ProtocolIdx].RecvData)
{
//#ifdef _OS_LINUX_DEBUG_
//printf("wen: SioParam[%d].RecvBuf.MsgCnt=%d\n", i + 1, SioParam[i].RecvBuf.MsgCnt);
//#endif
//printf("idx = %d\n", SioParam[i].ProtocolIdx);
FunCallPtr[SioParam[i].ProtocolIdx].RecvData(i,
SioParam[i].RecvBuf.MsgData,
SioParam[i].RecvBuf.MsgCnt);
// 端口接收到数据标志应该在这里清零 yizhonghu 20071029
//SioParam[i].PortNoDataCnt = 0;
// wen 2005.03.22 记录端口读取数据
//RecordData(i, RECORD_RO_BUF,SioParam[i].RecvBuf.MsgData,SioParam[i].RecvBuf.MsgCnt);
SioParam[i].RecvCharNum += SioParam[i].RecvBuf.MsgCnt;
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
// wen 2005.10.14 增加端口状态设置
//SioParam[i].Status = TRUE;
//-----------------------------------------------------
// 对于规约为bypass时不标记为运行正常
if (SioParam[i].ProtocolIdx > 1)
{
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// wen 2006.04.24 如果仅仅是接收到数据,不认为端口状态为正常,
// 必须是修改了数据库点才认为是端口正常。子站除外。
//SioParam[i].Status = TRUE;
//=============================================================
//TimeEx(&SioParam[i].dLineCommTime);
SioParam[i].Status = TRUE;
/*if (SioParam[i].m_psBaoHu)
{
// 如果为子站
//if(SioParam[i].m_psBaoHu->PortType)
//{
//if(1 == i)
// printf("设置端口%d端口状态为TRUE\n", i+1);
SioParam[i].Status = TRUE;
//}
}
else
{
SioParam[i].Status = TRUE;
if (i == SioParam[i].MasterPort)
SioParam[SioParam[i].SlavePort].Status = TRUE;
else
SioParam[SioParam[i].MasterPort].Status = TRUE;
}*/
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
}
}
SioParam[i].RecvBuf.MsgCnt = 0;
}
}
void BaohuLibMain(int netportnum)
{
int msec, tmp1, tmp2;
static int systimecnt = 0;
if (InitBaohuLibMainFlag == 0)
{
systimecnt = 0;
msec = 0;
// 读端口配置信息并设置6个网络端口
//ReadPortConfigInfo(netportnum);
ReadPortConfigInfoEx(netportnum);
InitBaohuLibMainFlag = 1;
}
//msec = (msec + TIMER_CNT) % 1000;
time_t stm=time(NULL);
struct tm* ptm = localtime(&stm);
DayTime.Year = ptm->tm_year + 1900;
DayTime.Month = ptm->tm_mon + 1;
DayTime.Day = ptm->tm_mday;
DayTime.Hour = ptm->tm_hour;
DayTime.Min = ptm->tm_min;
DayTime.Sec = ptm->tm_sec;
DayTime.mSec = 0;
Sleep(100);
// 网络端口写
//SetUdpRecv();
// 所有端口定时器处理
TimerProc();
// wen 2004.10.25 以下代码放入timerproc中以便规约模块控制发送时序
//for (i = 0; i < MaxPortNum; i++)
//{
// SendCmdFormPollCmdBuf(i);
//}
// 所有端口接收数据处理
PortRecvDataProcess();
PollAllPort();
//CreatUdpSetSock();
OpenAllPort();
if (ptm->tm_sec == 0)
{
tmp2 = ptm->tm_min * 60;
tmp1 = tmp2 - systimecnt;
if (tmp1 < 0)
tmp1 += 3600;
// 每300秒RTU对时一次
if ((tmp1 > SYSTEM_TIME) && (tmp2 != systimecnt))
{
systimecnt = tmp2;
SystemRtuTime();
}
}
}
int GetMaxPort()
{
return MaxPortNum;
}
//********************************************************************
//* 存储转发soe数据函数 *
//*参数int provport : 转发端口 *
//* SOE_DEF *ptrSoe : 待存储的soe数据 *
//*返回值int retval : soe数据数量 *
//********************************************************************
int ProvWriteSoeData(int provport, SOE_DEF *ptrSoe)
{
char szDbg[128];
int iRear, iSoeNum;
if(!IsBaoHuPtr(provport))
return 0;
iSoeNum = 0;
if(SioParam[provport].m_psBaoHu->ProvSoeBuf.ptrProvSoe)
{
iRear = SioParam[provport].m_psBaoHu->ProvSoeBuf.iRear;
memcpy((void *)&SioParam[provport].m_psBaoHu->ProvSoeBuf.ptrProvSoe[iRear],
(void *)ptrSoe, sizeof(SOE_DEF));
SioParam[provport].m_psBaoHu->ProvSoeBuf.iRear = (iRear+1) % MAX_PROV_SOE;
if(SioParam[provport].m_psBaoHu->ProvSoeBuf.iSoeNum < MAX_PROV_SOE)
{
SioParam[provport].m_psBaoHu->ProvSoeBuf.iSoeNum++;
}
else
{
SioParam[provport].m_psBaoHu->ProvSoeBuf.iFront
= (SioParam[provport].m_psBaoHu->ProvSoeBuf.iFront+1) % MAX_PROV_SOE;
sprintf(szDbg, "WARN(%04d): port%02d ProvSoe is overflow(max=%d).",
_getpid(), provport+1, MAX_PROV_SOE);
DebugPrint(szDbg);
}
iSoeNum = SioParam[provport].m_psBaoHu->ProvSoeBuf.iSoeNum;
}
return iSoeNum;
}
//********************************************************************
//* 初始化组数据定义函数 *
//*参数int provport : 转发端口 *
//* SOE_DEF *ptrSoe : 获取soe数据的缓冲区 *
//*返回值int retval : 剩余soe数据数量 *
//********************************************************************
int ProvAndDelGetSoeData(int provport, SOE_DEF *ptrSoe)
{
int iFront, iSoeNum;
SOE_DEF *ptrtmpSoe;
if (!IsBaoHuPtr(provport))
return 0;
iSoeNum = 0;
if(SioParam[provport].m_psBaoHu->ProvSoeBuf.ptrProvSoe
&& (SioParam[provport].m_psBaoHu->ProvSoeBuf.iSoeNum > 0))
{
ptrtmpSoe = SioParam[provport].m_psBaoHu->ProvSoeBuf.ptrProvSoe;
iFront = SioParam[provport].m_psBaoHu->ProvSoeBuf.iFront;
memcpy((void *)ptrSoe, (void *)&ptrtmpSoe[iFront], sizeof(SOE_DEF));
SioParam[provport].m_psBaoHu->ProvSoeBuf.iFront = (iFront+1) % MAX_PROV_SOE;
SioParam[provport].m_psBaoHu->ProvSoeBuf.iSoeNum--;
iSoeNum = SioParam[provport].m_psBaoHu->ProvSoeBuf.iSoeNum;
}
return iSoeNum;
}
//********************************************************************
//* 转发端口是否含有soe数据函数 *
//*参数int provport : 转发端口 *
//********************************************************************
int ProvHaveSoeData(int provport)
{
if(!IsBaoHuPtr(provport))
return 0;
return SioParam[provport].m_psBaoHu->ProvSoeBuf.iSoeNum;
}
//插到最后一个结点之后
int AddNodeEx(int commid, char *buf, int len)
{
int iMallocSize;
struct WR_DATA *wdata;
// wen 2004.12.28 停止系统发送数据
//if (StopSend)
//{
// return 0;
//}
// wen 2005.07.02 如果端口未打开,不进行缓冲
if (!ShmGetPortFlag(commid, FLAG_OPEN))
{
return 0;
}
// 增加缓冲区容量大小的限制(???)
if (SioParam[commid].iWriteDataNo > MAX_BUFUNIT_NUM)
{
return 0;
}
if (len <= 0)
{
// wen 2004.12.03
printf("TIP_: AddNode (commid = %d) buf is empty or invalid(len=%d)\n", commid, len);
return 0;
}
/*if (WatchPort == commid)
{
FillUdpSetBuf(commid, SDS_SIO_SEND_DATA, buf, len);
SetUdpSend();
}*/
iMallocSize = sizeof(struct WR_DATA) + len;
if (MIN_MALLOC_SIZE > iMallocSize)
{
iMallocSize = MIN_MALLOC_SIZE;
}
if (!SioParam[commid].WriteData) //empty before insert
{
SioParam[commid].WriteData = (struct WR_DATA*)HEAP_MALLOC(iMallocSize);
if (!SioParam[commid].WriteData)
{
// wen 2004.12.03
printf("TIP_(): AddNode (commid = %d) memory malloc error1(len=%d, totallen=%d)\n",
commid, len, iMallocSize);
return 0;
}
SioParam[commid].WriteData->Next = NULL;
memmove(SioParam[commid].WriteData->MsgData, buf, len);
SioParam[commid].WriteData->MsgLen = len;
SioParam[commid].WriteData->PLen = 0;
SioParam[commid].SendCharNum += len;
SioParam[commid].iWriteDataNo++;
// test
//if(commid == 16)
//{
// printf("TIP_(%04d): AddNode(commid=%d, len=%d), No=%d, MallocSize=%d\n",
// getpid(), commid, len, SioParam[commid].iWriteDataNo, iMallocSize);
//}
return len;
}
wdata = SioParam[commid].WriteData; //old link head
while (wdata->Next != NULL)
{
wdata = wdata->Next;
};
wdata->Next = (struct WR_DATA*)HEAP_MALLOC(iMallocSize);
if (!wdata->Next)
{
// wen 2004.12.03
printf("TIP_(): AddNode (commid = %d) memory malloc error3(len=%d, totallen=%d)\n",
commid, len, iMallocSize);
return 0;
}
wdata->Next->Next = NULL;
//memmove(wdata->Next->MsgData, buf, len);
memcpy(wdata->Next->MsgData, buf, len);
wdata->Next->MsgLen = len;
wdata->Next->PLen = 0;
SioParam[commid].SendCharNum += len;
SioParam[commid].iWriteDataNo++;
// test
//if(commid >= 16)
//{
// printf("TIP_(%04d): AddNode(commid=%d, len=%d), No=%d, MallocSize=%d\n",
// getpid(), commid, len, SioParam[commid].iWriteDataNo, iMallocSize);
//}
return len;
}
int AddNode(int commid, char *buf, int len)
{
// 修改内存分配策略
return AddNodeEx(commid, buf, len);
}
//********************************************************************
//* 发送数据函数 *
//*参数: int commid : 端口号 *
//* char *buf : 发送数据缓存区 *
//* int len : 发送数据长度 *
//*返回值:无 *
//********************************************************************
void SendDataToPort(int commid, char *buf, int len)
{
#ifdef FUNCTION_FEND
// 前置机为备用机时,端口下发数据处理方式
if(!IsSendDatatoPort(commid))
{
return;
}
#endif
//#ifdef OS_LINUX
AddNode(commid, (char *)buf, len);
//#else
// FillAndSendCmd(0, commid, SDS_SIO_SEND_DATA, (u_char*)buf, len);
// wen 2004.12.21 发送计数已经在发送时做了计算
// SioParam[commid].SendCharNum += len;
//#endif
}
//********************************************************************
//* 发送遥控遥调数据函数 *
//*参 数int commid : 端口号 *
//* char *buf : 发送数据缓存区 *
//* int len : 发送数据长度 *
//*返回值:无 *
//*说 明发送来自scada系统(server)的遥控遥调命令 *
//********************************************************************
void SendYkYtCommand(int commid, char *buf, int len)
{
// buf[0] --- 端口号(=commid)
// buf[1]
// buf[2]
// buf[3]
// buf[4] --- 控点号
// buf[5]
// buf[6]
// buf[7]
// buf[8] --- 操作类型(遥控:1=选择,2=执行,3=取消,7=直控;
// 遥调:4=选择,5=执行,6=取消,8=急停)
// buf[9] --- 控制状态(1=分到合2=合到分)
// (最高位为1时为返校命令, 1=控合, 2=控分, 3=失败)
DAY_TIME stime;
// 串行端口
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// wen 2005.10.10 修改可以在netport下发
//if(commid < MaxSerialPortNum)
if(commid < MaxPortNum)
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
{
if(!IsBaoHuPtr(commid))
return ;
if(!FunCallPtr)
return;
if(FunCallPtr[SioParam[commid].ProtocolIdx].YkYtProcess)
{
if((buf[9] & 0x80) == 0)
{
// 保存遥控参数及环境变量
GetLocalTimeEx(&stime);
memcpy((void *)&SioParam[commid].m_psBaoHu->ProvYkYtMsg.m_sDayTime,
(void *)&stime, sizeof(DAY_TIME));
SioParam[commid].m_psBaoHu->ProvYkYtMsg.m_iYkYtStep = buf[8];
// 该结构成员是用来保存操作类型的(不仅仅针对转发遥控)
SioParam[commid].m_psBaoHu->ProvYkYtMsg.m_iYkYtOperate = buf[9];
SioParam[commid].m_psBaoHu->ProvYkYtMsg.m_iCmdFrom = YKYT_CMD_FROM_SCADA;
}
FunCallPtr[SioParam[commid].ProtocolIdx].YkYtProcess(commid, (u_char *)buf, len);
}
}
}
//********************************************************************
//* 发送遥控遥调数据函数 *
//*参 数int commid : 端口号 *
//* PROV_YKYT_PARAM *pParam : 遥控遥调参数 *
//*返回值BOOL retval : 是否转发端口数据 *
//*说 明:发送来自转发端口的遥控遥调命令 *
//********************************************************************
BOOL SendYkYtCommand2(int commid, YKYT_PARAM *pParam)
{
DAY_TIME stime;
char Val[20];
int iRealCommid;
// Val[0] --- 端口号(=commid)
// Val[1]
// Val[2]
// Val[3]
// Val[4] --- 控点号
// Val[5]
// Val[6]
// Val[7]
// Val[8] --- 操作类型(遥控:1=选择,2=执行,3=取消,7=直控;
// 遥调:4=选择,5=执行,6=取消,8=急停)
// Val[9] --- 控制状态(1=分到合2=合到分)
// (最高位为1时为返校命令, 1=控合, 2=控分, 3=失败)
// 串行端口
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// wen 2005.10.10 修改可以在netport下发
//if(commid >= MaxSerialPortNum)
//{
// return FALSE;
//}
if(commid >= MaxPortNum)
{
return FALSE;
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
memset(Val, 0, sizeof(Val));
// 非转发数据
if(YKYT_SEND_DOWN == pParam->m_iYkYtUpDown)
{
iRealCommid = commid;
Val[0] = (BYTE)iRealCommid;
Val[1] = (BYTE)((iRealCommid & 0xFF00) >> 8);
Val[2] = (BYTE)((iRealCommid & 0xFF0000) >> 16);
Val[3] = (BYTE)((iRealCommid & 0xFF000000) >> 24);
Val[4] = (BYTE)pParam->m_iYkYtPnt;
Val[5] = (BYTE)((pParam->m_iYkYtPnt & 0xFF00) >> 8);
Val[6] = (BYTE)((pParam->m_iYkYtPnt & 0xFF0000) >> 16);
Val[7] = (BYTE)((pParam->m_iYkYtPnt & 0xFF000000) >> 24);
Val[8] = (BYTE)pParam->m_iYkYtStep;
Val[9] = (BYTE)pParam->m_iYkYtOperate;
}
else
{
if(!IsBaoHuPtr(commid))
return FALSE;
if(YKYT_CMD_FROM_SCADA == SioParam[commid].m_psBaoHu->ProvYkYtMsg.m_iCmdFrom)
{
// wen 2005.10.08 非转发数据从而上送给scada服务器
Val[0] = LOBYTE(pParam->m_iYkYtPnt);
Val[1] = HIBYTE(pParam->m_iYkYtPnt);
if(pParam->m_iYkYtOperate == 3)
{
Val[2] = 0;
}
else
{
Val[2] = 1;
}
// SetYkYtAckData(commid, Val);
return TRUE;
}
else if((IsProvYkYtAck(commid, &SioParam[commid].m_psBaoHu->ProvYkYtMsg))
&& (SioParam[commid].m_psBaoHu->ProvYkYtMsg.m_iYkYtPointIdx == pParam->m_iYkYtPnt))
{
iRealCommid = SioParam[commid].m_psBaoHu->ProvYkYtMsg.m_iProvPortIdx;
// 转发站号
Val[0] = (BYTE)iRealCommid;
Val[1] = (BYTE)((iRealCommid & 0xFF00) >> 8);
Val[2] = (BYTE)((iRealCommid & 0xFF0000) >> 16);
Val[3] = (BYTE)((iRealCommid & 0xFF000000) >> 24);
// 转发控点号
Val[4] = (BYTE)SioParam[commid].m_psBaoHu->ProvYkYtMsg.m_iProvYkYtPointIdx;
Val[5] = (BYTE)((SioParam[commid].m_psBaoHu->ProvYkYtMsg.m_iProvYkYtPointIdx & 0xFF00) >> 8);
Val[6] = (BYTE)((SioParam[commid].m_psBaoHu->ProvYkYtMsg.m_iProvYkYtPointIdx & 0xFF0000) >> 16);
Val[7] = (BYTE)((SioParam[commid].m_psBaoHu->ProvYkYtMsg.m_iProvYkYtPointIdx & 0xFF000000) >> 24);
Val[8] = (BYTE)pParam->m_iYkYtStep;
Val[9] = (BYTE)(pParam->m_iYkYtOperate);
Val[9] |= 0x80;
}
else// 异常数据
{
return FALSE;
}
}
if(FunCallPtr)
{
if(FunCallPtr[SioParam[iRealCommid].ProtocolIdx].YkYtProcess)
{
if(YKYT_SEND_UP != pParam->m_iYkYtUpDown)
{
// 保存遥控参数及环境变量
GetLocalTimeEx(&stime);
memcpy((void *)&SioParam[iRealCommid].m_psBaoHu->ProvYkYtMsg.m_sDayTime,
(void *)&stime, sizeof(DAY_TIME));
SioParam[iRealCommid].m_psBaoHu->ProvYkYtMsg.m_iYkYtStep = pParam->m_iYkYtStep;
SioParam[iRealCommid].m_psBaoHu->ProvYkYtMsg.m_iCmdFrom = YKYT_CMD_FROM_PROVPORT;
}
FunCallPtr[SioParam[iRealCommid].ProtocolIdx].YkYtProcess(iRealCommid, (u_char *)Val, 10);
}
}
return TRUE;
}
//********************************************************************
//* 查找转发控点的转发点函数 *
//*参数: int commid : 转发端口号 *
//* int ykytpnt: 转发的遥控遥调点 *
//* SIO_PARAM_BAOHU_DEF *pBaohuParam :转发端口保护参数 *
//*返回值int retval : 对应的实际转发点号 *
//********************************************************************
int FindProvPntFromYkYtPnt(int commid, int ykytpnt, SIO_PARAM_BAOHU_DEF *pBaohuParam)
{
int i;
PROV_DI_PNT *diprovptr;
if(!pBaohuParam)
{
printf("TIP_(%04d): commid=%d psBaoHu is NULL.\n",
_getpid(), commid + 1);
return -1;
}
diprovptr = (PROV_DI_PNT*)pBaohuParam->DiPtr;
if(!diprovptr)
{
printf("TIP_(%04d): commid=%d DiPtr is NULL.\n",
_getpid(), commid + 1);
return -1;
}
for(i=0; i<pBaohuParam->DiNum; i++)
{
if(((int)diprovptr[i].ControlNo) == ykytpnt)
break;
}
if(ShmGetDispYkYtFlag())
{
printf("TIP_(%04d): commid =%d ykytpnt=%d", _getpid(), commid + 1, ykytpnt);
if(i<pBaohuParam->DiNum)
{
printf(" ControlNo=%d.\n", diprovptr[i].ControlNo);
}
else
{
printf(".\n");
}
}
if(i >= pBaohuParam->DiNum)
return -1;
else
return i;
}
BOOL ProvPortYkYtIsTimeOut(int commid, PROV_YKYT_DEF *pProvYkYt)
{
time_t iSaveTime, iCurTime;
struct tm changetm;
DAY_TIME CurTime;
BOOL bReturn;
changetm.tm_hour = pProvYkYt->m_sDayTime.Hour;
changetm.tm_mday = pProvYkYt->m_sDayTime.Day;
changetm.tm_min = pProvYkYt->m_sDayTime.Min;
changetm.tm_mon = pProvYkYt->m_sDayTime.Month-1;
changetm.tm_sec = pProvYkYt->m_sDayTime.Sec;
changetm.tm_year = pProvYkYt->m_sDayTime.Year - 1900;
changetm.tm_isdst = -1;
iSaveTime = mktime(&changetm);
GetLocalTimeEx(&CurTime);
changetm.tm_hour = CurTime.Hour;
changetm.tm_mday = CurTime.Day;
changetm.tm_min = CurTime.Min;
changetm.tm_mon = CurTime.Month-1;
changetm.tm_sec = CurTime.Sec;
changetm.tm_year = CurTime.Year - 1900;
changetm.tm_isdst = -1;
iCurTime = mktime(&changetm);
if((iCurTime-iSaveTime) > pProvYkYt->m_iTimeOut)
{
bReturn = TRUE;
}
else
{
bReturn = FALSE;
}
if(ShmGetDispYkYtFlag())
{
printf("TIP_(%04d): commid =%d timeout=%d.\n",
_getpid(), commid + 1, pProvYkYt->m_iTimeOut);
printf("TIP_(%04d): curtime %02d:%02d:%02d.\n",
_getpid(), CurTime.Hour, CurTime.Min, CurTime.Sec);
printf("TIP_(%04d): savetime %02d:%02d:%02d.\n",
_getpid(), pProvYkYt->m_sDayTime.Hour,
pProvYkYt->m_sDayTime.Min,
pProvYkYt->m_sDayTime.Sec);
}
return bReturn;
}
BOOL IsProvYkYtAck(int commid, PROV_YKYT_DEF *pProvYkYt)
{
BOOL bReturn;
bReturn = FALSE;
if((STEP_YKYT_SELECT == pProvYkYt->m_iYkYtStep)
&& (pProvYkYt->m_iProvPntIdx >= 0))
{
if(!ProvPortYkYtIsTimeOut(commid, pProvYkYt))
{
bReturn = TRUE;
}
else
{
if(ShmGetDispYkYtFlag())
{
printf("TIP_(%04d): commid=%d ykytpnt is judging timeout.\n",
_getpid(), commid + 1);
}
}
}
else
{
if(ShmGetDispYkYtFlag())
{
printf("TIP_(%04d): commid=%d ykyt is not in condition. step=%d, provpntidx=%d.\n",
_getpid(), commid + 1, pProvYkYt->m_iYkYtStep, pProvYkYt->m_iProvPntIdx);
}
}
return bReturn;
}
BOOL ShmGetDispYkYtFlag()
{
return FALSE;
}
BOOL ShmGetDispHostLinkFlag()
{
return TRUE;
}
void PortMemoryMalloc(int commid, int type, SIO_PARAM_BAOHU_DEF *pBaohuParam)
{
int i, k, increase;
char entry[128], section[128], szbuf[128];
char *ptr, szDebugInfo[256];
AI_DEF aipntmsg;
DI_DEF dipntmsg;
PROV_AI_PNT aiprovpnt;
PROV_DI_PNT diprovpnt;
PROV_PI_PNT piprovpnt;
if ((commid < 0) || (commid >= MaxPortNum))
return;
if (!IsBaoHuPtr(commid))
return;
char szPortConfig[256];
//GetCurrentDirectory(sizeof(szDir), szDir);
sprintf(szPortConfig, "%s/portconfig.ini", IniFilePath);
switch(type)
{
case PROTOCOL_MASTER:
//SioParam[commid].PortType = PROTOCOL_MASTER;
if (pBaohuParam->AiPtr)
{
#ifdef _OS_WINDOWS_DEBUG_
sprintf(szDebugInfo, "TIP_(%04d): commid_%02d free(aiptr=0x%08x).\n",
_getpid(), commid + 1, pBaohuParam->AiPtr);
OutputDebugString((LPCWSTR)szDebugInfo);
#endif
free(pBaohuParam->AiPtr);
}
if (pBaohuParam->DiPtr)
{
#ifdef _OS_WINDOWS_DEBUG_
sprintf(szDebugInfo, "TIP_(%04d): commid_%02d free(diptr=0x%08x).\n",
_getpid(), commid + 1, pBaohuParam->DiPtr);
OutputDebugString((LPCWSTR)szDebugInfo);
#endif
free(pBaohuParam->DiPtr);
}
if (pBaohuParam->PiPtr)
{
#ifdef _OS_WINDOWS_DEBUG_
sprintf(szDebugInfo, "TIP_(%04d): commid_%02d free(piptr=0x%08x).\n",
_getpid(), commid + 1, pBaohuParam->PiPtr);
OutputDebugString((LPCWSTR)szDebugInfo);
#endif
free(pBaohuParam->PiPtr);
}
if(pBaohuParam->AiNum > 0)
{
pBaohuParam->AiPtr
= (AI_DEF*)malloc(sizeof(AI_DEF)*pBaohuParam->AiNum);
if (pBaohuParam->AiPtr)
{
memset((char*)pBaohuParam->AiPtr, 0,
sizeof(AI_DEF)*pBaohuParam->AiNum);
}
else
{
#ifdef _OS_WINDOWS_DEBUG_
sprintf(szDebugInfo, "ERR_(%04d): commid_%02d aiptr=malloc(%d) is failed.\n",
_getpid(), commid + 1, sizeof(AI_DEF)*pBaohuParam->AiNum);
OutputDebugString((LPCWSTR)szDebugInfo);
#endif
pBaohuParam->AiNum = 0;
}
}
else
{
pBaohuParam->AiNum = 0;
pBaohuParam->AiPtr = NULL;
}
if(pBaohuParam->DiNum > 0)
{
pBaohuParam->DiPtr
= (DI_DEF*)malloc(sizeof(DI_DEF)*pBaohuParam->DiNum);
if (pBaohuParam->DiPtr)
{
memset((char*)pBaohuParam->DiPtr, 0,
sizeof(DI_DEF)*pBaohuParam->DiNum);
}
else
{
#ifdef _OS_WINDOWS_DEBUG_
sprintf(szDebugInfo, "ERR_(%04d): commid_%02d diptr=malloc(%d) is failed.\n",
_getpid(), commid + 1, sizeof(DI_DEF)*pBaohuParam->DiNum);
OutputDebugString((LPCWSTR)szDebugInfo);
#endif
pBaohuParam->DiNum = 0;
}
}
else
{
pBaohuParam->DiNum = 0;
pBaohuParam->DiPtr = NULL;
}
if(pBaohuParam->PiNum > 0)
{
pBaohuParam->PiPtr
= (PI_DEF*)malloc(sizeof(PI_DEF)*pBaohuParam->PiNum);
if (pBaohuParam->PiPtr)
{
memset((char*)pBaohuParam->PiPtr, 0,
sizeof(PI_DEF)*pBaohuParam->PiNum);
}
else
{
#ifdef _OS_WINDOWS_DEBUG_
sprintf(szDebugInfo, "ERR_(%04d): commid_%02d piptr=malloc(%d) is failed.\n",
_getpid(), commid, sizeof(PI_DEF)*pBaohuParam->PiNum);
OutputDebugString((LPCWSTR)szDebugInfo + 1);
#endif
pBaohuParam->PiNum = 0;
}
}
else
{
pBaohuParam->PiNum = 0;
pBaohuParam->PiPtr = NULL;
}
sprintf(entry, "Port%d", commid + 1);
for (i = 0; i < pBaohuParam->AiNum; i++)
{
sprintf(section, "Ai%dPolar", i+1);
#ifdef _OS_WINDOWS_DEBUG_
sprintf(szDebugInfo, ">>>>(%04d):[%s]<%s>\n", _getpid(), entry, section);
OutputDebugString((LPCWSTR)szDebugInfo);
#endif
GetPrivateProString(entry, section, "", szbuf, 120, szPortConfig);
if (strlen(szbuf) == 0)
{
// wen 2005.09.28 缺省设置为1
aipntmsg.Polar = 1;
SetPntMsg(commid, i, (void*)&aipntmsg, AI_PNT_TYPE, PNT_POLAR);
continue;
}
aipntmsg.Polar = atoi(szbuf) ? 1 : 0;
SetPntMsg(commid, i, (void*)&aipntmsg, AI_PNT_TYPE, PNT_POLAR);
// wen 2005.09.13 增加顺序增加配置
sprintf(section, "Ai%dInc", i+1);
GetPrivateProString(entry, section, "", szbuf, 120, szPortConfig);
if(strlen(szbuf) == 0)
continue;
increase = atoi(szbuf);
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// wen 2005.12.01 修改连续增加的读取
//if(increase > 0)
//{
// for(k=i+1; k<increase; k++)
// {
// if(k >= pBaohuParam->AiNum)
// {
// break;
// }
// SetPntMsg(commid, k, (void*)&aipntmsg, AI_PNT_TYPE, PNT_POLAR);
// }
// i = k;
//}
//================================================================
if(increase > 0)
{
for(k=0; k<increase; k++)
{
if((i+k+1) >= pBaohuParam->AiNum)
{
break;
}
SetPntMsg(commid, i+k+1, (void*)&aipntmsg, AI_PNT_TYPE, PNT_POLAR);
}
i += k;
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
}
GetPrivateProString(entry, "DiAllCtrlNo", "", szbuf, 120, szPortConfig);
if (strlen(szbuf))
{
dipntmsg.ControlNo = atoi(szbuf);
for (i = 0; i < SioParam[commid].m_psBaoHu->DiNum; i++)
{
SetPntMsg(commid, i, (void*)&dipntmsg, DI_PNT_TYPE, PNT_CTRL_NO);
dipntmsg.ControlNo++;
}
}
for (i = 0; i < SioParam[commid].m_psBaoHu->DiNum; i++)
{
sprintf(section, "Di%dCtrlNo", i+1);
#ifdef _OS_WINDOWS_DEBUG_
sprintf(szDebugInfo, ">>>>(%04d):[%s]<%s>\n", _getpid(), entry, section);
OutputDebugString((LPCWSTR)szDebugInfo);
#endif
GetPrivateProString(entry, section, "", szbuf, 120, szPortConfig);
if (strlen(szbuf) == 0)
continue;
dipntmsg.ControlNo = atoi(szbuf);
SetPntMsg(commid, i, (void*)&dipntmsg, DI_PNT_TYPE, PNT_CTRL_NO);
}
break;
case PROTOCOL_SLAVE:
if ((ProvPortCnt+1) > MAX_PROV_PORT_NUM)
{
#ifdef _OS_WINDOWS_DEBUG_
sprintf(szDebugInfo, "WARN(%04d): commid_%02d is error, the Prov Port is over !!!\n",
_getpid(), commid + 1);
OutputDebugString((LPCWSTR)szDebugInfo);
#endif
break;
}
ProvPortCnt++;
//SioParam[commid].PortType = PROTOCOL_SLAVE;
if (pBaohuParam->AiPtr)
{
#ifdef _OS_WINDOWS_DEBUG_
sprintf(szDebugInfo, "TIP_(%04d): commid_%02d free(aiptr=0x%08x).\n",
_getpid(), commid + 1, pBaohuParam->AiPtr);
OutputDebugString((LPCWSTR)szDebugInfo);
#endif
free(pBaohuParam->AiPtr);
}
if (pBaohuParam->DiPtr)
{
#ifdef _OS_WINDOWS_DEBUG_
sprintf(szDebugInfo, "TIP_(%04d): commid_%02d free(diptr=0x%08x).\n",
_getpid(), commid + 1, pBaohuParam->DiPtr);
OutputDebugString((LPCWSTR)szDebugInfo);
#endif
free(pBaohuParam->DiPtr);
}
if (pBaohuParam->PiPtr)
{
#ifdef _OS_WINDOWS_DEBUG_
sprintf(szDebugInfo, "TIP_(%04d): commid_%02d free(piptr=0x%08x).\n",
_getpid(), commid + 1, pBaohuParam->PiPtr);
OutputDebugString((LPCWSTR)szDebugInfo);
#endif
free(pBaohuParam->PiPtr);
}
if(pBaohuParam->ProvSoeBuf.ptrProvSoe)
{
#ifdef _OS_WINDOWS_DEBUG_
sprintf(szDebugInfo, "TIP_(%04d): commid_%02d free(soeptr=0x%08x).\n",
_getpid(), commid + 1, pBaohuParam->ProvSoeBuf.ptrProvSoe);
OutputDebugString((LPCWSTR)szDebugInfo);
#endif
free(pBaohuParam->ProvSoeBuf.ptrProvSoe);
}
if(pBaohuParam->AiNum > 0)
{
pBaohuParam->AiPtr
= (PROV_AI_PNT*)malloc(sizeof(PROV_AI_PNT)*pBaohuParam->AiNum);
if (pBaohuParam->AiPtr)
{
memset((char*)pBaohuParam->AiPtr, 0,
sizeof(PROV_AI_PNT)*pBaohuParam->AiNum);
}
else
{
#ifdef _OS_WINDOWS_DEBUG_
sprintf(szDebugInfo, "ERR_(%04d): commid_%02d aiptr=malloc(%d) is failed.\n",
_getpid(), commid + 1, sizeof(PROV_AI_PNT)*pBaohuParam->AiNum);
OutputDebugString((LPCWSTR)szDebugInfo);
#endif
pBaohuParam->AiNum = 0;
}
}
else
{
pBaohuParam->AiPtr = NULL;
pBaohuParam->AiNum = 0;
}
if(pBaohuParam->DiNum > 0)
{
pBaohuParam->DiPtr
= (PROV_DI_PNT*)malloc(sizeof(PROV_DI_PNT)*pBaohuParam->DiNum);
if (pBaohuParam->DiPtr)
{
memset((char*)pBaohuParam->DiPtr, 0,
sizeof(PROV_DI_PNT)*pBaohuParam->DiNum);
}
else
{
#ifdef _OS_WINDOWS_DEBUG_
sprintf(szDebugInfo, "ERR_(%04d): commid_%02d diptr=malloc(%d) is failed.\n",
_getpid(), commid + 1, sizeof(PROV_DI_PNT)*pBaohuParam->DiNum);
OutputDebugString((LPCWSTR)szDebugInfo);
#endif
pBaohuParam->DiNum = 0;
}
pBaohuParam->ProvSoeBuf.ptrProvSoe
= (SOE_DEF *)malloc(sizeof(SOE_DEF)*MAX_PROV_SOE);
if(pBaohuParam->ProvSoeBuf.ptrProvSoe)
{
memset((char *)pBaohuParam->ProvSoeBuf.ptrProvSoe, 0,
sizeof(SOE_DEF)*MAX_PROV_SOE);
}
else
{
#ifdef _OS_WINDOWS_DEBUG_
sprintf(szDebugInfo, "ERR_(%04d): commid_%02d soeptr=malloc(%d) is failed.\n",
_getpid(), commid + 1, sizeof(SOE_DEF)*MAX_PROV_SOE);
OutputDebugString((LPCWSTR)szDebugInfo);
#endif
}
}
else
{
pBaohuParam->DiNum = 0;
pBaohuParam->DiPtr = NULL;
pBaohuParam->ProvSoeBuf.ptrProvSoe = NULL;
}
if(pBaohuParam->PiNum > 0)
{
pBaohuParam->PiPtr
= (PROV_PI_PNT*)malloc(sizeof(PROV_PI_PNT)*pBaohuParam->PiNum);
if (pBaohuParam->PiPtr)
{
memset((char*)pBaohuParam->PiPtr, 0,
sizeof(PROV_PI_PNT)*pBaohuParam->PiNum);
}
else
{
#ifdef _OS_WINDOWS_DEBUG_
sprintf(szDebugInfo, "ERR_(%04d): commid_%02d piptr=malloc(%d) is failed.\n",
_getpid(), commid + 1, sizeof(PROV_PI_PNT)*pBaohuParam->PiNum);
OutputDebugString((LPCWSTR)szDebugInfo);
#endif
pBaohuParam->PiNum = 0;
}
}
else
{
pBaohuParam->PiPtr = NULL;
pBaohuParam->PiNum = 0;
}
sprintf(entry, "Port%d", commid + 1);
for (i = 0; i < pBaohuParam->AiNum; i++)
{
aiprovpnt.fFactor = 1.0;
sprintf(section, "ProvAi%d", i+1);
#ifdef _OS_WINDOWS_DEBUG_
sprintf(szDebugInfo, ">>>>(%04d): [%s]<%s>\n", _getpid(), entry, section);
OutputDebugString((LPCWSTR)szDebugInfo);
#endif
GetPrivateProString(entry, section, "", szbuf, 120, szPortConfig);
StringToUpper(szbuf);
if (strlen(szbuf) == 0)
continue;
#ifdef _OS_WINDOWS_DEBUG_
sprintf(szDebugInfo, "####(%04d): %s#\n", _getpid(), szbuf);
OutputDebugString((LPCWSTR)szDebugInfo);
#endif
ptr = strstr(szbuf, "A");
if (!ptr)
continue;
aiprovpnt.PortNo = atoi(szbuf) - 1;
aiprovpnt.PntNo = atoi((char*)(ptr+1)) - 1;
#ifdef _OS_WINDOWS_DEBUG_
sprintf(szDebugInfo, "TIP_: commid_%02d provpnt=%d, realport=%d, realpnt=%d\n",
commid + 1, i + 1, aiprovpnt.PortNo, aiprovpnt.PntNo);
OutputDebugString((LPCWSTR)szDebugInfo);
#endif
ptr = strstr(ptr, ",");
if(ptr)
{
aiprovpnt.fFactor = (float)atof(ptr+1);
}
SetPorvPntMsg(commid, i, &aiprovpnt, AI_PNT_TYPE, PNT_PROV_MSG);
sprintf(section, "ProvAi%dInc", i+1);
GetPrivateProString(entry, section, "", szbuf, 120, szPortConfig);
k = atoi(szbuf);
while(k > 0)
{
sprintf(section, "ProvAi%d", i+2);
GetPrivateProString(entry, section, "", szbuf, 120, szPortConfig);
if(strlen(szbuf))
{
break;
}
i++;
aiprovpnt.PntNo++;
SetPorvPntMsg(commid, i, &aiprovpnt, AI_PNT_TYPE, PNT_PROV_MSG);
#ifdef _OS_WINDOWS_DEBUG_
sprintf(szDebugInfo, "TIP_: commid_%02d provpnt=%d, realport=%d, realpnt=%d\n",
commid + 1, i + 1, aiprovpnt.PortNo, aiprovpnt.PntNo);
OutputDebugString((LPCWSTR)szDebugInfo);
#endif
k--;
}
}
GetPrivateProString(entry, "ProvDiAllCtrlNo", "", szbuf, 120, szPortConfig);
if (strlen(szbuf))
{
diprovpnt.ControlNo = atoi(szbuf);
for (i = 0; i < pBaohuParam->DiNum; i++)
{
SetPorvPntMsg(commid, i, &diprovpnt, DI_PNT_TYPE, PNT_CTRL_NO);
diprovpnt.ControlNo++;
}
}
for (i = 0; i < pBaohuParam->DiNum; i++)
{
sprintf(section, "ProvDi%d", i+1);
#ifdef _OS_WINDOWS_DEBUG_
sprintf(szDebugInfo, ">>>>(%04d): [%s]<%s>\n", _getpid(), entry, section);
OutputDebugString((LPCWSTR)szDebugInfo);
#endif
GetPrivateProString(entry, section, "", szbuf, 120, szPortConfig);
StringToUpper(szbuf);
if (strlen(szbuf) == 0)
continue;
ptr = strstr(szbuf, "D");
if (!ptr)
continue;
diprovpnt.PortNo = atoi(szbuf) - 1;
diprovpnt.PntNo = atoi((char*)(ptr+1)) - 1;
SetPorvPntMsg(commid, i, &diprovpnt, DI_PNT_TYPE, PNT_PROV_MSG);
ptr = strstr(szbuf, ",");
if (ptr)
{
diprovpnt.ControlNo = atoi((char*)(ptr+1));
SetPorvPntMsg(commid, i, &diprovpnt, DI_PNT_TYPE, PNT_CTRL_NO);
}
// wen 2005.03.28 增加连续的控点配置
else
{
diprovpnt.ControlNo = -1;
}
sprintf(section, "ProvDi%dInc", i+1);
GetPrivateProString(entry, section, "", szbuf, 120, szPortConfig);
k = atoi(szbuf);
while(k > 0)
{
sprintf(section, "ProvDi%d", i+2);
GetPrivateProString(entry, section, "", szbuf, 120, szPortConfig);
if (strlen(szbuf))
break;
i++;
diprovpnt.PntNo++;
SetPorvPntMsg(commid, i, &diprovpnt, DI_PNT_TYPE, PNT_PROV_MSG);
k--;
// wen 2005.03.28 增加连续的控点配置
if(diprovpnt.ControlNo >= 0)
{
diprovpnt.ControlNo++;
SetPorvPntMsg(commid, i, &diprovpnt, DI_PNT_TYPE, PNT_CTRL_NO);
}
}
}
for (i = 0; i < pBaohuParam->DiNum; i++)
{
sprintf(section, "ProvDi%dCtrlNo", i+1);
#ifdef _OS_WINDOWS_DEBUG_
sprintf(szDebugInfo, ">>>>(%04d): [%s]<%s>\n", _getpid(), entry, section);
OutputDebugString((LPCWSTR)szDebugInfo);
#endif
GetPrivateProString(entry, section, "", szbuf, 120, szPortConfig);
if (strlen(szbuf) == 0)
continue;
diprovpnt.ControlNo = atoi(szbuf);
SetPorvPntMsg(commid, i, &diprovpnt, DI_PNT_TYPE, PNT_CTRL_NO);
}
for (i = 0; i < pBaohuParam->PiNum; i++)
{
piprovpnt.fFactor = 1.0;
sprintf(section, "ProvPi%d", i+1);
#ifdef _OS_WINDOWS_DEBUG_
sprintf(szDebugInfo, ">>>>(%04d): [%s]<%s>\n", _getpid(), entry, section);
OutputDebugString((LPCWSTR)szDebugInfo);
#endif
GetPrivateProString(entry, section, "", szbuf, 120, szPortConfig);
StringToUpper(szbuf);
if (strlen(szbuf) == 0)
continue;
ptr = strstr(szbuf, "P");
if (!ptr)
continue;
piprovpnt.PortNo = atoi(szbuf) - 1;
piprovpnt.PntNo = atoi((char*)(ptr+1)) - 1;
ptr = strstr(ptr, ",");
if(ptr)
{
piprovpnt.fFactor = (float)atof(ptr+1);
}
SetPorvPntMsg(commid, i, &piprovpnt, PI_PNT_TYPE, PNT_PROV_MSG);
sprintf(section, "ProvPi%dInc", i+1);
GetPrivateProString(entry, section, "", szbuf, 120, szPortConfig);
k = atoi(szbuf);
while(k > 0)
{
sprintf(section, "ProvPi%d", i+2);
GetPrivateProString(entry, section, "", szbuf, 120, szPortConfig);
if (strlen(szbuf))
break;
i++;
piprovpnt.PntNo++;
SetPorvPntMsg(commid, i, &piprovpnt, PI_PNT_TYPE, PNT_PROV_MSG);
k--;
}
}
break;
default:
break;
}
}
void PortMemoryMallocEx(int commid, int type, SIO_PARAM_BAOHU_DEF *pBaohuParam)
{
char szDebugInfo[256];
if ((commid < 0) || (commid >= MaxPortNum))
return;
if (!IsBaoHuPtr(commid))
return;
char szPortConfig[256];
//GetCurrentDirectory(sizeof(szDir), szDir);
sprintf(szPortConfig, "%s/portconfig.ini", IniFilePath);
switch(type)
{
case PROTOCOL_MASTER:
//SioParam[commid].PortType = PROTOCOL_MASTER;
if (pBaohuParam->AiPtr)
{
#ifdef _OS_WINDOWS_DEBUG_
sprintf(szDebugInfo, "TIP_(%04d): commid_%02d free(aiptr=0x%08x).\n",
_getpid(), commid + 1, pBaohuParam->AiPtr);
OutputDebugString((LPCWSTR)szDebugInfo);
#endif
free(pBaohuParam->AiPtr);
}
if (pBaohuParam->DiPtr)
{
#ifdef _OS_WINDOWS_DEBUG_
sprintf(szDebugInfo, "TIP_(%04d): commid_%02d free(diptr=0x%08x).\n",
_getpid(), commid + 1, pBaohuParam->DiPtr);
OutputDebugString((LPCWSTR)szDebugInfo);
#endif
free(pBaohuParam->DiPtr);
}
if (pBaohuParam->PiPtr)
{
#ifdef _OS_WINDOWS_DEBUG_
sprintf(szDebugInfo, "TIP_(%04d): commid_%02d free(piptr=0x%08x).\n",
_getpid(), commid + 1, pBaohuParam->PiPtr);
OutputDebugString((LPCWSTR)szDebugInfo);
#endif
free(pBaohuParam->PiPtr);
}
if(pBaohuParam->AiNum > 0)
{
pBaohuParam->AiPtr
= (AI_DEF*)malloc(sizeof(AI_DEF)*pBaohuParam->AiNum);
if (pBaohuParam->AiPtr)
{
memset((char*)pBaohuParam->AiPtr, 0,
sizeof(AI_DEF)*pBaohuParam->AiNum);
}
else
{
#ifdef _OS_WINDOWS_DEBUG_
sprintf(szDebugInfo, "ERR_(%04d): commid_%02d aiptr=malloc(%d) is failed.\n",
_getpid(), commid + 1, sizeof(AI_DEF)*pBaohuParam->AiNum);
OutputDebugString((LPCWSTR)szDebugInfo);
#endif
pBaohuParam->AiNum = 0;
}
}
else
{
pBaohuParam->AiNum = 0;
pBaohuParam->AiPtr = NULL;
}
if(pBaohuParam->DiNum > 0)
{
pBaohuParam->DiPtr
= (DI_DEF*)malloc(sizeof(DI_DEF)*pBaohuParam->DiNum);
if (pBaohuParam->DiPtr)
{
memset((char*)pBaohuParam->DiPtr, 0,
sizeof(DI_DEF)*pBaohuParam->DiNum);
}
else
{
#ifdef _OS_WINDOWS_DEBUG_
sprintf(szDebugInfo, "ERR_(%04d): commid_%02d diptr=malloc(%d) is failed.\n",
_getpid(), commid + 1, sizeof(DI_DEF)*pBaohuParam->DiNum);
OutputDebugString((LPCWSTR)szDebugInfo);
#endif
pBaohuParam->DiNum = 0;
}
}
else
{
pBaohuParam->DiNum = 0;
pBaohuParam->DiPtr = NULL;
}
if(pBaohuParam->PiNum > 0)
{
pBaohuParam->PiPtr
= (PI_DEF*)malloc(sizeof(PI_DEF)*pBaohuParam->PiNum);
if (pBaohuParam->PiPtr)
{
memset((char*)pBaohuParam->PiPtr, 0,
sizeof(PI_DEF)*pBaohuParam->PiNum);
}
else
{
#ifdef _OS_WINDOWS_DEBUG_
sprintf(szDebugInfo, "ERR_(%04d): commid_%02d piptr=malloc(%d) is failed.\n",
_getpid(), commid + 1, sizeof(PI_DEF)*pBaohuParam->PiNum);
OutputDebugString((LPCWSTR)szDebugInfo);
#endif
pBaohuParam->PiNum = 0;
}
}
else
{
pBaohuParam->PiNum = 0;
pBaohuParam->PiPtr = NULL;
}
break;
case PROTOCOL_SLAVE:
if ((ProvPortCnt+1) > MAX_PROV_PORT_NUM)
{
#ifdef _OS_WINDOWS_DEBUG_
sprintf(szDebugInfo, "WARN(%04d): commid_%02d is error, the Prov Port is over !!!\n",
_getpid(), commid + 1);
OutputDebugString((LPCWSTR)szDebugInfo);
#endif
break;
}
ProvPortCnt++;
if (pBaohuParam->AiPtr)
{
#ifdef _OS_WINDOWS_DEBUG_
sprintf(szDebugInfo, "TIP_(%04d): commid_%02d free(aiptr=0x%08x).\n",
_getpid(), commid + 1, pBaohuParam->AiPtr);
OutputDebugString((LPCWSTR)szDebugInfo);
#endif
free(pBaohuParam->AiPtr);
}
if (pBaohuParam->DiPtr)
{
#ifdef _OS_WINDOWS_DEBUG_
sprintf(szDebugInfo, "TIP_(%04d): commid_%02d free(diptr=0x%08x).\n",
_getpid(), commid + 1, pBaohuParam->DiPtr);
OutputDebugString((LPCWSTR)szDebugInfo);
#endif
free(pBaohuParam->DiPtr);
}
if (pBaohuParam->PiPtr)
{
#ifdef _OS_WINDOWS_DEBUG_
sprintf(szDebugInfo, "TIP_(%04d): commid_%02d free(piptr=0x%08x).\n",
_getpid(), commid + 1, pBaohuParam->PiPtr);
OutputDebugString((LPCWSTR)szDebugInfo);
#endif
free(pBaohuParam->PiPtr);
}
if(pBaohuParam->ProvSoeBuf.ptrProvSoe)
{
#ifdef _OS_WINDOWS_DEBUG_
sprintf(szDebugInfo, "TIP_(%04d): commid_%02d free(soeptr=0x%08x).\n",
_getpid(), commid + 1, pBaohuParam->ProvSoeBuf.ptrProvSoe);
OutputDebugString((LPCWSTR)szDebugInfo);
#endif
free(pBaohuParam->ProvSoeBuf.ptrProvSoe);
}
if(pBaohuParam->AiNum > 0)
{
pBaohuParam->AiPtr
= (PROV_AI_PNT*)malloc(sizeof(PROV_AI_PNT)*pBaohuParam->AiNum);
if (pBaohuParam->AiPtr)
{
memset((char*)pBaohuParam->AiPtr, 0,
sizeof(PROV_AI_PNT)*pBaohuParam->AiNum);
}
else
{
#ifdef _OS_WINDOWS_DEBUG_
sprintf(szDebugInfo, "ERR_(%04d): commid_%02d aiptr=malloc(%d) is failed.\n",
_getpid(), commid + 1, sizeof(PROV_AI_PNT)*pBaohuParam->AiNum);
OutputDebugString((LPCWSTR)szDebugInfo);
#endif
pBaohuParam->AiNum = 0;
}
}
else
{
pBaohuParam->AiPtr = NULL;
pBaohuParam->AiNum = 0;
}
if(pBaohuParam->DiNum > 0)
{
pBaohuParam->DiPtr
= (PROV_DI_PNT*)malloc(sizeof(PROV_DI_PNT)*pBaohuParam->DiNum);
if (pBaohuParam->DiPtr)
{
memset((char*)pBaohuParam->DiPtr, 0,
sizeof(PROV_DI_PNT)*pBaohuParam->DiNum);
}
else
{
#ifdef _OS_WINDOWS_DEBUG_
sprintf(szDebugInfo, "ERR_(%04d): commid_%02d diptr=malloc(%d) is failed.\n",
_getpid(), commid + 1, sizeof(PROV_DI_PNT)*pBaohuParam->DiNum);
OutputDebugString((LPCWSTR)szDebugInfo);
#endif
pBaohuParam->DiNum = 0;
}
pBaohuParam->ProvSoeBuf.ptrProvSoe
= (SOE_DEF *)malloc(sizeof(SOE_DEF)*MAX_PROV_SOE);
if(pBaohuParam->ProvSoeBuf.ptrProvSoe)
{
memset((char *)pBaohuParam->ProvSoeBuf.ptrProvSoe, 0,
sizeof(SOE_DEF)*MAX_PROV_SOE);
}
else
{
#ifdef _OS_WINDOWS_DEBUG_
sprintf(szDebugInfo, "ERR_(%04d): commid_%02d soeptr=malloc(%d) is failed.\n",
_getpid(), commid + 1, sizeof(SOE_DEF)*MAX_PROV_SOE);
OutputDebugString((LPCWSTR)szDebugInfo);
#endif
}
}
else
{
pBaohuParam->DiNum = 0;
pBaohuParam->DiPtr = NULL;
pBaohuParam->ProvSoeBuf.ptrProvSoe = NULL;
}
if(pBaohuParam->PiNum > 0)
{
pBaohuParam->PiPtr
= (PROV_PI_PNT*)malloc(sizeof(PROV_PI_PNT)*pBaohuParam->PiNum);
if (pBaohuParam->PiPtr)
{
memset((char*)pBaohuParam->PiPtr, 0,
sizeof(PROV_PI_PNT)*pBaohuParam->PiNum);
}
else
{
#ifdef _OS_WINDOWS_DEBUG_
sprintf(szDebugInfo, "ERR_(%04d): commid_%02d piptr=malloc(%d) is failed.\n",
_getpid(), commid + 1, sizeof(PROV_PI_PNT)*pBaohuParam->PiNum);
OutputDebugString((LPCWSTR)szDebugInfo);
#endif
pBaohuParam->PiNum = 0;
}
}
else
{
pBaohuParam->PiPtr = NULL;
pBaohuParam->PiNum = 0;
}
break;
default:
break;
}
}
BOOL PortDataPntInit(int commid, int type, SIO_PARAM_BAOHU_DEF *pBaohuParam)
{
int i, k, increase;
char entry[128], section[128], szbuf[128];
char *ptr;
AI_DEF aipntmsg;
DI_DEF dipntmsg;
PROV_AI_PNT aiprovpnt;
PROV_DI_PNT diprovpnt;
PROV_PI_PNT piprovpnt;
if((commid < 0) || (commid >= MaxPortNum))
{
return FALSE;
}
if(!IsBaoHuPtr(commid))
{
return FALSE;
}
char szPortConfig[256];
//GetCurrentDirectory(sizeof(szDir), szDir);
sprintf(szPortConfig, "%s/portconfig.ini", IniFilePath);
switch(type)
{
case PROTOCOL_MASTER:
sprintf(entry, "Port%d", commid + 1);
for (i = 0; i < pBaohuParam->AiNum; i++)
{
sprintf(section, "Ai%dPolar", i+1);
#ifdef _OS_WINDOWS_DEBUG_
sprintf(szDebugInfo, ">>>>(%04d):[%s]<%s>\n", _getpid(), entry, section);
OutputDebugString((LPCWSTR)szDebugInfo);
#endif
GetPrivateProString(entry, section, "", szbuf, 120, szPortConfig);
if (strlen(szbuf) == 0)
{
// wen 2005.09.28 缺省设置为1
aipntmsg.Polar = 1;
SetPntMsg(commid, i, (void*)&aipntmsg, AI_PNT_TYPE, PNT_POLAR);
continue;
}
aipntmsg.Polar = atoi(szbuf) ? 1 : 0;
SetPntMsg(commid, i, (void*)&aipntmsg, AI_PNT_TYPE, PNT_POLAR);
// wen 2005.09.13 增加顺序增加配置
sprintf(section, "Ai%dInc", i+1);
GetPrivateProString(entry, section, "", szbuf, 120, szPortConfig);
if(strlen(szbuf) == 0)
continue;
increase = atoi(szbuf);
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// wen 2005.12.01 修改连续增加的读取
//if(increase > 0)
//{
// for(k=i+1; k<increase; k++)
// {
// if(k >= pBaohuParam->AiNum)
// {
// break;
// }
// SetPntMsg(commid, k, (void*)&aipntmsg, AI_PNT_TYPE, PNT_POLAR);
// }
// i = k;
//}
//================================================================
if(increase > 0)
{
for(k=0; k<increase; k++)
{
if((i+k+1) >= pBaohuParam->AiNum)
{
break;
}
SetPntMsg(commid, i+k+1, (void*)&aipntmsg, AI_PNT_TYPE, PNT_POLAR);
}
i += k;
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
}
GetPrivateProString(entry, "DiAllCtrlNo", "", szbuf, 120, szPortConfig);
if (strlen(szbuf))
{
dipntmsg.ControlNo = atoi(szbuf);
for (i = 0; i < SioParam[commid].m_psBaoHu->DiNum; i++)
{
SetPntMsg(commid, i, (void*)&dipntmsg, DI_PNT_TYPE, PNT_CTRL_NO);
dipntmsg.ControlNo++;
}
}
for (i = 0; i < SioParam[commid].m_psBaoHu->DiNum; i++)
{
sprintf(section, "Di%dCtrlNo", i+1);
#ifdef _OS_WINDOWS_DEBUG_
sprintf(szDebugInfo, ">>>>(%04d):[%s]<%s>\n", _getpid(), entry, section);
OutputDebugString((LPCWSTR)szDebugInfo);
#endif
GetPrivateProString(entry, section, "", szbuf, 120, szPortConfig);
if (strlen(szbuf) == 0)
continue;
dipntmsg.ControlNo = atoi(szbuf);
SetPntMsg(commid, i, (void*)&dipntmsg, DI_PNT_TYPE, PNT_CTRL_NO);
}
break;
case PROTOCOL_SLAVE:
sprintf(entry, "Port%d", commid + 1);
for (i = 0; i < pBaohuParam->AiNum; i++)
{
aiprovpnt.fFactor = 1.0;
sprintf(section, "ProvAi%d", i+1);
#ifdef _OS_WINDOWS_DEBUG_
sprintf(szDebugInfo, ">>>>(%04d): [%s]<%s>\n", _getpid(), entry, section);
OutputDebugString((LPCWSTR)szDebugInfo);
#endif
GetPrivateProString(entry, section, "", szbuf, 120, szPortConfig);
StringToUpper(szbuf);
if (strlen(szbuf) == 0)
continue;
#ifdef _OS_WINDOWS_DEBUG_
sprintf(szDebugInfo, "####(%04d): %s#\n", _getpid(), szbuf);
OutputDebugString((LPCWSTR)szDebugInfo);
#endif
ptr = strstr(szbuf, "A");
if (!ptr)
continue;
aiprovpnt.PortNo = atoi(szbuf) - 1;
aiprovpnt.PntNo = atoi((char*)(ptr+1)) - 1;
#ifdef _OS_WINDOWS_DEBUG_
sprintf(szDebugInfo, "TIP_: commid_%02d provpnt=%d, realport=%d, realpnt=%d\n",
commid + 1, i + 1, aiprovpnt.PortNo, aiprovpnt.PntNo);
OutputDebugString((LPCWSTR)szDebugInfo);
#endif
ptr = strstr(ptr, ",");
if(ptr)
{
aiprovpnt.fFactor = (float)atof(ptr+1);
}
SetPorvPntMsg(commid, i, &aiprovpnt, AI_PNT_TYPE, PNT_PROV_MSG);
sprintf(section, "ProvAi%dInc", i+1);
GetPrivateProString(entry, section, "", szbuf, 120, szPortConfig);
k = atoi(szbuf);
while(k > 0)
{
sprintf(section, "ProvAi%d", i+2);
GetPrivateProString(entry, section, "", szbuf, 120, szPortConfig);
if (strlen(szbuf))
break;
i++;
aiprovpnt.PntNo++;
SetPorvPntMsg(commid, i, &aiprovpnt, AI_PNT_TYPE, PNT_PROV_MSG);
#ifdef _OS_WINDOWS_DEBUG_
sprintf(szDebugInfo, "TIP_: commid_%02d provpnt=%d, realport=%d, realpnt=%d\n",
commid + 1, i + 1, aiprovpnt.PortNo, aiprovpnt.PntNo);
OutputDebugString((LPCWSTR)szDebugInfo);
#endif
k--;
}
}
GetPrivateProString(entry, "ProvDiAllCtrlNo", "", szbuf, 120, szPortConfig);
if (strlen(szbuf))
{
diprovpnt.ControlNo = atoi(szbuf);
for (i = 0; i < pBaohuParam->DiNum; i++)
{
SetPorvPntMsg(commid, i, &diprovpnt, DI_PNT_TYPE, PNT_CTRL_NO);
diprovpnt.ControlNo++;
}
}
for (i = 0; i < pBaohuParam->DiNum; i++)
{
sprintf(section, "ProvDi%d", i+1);
#ifdef _OS_WINDOWS_DEBUG_
sprintf(szDebugInfo, ">>>>(%04d): [%s]<%s>\n", _getpid(), entry, section);
OutputDebugString((LPCWSTR)szDebugInfo);
#endif
GetPrivateProString(entry, section, "", szbuf, 120, szPortConfig);
StringToUpper(szbuf);
if (strlen(szbuf) == 0)
continue;
ptr = strstr(szbuf, "D");
if (!ptr)
continue;
diprovpnt.PortNo = atoi(szbuf) - 1;
diprovpnt.PntNo = atoi((char*)(ptr+1)) - 1;
SetPorvPntMsg(commid, i, &diprovpnt, DI_PNT_TYPE, PNT_PROV_MSG);
ptr = strstr(szbuf, ",");
if (ptr)
{
diprovpnt.ControlNo = atoi((char*)(ptr+1));
SetPorvPntMsg(commid, i, &diprovpnt, DI_PNT_TYPE, PNT_CTRL_NO);
}
// wen 2005.03.28 增加连续的控点配置
else
{
diprovpnt.ControlNo = -1;
}
sprintf(section, "ProvDi%dInc", i+1);
GetPrivateProString(entry, section, "", szbuf, 120, szPortConfig);
k = atoi(szbuf);
while(k > 0)
{
sprintf(section, "ProvDi%d", i+2);
GetPrivateProString(entry, section, "", szbuf, 120, szPortConfig);
if (strlen(szbuf))
break;
i++;
diprovpnt.PntNo++;
SetPorvPntMsg(commid, i, &diprovpnt, DI_PNT_TYPE, PNT_PROV_MSG);
k--;
// wen 2005.03.28 增加连续的控点配置
if(diprovpnt.ControlNo >= 0)
{
diprovpnt.ControlNo++;
SetPorvPntMsg(commid, i, &diprovpnt, DI_PNT_TYPE, PNT_CTRL_NO);
}
}
}
for (i = 0; i < pBaohuParam->DiNum; i++)
{
sprintf(section, "ProvDi%dCtrlNo", i+1);
#ifdef _OS_WINDOWS_DEBUG_
sprintf(szDebugInfo, ">>>>(%04d): [%s]<%s>\n", _getpid(), entry, section);
OutputDebugString((LPCWSTR)szDebugInfo);
#endif
GetPrivateProString(entry, section, "", szbuf, 120, szPortConfig);
if (strlen(szbuf) == 0)
continue;
diprovpnt.ControlNo = atoi(szbuf);
SetPorvPntMsg(commid, i, &diprovpnt, DI_PNT_TYPE, PNT_CTRL_NO);
}
for (i = 0; i < pBaohuParam->PiNum; i++)
{
piprovpnt.fFactor = 1.0;
sprintf(section, "ProvPi%d", i+1);
#ifdef _OS_WINDOWS_DEBUG_
sprintf(szDebugInfo, ">>>>(%04d): [%s]<%s>\n", _getpid(), entry, section);
OutputDebugString((LPCWSTR)szDebugInfo);
#endif
GetPrivateProString(entry, section, "", szbuf, 120, szPortConfig);
StringToUpper(szbuf);
if (strlen(szbuf) == 0)
continue;
ptr = strstr(szbuf, "P");
if (!ptr)
continue;
piprovpnt.PortNo = atoi(szbuf) - 1;
piprovpnt.PntNo = atoi((char*)(ptr+1)) - 1;
ptr = strstr(ptr, ",");
if(ptr)
{
piprovpnt.fFactor = (float)atof(ptr+1);
}
SetPorvPntMsg(commid, i, &piprovpnt, PI_PNT_TYPE, PNT_PROV_MSG);
sprintf(section, "ProvPi%dInc", i+1);
GetPrivateProString(entry, section, "", szbuf, 120, szPortConfig);
k = atoi(szbuf);
while(k > 0)
{
sprintf(section, "ProvPi%d", i+2);
GetPrivateProString(entry, section, "", szbuf, 120, szPortConfig);
if (strlen(szbuf))
break;
i++;
piprovpnt.PntNo++;
SetPorvPntMsg(commid, i, &piprovpnt, PI_PNT_TYPE, PNT_PROV_MSG);
k--;
}
}
break;
default:
break;
}
return TRUE;
}
void PortWriteDataFree(int commid)
{
/*
char szDebugInfo[256];
sprintf(szDebugInfo, "TIP_(%04d): commid_%02d free all write data begin...\n",
_getpid(), commid);
OutputDebugString((LPCWSTR)szDebugInfo);
while (SioParam[commid].WriteData)
{
DelNode(commid);
}
sprintf(szDebugInfo, "TIP_(%04d): commid_%02d free all write data end.\n",
_getpid(), commid);
OutputDebugString((LPCWSTR)szDebugInfo);
*/
}
void PortMemoryFree(int commid, SIO_PARAM_DEF *pGenParam)
{
char szDebugInfo[256];
//sprintf(szDebugInfo, "TIP_(%04d): commid_%02d free memory begin.\n", _getpid(), commid);
//OutputDebugString((LPCWSTR)szDebugInfo);
if (pGenParam->m_psBaoHu)
{
if (pGenParam->m_psBaoHu->AiPtr)
{
free(pGenParam->m_psBaoHu->AiPtr);
#ifdef _OS_WINDOWS_DEBUG_
sprintf(szDebugInfo, "TIP_(%04d): commid_%02d free ai memory.\n", _getpid(), commid + 1);
OutputDebugString((LPCWSTR)szDebugInfo);
#endif
}
if (pGenParam->m_psBaoHu->DiPtr)
{
free(pGenParam->m_psBaoHu->DiPtr);
#ifdef _OS_WINDOWS_DEBUG_
sprintf(szDebugInfo, "TIP_(%04d): commid_%02d free di memory.\n", _getpid(), commid + 1);
OutputDebugString((LPCWSTR)szDebugInfo);
#endif
}
if (pGenParam->m_psBaoHu->PiPtr)
{
free(pGenParam->m_psBaoHu->PiPtr);
#ifdef _OS_WINDOWS_DEBUG_
sprintf(szDebugInfo, "TIP_(%04d): commid_%02d free pi memory.\n", _getpid(), commid + 1);
OutputDebugString((LPCWSTR)szDebugInfo);
#endif
}
if(pGenParam->m_psBaoHu->ProvSoeBuf.ptrProvSoe)
{
free(pGenParam->m_psBaoHu->ProvSoeBuf.ptrProvSoe);
#ifdef _OS_WINDOWS_DEBUG_
sprintf(szDebugInfo, "TIP_(%04d): commid_%02d free ProvSoe memory.\n", _getpid(), commid + 1);
OutputDebugString((LPCWSTR)szDebugInfo);
#endif
}
free(pGenParam->m_psBaoHu);
#ifdef _OS_WINDOWS_DEBUG_
sprintf(szDebugInfo, "TIP_(%04d): commid_%02d free psBaohu memory.\n", _getpid(), commid + 1);
OutputDebugString((LPCWSTR)szDebugInfo);
#endif
}
if (pGenParam->m_psSerial)
{
free(pGenParam->m_psSerial);
#ifdef _OS_WINDOWS_DEBUG_
sprintf(szDebugInfo, "TIP_(%04d): commid_%02d free psSerial memory.\n", _getpid(), commid + 1);
OutputDebugString((LPCWSTR)szDebugInfo);
#endif
}
if (pGenParam->ExtInfo)
{
// wen 2005.04.13 规约结束时释放内存
if (FunCallPtr[SioParam[commid].ProtocolIdx].ProtocolExit)
{
FunCallPtr[SioParam[commid].ProtocolIdx].ProtocolExit(commid);
#ifdef _OS_WINDOWS_DEBUG_
sprintf(szDebugInfo, "TIP_(%04d): commid_%02d Protocol Exit.\n", _getpid(), commid + 1);
OutputDebugString((LPCWSTR)szDebugInfo);
#endif
}
HEAP_FREE(pGenParam->ExtInfo);
//free(pGenParam->ExtInfo);
#ifdef _OS_WINDOWS_DEBUG_
sprintf(szDebugInfo, "TIP_(%04d): commid_%02d free ExtInfo memory.\n", _getpid(), commid + 1);
OutputDebugString((LPCWSTR)szDebugInfo);
#endif
}
PortWriteDataFree(commid);
pGenParam->m_psBaoHu = NULL;
pGenParam->m_psSerial = NULL;
pGenParam->ExtInfo = NULL;
//sprintf(szDebugInfo, "TIP_(%04d): commid_%02d free memory end.\n", _getpid(), commid + 1);
//OutputDebugString((LPCWSTR)szDebugInfo);
}
BOOL IsBaoHuPtr(int commid)
{
char szInfo[256];
if ((commid < 0) || (commid >=MaxPortNum))
return FALSE;
if (!SioParam[commid].m_psBaoHu)
{
sprintf(szInfo, "commid_%02d psBaoHu is NULL.\n", commid + 1);
PrintFormatMessage(MSG_CLASS_TIP, szInfo);
return FALSE;
}
return TRUE;
}
BOOL IsExtInfoPtr(int commid)
{
char szInfo[256];
if ((commid < 0) || (commid >=MaxPortNum))
return FALSE;
if (!SioParam[commid].ExtInfo)
{
sprintf(szInfo, "commid_%02d ExtInfo is NULL.\n", commid + 1);
PrintFormatMessage(MSG_CLASS_TIP, szInfo);
return FALSE;
}
return TRUE;
}
BOOL IsSerialPtr(int commid)
{
char szInfo[256];
if ((commid < 0) || (commid >=MaxPortNum))
return FALSE;
if (!SioParam[commid].m_psSerial)
{
sprintf(szInfo, "commid_%02d psSerial is NULL.\n", commid + 1);
PrintFormatMessage(MSG_CLASS_TIP, szInfo);
return FALSE;
}
return TRUE;
}
int ShmGetPortFlag(int commid, int rwflag)
{
if(commid >= DEFAULT_MAX_PORT_NUM)
return 0;
return SioParam[commid].OpenFlag;
}
void SendProtectCmdToDev(int commid, RTUMSG *rtumsg)
{
//if(SioParam[commid].OpenFlag == 0)
if(!ShmGetPortFlag(commid, FLAG_OPEN))
{
return;
}
if(SioParam[commid].ProtocolIdx < 1)
{
return;
}
if(FunCallPtr[SioParam[commid].ProtocolIdx].BaoHuCmdProcess)
{
FunCallPtr[SioParam[commid].ProtocolIdx].BaoHuCmdProcess(commid, rtumsg, FALSE);
}
}
void PutBaohuDataToPort(RTUMSG *rtumsg)
{
int commid;
commid = rtumsg->PortIdx;
//if(SioParam[commid].OpenFlag == 0)
if(!ShmGetPortFlag(commid, FLAG_OPEN))
{
return;
}
if(SioParam[commid].ProtocolIdx < 1)
{
return;
}
if(FunCallPtr[SioParam[commid].ProtocolIdx].BaoHuCmdProcess)
{
FunCallPtr[SioParam[commid].ProtocolIdx].BaoHuCmdProcess(commid, rtumsg, TRUE);
}
}
void PutBaohuDataToPortEx(int commid, RTUMSG *rtumsg)
{
if(!ShmGetPortFlag(commid, FLAG_OPEN))
{
return;
}
if(SioParam[commid].ProtocolIdx < 1)
{
return;
}
if(FunCallPtr[SioParam[commid].ProtocolIdx].BaoHuCmdProcess)
{
FunCallPtr[SioParam[commid].ProtocolIdx].BaoHuCmdProcess(commid, rtumsg, TRUE);
}
}
void SendDataToAllScadaProtectPort(RTUMSG *rtumsg)
{
int i, ProtocolIdx;
ProtocolIdx = GetPortProtocolIdx((char*)"SCADAPROTECT");
for(i=0; i<MaxPortNum; i++)
{
if(SioParam[i].ProtocolIdx == ProtocolIdx)
{
//rtumsg->PortIdx = i;
//PutBaohuDataToPort(rtumsg);
PutBaohuDataToPortEx(i, rtumsg);
}
}
}
int GetBaohuDB(int commid, int iProvAddr, GROUPDEF **ppBaoHuDB)
{
int iGrpNum, iProtocolIdx;
if((commid < 0) || (commid > MaxPortNum))
{
return 0;
}
if(SioParam[commid].ProtocolIdx < 1)
{
return 0;
}
iProtocolIdx = SioParam[commid].ProtocolIdx;
if(FunCallPtr[iProtocolIdx].GetBaohuDataBase)
{
iGrpNum = FunCallPtr[iProtocolIdx].GetBaohuDataBase(commid, iProvAddr, ppBaoHuDB);
}
else
{
iGrpNum = 0;
}
if(iGrpNum == 0)
{
*ppBaoHuDB = NULL;
}
return iGrpNum;
}
int GetSioParam(SIO_PARAM_DEF **ppParam)
{
*ppParam = SioParam;
return GetMaxPort();
}
int GetDevParam(DEV_DEF **ppParam)
{
#if 0
*ppParam = &DevParam[iCurDevIdx];
#endif
return GetMaxPort();
}
int SetCurPort(int PortNo)
{
CurPort = PortNo;
if(CurPort < 0)
{
RealDataDispFlag = 0;
}
else
{
RealDataDispFlag = 1;
}
return CurPort;
}
int GetCurPortEx()
{
return CurPort;
}
int SetDispFlag(int iFlag)
{
RealDataDispFlag = iFlag;
return RealDataDispFlag;
}
// 获取全局变量
SIO_PARAM_DEF *GetSioParamPtr()
{
return SioParam;
}