操作设备文件
操作连接到主机串口的外设(密码键盘)
打开串口设备:
int open(char* devname)
{
int fd;
if((fd = open(devname, O_RDWR)) < 0)
{
cout<<"open termio error"<<endl;
return -1;
}
if((ioctl(fd, TCGETA, &oldtty)) < 0)
{
cout<<"get termio para error"<<endl;
close(fd);
reteurn -1;
}
newtty = oldtty;
newtty.c_iflag &= ~ICRNL;
newtty.c_oflag = newtty.c_oflag & ~ONLCR & TAB0 | OPOST;
newtty.c_lflag = newtty.c_lfalg & ~ICANON & ~ECHO;
newtty.c_cc[VMIN]=1;
newtty.c_cc[VQUIT]= 0;
if((ioctl(fd, TCSETA, newtty)) < 0)//设置设备属性
{
cout<<"set termio para error"<<endl;
close(fd);
return -1;
}
close(fd);
}
写入“您好,请输入密码”命令
void inputSe(int fd)
{
char buf[3];
buf[0] = 0x82;
buf[1]=0;
write(fd, buf, 1);
}
读取密码键盘输入
int readFromSe(int fd, char* buf, int len)
{
int i=0;
buf[0]=0;
while(i<len)
{
read(fd, &buf[i], 1);
buf[i] &= 0x7f;
++i;
}
buf[i]=0;
return i;
}
写入‘关闭密码键盘“命令
void closeSe(int fd)
{
char buf[3];
buf[0]=ox83;
buf[1]=0;
write(fd, buf, 1);
}
关闭串口设备
int closeTTY(int fd)
{
if((ioctl(fd, TCSETA, &oldtty)) < 0)//恢复设备属性
{
cout<<"tty para set error"<<endl;
close(fd);
return -1;
}
close(fd);
return 0;
}
操作连接到主机串口的外设(密码键盘)
打开串口设备:
int open(char* devname)
{
int fd;
if((fd = open(devname, O_RDWR)) < 0)
{
cout<<"open termio error"<<endl;
return -1;
}
if((ioctl(fd, TCGETA, &oldtty)) < 0)
{
cout<<"get termio para error"<<endl;
close(fd);
reteurn -1;
}
newtty = oldtty;
newtty.c_iflag &= ~ICRNL;
newtty.c_oflag = newtty.c_oflag & ~ONLCR & TAB0 | OPOST;
newtty.c_lflag = newtty.c_lfalg & ~ICANON & ~ECHO;
newtty.c_cc[VMIN]=1;
newtty.c_cc[VQUIT]= 0;
if((ioctl(fd, TCSETA, newtty)) < 0)//设置设备属性
{
cout<<"set termio para error"<<endl;
close(fd);
return -1;
}
close(fd);
}
写入“您好,请输入密码”命令
void inputSe(int fd)
{
char buf[3];
buf[0] = 0x82;
buf[1]=0;
write(fd, buf, 1);
}
读取密码键盘输入
int readFromSe(int fd, char* buf, int len)
{
int i=0;
buf[0]=0;
while(i<len)
{
read(fd, &buf[i], 1);
buf[i] &= 0x7f;
++i;
}
buf[i]=0;
return i;
}
写入‘关闭密码键盘“命令
void closeSe(int fd)
{
char buf[3];
buf[0]=ox83;
buf[1]=0;
write(fd, buf, 1);
}
关闭串口设备
int closeTTY(int fd)
{
if((ioctl(fd, TCSETA, &oldtty)) < 0)//恢复设备属性
{
cout<<"tty para set error"<<endl;
close(fd);
return -1;
}
close(fd);
return 0;
}