?? udbldmsg.c
字號:
/* $Header: "%n Ver=%v %f LastEdit=%w Locker=%l" */
/* "UDBLDMSG.C Ver=3 26-Nov-97,11:20:02 LastEdit=JIMV Locker=***_NOBODY_***" */
/***********************************************************************\
* *
* Copyright Wonderware Software Development Corp. 1992-1997 *
* *
* ThisFileName="L:\ww\dde_serv\src\udsample\udbldmsg.c" *
* LastEditDate="1997 Nov 26 11:20:00" *
* *
\***********************************************************************/
#define LINT_ARGS
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include "ntconv.h"
#include "hmemcpy.h"
#include "uddefs.h"
#include "debug.h"
#include "wwassert.h"
#include "Utitech.h"
#include "CheckItem.h"
USES_ASSERT
static char msg[120+1]; /* local buffer for creating message */
static VOID WINAPI ShowBldMsg (LPSTR lpStr, WORD len);
#ifndef USE_ASCII_HEX_MESSAGES
/* >>>>>>>> USE_ASCII_HEX_MESSAGES defined <<<<<<<< */
/***********************************************************************\
The following three values are protocol-dependent.
The example code below assumes send and receive messages are ASCII.
It also assumes a header of the form
:BBAAAANN
where BB is a command in hex ASCII
AAAA is an address in hex ASCII
NN is a number of words in hex ASCII
Alternatively, an error message may be received,
which will have one of the following forms:
:000000000000FF;
for a garbled message
:00BBAAAANN00XX;
for an invalid command BB
:00BBAAAANNDDXX;
for an invalid number of "cells" to read or write
So by the ninth character, we know how many are coming.
\***********************************************************************/
BOOL bBldSendMsgInBinary = FALSE; /* =TRUE if messages are sent in binary */
BOOL bBldRecvMsgInBinary = FALSE; /* =TRUE if messages are received in binary */
int nBldRecvMsgMinLen = 9; /* min chars needed to get msg length */
#else
/* >>>>>>>> USE_ASCII_HEX_MESSAGES not defined <<<<<<<< */
/***********************************************************************\
The following three values are protocol-dependent.
The example code below assumes send and receive messages are binary.
It also assumes a header of the form
BBAAAANN
where BB is a 1-byte command
AAAA is a 2-byte address
NN is a 1-byte number of words
Alternatively, an error message may be received,
which will have one of the following forms:
000000000000FF
for a garbled message
00BBAAAANN00XX
for an invalid command BB
00BBAAAANNDDXX
for an invalid number of "cells" to read or write
So by the fourth character, we know how many are coming.
\***********************************************************************/
BOOL bBldSendMsgInBinary = TRUE; /* =TRUE if messages are sent in binary */
BOOL bBldRecvMsgInBinary = TRUE; /* =TRUE if messages are received in binary */
int nBldRecvMsgMinLen = 4; /* min chars needed to get msg length */
#endif
/***********************************************************************/
/** Data conversion tables **/
/** This table provides a simple way of converting binary to ASCII hex **/
char BtoAtable[]=
{
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
};
/** This table provides a simple way of converting hex ASCII to binary **/
BYTE AtoBtable[] = {
0, 1, 2, 3, 4, 5, 6, 7,
8, 9, 0, 0, 0, 0, 0, 0,
0, 10, 11, 12, 13, 14, 15
};
/***********************************************************************/
/** put binary value for byte into indicated string
returns pointer to end of string */
LPSTR
WINAPI
put_bin_byte (LPSTR lpMsg, BYTE x)
{
/* put binary character in message */
*(lpMsg++) = x;
/* return pointer to end of string */
return (lpMsg);
} /* put_bin_byte */
/***********************************************************************/
/** put ASCII hex for byte into indicated string
returns pointer to end of string */
LPSTR
WINAPI
put_hex_byte (LPSTR lpMsg, BYTE x)
{
int y;
LPSTR p;
/* point to end of string */
lpMsg += 2;
p = lpMsg;
/* put hex characters in message, starting at lsnybble */
y = x;
*(--p) = BtoAtable[y & 0x0F];
y = y >> 4;
*(--p) = BtoAtable[y & 0x0F];
/* return pointer to end of string */
return (lpMsg);
} /* put_hex_byte */
/***********************************************************************/
/** put binary value for word into indicated string
returns pointer to end of string */
LPSTR
WINAPI
put_bin_word (LPSTR lpMsg, WORD x)
{
int y;
LPSTR p;
/* point to end of string */
lpMsg += 2;
p = lpMsg;
/* put binary characters in message, starting at lsbyte */
y = x;
*(--p) = (BYTE) (y & 0x00FF);
y = y >> 8;
*(--p) = (BYTE) (y & 0x00FF);
/* return pointer to end of string */
return (lpMsg);
} /* put_bin_word */
/***********************************************************************/
/** put ASCII hex for word into indicated string
returns pointer to end of string */
LPSTR
WINAPI
put_hex_word (LPSTR lpMsg, WORD x)
{
int y;
LPSTR p;
/* point to end of string */
lpMsg += 4;
p = lpMsg;
/* put hex characters in message, starting at lsnybble */
y = x;
*(--p) = BtoAtable[y & 0x000F];
y = y >> 4;
*(--p) = BtoAtable[y & 0x000F];
y = y >> 4;
*(--p) = BtoAtable[y & 0x000F];
y = y >> 4;
*(--p) = BtoAtable[y & 0x000F];
/* return pointer to end of string */
return (lpMsg);
} /* put_hex_word */
/***********************************************************************/
/** put binary value for long into indicated string
returns pointer to end of string */
LPSTR
WINAPI
put_bin_long (LPSTR lpMsg, long x)
{
long y;
LPSTR p;
/* point to end of string */
lpMsg += 4;
p = lpMsg;
/* put binary characters in message, starting at lsbyte */
y = x;
*(--p) = (BYTE) (y & 0x00FF);
y = y >> 8;
*(--p) = (BYTE) (y & 0x00FF);
y = y >> 8;
*(--p) = (BYTE) (y & 0x00FF);
y = y >> 8;
*(--p) = (BYTE) (y & 0x00FF);
/* return pointer to end of string */
return (lpMsg);
} /* put_bin_long */
/***********************************************************************/
/** put ASCII hex for long into indicated string
returns pointer to end of string */
LPSTR
WINAPI
put_hex_long (LPSTR lpMsg, long x)
{
long y;
int i;
LPSTR p;
/* point to end of string */
lpMsg += 8;
p = lpMsg;
/* put hex characters in message, starting at lsnybble */
y = x;
for (i = 0; i < 7; i++) {
*(--p) = BtoAtable[y & 0x000F];
y = y >> 4;
}
*(--p) = BtoAtable[y & 0x000F];
/* return pointer to end of string */
return (lpMsg);
} /* put_hex_long */
/***********************************************************************/
/* check whether character is hex ASCII */
BOOL
WINAPI
IsHexASCII (char ch)
{
return ((('0' <= ch) && (ch <= '9')) ||
(('A' <= ch) && (ch <= 'F')) ||
(('a' <= ch) && (ch <= 'f')));
} /* IsHexASCII */
/***********************************************************************/
/* convert hex ASCII to binary byte value */
BYTE
WINAPI
ah_to_bin (LPSTR lpStr, BOOL *ok)
{
char ch;
int byteval;
/* initialize return value, validity flag */
byteval = 0;
*ok = TRUE;
/* get character, check it */
ch = *(lpStr++);
if (('a' <= ch) && (ch <= 'f')) {
/* convert to uppper case */
ch = (char) (ch - 32);
}
if ((('0' <= ch) && (ch <= '9')) ||
(('A' <= ch) && (ch <= 'F')))
/* valid hex character, get corresponding value */
byteval = (AtoBtable[ch - (char) '0']) << 4;
else
/* invalid hex character, treat as '0' and flag error */
*ok = FALSE;
/* get character, check it */
ch = *lpStr;
if (('a' <= ch) && (ch <= 'f')) {
/* convert to uppper case */
ch = (char) (ch - 32);
}
if ((('0' <= ch) && (ch <= '9')) ||
(('A' <= ch) && (ch <= 'F')))
/* valid hex character, get corresponding value */
byteval |= (AtoBtable[ch - (char) '0']);
else
/* invalid hex character, treat as '0' and flag error */
*ok = FALSE;
/* return value, validity flag */
return ((BYTE) byteval);
} /* ah_to_bin */
/***********************************************************************/
/** convert hexadecimal digit string to long integer value
returns errflg TRUE if number is out of range **/
long
WINAPI
GetHexVal (LPSTR scan_ptr, int len, int *errflg)
{
char ch;
long x;
BOOL digit_found;
/* initialize value, error flags */
x = 0;
digit_found = FALSE;
*errflg = 0;
/* convert as hexadecimal value */
while (len > 0) {
/* get character, decrement count */
ch = *(scan_ptr++);
len--;
/* check character */
if ((('0' <= ch) && (ch <= '9')) ||
(('A' <= ch) && (ch <= 'F')) ||
(('a' <= ch) && (ch <= 'f'))) {
digit_found = TRUE;
if (x <= 0x0FFFFFFFL) {
if (ch >= 'a')
ch -= 32;
if (ch > '9')
ch -= 7;
x = (x << 4) + (ch - '0');
} else {
/* value too large, indicate error and quit */
*errflg = 1;
len = 0;
}
} else {
/* not a digit, quit */
len = 0;
}
}
/* check whether any digits were found */
if (!digit_found)
/* no digits, indicate error */
*errflg = 1;
/* return value */
return (x);
} /* GetHexVal */
#ifdef USE_ASCII_HEX_MESSAGES
/* >>>>>>>> USE_ASCII_HEX_MESSAGES defined <<<<<<<< */
/***********************************************************************/
/** calculate checksum of message **/
/*************************************************************\
The content of this routine is protocol dependent.
Modify it as required.
\*************************************************************/
BYTE
WINAPI
BldCks(LPSTR lpBuf, /* pointer to buffer containing message */
int len, /* length of message */
int *errflg) /* nonzero if invalid characters */
{
int cks, byteval;
char ch1, ch2;
/* initialize checksum, error flag */
cks = 0;
*errflg = 0;
/************************************************\
include byte lpBuf[i] in the checksum
Example:
\************************************************/
/* get byte values, i.e. pairs of hex ASCII characters */
while (len > 0) {
/* get pair of characters */
ch1 = *(lpBuf++); len--;
ch2 = 0;
if (len > 0) {
ch2 = *(lpBuf++); len--;
}
/* verify that both are ASCII hex */
if ((IsHexASCII (ch1)) && IsHexASCII (ch2)) {
/* get corresponding binary value */
if (ch1 >= 'a') ch1 -= 32; /* force to upper case */
if (ch1 > '9') ch1 -= 7; /* subtract offset for A-F */
byteval = (ch1 - '0');
if (ch2 >= 'a') ch2 -= 32; /* force to upper case */
if (ch2 > '9') ch2 -= 7; /* subtract offset for A-F */
byteval = (byteval << 4) + (ch2 - '0');
/* update checksum */
cks = (cks + byteval) & 0xFF;
} else {
/* indicate error, force end of scan */
*errflg = 1;
len = 0;
}
}
/* invert checksum and return it */
cks = 0xFF - cks;
return ((BYTE) cks);
} /* BldCks */
/***********************************************************************/
/** build message to read data from a point in the PLC,
return length of message **/
/************************************************************************\
Note: The last four arguments to this routine are protocol-dependent.
Modify them as required.
\************************************************************************/
WORD
WINAPI
BldRead(BYTE plcAddr, /* 2/28/2000 Modify by Chen jun local PLC address (PLC的通信地址) */
LPSTR *plpMsg, /* pointer to where the message is stored */
BYTE plcDataType, /* type of data to read(查詢數(shù)據(jù)的PLC) */
BYTE DDEType, /* type of data to transfer(查詢數(shù)據(jù)的DDE類型) */
WORD addr1, /* address of point(s) to be accessed(查詢的起始地址) */
WORD numRd) /* number of items to read(一次查詢的寄存器數(shù)量) */
{
LPSTR ptr;
BYTE msgCmd;
WORD len;
WORD cks;
//int errflg;
len = 0;
/* determine message command */
// 3/1/2000 Modify by chen jun
switch (plcDataType){
case ITEM_HOLDING_COIL:
msgCmd = 1;
break;
case ITEM_INPUT_COIL:
msgCmd = 2;
break;
case ITEM_HOLDING_REG:
msgCmd = 3;
break;
case ITEM_INPUT_REG:
msgCmd = 4;
break;
}
/* initialize message buffer pointer */
ptr = (LPSTR) msg;
/* message type */
// 2/29/2000 modify by Chen jun
ptr = put_bin_byte (ptr, plcAddr); len+=1;
ptr = put_bin_byte (ptr, msgCmd); len+=1;
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -