Reading with / dev / input

I have a USB RFID card reader that emulates a keyboard. So when I put the card, I see a line in the terminal window "0684a24bc1"

But I would like to read it in my C program. No problem using: scanf("%s",buff);

But when I use the code below, I received a lot (about 500 bytes) of unrecognized data. What for? I would like to have a non-blocking read.

 #include <stdio.h> #include <string.h> #include <unistd.h> #include <fcntl.h> #include <errno.h> #include <termios.h> int main(int argc, char ** argv) { int fd; char buf[256]; fd = open("/dev/input/event3", O_RDWR | O_NOCTTY | O_NDELAY); if (fd == -1) { perror("open_port: Unable to open /dev/ttyAMA0 - "); return(-1); } // Turn off blocking for reads, use (fd, F_SETFL, FNDELAY) if you want that fcntl(fd, F_SETFL, 0); } while(1){ n = read(fd, (void*)buf, 255); if (n < 0) { perror("Read failed - "); return -1; } else if (n == 0) printf("No data on port\n"); else { buf[n] = '\0'; printf("%i bytes read : %s", n, buf); } sleep(1); printf("i'm still doing something"); } close(fd); return 0; } 
+4
source share
3 answers

According to Linux input documentation , section 5, devices / dev / input / eventX return data as follows:

You can use blocking and non-blocking reads, as well as select () on / dev / input / eventX, and you will always get a range of input events when reading. Their location:

 struct input_event { struct timeval time; unsigned short type; unsigned short code; unsigned int value; }; 

'time' is a timestamp; it returns the time at which the event happened. Type is, for example, EV_REL for relative momentum, EV_KEY for press or release. Additional types are defined in include / linux / input.h.

'code' is an event code, for example REL_X or KEY_BACKSPACE, again the full list is in include / linux / input.h.

'value' is the value that the event carries. Either a relative change for EV_REL, an absolute new value for EV_ABS (joysticks ...) or 0 for EV_KEY for release, 1 for pressing a key, and 2 for auto-repeat.

+9
source

Your code is clearly flawed when opening the event device in /dev/input/ . Even your error message contradicts the choice:

 perror("open_port: Unable to open /dev/ttyAMA0 - "); 

Reading from /dev/input/eventN files returns binary data with descriptions of events (for example, moving the pointer or pressing buttons), not text. You might want to open some sort of serial emulation device.

0
source
 #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 38 27 00B 00 00 00 00 00 04 00 04 00 5A 00 07 00 35 49 C9 5C 00 00 00 00 00 38 27 0B 00 00 00 00 00 00 01 00 50 00 01 00 00 00 35 49 C9 5C 00 00 00 00 00 38 27 0B 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

This is raw data, in order to convert it to some kind of key, I need to read the link "Linux input documentation" above ...

0
source

Source: https://habr.com/ru/post/983284/


All Articles