I have an USB RFID card reader which emulates keyboard.
So when i put an card to it i see the string on a terminal window -i.e. \"0684a24bc1\"
But i wo
Your code is clearly wrong at opening an event device under /dev/input/
. Even your error message is contradicting the choice:
perror("open_port: Unable to open /dev/ttyAMA0 - ");
Reads from /dev/input/eventN
files return binary data with event descriptions (like pointer moving or button presses), not text. Your probably want to open some sort of serial emulation device instead.
According to the Linux input documentation, section 5, the /dev/input/eventX devices return data as following:
You can use blocking and nonblocking reads, also select() on the /dev/input/eventX devices, and you'll always get a whole number of input events on a read. Their layout is:
struct input_event { struct timeval time; unsigned short type; unsigned short code; unsigned int value; };
'time' is the timestamp, it returns the time at which the event happened. Type is for example EV_REL for relative moment, EV_KEY for a keypress or release. More types are defined in include/linux/input.h.
'code' is event code, for example REL_X or KEY_BACKSPACE, again a complete list is in include/linux/input.h.
'value' is the value the event carries. Either a relative change for EV_REL, absolute new value for EV_ABS (joysticks ...), or 0 for EV_KEY for release, 1 for keypress and 2 for autorepeat.
#include <fcntl.h>
#include <unistd.h>
#include <poll.h>
int main(int argc, char *argv[])
{
int timeout_ms = 5000;
char input_dev[] = "/dev/input/event17\0";
int st;
int ret;
struct pollfd fds[1];
fds[0].fd = open(input_dev, O_RDONLY|O_NONBLOCK);
if(fds[0].fd<0)
{
printf("error unable open for reading '%s'\n",input_dev);
return(0);
}
const int input_size = 4096;
unsigned char input_data[input_size];
memset(input_data,0,input_size);
fds[0].events = POLLIN;
int exit_on_key_press_count = 10;
while(true)
{
ret = poll(fds, 1, timeout_ms);
if(ret>0)
{
if(fds[0].revents)
{
ssize_t r = read(fds[0].fd,input_data,input_size);
if(r<0)
{
printf("error %d\n",(int)r);
break;
}
else
{
printf("total bytes read %d/%d\n",(int)r,input_size);
for(int i = 0; i<r;i++)
{
printf("%02X ",(unsigned char)input_data[i]);
}
printf("\n");
memset(input_data,0,input_size);
exit_on_key_press_count--;
if(exit_on_key_press_count<1)
break;
}
}
else
{
printf("error\n");
}
}
else
{
printf("timeout\n");
}
}
close(fds[0].fd);
return 0;
}
$ sudo ./keypressed
total bytes read 72/4096
35 49 C9 5C 00 00 00 00 38 27 0B 00 00 00 00 00 04 00 04 00 5A 00 07 00 35 49 C9 5C 00 00 00 00 38 27 0B 00 00 00 00 00 01 00 50 00 01 00 00 00 35 49 C9 5C 00 00 00 00 38 27 0B 00 00 00 00 00 00 00 00 00 00 00 00 00
This is raw data, to convert to some key, I need to read 'Linux input documentation' link abave...