📄 netsend.h
字号:
/////////////////////////////////网络有关/////////////////////////////////////
#define uchar unsigned char
#define uint unsigned int
#define OLEN 128 //定义输出缓冲区大小
unsigned char seroutbuf[OLEN+1]; //定义输出缓冲区
unsigned char outbit=0; //发送本帧的字节数
unsigned char idata curoutleng=OLEN;
#define ILEN 128 //定义接收缓冲区大小
unsigned char serinbuf[ILEN+1]; //定义接收缓冲区
unsigned char inbit=0; //当前接收字节位
unsigned char idata passtime=0; //时间间隔记数
bit seron; //接收本机命令帧状态
unsigned char machinebase; //本机设备地址unsigned char baud; //网络波特率
unsigned char curmanno; //当前操作者号
unsigned char netoutresult[OLEN+1]; //检测结果缓存
unsigned char enoutresult;
unsigned char netoutcurdat[OLEN+1]; //即时数据缓存
unsigned char netoutsigned[OLEN+1]; //标签报表缓存
unsigned char enoutsigned;
unsigned char netoutcurman[OLEN+1]; //当前授权缓存
unsigned char netoutcursta[OLEN+1]; //当前设备状态
unsigned char netinbuf[OLEN+1]; //接收命令数据缓存
unsigned char cardno[2][2][8]; //授权标识数据
void converdata() reentrant;
void writecardno(unsigned char nu) reentrant;
//主机命令信号量
OS_EVENT *NetComSem;
unsigned char code CRC[256]={
0,94,188,226,97,63,221,131,194,156,126,32,163,253,31,65,
157,195,33,127,252,162,64,30,95,1,227,189,62,96,130,220,
35,125,159,193,66,28,254,160,225,191,93,3,128,222,60,98,
190,224,2,92,223,129,99,61,124,34,192,158,29,67,161,255,
70,24,250,164,39,121,155,197,132,218,56,102,229,187,89,7,
219,133,103,57,186,228,6,88,25,71,165,251,120,38,196,154,
101,59,217,135,4,90,184,230,167,249,27,69,198,152,122,36,
248,166,68,26,153,199,37,123,58,100,134,216,91,5,231,185,
140,210,48,110,237,179,81,15,78,16,242,172,47,113,147,205,
17,79,173,243,112,46,204,146,211,141,111,49,178,236,14,80,
175,241,19,77,206,144,114,44,109,51,209,143,12,82,176,238,
50,108,142,208,83,13,239,177,240,174,76,18,145,207,45,115,
202,148,118,40,171,245,23,73,8,86,180,234,105,55,213,139,
87,9,235,181,54,104,138,212,149,203,41,119,244,170,72,22,
233,183,85,11,136,214,52,106,43,117,151,201,74,20,246,168,
116,42,200,150,21,75,169,247,182,232,10,84,215,137,107,53
};
//计算CRC码,
unsigned char GetMyCRC(unsigned char* source,unsigned char leng) reentrant
{
unsigned char dscrc;
dscrc=0x00;
for(;leng!=0;leng--)
{
dscrc=CRC[dscrc^(*source)];
source++;
}
return(dscrc);
}
//9600 11.0592MHz
void init_serial() reentrant
{
PCON &= 0x7F;
TH1 = 0xFD;
TMOD |= 0X20; //定时器1方式2
SCON = 0X50; //允许接收
TR1 = 1; //启动定时器1
ES = 1; //串行口中断允许
}
//////////////////////////////////////////////////
//放一串数据到发送缓冲区,长度为输出缓冲区大小
void myputbytes(unsigned char *outplace,unsigned char leng) reentrant
{
unsigned char i;
for(i = 0; i < leng; i++)
seroutbuf[i] = outplace[i];
outbit = 0;
curoutleng = leng;
}
//串口中断处理
void serial() reentrant
{
if(RI)
{
RI = 0;
if(passtime > 1)
{
serinbuf[0] = SBUF;
if(serinbuf[0] == machinebase)
{
inbit = 1;
seron = 1;
}
else
{
seron = 0;
}
}
else
{
if(seron)
{
serinbuf[inbit] = SBUF;
if(inbit < ILEN)
inbit++;
if(inbit > 3)
{
if(inbit == serinbuf[2]+4)
{
seron = 0;
OSSemPost(NetComSem);
}
}
}
}
passtime = 0;
}
else
{
TI = 0;
if(outbit < curoutleng)
{
SBUF = seroutbuf[outbit];
outbit++;
}
else
{
RS485 = 1;
}
}
}
void errorreturn() reentrant
{
netinbuf[1] += 128;
netinbuf[2] = 0;
netinbuf[3] = GetMyCRC(netinbuf,3);
myputbytes(netinbuf, 4);//压入新的发送数据到发送缓冲区
}
//网络数据传送
void netsend(void *lcnpdata) reentrant
{
unsigned char neterr;
unsigned char tem;
lcnpdata = lcnpdata;
NetComSem = OSSemCreate(0);
seron = 0;
RS485 = 1;
//当前设备状态
netoutcursta[0] = machinebase;
netoutcursta[1] = 1;
netoutcursta[2] = 4;
netoutcursta[3] = 0x03;
netoutcursta[4] = 0x06;
if(onpower == 0xA5)
{
netoutcursta[5] &= 0x06;//复位保留标签和结果数据
}
else
{
netoutcursta[5] = 0x00;//开机无数据
enoutresult = 0;
enoutsigned = 0;
}
/*
-------1 //有即时数据
------1- //有结果数据
-----1-- //有标签数据
----1--- //有授权标识
*/
init_serial();
while(1)
{
OSSemPend(NetComSem, 0, &neterr);
if(neterr == OS_NO_ERR)
{
neterr = serinbuf[2]+4;
if((serinbuf[1] < 128) && (serinbuf[neterr-1] == GetMyCRC(serinbuf,neterr-1)))
{
for(tem = 0; tem < neterr; tem++)
{
netinbuf[tem] = serinbuf[tem];
}
switch(netinbuf[1])
{
case 1://设备状态
if(neterr == 4)
{
netoutcursta[0] = machinebase;
netoutcursta[6] = curmanno;//当前设备操作者号
netoutcursta[7] = GetMyCRC(netoutcursta,7);
myputbytes(netoutcursta, 8);//压入新的发送数据到发送缓冲区
RS485 = 0;
TI = 1;
}
break;
case 2://即时数据
if(neterr == 4)
{
converdata();
myputbytes(netoutcurdat, 13);//压入新的发送数据到发送缓冲区
RS485 = 0;
TI = 1;
}
break;
case 3://检测结果
if(neterr == 4)
{
if(enoutresult)
{
myputbytes(netoutresult, 101);//压入新的发送数据到发送缓冲区
netoutcursta[5] &= 0xFD;//无检测结果
}
else
{
errorreturn();
}
RS485 = 0;
TI = 1;
}
break;
case 4://标签数据
if(neterr == 4)
{
if(enoutsigned)
{
myputbytes(netoutsigned, 29);//压入新的发送数据到发送缓冲区
netoutcursta[5] &= 0xFB;//无标签数据
}
else
{
errorreturn();
}
RS485 = 0;
TI = 1;
}
break;
case 5://发送当前授权标识数据
if(neterr == 4)
{
if( curmanno == 1)
{
netoutcurman[0] = machinebase;
netoutcurman[1] = 5;
netoutcurman[2] = 19;
for(tem = 0; tem < 8; tem++)
netoutcurman[3+tem] = cardno[0][0][tem];
netoutcurman[11] = cardno[0][1][0];
netoutcurman[12] = cardno[0][1][1];
netoutcurman[13] = cardno[0][1][2];
for(tem = 0; tem < 8; tem++)
netoutcurman[14+tem] = manname1[tem];
netoutcurman[22] = GetMyCRC(netoutcurman,22);
myputbytes(netoutcurman, 23);//压入新的发送数据到发送缓冲区
}
else
{
if( curmanno == 2)
{
netoutcurman[0] = machinebase;
netoutcurman[1] = 5;
netoutcurman[2] = 19;
for(tem = 0; tem < 8; tem++)
netoutcurman[3+tem] = cardno[1][0][tem];
netoutcurman[11] = cardno[1][1][0];
netoutcurman[12] = cardno[1][1][1];
netoutcurman[13] = cardno[1][1][2];
for(tem = 0; tem < 8; tem++)
netoutcurman[14+tem] = manname2[tem];
netoutcurman[22] = GetMyCRC(netoutcurman,22);
myputbytes(netoutcurman, 23);//压入新的发送数据到发送缓冲区
}
else//未授权错误
{
errorreturn();
}
}
RS485 = 0;
TI = 1;
}
break;
case 60://修改授权标识数据
if(neterr == 16)
{
if(netinbuf[3] == 0)
{
for(tem = 0; tem < 8; tem++)
{
cardno[0][0][tem] = netinbuf[tem+4];
}
cardno[0][1][0] = netinbuf[12];
cardno[0][1][1] = netinbuf[13];
cardno[0][1][2] = netinbuf[14];
writecardno(0);
myputbytes(netinbuf, 16);//压入新的发送数据到发送缓冲区
}
else
{
if(netinbuf[3] == 1)
{
for(tem = 0; tem < 8; tem++)
{
cardno[1][0][tem] = netinbuf[tem+4];
}
cardno[1][1][0] = netinbuf[12];
cardno[1][1][1] = netinbuf[13];
cardno[1][1][2] = netinbuf[14];
writecardno(1);
myputbytes(netinbuf, 16);//压入新的发送数据到发送缓冲区
}
else
{
errorreturn();
}
}
}
else
{
errorreturn();
}
RS485 = 0;
TI = 1;
break;
case 61://修改设备地址
if(neterr == 5)
{
machinebase = netinbuf[3];
Writei2c(&machinebase,14,1);
myputbytes(netinbuf, 5); //压入新的发送数据到发送缓冲区
RS485 = 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -