Chinaunix首页 | 论坛 | 博客
  • 博客访问: 630670
  • 博文数量: 1008
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 5175
  • 用 户 组: 普通用户
  • 注册时间: 2012-07-31 09:44
文章分类
文章存档

2012年(1008)

我的朋友

分类:

2012-07-31 11:50:49

3.   具体应用实例

#include

#include

#include

#include

#include

#include

#include

#include                             

#include  

#include  

//#include "COMM.h"

 

 

#define ACK_SUCCESS             0x00      // 操作成功

#define  ACK_FAIL             0x01     // 操作失败

#define  ACK_FULL            0x04     //  Full

#define ACK_NOUSER        0x05      // 无此用户

#define  ACK_USER_EXIST 0x07      // 用户已存在

#define  ACK_TIMEOUT             0x08      // 采集超时

#define ACK_COUNT          0x3   //发生错误时候,重试次数

 

extern unsigned char TXBUF[9900];

extern unsigned char RXBUF[9900];

extern unsigned char rev_ok;

extern unsigned int  TX_len;

extern unsigned int  RX_len;

extern unsigned char one_onecontrast(unsigned char user_number_h,unsigned char user_number_l);

extern unsigned char one_morecontrast(void);

extern unsigned char Get_UserNumber_Right(void);

extern unsigned char set_addmode(unsigned char yn_repeat);

extern unsigned char add_Fingerprint(unsigned char time_number,unsigned char user_number_h,

                                                        unsigned char user_number_l,unsigned char user_right);

extern unsigned char del_alluser(void);

extern unsigned char del_oneuser(unsigned char user_number_h,unsigned char user_number_l);

extern unsigned char read_usernumber(void);

 

 

int fd;

 

unsigned char USER_NUMBER_H;    // 用户号高8

unsigned char USER_NUMBER_L;     // 用户号低8

unsigned char USER_RIGHT;            // 用户权限

 

unsigned char FEATURE_BUFFER[512];   // 要下传的指纹特征值数据

unsigned char FEATURE_LEN;                 // 要下传的指纹特征值的长度

 

 

unsigned char CharToHex(unsigned char ch);

unsigned char select_one_onecontrast(void);

void select_Get_Usernumber_right(void);

 

 

/*********************************************************************************************

* name:        SERIAL_TX

* func:          发生数据到指纹模块串口

* para:          none

* ret:            none

* modify:

* comment:         

*********************************************************************************************/

int SERIAL_TX(void)

{

       int ret;

 

       ret = write(fd, TXBUF, TX_len);        // 试图从串口发送数                               

       if(ret == -1)                        // 确实接收到了数据,并打印出来

       {

       //     *(rcv_buf+ret)='\0';

              printf(" Write device error!\n");

              return -1;

       //     ret = 0;

       }

 

       return 0;

}

 

/*********************************************************************************************

* name:        SERIAL_RX

* func:          从指纹模块串口接收数据

* para:          none

* ret:            none

* modify:

* comment:         

*********************************************************************************************/

void SERIAL_RX(void)

{

      //  read(fd, RXBUF , RX_len);

#if 1

       int ret,n,pos,retval;

       fd_set rfds;

       struct timeval tv ;

       pos = 0;//指向接收缓冲

       tv.tv_sec = 2;

       tv.tv_usec = 0;

       for(n = 0; n < RX_len; n++)

       {

              RXBUF[n] = 0xFF;

       }

      

       //while(FD_ISSET(fd,&uart_r)||FD_ISSET(fd,&uart_w));      // 检测串口是否有读写动作

       while(1) // 检测串口是否有读写动作

       {    

              FD_ZERO(&rfds);// 清空串口接收端口集

              FD_SET(fd,&rfds);// 设置串口接收端口集   

              retval = select(fd+1,&rfds,NULL,NULL,&tv);  

              if(retval == -1)

              {

                     perror("select()");

                     break;

              }                             

              else if(retval)

              {  //判断是否还有数据

                     //sleep(2);                             

                     ret = read(fd, RXBUF, RX_len);

                     pos += ret;

                     //printf("ret = %d \n",ret);

                     if((RXBUF[pos-2] == '\r') & (RXBUF[pos-1] == '\n'))                           // 确实接收到了数据,并打印出来

                     {

                            FD_ZERO(&rfds);

                            FD_SET(fd,&rfds);      

                            retval = select(fd+1,&rfds,NULL,NULL,&tv);  

                            if(!retval)//no datas

                            {

                                   break;

                            }      

                     }

              }

              else

              {

                     break;

              }

       }

}

 

void init_ttyS(int fd)

{

       struct termios newtio;

 

       bzero(&newtio, sizeof(newtio));

       tcgetattr(fd, &newtio);                                   // 得到当前串口的参数

       cfsetispeed(&newtio, B19200);                            // 将输入波特率设为19200

       cfsetospeed(&newtio, B19200);                           // 将输出波特率设为19200

       newtio.c_cflag |= (CLOCAL | CREAD);                   // 使能接收并使能本地状态

       newtio.c_cflag &= ~PARENB;                         // 无校验 8位数据位1位停止位

       newtio.c_cflag &= ~CSTOPB;

       newtio.c_cflag &= ~CSIZE;

       newtio.c_cflag |= CS8;

       newtio.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // 原始数据输入

       newtio.c_oflag &= ~(OPOST);

 

       newtio.c_cc[VTIME]    = 0;                          // 设置等待时间和最小接收字符数

       newtio.c_cc[VMIN]     = 0;  

 

       tcflush(fd, TCIFLUSH);                             // 处理未接收的字符

       tcsetattr(fd,TCSANOW,&newtio);                         // 激活新配置

}

 

unsigned char set_addmode(unsigned char yn_repeat)

{

       unsigned char check, i;

       TXBUF[0] = 0xF5;

       TXBUF[1] = 0x2D;

       TXBUF[2] = 0x00;

       TXBUF[3] = yn_repeat;

       TXBUF[4] = 0x00;

       TXBUF[5] = 0x00;

       check = TXBUF[1];

       for (i = 2; i < 6; i++)

       {

              check ^= TXBUF[i];

       }

       TXBUF[6] = check;

       TXBUF[7] = 0xF5;

      

       rev_ok = 1;

       TX_len = 8;

       RX_len = 8;

 

       SERIAL_TX();

       sleep(delaytime);

       SERIAL_RX();

       rev_ok = RXBUF[4];

 

       return (rev_ok);

}

 

int main(int argc, char ** argv)

{

       int t,ret;

       char mode;

 

       char *device = "/dev/tts/0";                          // 设备路径,初始使用UART0

      

       for(t=1;t                                // 获取程序入口时输入的参数

       {

              if(!strcmp(argv[t],"-d") && (argc > (t+1)))

              {

                     device = argv[t+1];

              }

       }

       if(!strcmp(device,"/dev/tts/1"))               // 不允许使用UART1,因为它已和PC相连。

       {

              printf("can not use /dev/tts/1\n");

              return -1;     

       }

       fd = open(device, O_RDWR);                         // 打开设备

      

       if (fd < 0)                                          // 设备打开失败

       {

              printf("open device error\n");

              return -1;

       }

      

       init_ttyS(fd);                                   // 初始化设备

 

       while(1)

       {    

              set_addmode(1);                                                  // 设置指纹添加模式,禁止重复

       }    

       close(fd);                                                // 关闭打开的设备

       return 0;                                                 // 正常返回

}

阅读(339) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~