?? 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); //壓入新的發送數據到發送緩沖區
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -