求助:嵌入式读键盘

dengzhaoming 2011-04-16 07:50:55
我需要从开发板上的usb键盘读数据,于是写了一个带scanf函数的程序,下到开发板上运行但是只能读取pc机上超级终端的数据。然后我用开发板上QT自带的终端运行,就可以读键盘上的程序。因为这个程序需要自动运行,设置好后,开机后只能读pc机上的超级终端数据,而经验证,该程序开机后在运行。后来我怀疑是QT的问题,在开机自动运行中把qt注释掉后,还是不能读键盘。哪位高手帮忙解决一下,不胜感激。
...全文
187 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
dengzhaoming 2011-04-19
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 keywie007 的回复:]
fd = open ("/dev/tty1",O_RDWR);
然后将dup (fd,0);

因为嵌入式设备上面的shell(也就是你程序的father std输入输出 已经被重定向到串口了),so you need to 将其重新定向到键盘
[/Quote]
我像这样写还是不行哦,麻烦帮忙改下
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <linux/input.h>
int main()
{
char s[20];
int fd;
fd=open("dev/tty1",O_RDWR);
if (fd < 0)
{
perror("can not open device usbkeyboard!");
exit(1);
}
dup(fd,0);
scanf("%s",s);
printf("%s",s);
return 0;
}
dengzhaoming 2011-04-19
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 steven_miao 的回复:]
这个不是驱动也不是应用程序的问题,嵌入式的程序开发与pc机上的程序开发不是一个概念。就以scanf为例,在pc机上,我们通常会认为是从键盘输入的值,而在嵌入式中确是终端输入值,glibc可能是不同的,在嵌入式中一般获取键盘的键值会采用input的结构来获取键值。其获取其键值如下:
static int test_key()
{
read(key_fd, &data, sizeof……
[/Quote]
麻烦帮我看下这个有问什么问题啊,type,code,value全是0
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <linux/input.h>

struct input_event buff;
int fd;
char s[5];
int main(int argc, char *argv[])
{
fd = open("dev/tty0", O_RDONLY);
if (fd < 0)
{
perror("can not open device usbkeyboard!");
exit(1);
}
while(1)
{
read(fd,&buff,sizeof(struct input_event));
printf("type:%d code:%d value:%d\n",buff.type,buff.code,buff.value);
}
close(fd);
return 0;
}
玩世不恭 2011-04-18
  • 打赏
  • 举报
回复
fd = open ("/dev/tty1",O_RDWR);
然后将dup (fd,0);

因为嵌入式设备上面的shell(也就是你程序的father std输入输出 已经被重定向到串口了),so you need to 将其重新定向到键盘
steven_miao 2011-04-18
  • 打赏
  • 举报
回复
这个不是驱动也不是应用程序的问题,嵌入式的程序开发与pc机上的程序开发不是一个概念。就以scanf为例,在pc机上,我们通常会认为是从键盘输入的值,而在嵌入式中确是终端输入值,glibc可能是不同的,在嵌入式中一般获取键盘的键值会采用input的结构来获取键值。其获取其键值如下:
static int test_key()
{
read(key_fd, &data, sizeof(data));
if (data.type == EV_KEY)
printf(" type: EV_KEY, event = %d, value = %d\n",data.code, data.value);
return 0;
}
yuanlulu 2011-04-17
  • 打赏
  • 举报
回复
确定驱动没问题吧!读event试试。然后再找其他的问题

4,436

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 内核源代码研究区
社区管理员
  • 内核源代码研究区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧