- 論壇徽章:
- 0
|
我需要從開發(fā)板上的usb鍵盤讀數(shù)據(jù),于是寫了一個(gè)帶scanf函數(shù)的程序,下到開發(fā)板上運(yùn)行但是只能讀取pc機(jī)上超級終端的數(shù)據(jù)。然后我用開發(fā)板上QT自帶的終端運(yùn)行,就可以讀鍵盤上的程序。因?yàn)檫@個(gè)程序需要自動運(yùn)行,設(shè)置好后,開機(jī)后只能讀pc機(jī)上的超級終端數(shù)據(jù),而經(jīng)驗(yàn)證,該程序開機(jī)后在運(yùn)行。后來我懷疑是QT的問題,在開機(jī)自動運(yùn)行中把qt注釋掉后,還是不能讀鍵盤。哪位高手幫忙解決一下,不勝感激。
#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);
}
scanf("%s",s);
printf("%s",s);
return 0;
}
上面這個(gè)就只能讀超級終端的數(shù)據(jù)。
然后我聽別人建議換了一種讀的方式
#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;
}
結(jié)果讀出來的東西都是亂碼。
請各位大神們指點(diǎn)下。 |
|