rs_232 2.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,399 @@
1
+ /*
2
+ * Copyright (c) 2013, Ingenico Inc.
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
+ #include "initializer.h"
16
+
17
+ static void destructor(PortDescriptor *port)
18
+ {
19
+ xfree(port);
20
+ }
21
+
22
+
23
+ static void markDescriptor(PortDescriptor *port)
24
+ {
25
+ }
26
+
27
+
28
+ static VALUE allocateStruct(VALUE self)
29
+ {
30
+ PortDescriptor *port;
31
+
32
+ rb_iv_set(self, "@port", Qnil);
33
+
34
+ return Data_Make_Struct(self, PortDescriptor, markDescriptor, destructor, port);
35
+ }
36
+
37
+
38
+ void setBaudRate(VALUE self, VALUE int_baudRate)
39
+ {
40
+ PortDescriptor *port;
41
+
42
+ {
43
+ Check_Type(int_baudRate, T_FIXNUM);
44
+ }
45
+
46
+ int baudRate = FIX2INT(int_baudRate);
47
+
48
+ Data_Get_Struct(self, PortDescriptor, port);
49
+
50
+ int baud = 110;
51
+
52
+ switch (baudRate)
53
+ {
54
+ case BAUD110:
55
+ baud = 110;
56
+ break;
57
+ case BAUD300:
58
+ baud = 300;
59
+ break;
60
+ case BAUD1200:
61
+ baud = 1200;
62
+ break;
63
+ case BAUD2400:
64
+ baud = 2400;
65
+ break;
66
+ case BAUD4800:
67
+ baud = 4800;
68
+ break;
69
+ case BAUD9600:
70
+ baud = 9600;
71
+ break;
72
+ case BAUD38400:
73
+ baud = 38400;
74
+ break;
75
+ case BAUD57600:
76
+ baud = 57600;
77
+ break;
78
+ case BAUD115200:
79
+ baud = 115200;
80
+ break;
81
+ case BAUD19200:
82
+ baud = 19200;
83
+ break;
84
+ default:
85
+ rb_raise(rb_eException, "BaudRate value does not match specification, current: %d", baudRate);
86
+ break;
87
+ }
88
+
89
+ port->settings.BaudRate = (enum BaudRateType) baud;
90
+ port->toBeUpdated |= T_BaudRate;
91
+
92
+ updateSettings(port);
93
+ }
94
+
95
+
96
+ VALUE getBaudRate(VALUE self)
97
+ {
98
+ PortDescriptor *port;
99
+
100
+ Data_Get_Struct(self, PortDescriptor, port);
101
+
102
+ return INT2FIX(port->settings.BaudRate);
103
+ }
104
+
105
+
106
+ void setParity(VALUE self, VALUE int_parity)
107
+ {
108
+
109
+ {
110
+ Check_Type(int_parity, T_FIXNUM);
111
+ }
112
+
113
+ int parity = FIX2INT(int_parity);
114
+
115
+ PortDescriptor *port;
116
+
117
+ Data_Get_Struct(self, PortDescriptor, port);
118
+
119
+ enum ParityType parity_type = PAR_NONE;
120
+
121
+ switch (parity)
122
+ {
123
+ case PAR_NONE:
124
+ parity_type = PAR_NONE;
125
+ break;
126
+ case PAR_ODD:
127
+ parity_type = PAR_ODD;
128
+ break;
129
+ case PAR_EVEN:
130
+ parity_type = PAR_EVEN;
131
+ break;
132
+ default:
133
+ rb_raise(rb_eException, "Parity value does not match specification, current: %d", parity);
134
+ break;
135
+ }
136
+
137
+ port->settings.Parity = parity_type;
138
+ port->toBeUpdated |= T_Parity;
139
+
140
+ updateSettings(port);
141
+ }
142
+
143
+
144
+ VALUE getParity(VALUE self)
145
+ {
146
+
147
+ PortDescriptor *port;
148
+
149
+ Data_Get_Struct(self, PortDescriptor, port);
150
+
151
+ return INT2FIX(port->settings.Parity);
152
+ }
153
+
154
+
155
+ void setDataBits(VALUE self, VALUE intDataBits)
156
+ {
157
+
158
+ {
159
+ Check_Type(intDataBits, T_FIXNUM);
160
+ }
161
+
162
+ int dataBits = FIX2INT(intDataBits);
163
+
164
+ PortDescriptor *port;
165
+
166
+ Data_Get_Struct(self, PortDescriptor, port);
167
+
168
+ enum DataBitsType dataBitsType = DATA_8;
169
+
170
+ switch (dataBits)
171
+ {
172
+ case DATA_5:
173
+ dataBitsType = DATA_5;
174
+ break;
175
+ case DATA_6:
176
+ dataBitsType = DATA_6;
177
+ break;
178
+ case DATA_7:
179
+ dataBitsType = DATA_7;
180
+ break;
181
+ case DATA_8:
182
+ dataBitsType = DATA_8;
183
+ break;
184
+ default:
185
+ rb_raise(rb_eException, "DataBits value does not match specification, current: %d", dataBits);
186
+ break;
187
+ }
188
+
189
+ port->settings.DataBits = dataBitsType;
190
+ port->toBeUpdated |= T_DataBits;
191
+
192
+ updateSettings(port);
193
+ }
194
+
195
+
196
+ VALUE getDataBits(VALUE self)
197
+ {
198
+
199
+ PortDescriptor *port;
200
+
201
+ Data_Get_Struct(self, PortDescriptor, port);
202
+
203
+ return INT2FIX(port->settings.DataBits);
204
+ }
205
+
206
+
207
+ void setStopBits(VALUE self, VALUE intStopBits)
208
+ {
209
+
210
+ {
211
+ Check_Type(intStopBits, T_FIXNUM);
212
+ }
213
+
214
+ int stopBits = FIX2INT(intStopBits);
215
+
216
+ PortDescriptor *port;
217
+
218
+ Data_Get_Struct(self, PortDescriptor, port);
219
+
220
+ enum StopBitsType stopBitsType = STOP_1;
221
+
222
+ switch (stopBits)
223
+ {
224
+ case STOP_1:
225
+ stopBitsType = STOP_1;
226
+ break;
227
+ case STOP_2:
228
+ stopBitsType = STOP_2;
229
+ break;
230
+ default:
231
+ rb_raise(rb_eException, "StopBits value does not match specification, current: %d", stopBits);
232
+ break;
233
+ }
234
+
235
+ port->settings.StopBits = stopBitsType;
236
+ port->toBeUpdated |= T_StopBits;
237
+
238
+ updateSettings(port);
239
+ }
240
+
241
+
242
+ VALUE getStopBits(VALUE self)
243
+ {
244
+
245
+ PortDescriptor *port;
246
+
247
+ Data_Get_Struct(self, PortDescriptor, port);
248
+
249
+ return INT2FIX(port->settings.StopBits);
250
+ }
251
+
252
+
253
+ void setFlowControl(VALUE self, VALUE intFlow)
254
+ {
255
+
256
+ {
257
+ Check_Type(intFlow, T_FIXNUM);
258
+ }
259
+
260
+ int flow = FIX2INT(intFlow);
261
+
262
+ PortDescriptor *port;
263
+
264
+ Data_Get_Struct(self, PortDescriptor, port);
265
+
266
+ enum FlowType flowType = FLOW_OFF;
267
+
268
+ switch (flow)
269
+ {
270
+ case FLOW_OFF:
271
+ flowType = FLOW_OFF;
272
+ break;
273
+ case FLOW_HARDWARE:
274
+ flowType = FLOW_HARDWARE;
275
+ break;
276
+ case FLOW_XONXOFF:
277
+ flowType = FLOW_XONXOFF;
278
+ break;
279
+ default:
280
+ rb_raise(rb_eException, "FlowControl value does not match specification, current: %d", flow);
281
+ break;
282
+ }
283
+
284
+ port->settings.FlowControl = flowType;
285
+ port->toBeUpdated |= T_StopBits;
286
+
287
+ updateSettings(port);
288
+ }
289
+
290
+
291
+ VALUE getFlowControl(VALUE self)
292
+ {
293
+
294
+ PortDescriptor *port;
295
+
296
+ Data_Get_Struct(self, PortDescriptor, port);
297
+
298
+ return INT2FIX(port->settings.FlowControl);
299
+ }
300
+
301
+
302
+ void setTimeout(VALUE self, VALUE rb_timeout)
303
+ {
304
+
305
+ {
306
+ Check_Type(rb_timeout, T_FIXNUM);
307
+ }
308
+
309
+ PortDescriptor *port;
310
+
311
+ Data_Get_Struct(self, PortDescriptor, port);
312
+
313
+ long timeout = FIX2LONG(rb_timeout);
314
+
315
+ port->settings.Timeout_Millisec = timeout;
316
+ port->toBeUpdated |= T_TimeOut;
317
+
318
+ updateSettings(port);
319
+ }
320
+
321
+
322
+ VALUE getTimeout(VALUE self)
323
+ {
324
+
325
+ PortDescriptor *port;
326
+
327
+ Data_Get_Struct(self, PortDescriptor, port);
328
+
329
+ return INT2FIX(port->settings.Timeout_Millisec);
330
+ }
331
+
332
+
333
+ void setSettings(VALUE self)
334
+ {
335
+
336
+ PortDescriptor *port;
337
+
338
+ Data_Get_Struct(self, PortDescriptor, port);
339
+
340
+ setBaudRate(self, INT2FIX(port->settings.BaudRate));
341
+ setParity(self, INT2FIX(port->settings.Parity));
342
+ setDataBits(self, INT2FIX(port->settings.DataBits));
343
+ setStopBits(self, INT2FIX(port->settings.StopBits));
344
+ setFlowControl(self, INT2FIX(port->settings.FlowControl));
345
+ setTimeout(self, LONG2FIX(port->settings.Timeout_Millisec));
346
+
347
+ port->toBeUpdated = T_ALL;
348
+
349
+ updateSettings(port);
350
+ }
351
+
352
+
353
+ void Init_rs_232(void)
354
+ {
355
+
356
+
357
+ VALUE root, rb_cPort;
358
+
359
+ root = rb_define_module("CommPort");
360
+
361
+ Constants_Init(root);
362
+
363
+ rb_cPort = rb_define_class_under(root, "Rs232", rb_cObject);
364
+ rb_define_alloc_func(rb_cPort, allocateStruct);
365
+ rb_define_method(rb_cPort, "initialize", (VALUE ( *)()) initializeStruct, 1);
366
+
367
+ rb_define_method(rb_cPort, "baud_rate=", (VALUE ( *)()) setBaudRate, 1);
368
+ rb_define_method(rb_cPort, "baud_rate", (VALUE ( *)()) getBaudRate, 0);
369
+
370
+ rb_define_method(rb_cPort, "parity=", (VALUE ( *)()) setParity, 1);
371
+ rb_define_method(rb_cPort, "parity", (VALUE ( *)()) getParity, 0);
372
+
373
+ rb_define_method(rb_cPort, "data_bits=", (VALUE ( *)()) setDataBits, 1);
374
+ rb_define_method(rb_cPort, "data_bits", (VALUE ( *)()) getDataBits, 0);
375
+
376
+ rb_define_method(rb_cPort, "stop_bits=", (VALUE ( *)()) setStopBits, 1);
377
+ rb_define_method(rb_cPort, "stop_bits", (VALUE ( *)()) getStopBits, 0);
378
+
379
+ rb_define_method(rb_cPort, "flow_control=", (VALUE ( *)()) setFlowControl, 1);
380
+ rb_define_method(rb_cPort, "flow_control", (VALUE ( *)()) getFlowControl, 0);
381
+
382
+ rb_define_method(rb_cPort, "timeout=", (VALUE ( *)()) setTimeout, 1);
383
+ rb_define_method(rb_cPort, "timeout", (VALUE ( *)()) getTimeout, 0);
384
+
385
+ rb_define_method(rb_cPort, "open", (VALUE ( *)()) openIO, 0);
386
+ rb_define_method(rb_cPort, "close", (VALUE ( *)()) closeIO, 0);
387
+ rb_define_method(rb_cPort, "flush", (VALUE ( *)()) flushIO, 0);
388
+ rb_define_method(rb_cPort, "closed?", (VALUE ( *)()) isClosedIO, 0);
389
+
390
+
391
+ rb_define_method(rb_cPort, "write", (VALUE ( *)()) writeIO, 1);
392
+ rb_define_method(rb_cPort, "read", (VALUE ( *)()) readIO, 1);
393
+ rb_define_method(rb_cPort, "available?", (VALUE ( *)()) bytesAvailableIO, 0);
394
+
395
+ rb_define_method(rb_cPort, "line_status", (VALUE ( *)()) lineStatusIO, 0);
396
+ rb_define_method(rb_cPort, "set_rts", (VALUE ( *)()) setRtsIO, 1);
397
+ rb_define_method(rb_cPort, "set_dtr", (VALUE ( *)()) setDtrIO, 1);
398
+
399
+ }
@@ -0,0 +1,54 @@
1
+ /*
2
+ * Copyright (c) 2013, Ingenico Inc.
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
+ #ifndef rs_232_initializer____FILEEXTENSION___
16
+ #define rs_232_initializer____FILEEXTENSION___
17
+
18
+ #if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
19
+
20
+ # include "windows/port.h"
21
+
22
+ #else
23
+
24
+ # include "posix/port.h"
25
+
26
+ #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
+
54
+ #endif