#include#include #include #include #include #include #define BYTE unsigned char/*// C prototype : void StrToHex(BYTE *pbDest, BYTE *pbSrc, int nLen)// parameter(s): [OUT] pbDest - 输出缓冲区// [IN] pbSrc - 字符串// [IN] nLen - 16进制数的字节数(字符串的长度/2)// return value: // remarks : 将字符串转化为16进制数*/void StrToHex(BYTE *pbDest, BYTE *pbSrc, int nLen){char h1,h2;BYTE s1,s2;int i;for (i=0; i 9) s1 -= 7;s2 = toupper(h2) - 0x30;if (s2 > 9) s2 -= 7;pbDest[i] = s1*16 + s2;}}/*// C prototype : void HexToStr(BYTE *pbDest, BYTE *pbSrc, int nLen)// parameter(s): [OUT] pbDest - 存放目标字符串// [IN] pbSrc - 输入16进制数的起始地址// [IN] nLen - 16进制数的字节数// return value: // remarks : 将16进制数转化为字符串*/void HexToStr(BYTE *pbDest, BYTE *pbSrc, int nLen){char ddl,ddh;int i;for (i=0; i 57) ddh = ddh + 7;if (ddl > 57) ddl = ddl + 7;pbDest[i*2] = ddh;pbDest[i*2+1] = ddl;}pbDest[nLen*2] = '\0';}int RS232_SendBuf(int comport_number,unsigned char *data_buf,int size){ char str[1024] = ""; HexToStr(str, data_buf, size); printf("str == %s\n", str); return 0;}int RS232_SxtBuf(int comport_number,int cardID,unsigned char cmd,unsigned char cmd_Sxt,unsigned char *buf,int size){ //unsigned char data_buf[size+8]; int i; int satae = -1; unsigned char data_buf[64]; switch(cmd) { case 0x50: // data_buf = (unsigned char*)malloc((size+7)*sizeof(unsigned char)); data_buf[0] = 0XA0; data_buf[1] = 0X90|((cardID>>8)&0x0F); data_buf[2] =(unsigned char)(cardID&0x00ff); data_buf[3] = 0x50; data_buf[4] = size/255; data_buf[5] = size%255; if(size != 0) { for(i = 0;i >8)&0x0F); data_buf[2] =(unsigned char)(cardID&0x00ff); data_buf[3] = 0xA5; data_buf[4] = size/255; data_buf[5] = size%255; if(size != 0) { for( i = 0;i >8)&0x0F); data_buf[2] =(unsigned char)(cardID&0x00ff); data_buf[3] = 0x58; if(size == 0) { data_buf[4] = 0; data_buf[5] = 0; data_buf[6] = data_buf[5]^data_buf[4]^data_buf[3]^data_buf[2]; satae = RS232_SendBuf(comport_number,data_buf,7); } else { data_buf[4] = 0; data_buf[5] = 1; data_buf[6] = 0xFF; data_buf[7] = data_buf[6]^data_buf[5]^data_buf[4]^data_buf[3]^data_buf[2]; satae = RS232_SendBuf(comport_number,data_buf,8); } break; case 0x48: data_buf[0] = 0XA0; data_buf[1] = 0X90|((cardID>>8)&0x0F); data_buf[2] =(unsigned char)(cardID&0x00ff); data_buf[3] = 0x48; data_buf[4] = 0; data_buf[5] = 1; data_buf[6] = cmd_Sxt; data_buf[7] = data_buf[6]^data_buf[5]^data_buf[4]^data_buf[3]^data_buf[2]; satae = RS232_SendBuf(comport_number,data_buf,8); break; case 0x49: // data_buf = (unsigned char*)malloc((size+9)*sizeof(unsigned char)); data_buf[0] = 0XA0; data_buf[1] = 0X90|((cardID>>8)&0x0F); data_buf[2] =(unsigned char)(cardID&0x00ff); data_buf[3] = 0x49; data_buf[4] = (size+1)/255; data_buf[5] = (size+1)%255; if(size != 0) { data_buf[6] = cmd_Sxt; for(i = 0;i >8)&0x0F); data_buf[2] =(unsigned char)(cardID&0x00ff); data_buf[3] = 0x44; data_buf[4] = size/255; data_buf[5] = size%255; if(size != 0) { for(int i = 0;i >8)&0x0F); data_buf[2] =(unsigned char)(cardID&0x00ff); data_buf[3] = 0x51; data_buf[4] = size/255; data_buf[5] = size%255; if(size != 0) { for( i = 0;i >8)&0x0F); data_buf[2] =(unsigned char)(cardID&0x00ff); data_buf[3] = 0x52; data_buf[4] = size/255; data_buf[5] = size%255; if(size != 0) { for( i = 0;i