Robot Agent  1.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
rfid.c
Go to the documentation of this file.
1 
12 /* -- Includes -- */
13 /* system libraries */
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h> /* String function definitions */
17 #include <unistd.h> /* UNIX standard function definitions */
18 #include <sys/time.h>
19 #include <sys/select.h>
20 /* project libraries */
21 #include "config.h"
22 #include "def.h"
23 #include "serialport.h"
24 #include "rfid.h"
25 
26 /* -- Defines -- */
27 
28 /* -- Functions -- */
29 
35 rfid_t *rfid_open(char *device_path)
36 {
37  // RFID structure
38  rfid_t *rfids = (rfid_t *) malloc(sizeof(rfid_t));
39 
40  // Open serial port
41  rfids->sps = serialport_open(device_path);
42  // Configure serial port
43  serialport_config(rfids->sps, 2400);
44 
45  return rfids;
46 }
47 
53 int rfid_close(rfid_t *rfids)
54 {
55  int res;
56 
57  // Close serial port
58  res = serialport_close(rfids->sps);
59  // Free memory
60  free(rfids);
61 
62  return res;
63 }
64 
71 {
72  char buffer[32];
73  char *bufptr; // Current char in buffer
74  int nbytes, tbytes; // Number of bytes read
75 
76  // Read characters into buffer until we get a CR or NL
77  bufptr = buffer;
78 
80 
81  while ((nbytes = (int)read(rfids->sps->descriptor, bufptr, 1)) > 0)
82  {
83  // Wait for start byte -> "NL"
84  if(*bufptr == '\n') {
85  //printf("__NL");
86  tbytes = 0;
87  }
88  // If "CR" received then check if 10 bytes collected if yes -> finished
89  else if(*bufptr == '\r') {
90  //printf("CR");
91  *bufptr = '\0';
92  if (tbytes == 10) {
93  break;
94  }
95  }
96  // Collect bytes
97  else {
98  //printf("%s : %d\n",buffer,tbytes);
99  // Check overflow
100  if (tbytes < 11) {
101  tbytes += nbytes;
102  bufptr += nbytes;
103  }
104  else
105  {
106  break;
107  }
108  }
109  }
110 
111  //Copy ID to structure
112  strncpy(rfids->last_id, buffer, 11);
113 
115 
116  return 0;
117 }
118 
124 int rfid_read(rfid_t *rfids)
125 {
126  char buffer[32];
127  char *bufptr; // Current char in buffer
128  int nbytes, tbytes, n; // Number of bytes read
129 
130  // Prepare select()
131  struct timeval tv;
132  tv.tv_sec = 0;
133  tv.tv_usec = 200000;
134  fd_set infds;
135  FD_ZERO(&infds);
136  FD_SET(rfids->sps->descriptor, &infds);
137 
138  struct timeval tv2;
139  tv2.tv_sec = 0;
140  tv2.tv_usec = 10000;
141  fd_set infds2;
142  FD_ZERO(&infds2);
143  FD_SET(rfids->sps->descriptor, &infds2);
144 
145  // Read characters into buffer until we get a CR or NL
146  bufptr = buffer;
147 
148  if((n = select(rfids->sps->descriptor + 1, &infds2, NULL, NULL, &tv2)) > 0)
149  {
150  //while((n = select(rfids->sps.descriptor + 1, &infds, NULL, NULL, &tv)) > 0)
151  while ((nbytes = (int)read(rfids->sps->descriptor, bufptr, 1)) > 0)
152  {
153  //nbytes = (int)read(rfids->sps.descriptor, bufptr, 1);
154 
155  // Wait for start byte -> "NL"
156  if(*bufptr == '\n') {
157  tbytes = 0;
158  }
159  // If "CR" received then check if 10 bytes collected if yes -> finished
160  else if(*bufptr == '\r') {
161  *bufptr = '\0';
162  if (tbytes == 10) {
163  strncpy(rfids->id, buffer, 11);
164  strncpy(rfids->last_id, buffer, 11);
165 
166  // Flush input buffer
167  //printf("FLUSHED: %d\n", serialport_flush_input(rfids->sps));
168 
169  return 0;
170  }
171  }
172  // Collect bytes
173  else {
174  //printf("%s : %d\n",buffer,tbytes);
175  // Check overflow
176  if (tbytes < 11) {
177  tbytes += nbytes;
178  bufptr += nbytes;
179  }
180  else
181  {
182  break;
183  }
184  }
185  }
186  }
187  else
188  {
189  //Copy ID to structure
190  strncpy(rfids->id, s_CONFIG_RFID_EMPTY_TAG, 11);
191  }
192 
194 
195  return 0;
196 }
197 
int rfid_read(rfid_t *rfids)
Definition: rfid.c:124
int rfid_read_locked(rfid_t *rfids)
Definition: rfid.c:70
char last_id[11]
Definition: rfid.h:28
rfid_t * rfid_open(char *device_path)
Definition: rfid.c:35
char id[11]
Definition: rfid.h:27
serialport_t * serialport_open(const char *name)
Definition: serialport.c:33
#define s_CONFIG_RFID_EMPTY_TAG
Definition: config.h:83
serialport_t * sps
Definition: rfid.h:26
RFID structure.
Definition: rfid.h:24
int serialport_config(serialport_t *sps, unsigned int baud)
Definition: serialport.c:82
int serialport_close(serialport_t *sps)
Definition: serialport.c:58
int rfid_close(rfid_t *rfids)
Definition: rfid.c:53