整理wiegand26和wiegand34的输入输出


#define WIEGAND_DATA0_UP() 		GPIO_Ctrl_Api(GPIO_CMD_SET_WG0, 1, NULL)
#define WIEGAND_DATA0_DOWN() 	GPIO_Ctrl_Api(GPIO_CMD_SET_WG0, 0, NULL)
#define WIEGAND_DATA1_UP() 		GPIO_Ctrl_Api(GPIO_CMD_SET_WG1, 1, NULL)
#define WIEGAND_DATA1_DOWN() 	GPIO_Ctrl_Api(GPIO_CMD_SET_WG1, 0, NULL)

#define USLEEP_FILECTRL 		0xa0	//0xa1
#define SLEEP_CTRL0 		0xa0	
#define SLEEP_CTRL1 		0xa1

#define USLEEP_TIMELOW		 	100	
#define USLEEP_TIMECYCLE  		1600	

#define GPIO_CMD_SET_WG0  36     /*********设置wg0**********/
#define GPIO_CMD_SET_WG1  37     /*********设置wg1**********/

/***韦根协议***/
#define WG_CMD_MAGIC 		'x'
#define WG_26_MODE			_IO(WG_CMD_MAGIC, 0x01)
#define WG_34_MODE			_IO(WG_CMD_MAGIC, 0x02)
static int Wiegand_fd 		= -1;//Wiegand 韦根协议操作文件
typedef struct _wiegand_s
{
	int  nType;						//in-WG类型       26/34/36/44
	int  nEven;						//out-前偶校验位
	int  nOdd; 						//out-后奇校验位
	unsigned char byHexStr[32];		//out-字符串 存放WG二进制数据
}wiegand_s;

typedef struct _WIEGAND_CONFIG_S//size=3
{
	BYTE	byWiegandEn;	//Wiegand 	  韦根开关 0-韦根关        1-韦根开
	BYTE	byWiegandMode;  //WiegandMode 韦根模式     26-韦根26   34-韦根34
	BYTE	byWiegandType;  //WiegandType 韦根输出 ODVT_FACE-工号输出 ODVT_CARD-卡号输出
}WIEGAND_CONFIG_S;



static WIEGAND_CONFIG_S g_stWiegandcfg = {0};//Wiegand 韦根协议模式26/34

static int even = 0;	//前偶校验位
static int odd = 0;		//后奇校验位
static wiegand_s g_stWD;
static int g_time_fd = 0;

int Serial_OpenTimeFile(void)
{
    g_time_fd = open("/dev/jun_sleep_demo",O_RDWR);
    if(g_time_fd < 0)
    {
        perror("Open file failed!!!\r\n");
        return -1;
    }
	return 0;
}
void Serial_CloseTimeFile(void)
{
    if(g_time_fd > 0)
    {
       close(g_time_fd);
    }
}

int Serial_UsleepFile(int us)
{
	int ret = 0;
	//while(1)
	{     
		ret = ioctl(g_time_fd, USLEEP_FILECTRL, &us);
		if (ret)
		{
			perror("ioctl write");
			return -1;
		}
		//else
		//{
			//break;
		//}
	}
	return 0;
}


int Serial_Data0()
{
	int ret = 0;
	int us;
	//while(1)
	{     
		ret = ioctl(g_time_fd, SLEEP_CTRL0, &us);
		if (ret)
		{
			perror("ioctl write");
			return -1;
		}
		//else
		//{
			//break;
		//}
	}
	return 0;
}

int Serial_Data1()
{
	int ret = 0;
	int us;
	//while(1)
	{     
		ret = ioctl(g_time_fd, SLEEP_CTRL1, &us);
		if (ret)
		{
			perror("ioctl write");
			return -1;
		}
		//else
		//{
			//break;
		//}
	}
	return 0;
}



//12753371  1100 0010 1001 1001 1101 1011
/**
* fun:int型数字转换为二进制24位字符串              确认奇偶校验位 "12753371 -> 1100 0010 1001 1001 1101 1011"
* @pstDorfaceresultmsg-in:输入比对信息
* @return:void;
**/
void  wiegand26_int_to_char(int data)
{
	int i = 0;
	odd = 0;
	even = 0;
	for(i=0; i<24; i++)
	{
		if((1<<i) & data)
		{
			g_stWD.byHexStr[23-i] = '1';
			if(i < 12)
				odd++;
			else
				even++;
		}
		else
		{
			g_stWD.byHexStr[23-i] = '0';
		}
	}
	printf("data is %#x\n", data);     				//0xc299db
	printf("str is: %s\n", g_stWD.byHexStr);			//110000101001100111011011
	printf("check even=%d, odd=%d\n", even, odd);	//even=5, odd=8
	return;
}
 
int wiegand34_int_to_char(int data)
{
	int i = 0;
	odd = 0;
	even = 0;
	for(i=0; i<32; i++)
	{
		if((1<<i) & data)
		{
			g_stWD.byHexStr[31-i] = '1';
			if(i < 16)
				odd++;
			else
				even++;
		}
		else
		{
			g_stWD.byHexStr[31-i] = '0';
		}
	}
	printf("data is %#x\n", data);
	printf("str is: %s\n", g_stWD.byHexStr);
	printf("check even=%d, odd=%d\n", even, odd);
	return 0;
}

//12753371  1100 0010 1001 1001 1101 1011
/**
* fun:int型数字转换为二进制字符串并且            确认奇偶校验位 "12753371 -> 1100 0010 1001 1001 1101 1011"
* @data-in:输入比对信息
* @return:0-success;
**/
int  Wiegand_int_to_char(int nCards,wiegand_s *pstWGOut)
{
	int i = 0;
	if(pstWGOut->nType < 1)
	{
		PRT_NONE("protocol type 26 or 34 or 36 or 44");
		return -1;
	}
	pstWGOut->nOdd = 0;
	pstWGOut->nEven = 0;
	for(i=0; i<(pstWGOut->nType - 2); i++)//数据位赋值
	{
		if((1<<i) & nCards)
		{
			pstWGOut->byHexStr[(pstWGOut->nType - 3)-i] = '1';
			if(i < (pstWGOut->nType - 2)/2)
				pstWGOut->nOdd++; //偶校验位
			else
				pstWGOut->nEven++;//奇校验位
		}
		else
		{
			pstWGOut->byHexStr[(pstWGOut->nType - 3)-i] = '0';
		}
	}
	PRT_NONE("data is %#x", nCards);     								//0xc299db
	PRT_NONE("str is: %s", pstWGOut->byHexStr);							//110000101001100111011011
	PRT_NONE("check even=%d, odd=%d", pstWGOut->nEven, pstWGOut->nOdd);	//even=5, odd=8
	return 0;
}

/**
* fun:发送韦根数据0       先拉低WG-DATA0电平100us再拉高电平1500us (WG-DATA0输出低电平)
* @return:void;
**/
void WieGand_Send_Bit_0(void)
{
	#if 1
    WIEGAND_DATA0_DOWN();		 //WG-DATA0 输出低
   	Serial_UsleepFile(USLEEP_TIMELOW);	 //韦根标准延时100us
    WIEGAND_DATA0_UP();			 //WG-DATA0 输出高
  	Serial_UsleepFile(USLEEP_TIMECYCLE); //韦根标准延时一个发送周期
	#else
	//PRT_YELLOW("=========================begin");
	Serial_Data0();
	//PRT_YELLOW("=========================end");
	#endif
}

/**
* fun:发送韦根数据1 先拉低WG-DATA1电平100us再拉高电平1500us (WG-DATA1输出低电平)
* @return:void;
**/
void WieGand_Send_Bit_1(void)
{
	#if 1
    WIEGAND_DATA1_DOWN();    	 //WG-DATA1输出低
  	Serial_UsleepFile(USLEEP_TIMELOW);  //韦根标准延时100us
    WIEGAND_DATA1_UP();   		 //WG-DATA1输出高
	Serial_UsleepFile(USLEEP_TIMECYCLE); //韦根标准延时一个发送周期
	#else
	//PRT_YELLOW("=========================begin1");
	Serial_Data1();
	//PRT_YELLOW("=========================end1");
	#endif
}


//韦根26格式数据发送
int Hissend_wiegand26(unsigned char *str)
{
	int i = 0;
	//gpio_set_output();
 	//usleepFile(SLEEP_CTRL_1500);
 	
	//偶校验:当实际数据中1的个数为偶数的时候,这个校验位就是0,否则这个校验位就是1
	if(even % 2)
	{
		//PRT_GREEN("1 ");
		WieGand_Send_Bit_1();//wg_send_bit_1(); //偶校验位为1
	}
	else
	{
		//PRT_GREEN("0");
		WieGand_Send_Bit_0();//wg_send_bit_0(); //偶校验位为0
	}
	
	for(i=0; i<24; i++)
	{
		if(str[i] & 0x01) //数据位为1的情况
		{
			//PRT_GREEN("1");
			WieGand_Send_Bit_1();//wg_send_bit_1();
		}
		else //数据位为0的情况
		{
			//PRT_GREEN("0");
			WieGand_Send_Bit_0();//wg_send_bit_0();
		}
	}

	//奇校验:当实际数据中1的个数为偶数的时候,这个校验位就是1,否则这个校验位就是0
	if(odd % 2)
	{
		WieGand_Send_Bit_0();//wg_send_bit_0();
	}
	else
	{
		WieGand_Send_Bit_1();//wg_send_bit_1();
	}
	
	return 0;
}

//韦根34格式数据发送
int Hissend_wiegand34(unsigned char *str)
{
	int i = 0;
	//gpio_set_output();
	//usleepFile(SLEEP_CTRL_1500);
 
	//偶校验:当实际数据中1的个数为偶数的时候,这个校验位就是0,否则这个校验位就是1
	if(even % 2)
	{
		WieGand_Send_Bit_1();//wg_send_bit_1();
	}
	else
	{
		WieGand_Send_Bit_0();//wg_send_bit_0();
	}
	
	for(i=0; i<32; i++)
	{
		if(str[i] & 0x01) //数据位为1的情况
		{
			WieGand_Send_Bit_1();//wg_send_bit_1();
		}
		else //数据位为0的情况
		{
			WieGand_Send_Bit_0();//wg_send_bit_0();
		}
	}

	//奇校验:当实际数据中1的个数为偶数的时候,这个校验位就是1,否则这个校验位就是0
	if(odd % 2)
	{
		WieGand_Send_Bit_0();//wg_send_bit_0();
	}
	else
	{
		WieGand_Send_Bit_1();//wg_send_bit_1();
	}
	return 0;
}

//韦根各种格式数据发送
int Hissend_wiegand(wiegand_s *pstWG)
{
	int i = 0;
	//gpio_set_output();
 	//usleepFile(SLEEP_CTRL_1500);
 	
	//偶校验:当实际数据中1的个数为偶数的时候,这个校验位就是0,否则这个校验位就是1
	if(pstWG->nEven % 2)
	{
		//PRT_GREEN("1 ");
		WieGand_Send_Bit_1();//wg_send_bit_1(); //偶校验位为1
	}
	else
	{
		//PRT_GREEN("0");
		WieGand_Send_Bit_0();//wg_send_bit_0(); //偶校验位为0
	}
	
	for(i=0; i<(pstWG->nType - 2); i++)
	{
		if(pstWG->byHexStr[i] & 0x01) //数据位为1的情况
		{
			//PRT_GREEN("1");
			WieGand_Send_Bit_1();//wg_send_bit_1();
		}
		else //数据位为0的情况
		{
			//PRT_GREEN("0");
			WieGand_Send_Bit_0();//wg_send_bit_0();
		}
	}

	//奇校验:当实际数据中1的个数为偶数的时候,这个校验位就是1,否则这个校验位就是0
	if(pstWG->nOdd % 2)
	{
		WieGand_Send_Bit_0();//wg_send_bit_0();
	}
	else
	{
		WieGand_Send_Bit_1();//wg_send_bit_1();
	}
	
	return 0;
}

//发送一个字节韦根数据
void WieGand_Send_Byte(unsigned char WieGand_Byte)
{
    unsigned char i;
    for(i = 0; i < 8; i++) {
        if(WieGand_Byte & 0x80)
    	{
            WieGand_Send_Bit_1();
    	}
        else
    	{
            WieGand_Send_Bit_0();
    	}
        WieGand_Byte = WieGand_Byte << 1;
    }
}
//1100 0010 1001 0000  1001 0000 1101 1011    C2 90  90 DB
/**
* fun:发送韦根34位数据 {0xC2,0x90,0x90,0xDB} -> 11000010100100001001000011011011 将4位16进制的数组转成2进制的高低电平发送到GPIO口 
* @str-in:输入4位16进制的数组
* @return:void;
**/
void Send_WieGand34(unsigned char *str)
{
    unsigned char i;
    unsigned char check_one;  //韦根包奇偶效验中间暂存
    unsigned char even;       //韦根包前12位偶效验
    unsigned char odd;        //韦根包后12位齐效验

    /*进行前24位的偶校验*/
    check_one = ((str[0] & 0x80) >> 7) + ((str[0] & 0x40) >> 6) + ((str[0] & 0x20) >> 5) + ((str[0] & 0x10) >> 4) + ((str[0] & 0x08) >> 3) + ((str[0] & 0x04) >> 2) + ((str[0] & 0x02) >> 1) + ((str[0] & 0x01)) + \
    			((str[1] & 0x80) >> 7) + ((str[1] & 0x40) >> 6) + ((str[1] & 0x20) >> 5) + ((str[1] & 0x10) >> 4) + ((str[1] & 0x08) >> 3) + ((str[1] & 0x04) >> 2) + ((str[1] & 0x02) >> 1) + ((str[1] & 0x01));

	//PRT_PURPLR("check_one:%d;check_one:%x",check_one,check_one);
    if((check_one & 0x01) == 0)
        even = 0;       //偶校验 偶数个1
    else
        even = 1;       //偶校验 奇数个1	

    /*进行后24位的奇校验*/
    check_one = ((str[2] & 0x80) >> 7) + ((str[2] & 0x40) >> 6) + ((str[2] & 0x20) >> 5) + ((str[2] & 0x10) >> 4) + ((str[2] & 0x08) >> 3) + ((str[2] & 0x04) >> 2) + ((str[2] & 0x02) >> 1) + (str[2] & 0x01) + \
    			((str[3] & 0x80) >> 7) + ((str[3] & 0x40) >> 6) + ((str[3] & 0x20) >> 5) + ((str[3] & 0x10) >> 4) + ((str[3] & 0x08) >> 3) + ((str[3] & 0x04) >> 2) + ((str[3] & 0x02) >> 1) + (str[3] & 0x01);
				
	//PRT_PURPLR("check_one:%d;check_one:%x",check_one,check_one);
    if((check_one & 0x01) == 1)
        odd = 0;    //奇校验 奇数个1
    else
        odd = 1;    //奇校验 偶数个1

	//韦根 输出端初始化
    WIEGAND_DATA0_UP();
    WIEGAND_DATA1_UP();
	Serial_UsleepFile(USLEEP_TIMELOW);//防止第一次的低电平时间不均匀

    //发送偶效验
    if(even == 0) {
        WieGand_Send_Bit_0();
    } else {
        WieGand_Send_Bit_1();
    }
	
	//PRT_PURPLR("even:%d;odd:%d",even,odd);

    //发送24位数据
    for(i = 0; i < 4; i++) {
		//PRT_PURPLR("str[i]:%d;%x",str[i],str[i]);
        WieGand_Send_Byte(str[i]);
    }

    //发送奇效验位
    if(odd == 0) {
        WieGand_Send_Bit_0();
    } else {
        WieGand_Send_Bit_1();
    }
}

// 2-13/ 2-17 偶校验位 偶数个为0(WGData-0) 奇数个为1(WGData-1)
//14-25/18-33 奇校验位 奇数个为0(WGData-0) 偶数个为1(WGData-1)

//1100 0010 1001   1001 1101 1011    C2 99 DB
/**
* fun:发送韦根26位数据 {0xC2,0x99,0xDB} -> 110000101001100111011100 将3位16进制的数组转成2进制的高低电平发送到GPIO口 
* @str-in:输入3位16进制的数组
* @return:void;
**/
void Send_WieGand26(unsigned char *str)
{
    unsigned char i;
    unsigned char check_one;  //韦根包奇偶效验中间暂存
    unsigned char even;       //韦根包前12位偶效验
    unsigned char odd;        //韦根包后12位齐效验

	

    /*进行前12位的偶校验*/
    check_one = ((str[0] & 0x80) >> 7) + ((str[0] & 0x40) >> 6) + ((str[0] & 0x20) >> 5) + ((str[0] & 0x10) >> 4) + ((str[0] & 0x08) >> 3) + ((str[0] & 0x04) >> 2) + \
                ((str[0] & 0x02) >> 1) + ((str[0] & 0x01)) + ((str[1] & 0x80) >> 7) + ((str[1] & 0x40) >> 6) + ((str[1] & 0x20) >> 5) + ((str[1] & 0x10) >> 4);

//	PRT_PURPLR("check_one:%d;check_one:%x",check_one,check_one);

    if((check_one & 0x01) == 0)
        even = 0;       //偶校验 偶数个1
    else
        even = 1;       //偶校验 奇数个1

    /*进行后12位的奇校验*/
    check_one = ((str[1] & 0x08) >> 3) + ((str[1] & 0x04) >> 2) + ((str[1] & 0x02) >> 1) + ((str[1] & 0x01)) + ((str[2] & 0x80) >> 7) + ((str[2] & 0x40) >> 6) + \
                ((str[2] & 0x20) >> 5) + ((str[2] & 0x10) >> 4) + ((str[2] & 0x08) >> 3) + ((str[2] & 0x04) >> 2) + ((str[2] & 0x02) >> 1) + (str[2] & 0x01);

//	PRT_PURPLR("check_one:%d;check_one:%x",check_one,check_one);

    if((check_one & 0x01) == 1)
        odd = 0;    //奇校验 奇数个1
    else
        odd = 1;    //奇校验 偶数个1


	Serial_OpenTimeFile();

	PRT_YELLOW("=========================");

    //韦根 输出端初始化
    //WIEGAND_DATA0_UP();
    //WIEGAND_DATA1_UP();

	//Serial_UsleepFile(USLEEP_TIMELOW);//防止第一次的低电平时间不均匀

    //发送偶效验
    if(even == 0) {
        WieGand_Send_Bit_0();
    } else {
        WieGand_Send_Bit_1();
    }
	
//	PRT_PURPLR("even:%d;odd:%d",even,odd);
    //发送24位数据
    for(i = 0; i < 3; i++) {
//		PRT_PURPLR("str[i]:%d;%x",str[i],str[i]);
        WieGand_Send_Byte(str[i]);
    }

    //发送奇效验位
    if(odd == 0) {
        WieGand_Send_Bit_0();
    } else {
        WieGand_Send_Bit_1();
    }

	Serial_CloseTimeFile();
	
}

/**
* fun:二进制数组转成16进制的数组 "11100001010011001110111001->C299DC"
* @str-in:输入WeiGand26形式的2进制数组
* @dir_ptr-out:输出WeiGand26形式的16进制三字节数组
* @return:0-success;-1-fail;
**/
void Recive_WeiGand26(unsigned char *str, uint8_t *dir_ptr)
{
    unsigned char k;
    unsigned char Even; // 偶检验位
    unsigned char Odd; // 奇检验位
    unsigned char CheakEven = 0; //偶检验
    unsigned char CheakOdd = 1; //奇检验

//unsigned char WGstr[3] = {0x00, 0x00, 0x00}; //存韦根ID卡卡号的HID和PID码,其中HID码为8位即一字节,PID码16位两字节
    for(k = 0; k < 26; k++) 
	{
        if(k <= 0) 
		{ //读偶检验位
            if(str[k] == 0)
                Even = 0;
            else
                Even = 1;
        }
        if(k >= 25) 
        { 
        	//读奇检验位
            if(str[k] == 0)
                Odd = 0;
            else
                Odd = 1;
        }
        
        if(k <= 8) { //读HID码低8位
            if(str[k] == 0x00)
                dir_ptr[0] |= 0x00;
            else {
                dir_ptr[0] |= 0x01;
                CheakEven = ~ CheakEven; // 根据HID码低8位1的个数来确定偶检验位是1还是0
            }
            if(k < 8)
                dir_ptr[0] = dir_ptr[0] << 1;
        }

		
        if(k <= 16) { //读PID码高8位
            if(str[k] == 0x00)
                dir_ptr[1] |= 0x00;
            else {
                dir_ptr[1] |= 0x01;
                CheakOdd = ~CheakOdd;  根据PID码高8位1的个数来确定奇检验位是1还是0
            }
            if(k < 16)
                dir_ptr[1] = dir_ptr[1] << 1;
        } 
		else 
		{ //读PID码的低8位
            if(str[k] == 0x00)
                dir_ptr[2] |= 0x00;
            else 
			{
                dir_ptr[2] |= 0x01;
                CheakOdd = ~CheakOdd; // 根据PID码低8位1的个数来确定奇检验位是1还是0
            }
            if(k < 24)
                dir_ptr[2] = dir_ptr[2] << 1;
        }
    }
    if((Even == CheakEven) && (Odd == CheakOdd))
        return;
}

//1 1100 0010 1001 0000 , 1001 0000 1101 1100 1   C29090DC
/**
* fun:二进制数组转成16进制的数组 "1110000101001000010010000110111001->C29090DC"
* @str-in:输入WeiGand34形式的2进制数组
* @dir_ptr-out:输出WeiGand34形式的16进制四字节数组
* @return:0-success;-1-fail;
**/
int  Recive_WeiGand34(unsigned char *str, uint8_t *dir_ptr)
{
    unsigned char k;
    unsigned char Even; // 偶检验位
    unsigned char Odd; // 奇检验位
    unsigned char CheakEven = 0; //偶检验
    unsigned char CheakOdd = 1; //奇检验

//    unsigned char WGstr[3] = {0x00, 0x00, 0x00}; //存韦根ID卡卡号的HID和PID码,其中HID码为8位即一字节,PID码16位两字节
    for(k = 0; k < 34; k++) {
        if(k <= 0) { //读偶检验位
            if(str[k] == 0)
                Even = 0;
            else
                Even = 1;
        }
        if(k >= 33) { //读奇检验位
            if(str[k] == 0)
                Odd = 0;
            else
                Odd = 1;
        }
		
       if(k <= 8) { //读HID码低8位
            if(str[k] == 0x00)
                dir_ptr[0] |= 0x00;
            else {
                dir_ptr[0] |= 0x01;
                CheakEven = ~ CheakEven; // 根据HID码低8位1的个数来确定偶检验位是1还是0
            }
            if(k < 8)
                dir_ptr[0] = dir_ptr[0] << 1;
        }

	   if(k <= 16) { //读HID码高8位
            if(str[k] == 0x00)
                dir_ptr[1] |= 0x00;
            else {
                dir_ptr[1] |= 0x01;
                CheakEven = ~ CheakEven; // 根据HID码低8位1的个数来确定偶检验位是1还是0
            }
            if(k < 16)
                dir_ptr[1] = dir_ptr[1] << 1;
        }

		
        if(k <= 24) { //读PID码高8位
            if(str[k] == 0x00)
                dir_ptr[2] |= 0x00;
            else {
                dir_ptr[2] |= 0x01;
                CheakOdd = ~CheakOdd;  根据PID码高8位1的个数来确定奇检验位是1还是0
            }
            if(k < 24)
                dir_ptr[2] = dir_ptr[2] << 1;
        } else { //读PID码的低8位
            if(str[k] == 0x00)
                dir_ptr[3] |= 0x00;
            else {
                dir_ptr[3] |= 0x01;
                CheakOdd = ~CheakOdd; // 根据PID码低8位1的个数来确定奇检验位是1还是0
            }
            if(k < 32)
                dir_ptr[3] = dir_ptr[3] << 1;
        }
    }
    if((Even == CheakEven) && (Odd == CheakOdd))
        return -1;
	return 0;
}
//110000101001100111011011 C299DB
//110000101001100111011100 C299DC
/**
* fun:16进制的字符串转成10进制数字 "C299DB" -> 12753371
* @s-in:输入比对信息
* @return:sum-10进制数字; 4294967295
**/
unsigned long HexToNumfun(const char *s)
{
	int i;
	long t;			
	unsigned long sum =0;
	for(i=0;s[i];i++)
	{
		if( s[i] >= '0' && s[i] <= '9')
			t = s[i] - '0'; 	  			//当字符是0~9时保持原数不变
		if( s[i] >= 'a' && s[i] <= 'z')
			t = s[i] - 'a' + 10;
		if( s[i] >= 'A' && s[i] <= 'Z')
			t = s[i] - 'A' + 10;
		sum = sum*16 + t;
	}
	printf("i:%d\n",i);
	return sum;
}

void StringToHex(char *str, unsigned char *strhex)
{
	uint8_t i,cnt=0;
	char *p = str;             //直针p初始化为指向str
	uint8_t len = strlen(str); //获取字符串中的字符个数
	
	while(*p != '\0') {        //结束符判断
		for (i = 0; i < len; i + 2)  //循环判断当前字符是数字还是小写字符还是大写字母
		{
			if ((*p >= '0') && (*p <= '9')) //当前字符为数字0~9时
				strhex[cnt] = *p - '0' + 0x30;//转为十六进制
			
			if ((*p >= 'A') && (*p <= 'Z')) //当前字符为大写字母A~Z时
				strhex[cnt] = *p - 'A' + 0x41;//转为十六进制
			
			if ((*p >= 'a') && (*p <= 'z')) //当前字符为小写字母a~z时
				strhex[cnt] = *p - 'a' + 0x61;  //转为十六进制
		
			p ++;    //指向下一个字符
			cnt ++;  
		}
	}
}

/**
* fun:把字符转成 16进制的本身的值 '1'->0x1 'b'-> 0xb
* @s-c:输入比对信息
* @return:value-16进制数字;
**/
int Serial_sthvalue(const char c)
{
        int value;
        if((c >= '0') && (c <= '9'))
                value = 48;
        else if ((c >= 'a') && (c <='f'))
                value = 87;
        else if ((c >= 'A') && (c <='F'))
                value = 55;
        else {
                printf("invalid data %c",c);
                return -1;
        }
        return value;
}

/**
* fun:把偶数位字符串和一个数组当做参数,这个函数会把str的值,每两个组合成一个16进制的数 "CF0486 -> 0XCF 0X4 0X86" 
* @byMode-in:输入韦根模式 26 34 36 44
* @str-in:输入偶数位字符串 "CF0486"
* @data-out:输出16进制数组 "0XCF 0X4 0X86"
* @return:len-获取字符串的字符个数;
**/
int Serial_strtohex(unsigned char byMode,char *str, unsigned char *data)
{
	int len = 0;
	int sum = 0;
	int low = 0;
	int high = 0;
	int nValue = 0;
	int charlen = 0;
	int j=0;	
	char szStr[32] = {0};
	//char str[32] = "00";
	char *pstr = str;
	char zero[16] = {0};

	len = strlen(str);//获取字符串的字符个数
	if(byMode == 34)//判断是否发送4字节的Wiegand34
	{
		charlen = 8;
	}
	else//判断是否发送3字节的Wiegand26
	{
		charlen = 6;
	}

	if(len != charlen)
	{
		if(len < charlen)
		{
			for(int j = 0;j<charlen-len;j++)
			{
				zero[j] = '0';
			}
			sprintf(szStr,"%s%s",zero,str);
		}
		else //大于charlen位只拷贝尾部的charlen个字节
		{
			memcpy(szStr,pstr+(len-charlen),charlen);
		}
	}
	else
	{
		sprintf(szStr,"%s",str);
	}
	
	//printf("szStr:%s\nlen:%d\n",szStr,len);
	len = strlen(szStr);	
	//在for循环中,从0开始,每两个数组成一个16进制,高4位和低4位,然后放技能数组中去
	for(int i=0; i<len; i++)
	{
		//printf("high-n:0x%02x\n", szStr[i]);
		nValue = Serial_sthvalue(szStr[i]);
		high = (((szStr[i]-nValue)&0xF)<<4);//获取数据,成为高4位
		//printf("high:0x%02x\n", high);
		//printf("low-n:0x%02x\n", szStr[i+1]);
		nValue = Serial_sthvalue(szStr[i+1]);
		low = ((szStr[i+1]-nValue)&0xF);//获取数据,成为低4位
		//printf("low:0x%02x\n", low);
		sum = high | low; //组合高低四位数,成为一byte数据
		//printf("sum:0x%02x\n", sum);
		j = i / 2; //由于两个字符组成一byte数,这里的j值要注意
		data[j] = sum;//把这byte数据放到数组中
		i=i+1; //每次循环两个数据,i的值要再+1
	}
	return len;
}

#if 1
/**
* fun:在应用层发送韦根bit数据
* @byMode-in:输入韦根模式 34 26
* @pszCard-in:输入卡号字符串 "C299DBA8"
* @return:0-success;
**/
int Serial_SendWiegand(BYTE byMode,char *pszCard)
{
	int ret = 0;
	unsigned char szCardStr[4] = {0xC2,0x99,0xDB,0xA8};
	memset(szCardStr,0,sizeof(szCardStr));
	Serial_strtohex(byMode,pszCard,szCardStr);
	Send_WieGand26(szCardStr);
	return 0;
}
#else
/**
* fun:在驱动层发送韦根bit数据
* @byMode-in:输入韦根模式 1-34 0-26
* @pszCard-in:输入卡号字符串 "C299DBA8"
* @return:0-success;
**/
int Serial_SendWiegand(BYTE byMode,char *pszCard)
{
	
	int ret = 0;
	unsigned char szCardStr[4] = {0xC2,0x99,0xDB,0xA8};
	memset(szCardStr,0,sizeof(szCardStr));
	Serial_strtohex(byMode,pszCard,szCardStr);

	
	if(Wiegand_fd < 0)
	{
		PRT_ERR("SendWiegand Open file failed!!!");
		return -1;
	}

	if(byMode == 34)
		ret = ioctl(Wiegand_fd, WG_34_MODE,szCardStr);
	else
		ret = ioctl(Wiegand_fd, WG_26_MODE,szCardStr);
	
	if(ret)
	{
		PRT_ERR("SendWiegand ioctl write");
		return -1;
	}
	
	return 0;
}
#endif
int Serial_WiegandInit(void)
{
	//复用为GPIO口
	//设置方向 输入输出
	//设置数据
	
	//himm 0x10FF0030 0x500	//--->复用GPIO1_5
	//himm 0x10FF0034 0x500	//--->复用GPIO1_6
	//himm 0x120d1400 0xff	//--->方向 设置方向为输出
	//himm 0x120d1080 0xff	//--->赋值GPIO1_5高电平
	//himm 0x120d1080 0x00	//--->赋值GPIO1_5低电平
	//himm 0x120d1100 0xff	//--->赋值GPIO1_6高电平
	//himm 0x120d1100 0x00	//--->赋值GPIO1_6低电平
	
	system("insmod /komod/wiegand.ko");	//--->安装驱动
	//system("himm 0x10FF0030 0x500");	//--->复用GPIO1_5
	//system("himm 0x10FF0034 0x500");	//--->复用GPIO1_6
	//system("himm 0x120d1400 0xff"); 	//--->方向 设置方向为输出

	Wiegand_fd = open("/dev/wiegand",O_RDWR);
	if(Wiegand_fd < 0)
	{
		PRT_ERR("SendWiegand Open file failed!!!");
		return -1;
	}
	return 0;
}
void Serial_WiegandReInit(void)
{
	if(Wiegand_fd > 0)
	{
		close(Wiegand_fd);
	}
}


int main()
{
	#if 1
	system("himm 0x10FF0030 0x500");	//--->复用GPIO1_5
	system("himm 0x10FF0034 0x500");	//--->复用GPIO1_6
	system("himm 0x120d1400 0xff"); 	//--->方向 设置方向为输出
	Serial_SendWiegand(26,"C299DBA8");
	#else
	Serial_WiegandInit();
	Serial_WiegandReInit();
	#endif
	return 0;
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值