[go: up one dir, main page]

Menu

[0a098c]: / modules / fSio.cc  Maximize  Restore  History

Download this file

331 lines (281 with data), 5.7 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
// fSio.cpp
//
// C++ class wrapper for USB<-> Serial Driver.
//
//
#define USBDEVICE "/dev/usb/ttyUSB0"
#include <modules/fSio.h>
#include <string.h>
#include <errno.h>
#include <sys/poll.h>
#include <sys/ioctl.h>
// constructor
fSio::fSio(const char *dev_name)
{
error = 0;
fd = open(dev_name, O_RDWR | O_NOCTTY );
if (fd < 0)
{
perror(dev_name);
exit(-1);
}
bzero(&tio, sizeof(tio)); /* clear struct for new port settings */
/*
BAUDRATE: Set bps rate. You could also use cfsetispeed and cfsetospeed.
CRTSCTS : output hardware flow control (only used if the cable has
all necessary lines. See sect. 7 of Serial-HOWTO)
CS8 : 8n1 (8bit,no parity,1 stopbit)
CLOCAL : local connection, no modem contol
CREAD : enable receiving characters
*/
//tio.c_cflag |= (CLOCAL | CREAD);
//tio.c_cflag &= ~PARENB;
//tio.c_cflag &= ~CSTOPB;
//tio.c_cflag &= ~CSIZE;
//tio.c_cflag |= CS8;
//tio.c_cflag &= ~CRTSCTS;
//cfsetispeed(&tio, B9600);
//cfsetospeed(&tio, B9600);
//tio.c_lflag |= ICANON;
/*
initialize all control characters
default values can be found in /usr/include/termios.h
*/
tio.c_cc[VTIME] = 1; /* delay timer unused (time = 0.1s * VTIME) */
tio.c_cc[VMIN] = 0; /* blocking read until 1 character arrives or time expires */
/*
now clean the modem line and activate the settings for the port
*/
tcflush(fd, TCIFLUSH);
tcsetattr(fd, TCSANOW, &tio);
printf("USB Device open\n");
};
// destructor
fSio::~fSio(void)
{};
// Serial I/O (SIO) functions
int fSio::fSioReset(int Port, int InQueue, int OutQueue)
{
if (fd > 0)
{
tcflush(fd, TCIFLUSH);
tcsetattr(fd, TCSANOW, &tio);
return 1;
}
else
{
error = F_DEVICENOTOPEN;
return -1;
}
}
int fSio::fSioDone(int Port)
{
if (fd > 0)
{
close(fd);
return 1;
}
else
{
error = F_DEVICENOTOPEN;
return -1;
}
}
int fSio::fSioBaud(int Port, unsigned int BaudRate)
{
error = F_NOTIMPLEMENTED;
return -1;
}
int fSio::fSioPutc(int Port, char Byte)
{
char buf[2];
if (fd > 0)
{
buf[0] = Byte;
return write(fd, buf, 1);
}
else
{
error = F_DEVICENOTOPEN;
return -1;
}
}
int fSio::fSioPuts(int Port, char * Buffer, unsigned int Size)
{
if (fd > 0)
{
return write(fd, Buffer, Size);
}
else
{
error = F_DEVICENOTOPEN;
return -1;
}
}
unsigned int fSio::fSioGetc(int Port)
{
unsigned char value;
if (fd > 0)
{
if (read(fd, &value, 1) <= 0)
{
error = F_READERROR;
return -1;
}
else
{
return (unsigned int) value;
}
}
else
{
error = F_DEVICENOTOPEN;
return -1;
}
}
int fSio::fSioGets(int Port, char *Buffer, unsigned int Size)
{
unsigned int i = 0;
if (fd <= 0)
{
error = F_DEVICENOTOPEN;
return -1;
}
while (i < Size)
{
if (read(fd, (void *) &Buffer[i], 1) <= 0)
{
error = F_READERROR;
return -1;
}
i++;
}
return 0;
}
int fSio::fSioDTR(int Port, char Cmd)
{
error = F_NOTIMPLEMENTED;
return -1;
}
int fSio::fSioRTS(int Port, char Cmd)
{
error = F_NOTIMPLEMENTED;
return -1;
}
int fSio::fSioTxClear(int Port)
{
error = F_NOTIMPLEMENTED;
return -1;
}
int fSio::fSioRxClear(int Port)
{
error = F_NOTIMPLEMENTED;
return -1;
}
int fSio::fSioTxQue(int Port)
{
error = F_NOTIMPLEMENTED;
return -1;
}
int fSio::fSioRxQue(int Port)
{
error = F_NOTIMPLEMENTED;
return -1;
}
int fSio::fSioStatus(int Port, unsigned int Mask)
{
error = F_NOTIMPLEMENTED;
return -1;
}
int fSio::fSioFlow(int Port, char Cmd)
{
error = F_NOTIMPLEMENTED;
return -1;
}
int fSio::fSioParms(int Port, int Parity, int StopBits, int DataBits)
{
error = F_NOTIMPLEMENTED;
return -1;
}
int fSio::fSioCTS(int Port)
{
error = F_NOTIMPLEMENTED;
return -1;
}
int fSio::fSioDSR(int Port)
{
error = F_NOTIMPLEMENTED;
return -1;
}
int fSio::fSioRI(int Port)
{
error = F_NOTIMPLEMENTED;
return -1;
}
int fSio::fSioDebug(int Parm)
{
error = F_NOTIMPLEMENTED;
return -1;
}
int fSio::fSioInfo(char Parm)
{
error = F_NOTIMPLEMENTED;
return -1;
}
int fSio::fSioDCD(int Port)
{
error = F_NOTIMPLEMENTED;
return -1;
}
int fSio::fSioBrkSig(int Port, char Cmd)
{
error = F_NOTIMPLEMENTED;
return -1;
}
int fSio::fSioUnGetc(int Port, char Chr)
{
error = F_NOTIMPLEMENTED;
return -1;
}
int fSio::fSioWinError(char * Buffer, int Size)
{
error = F_NOTIMPLEMENTED;
return -1;
}
int fSio::fSioRead(int Port, int Reg)
{
error = F_NOTIMPLEMENTED;
return -1;
}
int fSio::fSioEvent(int Port, long Mask)
{
/*
Let's implement this as a call to poll...
In any case I interpret the Mask as the time out
for now...
*/
error = 0;
struct pollfd ufds =
{
fd, POLLIN | POLLPRI, 0
};
int rc = poll(&ufds, 1, Mask);
if ( ufds.revents & POLLNVAL)
error = F_DEVICENOTOPEN;
if ( ufds.revents & (POLLERR | POLLHUP))
error = F_READERROR;
if (!rc)
return 0;
else
{
return ufds.revents & (POLLIN | POLLPRI) ? 1 : 0;
}
error = F_NOTIMPLEMENTED;
return 1;
}
int fSio::fSioNleft(int Port)
{
int rc, nleft;
rc = ioctl(fd, FIONREAD, &nleft);
return ( rc < 0 ? rc : nleft);
}