📄 msghdler.cpp
字号:
//**************************************************************************
//
// Filename : Msghdler.cpp
//
// Subsystem :
//
// Description: Protocol message handlers.
//
//**************************************************************************
/*
* NT Definitions
*/
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <math.h>
#include "gencom.h"
#include "comiosrv.h"
#include "locmsgdef.h"
#define byte_swap(value) (((value)>>8 & 0x00ff) | ((value)<<8 & 0xff00))
#define GET_BIT(a, b) (BYTE)a & (1 << b)
static BYTE FrameCheckSum(char *p_msg, int count)
{
char *p_buf = p_msg;
BYTE chksum = 0;
int index = 0;
while(index++ < count) {
chksum += *p_buf;
p_buf++;
}
return chksum;
}
VOID PollMarshall(REQUEST_MSGS* pReqMsg, CPio* pPio)
{
int rsplen, msg_size = 0;
BYTE *p_buf;
/*
* If we are starting a poll, the command setup device in polled mode.
*/
p_buf = GetPioWrtBuf(pPio);
switch ( pReqMsg->FuncCode ) {
case INIT_POLL:
case INFO_POLL: // normal polling
SetRtuCmdStep(pReqMsg->pRtu, POLLDIG);
// issue digital data poll cmd
digpollcmd[POLLADRPOS] = (BYTE)GetRtuNetId(pReqMsg->pRtu);
memcpy(p_buf, digpollcmd, POLLREQLEN);
rsplen = POLLDIGLEN + POLLREQLEN;
SetPioReadHeadCnt(pPio, rsplen);
SetPioReadCnt(pPio, rsplen);
SetPioUnPackFunc(pPio, PollUnmarshall);
break;
case RFLH_POLL: // send additional polling
if (GetRtuCmdStep(pReqMsg->pRtu) == POLLANA) {
// issue analog data poll cmd
anapollcmd[POLLADRPOS] = (BYTE)GetRtuNetId(pReqMsg->pRtu);
memcpy(p_buf, anapollcmd, POLLREQLEN);
rsplen = POLLANALEN + POLLREQLEN;
SetPioReadHeadCnt(pPio, rsplen);
SetPioReadCnt(pPio, rsplen);
SetPioUnPackFunc(pPio, PollUnmarshall);
}
break;
default:
printf( "PollMarshall(), Unexpected Cmd: %d\n", pReqMsg->FuncCode);
break;
}
memcpy(GetPioUserDefMsg(pPio), pReqMsg, sizeof(REQUEST_MSGS) );
SetPioWriteCnt(pPio, POLLREQLEN);
SetPioWriteOffset(pPio, 0);
//
// STS commands are acknowledged
// Set this stuff for a reply!
//
// One pass read, Simple two pass, purge
SetPioComState(pPio, FALSE, FALSE, TRUE);
// write data to debug buffer if rtu in debug mode
SetPioRtuDbgOutMsg(pPio, pReqMsg->pRtu);
SetPioReadOffset(pPio, 0);
SetPioReadFixupCnt(pPio, 0);
memset((char *)GetPioReadBuf(pPio), 0, 1);
}
VOID PollUnmarshall(CPio *pPio, int ioStream)
{
BYTE *p_buf;
int i, j, gix, pix;
int sign, pwer, value, procdi;
float fvalue;
BOOL bChange = FALSE;
POLDIG *pDigRsp = NULL;
POLANA *pAnaRsp = NULL;
POLLSTS nStep;
REQUEST_MSGS *sendInfo = (REQUEST_MSGS*)GetPioUserDefMsg(pPio);
p_buf = GetPioReadBuf(pPio);
if ( GetPioRecvStatus(pPio) == PIO__SUCCESS ) {
// write data to debug buffer if rtu in debug mode
SetPioRtuDbgInMsg(pPio, sendInfo->pRtu);
nStep = (POLLSTS)GetRtuCmdStep(sendInfo->pRtu);
switch (nStep) {
case POLLDIG: // digital response data
SetRtuCmdStep(sendInfo->pRtu, POLLANA);
// get digital response data from buffer
pDigRsp = (POLDIG *)p_buf;
if (pDigRsp->crc == FrameCheckSum((char *)pDigRsp->inpdat, POLLDIGLEN)) {
gix = DINORMSLOT;
pix = 0;
for (i = 0; i < POLLDIGLEN; i++) {
for (j = 0; j < 8; j++) {
value = (UCHAR)GET_BIT(pDigRsp->inpdat[i], j) ? 1 : 0;
procdi = UpdRtuRawData(sendInfo->pRtu, gix, pix, value);
if (procdi) {
bChange = TRUE;
}
pix++;
}
if ((i % 4) == 3) {
gix++;
pix = 0;
}
}
if (bChange) { // any DI change during process
UpdRtuPntSts(sendInfo->pRtu, 1);
}
// continue to poll analog data
sendInfo->FuncCode = RFLH_POLL;
// add a high priority refresh poll
AddMorePollCmd(sendInfo, TRUE);
}
else {
SetPioRecvStatus(pPio, PIO__BAD_CHECKSUM);
}
break;
case POLLANA: // analog response data
// get analog response data from buffer
pAnaRsp = (POLANA *)p_buf;
if (pAnaRsp->crc == FrameCheckSum((char *)pAnaRsp->inpdat, POLLANALEN)) {
for (i = 0; i < POLLANAITM; i++) {
if (pAnaRsp->inpdat[i][0] == (BYTE)0xff &&
pAnaRsp->inpdat[i][1] == (BYTE)0xff &&
pAnaRsp->inpdat[i][2] == (BYTE)0xff)
continue; // ignore
// get data sign and power number
sign = ((pAnaRsp->inpdat[i][0] & 0xf0) == 0xf0) ? 1 : 0;
pwer = pAnaRsp->inpdat[i][0] & 0x0f;
// format data value
value = (pAnaRsp->inpdat[i][1] >> 4) * 1000 + (pAnaRsp->inpdat[i][1] & 0x0f) * 100 +
(pAnaRsp->inpdat[i][2] >> 4) * 10 + (pAnaRsp->inpdat[i][2] & 0x0f);
fvalue = (float)value * (float)pow( 10.0, (double)(pwer - 4));
if (sign) fvalue = -fvalue;
UpdRtuFltData(sendInfo->pRtu, 0, i, fvalue, 0);
}
}
else {
SetPioRecvStatus(pPio, PIO__BAD_CHECKSUM);
}
break;
default:
printf("Unknow communication step: %d\n", nStep);
break;
}
}
// clear polling flag for next polling
ClrRtuPollSts(sendInfo->pRtu);
ClrPioRtuStream(ioStream);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -