#include #include "servermainframe.h" #define uchar unsigned char #define uint unsigned int #define ILEN 96 /* size of serial receiving buffer */ idata unsigned char inbuf[ILEN]; unsigned char idata *inlast=inbuf; //最后由中断进入接收缓冲区的字节位置 unsigned char idata *getlast=inbuf; //最后取走的字节位置 bit inbufsign; //接收缓冲区非空标志 有=1 bit inbufful; //输入缓冲区满标志 满=1 unsigned char _szCommandWords[COMMANDWORDSCOUNT]; unsigned char* _pCommandWords; unsigned char _NewstringIN;//1 there is new string,0 there is none. //***************************** //放入一个字节到发送缓冲区 putbyte(char c){ ES=0; //暂停串行中断,以免数据比较时出错? TI = 0; SBUF=c; //未发送完继续发送 while(!TI); TI=0; ES=1; /* ES=0; //暂停串行中断,以免数据比较时出错 *putlast=c; //放字节进入缓冲区 putlast++; //发送缓冲区指针加一 if (putlast==outbuf+OLEN) putlast=outbuf; //指针到了顶部换到底部 outbufsign=1; if (!outbufsign0){ outbufsign0=1; TI=1; } //缓冲区开始为空置为有,启动发送 ES=1; */ } //*************************************** //发送一个定义在程序存储区的字符串到串口 putstring(unsigned char *puts){ COMENABLE=SENDENABLE; for (;*puts!=0;puts++) //遇到停止符0结束 putbyte(*puts); COMENABLE=RECEIVEENABLE; } //************************************* //从接收缓冲区取一个byte,如不想等待则在调用前检测inbufsign是否为1。 uchar getbyte (void){ char idata c ; while (!inbufsign); //缓冲区空等待 ES=0; c= *getlast; //取数据 getlast++; //最后取走的数据位置加一 inbufful=0; //输入缓冲区的满标志清零 if (getlast==inbuf+ILEN) getlast=inbuf; //地址到顶部回到底部 if (getlast==inlast) inbufsign=0; //地址相等置接收缓冲区空空标志,再取数前要检该标志 ES=1; return (c); //取回数据 } //***************************************** //接收一行数据,必须定义放数据串的指针位置和大小 del=0x7f,backspace=0x08,cr=0x0d,lf=0x0a void getline (uchar idata *line) { unsigned char cnt = 0; //定义已接收的长度 char c; do { c = getbyte (); if(c == 0x0d){//enter line++; break; } if(++cnt>16){ _NewstringIN=1; break; } *line = c; line++; //指针加一 //cnt++; //总数目加一 } while (1); //数目到了,回车或ESC停止 } //***************************************** //串口中断处理 serial () interrupt 4 { /* if (TI ){// 全双工时用这个方法。 COMENABLE=SENDENABLE; //Delayms(1);//15 TI = 0; if (outbufsign) {SBUF=*outlast; //未发送完继续发送 outlast++; //最后传出去的字节位置加一 if (outlast==outbuf+OLEN) outlast=outbuf;//地址到顶部回到底部 if (putlast==outlast) outbufsign=0; //数据发送完置发送缓冲区空标志 } else outbufsign0=0; COMENABLE=RECEIVEENABLE; } */ if (RI){ RI = 0; if(!inbufful){ *inlast= SBUF; //放入数据 if(*inlast==0x0d) _NewstringIN=1; inlast++; //最后放入的位置加一 inbufsign=1; if (inlast==inbuf+ILEN) inlast=inbuf;//地址到顶部回到底部 if (inlast==getlast) inbufful=1; //接收缓冲区满置满标志 } } } //***************************** //串口初始化 0xfd=19200,0xfa=9600,0xf4=4800,0xe8=2400,0xd0=1200 serial_init () { TMOD = 0x20; //MOV 89H,#20H ;timer 1 mode 2: 8-Bit reload(定时器T1 模式2: 8位自动初值重装) TH1 = 0xFA; //MOV 8DH,#0E8H TL1 = 0xFA; //MOV 8BH,#0E8H ;9600bps, 22.1184M TR1 = 1; //SETB 8EH ;启动定时器1 SCON = 0x50; // mode 1: 10-bit UART, enable receiver(模式1: 10位异步发送/接收, 使能接收允许位) SM2 = 1; //SETB O9DH ;收到有效的停止位时才将RI置1 ES = 1; //SETB 0ACH ;允许串行中断 EA = 1; //SETB 0AFH ;总中断开 _pCommandWords=_szCommandWords; _NewstringIN=0;//1 there is new string,0 there is none. COMENABLE=RECEIVEENABLE;//receive enable }