I/O操作

操作设备文件
操作连接到主机串口的外设(密码键盘)
打开串口设备:
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;

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值