📄 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 "gencom.h"
#include "comiosrv.h"
#include "locmsgdef.h"
#define byte_swap(value) (((value)>>8 & 0x00ff) | ((value)<<8 & 0xff00))
static unsigned char CheckSum(unsigned char *p_msg, int count)
{
unsigned char *p_buf = p_msg;
unsigned char chksum = 0;
int index = 0;
while(index++ < count) {
chksum += *p_buf;
p_buf++;
}
return chksum;
}
VOID PollMarshall(REQUEST_MSGS* pReqMsg, CPio* pPio)
{
int 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
wsReqCmd.uid = (BYTE)GetComRtuId(pReqMsg);
wsReqCmd.addr = WSSTADDR;
wsReqCmd.datlen = WSREQLEN;
wsReqCmd.chksum = CheckSum((unsigned char *)&wsReqCmd, WSREQCHK);
memcpy( p_buf, &wsReqCmd, sizeof(wsReqCmd) );
SetPioReadHeadCnt(pPio, WSREPLEN);
SetPioReadCnt(pPio, WSREPLEN);
SetPioUnPackFunc(pPio, PollUnmarshall);
break;
default:
printf( "PollMarshall(), Unexpected Cmd: %d\n", pReqMsg->FuncCode);
break;
}
memcpy(GetPioUserDefMsg(pPio), pReqMsg, sizeof(REQUEST_MSGS) );
SetPioWriteCnt(pPio, sizeof(wsReqCmd));
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;
UINT *pVal, value;
int i, nStart, nPntnum, nGix, nPix;
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);
if (p_buf[1] != 0x4f || p_buf[2] != 0x4b) {
// printf("Meter Error!\n");
SetPioRecvStatus(pPio, PIO__ACK_NOT_FOUND);
}
else if ((CheckSum( &p_buf[WSREPBEG], WSREPCHL ) != p_buf[WSREPCHK]) ) {
// post device data checksuum mismatch message
// printf("CheckSum Error!\n");
SetPioRecvStatus(pPio, PIO__BAD_CHECKSUM);
}
else {
// good communication
pRetData = (WS_RESP *)&p_buf[WSREPBEG];
nStart = pRetData->addr - WSSTADDR;
// printf("Meter Successful! start: %d\n", nStart);
nPntnum = pRetData->datlen / 4;
pVal = (UINT *)pRetData->data;
for (i = 0; i < nPntnum; i++) {
nGix = i / PNTPERCARD;
nPix = i % PNTPERCARD;
value = *pVal;
// for directly set PI value, let last param (offset) = 1
UpdRtuIntData(sendInfo->pRtu, nGix, nPix, value, 1);
pVal++;
}
}
}
// clear polling flag for next polling
ClrRtuPollSts(sendInfo->pRtu);
ClrPioRtuStream(ioStream);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -