📄 nais.c
字号:
#include "config.h"
#define RCS ('R' << 16) + ('C' << 8) + 'S'
#define WCS ('W' << 16) + ('C' << 8) + 'S'
#define RCP ('R' << 16) + ('C' << 8) + 'P'
#define WCP ('W' << 16) + ('C' << 8) + 'P'
#define RCC ('R' << 16) + ('C' << 8) + 'C'
#define WCC ('W' << 16) + ('C' << 8) + 'C'
#define SC ('S' << 8) + 'C'
#define RD ('R' << 8) + 'D'
#define WD ('W' << 8) + 'D'
#define SD ('S' << 8) + 'D'
#define RS ('R' << 8) + 'S'
#define WS ('W' << 8) + 'S'
#define RK ('R' << 8) + 'K'
#define WK ('W' << 8) + 'K'
#define MC ('M' << 8) + 'C'
#define MD ('M' << 8) + 'D'
#define MG ('M' << 8) + 'G'
#define RR ('R' << 8) + 'R'
#define WR ('W' << 8) + 'R'
#define RT ('R' << 8) + 'T'
#define RP ('R' << 8) + 'P'
//#define WPX ('R' << 8) + 'P'
#define RM ('R' << 8) + 'M'
#define AB ('A' << 8) + 'B'
//WORD X[8];
//WORD Y[8];
//WORD R[8];
//WORD T[8];
//WORD C[8];
//WORD L[8];
UCHAR Uart_GetBcc(UCHAR *p,UCHAR n)
{
UCHAR i;
UCHAR bcc;
bcc = 0;
for(i = 0; i < n; i++) bcc ^= *p++;
return bcc;
}//UCHAR Uart_GetBcc(UCHAR *p,UCHAR n)
UCHAR AsciiToHex(UCHAR x)
{
if(x > 0x40) x = x - 0x37;
else x = x - 0x30;
return x;
}//UCHAR AsciiToHex(UCHAR x)
UCHAR HexToAscii(UCHAR x)
{
if(x < 0xa) x = x + 0x30;
else x = x + 0x37;
return x;
}//UCHAR HexToAscii(UCHAR x)
void Uart_ACK(UCHAR x)
{
UCHAR i;
UCHAR bcc;
uwY0 |= (1 << 7);
for(i = 0; i < 6; i++) Uart_ucSendBuff[i] = Uart_ucReceBuff[i];
Uart_ucSendBuff[3] = '$';
x = x + 6;
bcc = Uart_GetBcc(Uart_ucSendBuff,x);
Uart_ucSendBuff[x++] = HexToAscii((bcc >> 4) & 0x0f);
Uart_ucSendBuff[x++] = HexToAscii((bcc >> 0) & 0x0f);
Uart_ucSendBuff[x++] = 0x0d;
Uart_ucSendLen = x;
}//void Uart_ACK(UCHAR x)
void Uart_NACK(UCHAR x)
{
UCHAR i;
UCHAR bcc;
for(i = 0; i < 6; i++) Uart_ucSendBuff[i] = Uart_ucReceBuff[i];
Uart_ucSendBuff[3] = '!';
x = x + 6;
bcc = Uart_GetBcc(Uart_ucSendBuff,x);
Uart_ucSendBuff[x++] = HexToAscii((bcc >> 4) & 0x0f);
Uart_ucSendBuff[x++] = HexToAscii((bcc >> 0) & 0x0f);
Uart_ucSendBuff[x++] = 0x0d;
Uart_ucSendLen = x;
}//void Uart_NACK(UCHAR x)
//RCS Read single point of contact information 读取单个触点的状态信息
void Uart_RCS(void)
{
UCHAR temp;
int index;
index = (Uart_ucReceBuff[8] * 100) + (Uart_ucReceBuff[9] * 10) + Uart_ucReceBuff[10];
temp = 0;
Uart_ucSendBuff[6] = HexToAscii(temp);
Uart_ACK(1);
}//void Uart_RCS(void)
//WCS Write single point of contact information 写入单个触点的状态信息
void Uart_WCS(void)
{
int index;
UCHAR temp;
index = (Uart_ucReceBuff[8] * 100) + (Uart_ucReceBuff[9] * 10) + Uart_ucReceBuff[10];
temp = AsciiToHex(Uart_ucReceBuff[12]);
Uart_ACK(0);
}//void Uart_WCS(void)
//RCP Read plural point of contact information 读取多个触点的状态信息
void Uart_RCP(void)
{
int i,n,index;
UCHAR temp;
n = AsciiToHex(Uart_ucReceBuff[7]);
for(i = 0; i < n; i++)
{
index = (Uart_ucReceBuff[9 + i * 5] * 100) + (Uart_ucReceBuff[10 + i * 5] * 10) + Uart_ucReceBuff[11 + i * 5];
temp = 0;
Uart_ucSendBuff[6 + i] = HexToAscii(temp);
}
Uart_ACK(n);
}//void Uart_RCP(void)
//WCP Write plural point of contact information 写入多个触点的状态信息
void Uart_WCP(void)
{
int i,n,index;
UCHAR temp;
n = AsciiToHex(Uart_ucReceBuff[7]);
for(i = 0; i < n; i++)
{
index = (Uart_ucReceBuff[9 + i * 6] * 100) + (Uart_ucReceBuff[10 + i * 6] * 10) + Uart_ucReceBuff[11 + i * 6];
temp = AsciiToHex(Uart_ucReceBuff[13]);
}
Uart_ACK(0);
}//void Uart_WCP(void)
//RCC Read word unit of contact information 读取字单位的触点的状态信息
void Uart_RCC(void)
{
int i,sIndex,eIndex,temp;
sIndex = (Uart_ucReceBuff[8] * 1000) + (Uart_ucReceBuff[9] * 100) + (Uart_ucReceBuff[10] * 10) + (Uart_ucReceBuff[11] * 1);
eIndex = (Uart_ucReceBuff[12] * 1000) + (Uart_ucReceBuff[13] * 100) + (Uart_ucReceBuff[14] * 10) + (Uart_ucReceBuff[15] * 1);
for(i = sIndex; i < eIndex; i++)
{
temp = 0;
Uart_ucSendBuff[6 + (i - sIndex) * 4] = HexToAscii((temp >> 4) & 0x0f);
Uart_ucSendBuff[7 + (i - sIndex) * 4] = HexToAscii((temp >> 0) & 0x0f);
Uart_ucSendBuff[8 + (i - sIndex) * 4] = HexToAscii((temp >> 12) & 0x0f);
Uart_ucSendBuff[9 + (i - sIndex) * 4] = HexToAscii((temp >> 8) & 0x0f);
}
Uart_ACK((eIndex - sIndex) * 4);
}//void Uart_RCC(void)
//WCC Write word unit of contact information 写入字单位的触点的状态信息
void Uart_WCC(void)
{
int i,sIndex,eIndex,temp;
sIndex = (Uart_ucReceBuff[8] * 1000) + (Uart_ucReceBuff[9] * 100) + (Uart_ucReceBuff[10] * 10) + (Uart_ucReceBuff[11] * 1);
eIndex = (Uart_ucReceBuff[12] * 1000) + (Uart_ucReceBuff[13] * 100) + (Uart_ucReceBuff[14] * 10) + (Uart_ucReceBuff[15] * 1);
for(i = sIndex; i < eIndex; i++)
{
temp = 0;
temp += AsciiToHex(Uart_ucReceBuff[16 + (i - sIndex) * 4]) << 4;
temp += AsciiToHex(Uart_ucReceBuff[17 + (i - sIndex) * 4]) << 0;
temp += AsciiToHex(Uart_ucReceBuff[18 + (i - sIndex) * 4]) << 12;
temp += AsciiToHex(Uart_ucReceBuff[19 + (i - sIndex) * 4]) << 8;
}
Uart_ACK(0);
}//void Uart_WCC(void)
//SC Preset word unit in contact area 预置字单位的触点
void Uart_SC(void)
{
int i,sIndex,eIndex,temp;
sIndex = (Uart_ucReceBuff[7] * 1000) + (Uart_ucReceBuff[8] * 100) + (Uart_ucReceBuff[9] * 10) + (Uart_ucReceBuff[10] * 1);
eIndex = (Uart_ucReceBuff[11] * 1000) + (Uart_ucReceBuff[12] * 100) + (Uart_ucReceBuff[13] * 10) + (Uart_ucReceBuff[14] * 1);
for(i = sIndex; i < eIndex; i++)
{
temp = 0;
temp += AsciiToHex(Uart_ucReceBuff[15 + (i - sIndex) * 4]) << 4;
temp += AsciiToHex(Uart_ucReceBuff[16 + (i - sIndex) * 4]) << 0;
temp += AsciiToHex(Uart_ucReceBuff[17 + (i - sIndex) * 4]) << 12;
temp += AsciiToHex(Uart_ucReceBuff[18 + (i - sIndex) * 4]) << 8;
}
Uart_ACK(0);
}//void Uart_SC(void)
//RD Read data area 读取数据寄存器值
void Uart_RD(void)
{
int i,temp,sIndex,eIndex;
sIndex = (AsciiToHex(Uart_ucReceBuff[7]) * 10000) + (AsciiToHex(Uart_ucReceBuff[8]) * 1000) +
(AsciiToHex(Uart_ucReceBuff[9]) * 100) + (AsciiToHex(Uart_ucReceBuff[10]) * 10) + (AsciiToHex(Uart_ucReceBuff[11]) * 1);
eIndex = (AsciiToHex(Uart_ucReceBuff[12]) * 10000) + (AsciiToHex(Uart_ucReceBuff[13]) * 1000) +
(AsciiToHex(Uart_ucReceBuff[14]) * 100) + (AsciiToHex(Uart_ucReceBuff[15]) * 10) + (AsciiToHex(Uart_ucReceBuff[16]) * 1);
for(i = sIndex; i < eIndex; i++)
{
temp = 0;
Uart_ucSendBuff[6 + (i - sIndex) * 4] = HexToAscii((temp >> 4) & 0x0f);
Uart_ucSendBuff[7 + (i - sIndex) * 4] = HexToAscii((temp >> 0) & 0x0f);
Uart_ucSendBuff[8 + (i - sIndex) * 4] = HexToAscii((temp >> 12) & 0x0f);
Uart_ucSendBuff[9 + (i - sIndex) * 4] = HexToAscii((temp >> 8) & 0x0f);
}
Uart_ACK((eIndex - sIndex) * 4);
}//void Uart_RD(void)
//WD Write data area 写入数据寄存器值
void Uart_WD(void)
{
int i,temp,sIndex,eIndex;
sIndex = (AsciiToHex(Uart_ucReceBuff[7]) * 10000) + (AsciiToHex(Uart_ucReceBuff[8]) * 1000) +
(AsciiToHex(Uart_ucReceBuff[9]) * 100) + (AsciiToHex(Uart_ucReceBuff[10]) * 10) + (AsciiToHex(Uart_ucReceBuff[11]) * 1);
eIndex = (AsciiToHex(Uart_ucReceBuff[12]) * 10000) + (AsciiToHex(Uart_ucReceBuff[13]) * 1000) +
(AsciiToHex(Uart_ucReceBuff[14]) * 100) + (AsciiToHex(Uart_ucReceBuff[15]) * 10) + (AsciiToHex(Uart_ucReceBuff[16]) * 1);
for(i = sIndex; i < eIndex; i++)
{
temp = 0;
temp += AsciiToHex(Uart_ucReceBuff[17 + (i - sIndex) * 4]) << 4;
temp += AsciiToHex(Uart_ucReceBuff[18 + (i - sIndex) * 4]) << 0;
temp += AsciiToHex(Uart_ucReceBuff[19 + (i - sIndex) * 4]) << 12;
temp += AsciiToHex(Uart_ucReceBuff[20 + (i - sIndex) * 4]) << 8;
}
Uart_ACK(0);
}//void Uart_WD(void)
//SD Preset of data area 预置数据寄存器值
void Uart_SD(void)
{
int i,temp,sIndex,eIndex;
uwY0 |= (1 << 4);
sIndex = (AsciiToHex(Uart_ucReceBuff[7]) * 10000) + (AsciiToHex(Uart_ucReceBuff[8]) * 1000) +
(AsciiToHex(Uart_ucReceBuff[9]) * 100) + (AsciiToHex(Uart_ucReceBuff[10]) * 10) + (AsciiToHex(Uart_ucReceBuff[11]) * 1);
eIndex = (AsciiToHex(Uart_ucReceBuff[12]) * 10000) + (AsciiToHex(Uart_ucReceBuff[13]) * 1000) +
(AsciiToHex(Uart_ucReceBuff[14]) * 100) + (AsciiToHex(Uart_ucReceBuff[15]) * 10) + (AsciiToHex(Uart_ucReceBuff[16]) * 1);
for(i = sIndex; i < eIndex; i++)
{
temp = 0;
temp += AsciiToHex(Uart_ucReceBuff[17 + (i - sIndex) * 4]) << 4;
temp += AsciiToHex(Uart_ucReceBuff[18 + (i - sIndex) * 4]) << 0;
temp += AsciiToHex(Uart_ucReceBuff[19 + (i - sIndex) * 4]) << 12;
temp += AsciiToHex(Uart_ucReceBuff[20 + (i - sIndex) * 4]) << 8;
}
Uart_ACK(0);
}//void Uart_SD(void)
//RS Read timer and counter set value area 读取定时器/计数器目标值
void Uart_RS(void)
{
int i,temp,sIndex,eIndex;
sIndex = (AsciiToHex(Uart_ucReceBuff[6]) * 1000) + (AsciiToHex(Uart_ucReceBuff[7]) * 100) +
(AsciiToHex(Uart_ucReceBuff[8]) * 10) + (AsciiToHex(Uart_ucReceBuff[9]) * 1);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -