📄 rscomm.c
字号:
//RsComm.c 串口通讯 #define TIME_OUT_CONST_A 1500 typedef unsigned char BYTE; typedef unsigned int WORD; typedef unsigned long DWORD; //串口发送数据void COM_SendString( int COM_PortNo,BYTE str[],int len ) { int i,time; for(i=0; i<len; i++){ //time = 100000; //this delay must be reserved, because the IC card reader processing is lower speed //while(time --); while(!(COM_PortStatus(COM_PortNo))); COM_Send(COM_PortNo,str[i]); } } //串口按保利协议接收数据 //返回:cnt>0->接收到数据个数,-1->累加和出错,-2->超时, //ESC,CMD80,SUBxx,TotalLen=8,"abc",cs ->测试通讯协议 //返回:cnt>0->接收到数据个数,-1->累加和出错,-2->超时,int ReceiDataAsPOLYProtocal_0( int PortNo , char buf[] ) { int cnt=0,step=0,len; long timeout=0; char ChkSum=0; BYTE CommonByteC; do{ if( COM_RecvStatus( PortNo ) ) { //串口 接收指针 头 == 尾->无数据在接收缓冲区 COM_Receive( PortNo , &CommonByteC ); //将数据从接收缓冲区取出->CommonByteC switch(step){ case 0: //ESC if(CommonByteC == 0x1B){ buf[cnt++] = CommonByteC; step = 1; } break; case 1: //CMD80,SUBxx, buf[cnt++] = CommonByteC; if(cnt==3) step = 2; break; case 2: //len buf[cnt++] = CommonByteC; len = CommonByteC; if(len==5) step = 4; //无数据内容,直接接收 累加和 else step = 3; //有数据内容 break; case 3: //data buf[cnt++] = CommonByteC; if((len-1)==cnt) step = 4; break; case 4: //cs buf[cnt++] = CommonByteC; ////if(ChkSum != CommonByteC) cnt = -1; step = 5; break; } //end switch ChkSum += CommonByteC; //累加和 if(step == 5) //接收 数据结束 break; } //end if( ) timeout ++; } while( timeout < TIME_OUT_CONST_A * 10); if( timeout == TIME_OUT_CONST_A * 10) cnt = -2; //超时 return cnt; } /*int COM_ReceiveStringAsPoly( int COM_PortNo,BYTE str[] ) { int i, time; //接收 for(i=0; i<6; i++){ while(!(COM_RecvStatus(1))); COM_Receive(1,reader_r[0]); printf("COM_1 data = %2x\n",reader_r[0]); } } */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -