#include <stdio.h> /* Standard input/output definitions */ #include <string.h> /* String function definitions */ #include <unistd.h> /* UNIX standard function definitions */ #include <fcntl.h> /* File control definitions */ #include <errno.h> /* Error number definitions */ #include <termios.h> /* POSIX terminal control definitions */ char DEV_RFID[16]="/dev/ttyS0"; int main(int argc, char **argv) { struct timeval timeout; timeout.tv_sec = 0; timeout.tv_usec = 500; // ttyS1 can be chosed if ttyS0 is in used if(argc>1) strcpy(DEV_RFID, "/dev/ttyS1"); fprintf(stdout, "[RFID] ----------- DEMO -------------------\n"); fprintf(stdout, "[RFID] Connecting to %s\n", DEV_RFID); // Open a Serial Port int fd; fd = open(DEV_RFID, O_RDWR | O_NOCTTY); if (fd < 0) { perror(DEV_RFID); exit(1); } fprintf(stdout, "[RFID] %s is connected\n", DEV_RFID); struct termios oldtio, newtio; // Gets the current serial port settings. tcgetattr(fd, &oldtio); bzero(&newtio, sizeof(newtio)); // Termios Structure Members -------------------------------------- // Control options, 19200 baud/8 data bits/Enable receiver/Local line newtio.c_cflag |= (B19200|CS8|CREAD|CLOCAL); newtio.c_lflag &= (~(ICANON|ECHO|ECHOE|ECHOK|ECHONL|ISIG)); // Input options newtio.c_iflag &= (~(INPCK|IGNPAR|PARMRK|ISTRIP|ICRNL|IXANY)); // Output options newtio.c_oflag &= (~OPOST); // Control characters newtio.c_cc[VMIN] = 0; newtio.c_cc[VTIME] = 5; // no flow control newtio.c_cflag&=(~CRTSCTS); newtio.c_iflag&=(~(IXON|IXOFF|IXANY)); // Flushes the input and/or output queue. tcflush(fd, TCIFLUSH); // Make changes now without waiting for data to complete tcsetattr(fd, TCSANOW, &newtio); // Variables for Read/Write char cmd[3] = { 0x1B, 'I', 0x0D }; unsigned char buf[256]={0} ; char id[32]={0}, chs[2]={0}; int i=0, res; while(1) { bzero(id, sizeof(id)); fprintf(stdout, "[RFID] Reading ...\n"); // Send Request to RFID Reader write(fd, cmd, 3); // Read Response from RFID Reader res = read(fd, buf, 16); // Print the Response in Hex if(res > 0) { // Convert Response into Hex Format // For checking the Respones String for(i=0; i<res; i++) { sprintf(chs, "%02X", buf[i]); strcat(id, chs ); } fprintf(stdout, "[RFID] Card ID: "); fprintf(stdout, " %s\n", id); } sleep(3); } // Close DEV_RFID used. close(fd); fprintf(stdout, "\n[RFID] Closed\n"); // Restore old terminal settings tcsetattr(fd, TCSANOW, &oldtio); fprintf(stdout, "[RFID] ---------- End of Program ----------\n"); return 0; }$ ./myRFIDreader 1 [RFID] ----------- DEMO ------------------- [RFID] Connecting to /dev/ttyS1 [RFID] /dev/ttyS1 is connected [RFID] Reading ... [RFID] Card ID: 1B054661696C21 (Nothing read return value) [RFID] Reading ... [RFID] Card ID: 1B054661696C21 [RFID] Reading ... [RFID] Card ID: 1B0E0F3871C1CD0A (Part of Card 1) [RFID] Reading ... [RFID] Card ID: 8007E000003F038B (Part of Card 1) [RFID] Reading ... [RFID] Card ID: 1B0E0F3058C1CD0A8007E000003F038B (Card 1 No.) [RFID] Reading ... [RFID] Card ID: 1B054661696C21 [RFID] Reading ... [RFID] Card ID: 1B054661696C21 [RFID] Reading ... [RFID] Card ID: 1B0E0F3871C1CD0A8007E000003F038B (Card 2 No.) [RFID] Reading ... [RFID] Card ID: 1B054661696C21 [RFID] Reading ... [RFID] Card ID: 1B054661696C21 .
2009/06/21
RFID Reader - ttyS0
Refererence,
1. Serial Programming Guide for POSIX Operating Systems
2. Serial Programming HOWTO (Translated)
3. Summit RFID Reader
4. QextSerialPort ... a Qt Serial Class
Programmed as below,
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言