rs_232 2.3.2.pre → 3.0.0.pre2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright (c) 2013, Ingenico Inc.
2
+ * Copyright (c) 2013, Roman Lishtaba.
3
3
  *
4
4
  * Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted,
5
5
  * provided that the above copyright notice and this permission notice appear in all copies.
@@ -12,43 +12,52 @@
12
12
  *
13
13
  **/
14
14
 
15
- #ifndef rs_232_initializer____FILEEXTENSION___
16
- #define rs_232_initializer____FILEEXTENSION___
17
-
18
- #if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
15
+ /*
16
+ * @author Roman Lishtaba
17
+ */
19
18
 
20
- # include "windows/port.h"
19
+ #pragma once
21
20
 
21
+ #ifdef __cplusplus
22
+ extern "C" {
23
+ #endif
24
+
25
+ #if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
26
+
27
+ # include "windows/Port.h"
28
+
22
29
  #else
23
-
24
- # include "posix/port.h"
25
-
30
+
31
+ # include "posix/Port.h"
32
+
26
33
  #endif
27
-
28
- #include "structs.h"
29
-
30
- VALUE initializeStruct(VALUE, VALUE);
31
-
32
- void updateSettings(PortDescriptor *port);
33
-
34
- VALUE isClosedIO(VALUE);
35
-
36
- VALUE openIO(VALUE);
37
-
38
- VALUE closeIO(VALUE);
39
-
40
- VALUE bytesAvailableIO(VALUE);
41
-
42
- VALUE flushIO(VALUE);
43
-
44
- VALUE writeIO(VALUE, VALUE);
45
-
46
- VALUE readIO(VALUE, VALUE);
47
-
48
- VALUE lineStatusIO(VALUE);
49
-
50
- VALUE setRtsIO(VALUE, VALUE);
51
-
52
- VALUE setDtrIO(VALUE, VALUE);
53
-
34
+
35
+ #include "Structs.h"
36
+
37
+ VALUE initializeStruct(VALUE, VALUE);
38
+
39
+ void updateSettings(PortDescriptor *port);
40
+
41
+ VALUE isOpenIO(VALUE);
42
+
43
+ VALUE openIO(VALUE);
44
+
45
+ VALUE closeIO(VALUE);
46
+
47
+ VALUE bytesAvailableIO(VALUE);
48
+
49
+ VALUE flushIO(VALUE);
50
+
51
+ VALUE writeIO(VALUE, VALUE);
52
+
53
+ VALUE readIO(VALUE, VALUE);
54
+
55
+ VALUE lineStatusIO(VALUE);
56
+
57
+ VALUE setRtsIO(VALUE, VALUE);
58
+
59
+ VALUE setDtrIO(VALUE, VALUE);
60
+
61
+ #ifdef __cplusplus
62
+ }
54
63
  #endif
@@ -0,0 +1,63 @@
1
+ /*
2
+ * Copyright (c) 2013, Roman Lishtaba.
3
+ *
4
+ * Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted,
5
+ * provided that the above copyright notice and this permission notice appear in all copies.
6
+ *
7
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
8
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
9
+ * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
10
+ * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
11
+ * PERFORMANCE OF THIS SOFTWARE.
12
+ *
13
+ **/
14
+
15
+ /*
16
+ * @author Roman Lishtaba
17
+ */
18
+
19
+ #pragma once
20
+
21
+ #ifdef __cplusplus
22
+ extern "C" {
23
+ #endif
24
+
25
+ #include "Constants.h"
26
+
27
+ /*
28
+ * structure to contain port settings
29
+ */
30
+ typedef struct PortSettings_T
31
+ {
32
+ char ComPort[40];
33
+ enum BaudRateType BaudRate;
34
+ enum DataBitsType DataBits;
35
+ enum ParityType Parity;
36
+ enum StopBitsType StopBits;
37
+ enum FlowType FlowControl;
38
+ long Timeout_Millisec;
39
+ } PortSettings;
40
+
41
+
42
+ /*
43
+ * port descriptor structure
44
+ */
45
+ typedef struct portDescriptor_T
46
+ {
47
+ #if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
48
+ HANDLE fd;
49
+ COMMCONFIG commConfig;
50
+ COMMTIMEOUTS commTimeouts;
51
+ #else
52
+ int fd;
53
+ struct termios posixConfig;
54
+ struct termios previousPosixConfig;
55
+ #endif
56
+ int status;
57
+ PortSettings settings;
58
+ int toBeUpdated;
59
+ } PortDescriptor;
60
+
61
+ #ifdef __cplusplus
62
+ }
63
+ #endif
@@ -1,13 +1,16 @@
1
1
  require 'mkmf'
2
2
  require 'rbconfig'
3
3
 
4
- if ENV['DEBUG_C']
4
+ EXTENSION_NAME = 'rs_232_native'.freeze
5
+ dir_config(EXTENSION_NAME)
6
+
7
+ if ENV['DEBUG']
5
8
  $CFLAGS << ' -DDEBUG'
6
- $CFLAGS << ' -g -O'
9
+ $CFLAGS << ' -g'
7
10
  $stdout.puts "compiling in debug mode... flags: #{$CFLAGS}"
8
11
  end
9
12
 
10
- dir_config('rs_232')
13
+ $CFLAGS << ' -Wall -g'
11
14
 
12
15
  $warnflags = '-Wall'
13
16
 
@@ -30,6 +33,7 @@ OS = case RbConfig::CONFIG['host_os'].downcase
30
33
 
31
34
  have_header('ruby.h')
32
35
  have_header('stdio.h')
36
+ have_library( 'stdc++' )
33
37
 
34
38
  if OS == 'windows'
35
39
  $VPATH << '$(srcdir)/windows'
@@ -49,10 +53,10 @@ elsif %w(linux darwin).include? OS
49
53
  have_header('errno.h')
50
54
  have_header('sys/ioctl.h')
51
55
  else
52
- fail "RS-233 implementation is not tested for this #{OS} platform."
56
+ fail "RS-233 implementation wasn't been tested for #{OS} platform."
53
57
  end
54
58
 
55
- $objs = %w(constants.o port.o initializer.o)
59
+ $objs = %w(Constants.o Port.o Rs232.o).freeze
56
60
 
57
61
  $CFLAGS += " -DOS_#{OS.upcase}"
58
62
 
@@ -62,4 +66,4 @@ Extending with: #{$CFLAGS}
62
66
 
63
67
  MSG
64
68
 
65
- create_makefile('rs_232')
69
+ create_makefile(EXTENSION_NAME)
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright (c) 2013, Ingenico Inc.
2
+ * Copyright (c) 2013, Roman Lishtaba.
3
3
  *
4
4
  * Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted,
5
5
  * provided that the above copyright notice and this permission notice appear in all copies.
@@ -12,66 +12,57 @@
12
12
  *
13
13
  **/
14
14
 
15
- #include "port.h"
15
+ /*
16
+ * @author Roman Lishtaba
17
+ */
16
18
 
17
- VALUE setDtrIO(VALUE self, VALUE rb_int)
18
- {
19
- {
20
- Check_Type(rb_int, T_FIXNUM);
21
- }
19
+ #include "Port.h"
22
20
 
23
- int boolean = FIX2INT(rb_int);
24
21
 
22
+ VALUE setDtrIO(VALUE self, VALUE rb_int)
23
+ {
24
+ Check_Type(rb_int, T_FIXNUM);
25
+ const int boolean = FIX2INT(rb_int);
26
+ int status;
25
27
  PortDescriptor *port = NULL;
26
-
27
28
  Data_Get_Struct(self, PortDescriptor, port);
28
-
29
- int status;
29
+
30
30
  ioctl(port->fd, TIOCMGET, &status);
31
31
  if (boolean == 1)
32
32
  status |= TIOCM_DTR;
33
33
  else
34
34
  status &= ~TIOCM_DTR;
35
35
  ioctl(port->fd, TIOCMSET, &status);
36
-
36
+
37
37
  return INT2FIX(status);
38
-
39
38
  }
40
39
 
41
40
 
42
41
  VALUE setRtsIO(VALUE self, VALUE rb_int)
43
42
  {
44
- {
45
- Check_Type(rb_int, T_FIXNUM);
46
- }
47
-
48
- int boolean = FIX2INT(rb_int);
49
-
43
+ Check_Type(rb_int, T_FIXNUM);
44
+ const int boolean = FIX2INT(rb_int);
50
45
  PortDescriptor *port = NULL;
51
-
52
46
  Data_Get_Struct(self, PortDescriptor, port);
53
-
54
47
  int status;
48
+
55
49
  ioctl(port->fd, TIOCMGET, &status);
56
50
  if (boolean == 1)
57
51
  status |= TIOCM_RTS;
58
52
  else
59
- status &=
60
- ~TIOCM_RTS;
53
+ status &= ~TIOCM_RTS;
61
54
  ioctl(port->fd, TIOCMSET, &status);
62
-
55
+
63
56
  return INT2FIX(status);
64
57
  }
65
58
 
66
59
 
67
60
  VALUE lineStatusIO(VALUE self)
68
61
  {
69
-
70
62
  PortDescriptor *port = NULL;
71
-
72
63
  Data_Get_Struct(self, PortDescriptor, port);
73
-
74
64
  unsigned long Status = 0, Temp = 0;
65
+
75
66
  ioctl(port->fd, TIOCMGET, &Temp);
76
67
  if (Temp & TIOCM_CTS) Status |= LS_CTS;
77
68
  if (Temp & TIOCM_DSR) Status |= LS_DSR;
@@ -81,7 +72,7 @@ VALUE lineStatusIO(VALUE self)
81
72
  if (Temp & TIOCM_RTS) Status |= LS_RTS;
82
73
  if (Temp & TIOCM_ST ) Status |= LS_ST;
83
74
  if (Temp & TIOCM_SR ) Status |= LS_SR;
84
-
75
+
85
76
  return LONG2FIX(Status);
86
77
  }
87
78
 
@@ -89,7 +80,7 @@ VALUE lineStatusIO(VALUE self)
89
80
  static void platformInitIO(PortDescriptor *port)
90
81
  {
91
82
  port->fd = -1;
92
- port->status = 1;
83
+ port->status = PORT_CLOSED;
93
84
  }
94
85
 
95
86
 
@@ -107,19 +98,18 @@ static void setPosixBaudRate(PortDescriptor *port)
107
98
 
108
99
  void updateSettings(PortDescriptor *port)
109
100
  {
110
-
111
- if (port->status != 0)
101
+ if (PORT_OPEN != port->status)
112
102
  rb_raise(rb_eException, "Can not set due to comport is not open, status: %d\n", port->status);
113
-
103
+
114
104
  /*
115
- * BaudRate clause ************************************************
116
- */
105
+ * BaudRate clause
106
+ */
117
107
  if (port->toBeUpdated & T_BaudRate)
118
108
  setPosixBaudRate(port);
119
-
109
+
120
110
  /*
121
- * Parity clause ************************************************
122
- */
111
+ * Parity clause
112
+ */
123
113
  if (port->toBeUpdated & T_Parity)
124
114
  {
125
115
  switch (port->settings.Parity)
@@ -136,11 +126,10 @@ void updateSettings(PortDescriptor *port)
136
126
  break;
137
127
  }
138
128
  }
139
-
129
+
140
130
  /*
141
- * Data Bits clause ************************************************
142
- */
143
-
131
+ * Data Bits clause
132
+ */
144
133
  if (port->toBeUpdated & T_DataBits)
145
134
  {
146
135
  port->posixConfig.c_cflag &= (~CSIZE);
@@ -160,11 +149,11 @@ void updateSettings(PortDescriptor *port)
160
149
  break;
161
150
  }
162
151
  }
163
-
152
+
164
153
  /*
165
- * StopBits clause ************************************************
166
- */
167
-
154
+ * StopBits clause
155
+ */
156
+
168
157
  if (port->toBeUpdated & T_StopBits)
169
158
  {
170
159
  switch (port->settings.StopBits)
@@ -177,11 +166,11 @@ void updateSettings(PortDescriptor *port)
177
166
  break;
178
167
  }
179
168
  }
180
-
169
+
181
170
  /*
182
- * FlowControl clause ************************************************
183
- */
184
-
171
+ * FlowControl clause
172
+ */
173
+
185
174
  if (port->toBeUpdated & T_Flow)
186
175
  {
187
176
  switch (port->settings.FlowControl)
@@ -201,25 +190,25 @@ void updateSettings(PortDescriptor *port)
201
190
  break;
202
191
  }
203
192
  }
204
-
205
-
193
+
194
+
206
195
  /*
207
- * In case of update flush IO
208
- */
209
-
196
+ * In case of update flush IO
197
+ */
198
+
210
199
  if (port->toBeUpdated & T_SettingsDone)
211
200
  {
212
201
  tcsetattr(port->fd, TCSAFLUSH, &port->posixConfig);
213
202
  }
214
-
203
+
215
204
  /*
216
- * Timeout clause ************************************************
217
- */
218
-
205
+ * Timeout clause
206
+ */
207
+
219
208
  if (port->toBeUpdated & T_TimeOut)
220
209
  {
221
210
  int timeout = port->settings.Timeout_Millisec;
222
-
211
+
223
212
  if (timeout == -1)
224
213
  {
225
214
  fcntl(port->fd, F_SETFL, O_NDELAY); /* O_NDELAY should release if no any data here */
@@ -230,148 +219,116 @@ void updateSettings(PortDescriptor *port)
230
219
  }
231
220
  tcgetattr(port->fd, &port->posixConfig);
232
221
  port->posixConfig.c_cc[VTIME] = (timeout / 100);
233
-
222
+
234
223
  tcsetattr(port->fd, TCSAFLUSH, &port->posixConfig);
235
224
  }
236
-
225
+
237
226
  port->toBeUpdated = 0;
238
-
239
227
  }
240
228
 
241
229
 
242
230
  int queryStatusIO(VALUE self)
243
231
  {
244
232
  PortDescriptor *port = NULL;
245
-
246
233
  Data_Get_Struct(self, PortDescriptor, port);
247
-
234
+
248
235
  return port->status;
249
236
  }
250
237
 
251
238
 
252
- VALUE isClosedIO(VALUE self)
239
+ VALUE isOpenIO(VALUE self)
253
240
  {
254
- return queryStatusIO(self) == 1 ? Qtrue : Qfalse;
241
+ return queryStatusIO(self) == PORT_OPEN ? Qtrue : Qfalse;
255
242
  }
256
243
 
257
244
 
258
245
  VALUE flushIO(VALUE self)
259
246
  {
260
247
  PortDescriptor *port = NULL;
261
-
262
248
  Data_Get_Struct(self, PortDescriptor, port);
263
-
249
+
264
250
  return INT2FIX( tcdrain(port->fd) );
265
251
  }
266
252
 
267
253
 
268
254
  VALUE bytesAvailableIO(VALUE self)
269
255
  {
270
-
271
256
  PortDescriptor *port = NULL;
272
-
273
257
  Data_Get_Struct(self, PortDescriptor, port);
274
-
275
258
  int bytesQueued;
276
-
259
+
277
260
  if (ioctl(port->fd, FIONREAD, &bytesQueued) == -1)
278
- {
279
261
  return INT2FIX(0);
280
- }
262
+
281
263
  return INT2FIX(bytesQueued);
282
264
  }
283
265
 
284
266
 
285
267
  VALUE openIO(VALUE self)
286
268
  {
287
-
288
269
  PortDescriptor *port = NULL;
289
-
290
270
  Data_Get_Struct(self, PortDescriptor, port);
291
-
292
- if ((port->fd = open(port->settings.ComPort, O_RDWR | O_NOCTTY | O_SYNC | O_NDELAY)) != -1)
271
+
272
+ if ((port->fd = open(port->settings.ComPort, O_RDWR | O_NOCTTY | O_NDELAY)) != -1)
293
273
  {
294
-
295
- port->status = 0;
296
-
297
- {
298
- rb_iv_set(self, "@open", INT2FIX(port->status));
299
- }
300
-
274
+ port->status = PORT_OPEN;
275
+ rb_iv_set(self, "@open", INT2FIX(port->status));
276
+
301
277
  tcgetattr(port->fd, &port->previousPosixConfig);
302
278
  port->posixConfig = port->previousPosixConfig;
303
279
  cfmakeraw(&port->posixConfig);
304
-
305
-
280
+
306
281
  port->posixConfig.c_cflag |= CREAD | CLOCAL;
307
- port->posixConfig.c_lflag &= (~(ICANON|ECHO|ECHOE|ECHOK|ECHONL|ISIG));
308
- port->posixConfig.c_iflag &= (~(INPCK|IGNPAR|PARMRK|ISTRIP|ICRNL|IXANY));
282
+ port->posixConfig.c_lflag &= (~(ICANON| ECHO| ECHOE| ECHOK| ECHONL| ISIG));
283
+ port->posixConfig.c_iflag &= (~(INPCK| IGNPAR| PARMRK| ISTRIP| ICRNL| IXANY));
309
284
  port->posixConfig.c_oflag &= (~OPOST);
310
285
  port->posixConfig.c_cc[VMIN] = 0;
311
286
  port->posixConfig.c_cc[VTIME] = 0;
312
-
287
+
313
288
  port->toBeUpdated = T_ALL;
314
-
289
+
315
290
  setSettings(self);
316
-
317
291
  } else
318
292
  {
319
- port->status = 1;
320
- rb_raise(rb_eException, "Unable to open comport: %s\n", port->settings.ComPort);
293
+ port->status = PORT_CLOSED;
294
+ rb_raise(rb_eRuntimeError, "Unable to open comport: `%s`", port->settings.ComPort);
321
295
  }
322
-
323
- return INT2FIX(port->status);
296
+
297
+ return self;
324
298
  }
325
299
 
326
300
 
327
301
  VALUE writeIO(VALUE self, VALUE message)
328
302
  {
329
-
330
- {
331
- Check_Type(message, T_STRING);
332
- }
333
-
334
- int recv;
335
- int len;
336
-
303
+ Check_Type(message, T_STRING);
337
304
  PortDescriptor *port = NULL;
338
-
339
305
  Data_Get_Struct(self, PortDescriptor, port);
340
-
341
- len = (int) RSTRING_LEN(message);
306
+
307
+ const int len = (int) RSTRING_LEN(message);
342
308
  char cStr[len];
343
309
  strcpy(cStr, RSTRING_PTR(message));
344
-
345
- recv = (int) write(port->fd, cStr, (size_t) sizeof(cStr));
346
-
347
- if (recv < 0){
310
+
311
+ const int recv = (int) write(port->fd, cStr, (size_t) sizeof(cStr));
312
+
313
+ if (recv < 0)
348
314
  rb_raise(rb_eEOFError, "TX: writing of the %d bytes has failed", len);
349
- }
350
-
315
+
351
316
  return INT2FIX(recv);
352
317
  }
353
318
 
354
319
 
355
320
  VALUE readIO(VALUE self, VALUE rb_int)
356
321
  {
357
-
358
- {
359
- Check_Type(rb_int, T_FIXNUM);
360
- }
361
-
322
+ Check_Type(rb_int, T_FIXNUM);
362
323
  int recv;
363
-
364
324
  PortDescriptor *port = NULL;
365
-
366
325
  Data_Get_Struct(self, PortDescriptor, port);
367
-
368
- int len = FIX2INT(rb_int);
369
-
326
+ const int len = FIX2INT(rb_int);
370
327
  char in[len];
371
-
328
+
372
329
  recv = (int) read(port->fd, in, sizeof(in));
373
330
  if (recv > 0)
374
- return rb_str_new(in, recv);
331
+ return rb_str_new(in, recv);
375
332
 
376
333
  return Qnil;
377
334
  }
@@ -379,51 +336,38 @@ VALUE readIO(VALUE self, VALUE rb_int)
379
336
 
380
337
  VALUE closeIO(VALUE self)
381
338
  {
382
-
383
- int closed;
384
-
385
339
  PortDescriptor *port = NULL;
386
-
387
340
  Data_Get_Struct(self, PortDescriptor, port);
388
-
389
341
  flushIO(self);
390
-
391
342
  tcsetattr(port->fd, TCSAFLUSH | TCSANOW, &port->previousPosixConfig);
392
- closed = close(port->fd);
393
-
394
- port->status = closed == 0 ? 1 : 0;
395
-
343
+
344
+ const int state = close(port->fd);
345
+
346
+ port->status = (state == 0 ? PORT_CLOSED : PORT_OPEN);
347
+
396
348
  rb_iv_set(self, "@open", INT2FIX(port->status));
397
349
  rb_iv_set(self, "@fd", INT2FIX(port->fd));
398
-
399
- return INT2FIX(closed);
400
-
350
+
351
+ return INT2FIX(state);
401
352
  }
402
353
 
403
354
 
404
355
  VALUE initializeStruct(VALUE self, VALUE portName)
405
356
  {
406
-
407
- {
408
- Check_Type(portName, T_STRING);
409
- }
410
-
357
+ Check_Type(portName, T_STRING);
411
358
  PortDescriptor *port = NULL;
412
-
413
359
  Data_Get_Struct(self, PortDescriptor, port);
414
-
360
+
415
361
  port->settings.BaudRate = BAUD115200;
416
362
  port->settings.Parity = PAR_NONE;
417
363
  port->settings.FlowControl = FLOW_OFF;
418
364
  port->settings.DataBits = DATA_8;
419
365
  port->settings.StopBits = STOP_1;
420
366
  port->settings.Timeout_Millisec = 0;
421
-
367
+
422
368
  strcpy(port->settings.ComPort, RSTRING_PTR(portName));
423
-
424
369
  platformInitIO(port);
425
-
426
370
  rb_iv_set(self, "@port", portName);
427
-
371
+
428
372
  return self;
429
- }
373
+ }