📄 demo607.c
字号:
//Prog:demo607.c showing the reading & writing operation of lk607.
//compiler:turboc
#include <conio.h>
#include <string.h>
#define MT 1000
int C_write(int comn,long baud,char *data ) //data:"%tk1?;tk2?+tk3?"
/* 写卡函数 C_write
参数:
comn 整型 ,可为: 1,代表串口1;2,代表串口2 。
baud 长整型,代表波特率 ,可为:9600,4800,2400,1200
data 字符串指针,代表三道数据 "%TK1?;TK2?+TK3?"
TK1 一道数据(大写字母或数字,最多76个)
TK2 二道数据(纯数字,最多37个)
TK3 三道数据(纯数字,最多104个)
返回:整型
1:写卡正确
0:写卡不成功
2: 按ESC键退出
-1: 串口设置有误
*/
{
int TRX,LSR,LCR,BRD_LSB,MCR,IER,div,i=0,stl=0;
char str[300],bcc=0,*p,instr[300];
long timer;
str[0]=2; //STX
strcpy(str+1,data);
while(1)
{
i++;
bcc=str[i]^bcc;
if(str[i]==0)
{
str[i]=3; //ETX
bcc=3^bcc ;
str[i+1]=bcc ; //BCC
str[i+2]=0 ;
stl=i+2 ;
break ;
}
}
if(comn==1) //COM1
TRX=0x3f8,LSR=0x3fd,LCR=0x3fb,BRD_LSB=0x3f8,IER=0X3F9,MCR=0x3fc;
else if(comn==2) //COM2
TRX=0x2f8,LSR=0x2fd,LCR=0x2fb,BRD_LSB=0x2f8,IER=0X2F9,MCR=0x2fc;
else //error
{
strcpy(str,"Parameter Error");
return -1 ;
}
div=(9600/baud)*12;
outp(LCR,0x80);
outpw(BRD_LSB,div); //Set baud rate
outp(LCR,0x0A); //ODD parity, 7 data bits, 1 stop bit
outp(IER,0); //disable int
outp(MCR,11); //CS,RS handshaking
i=0;
while(1)
{
if(inp(LSR)&0x20) outp(TRX,str[i++]); //sending
if(i>=stl) break;
}
timer=(9600/baud)*MT;
p=instr;
while(!(inp(LSR)&0x01)) //waiting
{
if(kbhit())
{
if(getch()==27)
{
outp(MCR,0);
return 2 ;
}
}
}
while(timer--)
{
if(inp(LSR)&0x01)
{
*p++=inp(TRX) ; //receiving
timer=(9600/baud)*MT;
}
}
*p=0;
outp(MCR,0);
if(strcmp(str,instr)==0) return 1 ;
else return 0 ;
}
char *C_read(int comn,long baud) //read a card
/* 读卡函数 C_read
参数:
comn 整型 ,可为: 1,代表串口1;2,代表串口2 。
baud 长整型,代表波特率 ,可为:9600,4800,2400,1200
返回: 字符串
"se" 串口设置有误
"esc" 按ESC键退出
"re" 读卡有误
"%TK1?;TK2?+TK3?" 读卡正确
TK1 一道数据(字母或数字)
TK2 二道数据(纯数字)
TK3 三道数据(纯数字)
*/
{
int RX,LSR,LCR,BRD_LSB,MCR,IER,div;
char *p,str[300];
long timer;
if(comn==1) //COM1
RX=0x3f8,LSR=0x3fd,LCR=0x3fb,BRD_LSB=0x3f8,MCR=0x3fc,IER=0x3f9;
else if(comn==2) //COM2
RX=0x2f8,LSR=0x2fd,LCR=0x2fb,BRD_LSB=0x2f8,MCR=0x2fc,IER=0x2f9;
else return("se");
div=(9600/baud)*12;
outp(LCR,0x80);
outpw(BRD_LSB,div);
outp(LCR,0x0A);
outp(IER,0);
outp(MCR,11);
timer=(9600/baud)*MT;
p=str;
while(!(inp(LSR)&0x01)) //waiting
{
if(kbhit())
{
if(getch()==27)
{
outp(MCR,0);
return("esc") ;
}
}
}
while(timer--)
{
if(inp(LSR)&0x01)
{
*p++=inp(RX) ;
timer=(9600/baud)*MT;
}
}
*p=0;
outp(MCR,0);
if(str[1]==0x15) strcpy(str,"re");
else
{
if(*(p-2)==3) *(p-2)=0;
else *(p-1)=0;
strcpy(str,str+1);
}
return(str);
}
main()
{
int ws=0;
puts("Write mode: swipe a card when yellow LED light.(press ESC to quit)");
ws=C_write(2,9600,"%ASDNFOC123?;0123456789?+0123456789?");
if(ws==1) puts("ok");
else if(ws==2) puts("esc") ;
else puts("error");
puts("\nRead mode: swipe a card when green LED light.(press ESC to quit)");
puts("Card data:");
puts(C_read(2,9600)); //read card from COM2, baud rate 9600 bps
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -