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 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600
|
/* Ruby/SerialPort
* Guillaume Pierronnet <moumar@netcourrier.com>
* Alan Stern <stern@rowland.harvard.edu>
* Daniel E. Shipton <dshipton@redshiptechnologies.com>
* Jonas Bähr <jonas.baehr@fs.ei.tum.de>
* Ryan C. Payne <rpayne-oss@bullittsystems.com>
*
* This code is hereby licensed for public consumption under either the
* GNU GPL v2 or greater.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "serialport.h"
VALUE cSerialPort; /* serial port class */
VALUE sBaud, sDataBits, sStopBits, sParity; /* strings */
VALUE sRts, sDtr, sCts, sDsr, sDcd, sRi;
#ifndef HAVE_RB_IO_OPEN_DESCRIPTOR
VALUE
io_open_descriptor_fallback(VALUE klass, int descriptor, int mode, VALUE path, VALUE timeout, void *encoding)
{
VALUE arguments[2] = {
(rb_update_max_fd(descriptor), INT2NUM(descriptor)),
INT2FIX(mode),
};
VALUE self = rb_class_new_instance(2, arguments, klass);
rb_io_t *fptr;
GetOpenFile(self, fptr);
fptr->pathv = path;
fptr->mode |= mode;
return self;
}
#endif
/*
* @api private
*
* @see SerialPort#new
* @see SerialPort#open
*/
static VALUE sp_create(VALUE class, VALUE _port)
{
return sp_create_impl(class, _port);
}
/*
* Configure the serial port. You can pass a hash or multiple values
* as separate arguments. Invalid or unsupported values will raise
* an ArgumentError.
*
* When using a hash the following keys are recognized:
* ["baud"] Integer from 50 to 256000, depending on platform
* ["data_bits"] Integer from 5 to 8 (4 is allowed on Windows too)
* ["stop_bits"] Integer, only allowed values are 1 or 2 (1.5 is not supported)
* ["parity"] One of the constants NONE, EVEN or ODD (Windows allows also MARK and SPACE)
*
* When using separate arguments, they are interpreted as:
* (baud, data_bits = 8, stop_bits = 1, parity = (previous_databits == 8 ? NONE : EVEN))
* A baudrate of nil will keep the old value.
* The default parity depends on the number of databits configured before this function call.
*
* @overload set_modem_params(baud, data_bits, stop_bits, parity)
* @param baud [Integer] the baud rate
* @param data_bits [Integer] the number of data bits
* @param stop_bits [Integer] the number of stop bits
* @param parity [Integer] the type of parity checking
* @overload set_modem_params(hash)
* @param opts [Hash] the options to configure port
*
* @return [Hash] the original paramters
* @raise [ArgumentError] if values are invalide or unsupported
*/
static VALUE sp_set_modem_params(int argc, VALUE *argv, VALUE self)
{
return sp_set_modem_params_impl(argc, argv, self);
}
/*
* Send a break for the given time
*
* @param time [Integer] break time in tenths-of-a-second
* @return [nil]
* @note (POSIX) this value is very approximate
*/
static VALUE sp_break(VALUE self, VALUE time)
{
return sp_break_impl(self, time);
}
/*
* Get the state of the DTR line
*
* @note (Windows) DTR is not available
* @return [Integer] the state of DTR line, 0 or 1
*/
static VALUE sp_get_dtr(VALUE self)
{
return sp_get_dtr_impl(self);
}
/*
* Get the flow control flag
*
* @return [Integer] the flow control flag
* @see SerialPort#set_flow_control
*/
static VALUE sp_get_flow_control(VALUE self)
{
return sp_get_flow_control_impl(self);
}
/*
* Get the read timeout value
*
* @return [Integer] the read timeout, in milliseconds
* @see SerialPort#set_read_timeout
*/
static VALUE sp_get_read_timeout(VALUE self)
{
return sp_get_read_timeout_impl(self);
}
/*
* Get the state of the RTS line
*
* @return [Integer] the state of RTS line, 0 or 1
* @note (Windows) RTS is not available
*/
static VALUE sp_get_rts(VALUE self)
{
return sp_get_rts_impl(self);
}
/*
* Get the write timeout
*
* @return [Integer] the write timeout, in milliseconds
* @note (POSIX) write timeouts are not implemented
*/
static VALUE sp_get_write_timeout(VALUE self)
{
return sp_get_write_timeout_impl(self);
}
/*
* Set the state of the DTR line
*
* @param val [Integer] the desired state of the DTR line, 0 or 1
* @return [Integer] the original +val+ parameter
*/
static VALUE sp_set_dtr(VALUE self, VALUE val)
{
return sp_set_dtr_impl(self, val);
}
/*
* Set the flow control
*
* @param val [Integer] the flow control flag,
* +NONE+, +HARD+, +SOFT+, or (+HARD+ | +SOFT+)
* @return [Integer] the original +val+ parameter
* @note SerialPort::HARD mode is not supported on all platforms.
* @note SerialPort::HARD uses RTS/CTS handshaking.
* DSR/DTR is not supported.
*/
static VALUE sp_set_flow_control(VALUE self, VALUE val)
{
return sp_set_flow_control_impl(self, val);
}
/*
* Set the timeout value (in milliseconds) for reading.
* A negative read timeout will return all the available data without
* waiting, a zero read timeout will not return until at least one
* byte is available, and a positive read timeout returns when the
* requested number of bytes is available or the interval between the
* arrival of two bytes exceeds the timeout value.
*
* @param timeout [Integer] the read timeout in milliseconds
* @return [Integer] the original +timeout+ parameter
* @note Read timeouts don't mix well with multi-threading
*/
static VALUE sp_set_read_timeout(VALUE self, VALUE val)
{
return sp_set_read_timeout_impl(self, val);
}
/*
* Set the state of the RTS line
*
* @param val [Integer] the state of RTS line, 0 or 1
* @return [Integer] the original +val+ parameter
*/
static VALUE sp_set_rts(VALUE self, VALUE val)
{
return sp_set_rts_impl(self, val);
}
/*
* Set a write timeout
*
* @param val [Integer] the write timeout in milliseconds
* @return [Integer] the original +val+ parameter
* @note (POSIX) write timeouts are not implemented
*/
static VALUE sp_set_write_timeout(VALUE self, VALUE val)
{
return sp_set_write_timeout_impl(self, val);
}
/*
* @private helper
*/
static void get_modem_params(VALUE self, struct modem_params *mp)
{
get_modem_params_impl(self, mp);
}
/*
* Set the baud rate
*
* @param data_rate [Integer] the baud rate
* @return [Integer] the original +data_rate+ parameter
* @see SerialPort#set_modem_params
*/
static VALUE sp_set_data_rate(VALUE self, VALUE data_rate)
{
VALUE argv[4];
argv[0] = data_rate;
argv[1] = argv[2] = argv[3] = Qnil;
sp_set_modem_params(4, argv, self);
return data_rate;
}
/*
* Set the data bits
*
* @param data_bits [Integer] the number of data bits
* @return [Integer] the original +data_bits+ parameter
* @see SerialPort#set_modem_params
*/
static VALUE sp_set_data_bits(VALUE self, VALUE data_bits)
{
VALUE argv[4];
argv[1] = data_bits;
argv[0] = argv[2] = argv[3] = Qnil;
sp_set_modem_params(4, argv, self);
return data_bits;
}
/*
* Set the stop bits
*
* @param stop_bits [Integer] the number of stop bits
* @return [Integer] the original +stop_bits+ parameter
* @see SerialPort#set_modem_params
*/
static VALUE sp_set_stop_bits(VALUE self, VALUE stop_bits)
{
VALUE argv[4];
argv[2] = stop_bits;
argv[0] = argv[1] = argv[3] = Qnil;
sp_set_modem_params(4, argv, self);
return stop_bits;
}
/*
* Set the parity
*
* @param parity [Integer] the parity type
* @return [Integer] the original +parity+ parameter
* @see SerialPort#set_modem_params
*/
static VALUE sp_set_parity(VALUE self, VALUE parity)
{
VALUE argv[4];
argv[3] = parity;
argv[0] = argv[1] = argv[2] = Qnil;
sp_set_modem_params(4, argv, self);
return parity;
}
/*
* Get the current baud rate
*
* @return [Integer] the current baud rate
* @see SerialPort#set_modem_params
*/
static VALUE sp_get_data_rate(VALUE self)
{
struct modem_params mp;
get_modem_params(self, &mp);
return INT2FIX(mp.data_rate);
}
/*
* Get the current data bits
*
* @return [Integer] the current number of data bits
* @see SerialPort#set_modem_params
*/
static VALUE sp_get_data_bits(VALUE self)
{
struct modem_params mp;
get_modem_params(self, &mp);
return INT2FIX(mp.data_bits);
}
/*
* Get the current stop bits
*
* @return [Integer] the current number of stop bits
* @see SerialPort#set_modem_params for details
*/
static VALUE sp_get_stop_bits(VALUE self)
{
struct modem_params mp;
get_modem_params(self, &mp);
return INT2FIX(mp.stop_bits);
}
/*
* Get the current parity
*
* @return [Integer] the current parity
* @see SerialPort#set_modem_params
*/
static VALUE sp_get_parity(VALUE self)
{
struct modem_params mp;
get_modem_params(self, &mp);
return INT2FIX(mp.parity);
}
/*
* Get the configure of the serial port
*
* @return [Hash] the serial port configuration
* @see SerialPort#set_modem_params
*/
static VALUE sp_get_modem_params(VALUE self)
{
struct modem_params mp;
VALUE hash;
get_modem_params(self, &mp);
hash = rb_hash_new();
rb_hash_aset(hash, sBaud, INT2FIX(mp.data_rate));
rb_hash_aset(hash, sDataBits, INT2FIX(mp.data_bits));
rb_hash_aset(hash, sStopBits, INT2FIX(mp.stop_bits));
rb_hash_aset(hash, sParity, INT2FIX(mp.parity));
return hash;
}
/*
* @api private
*/
void get_line_signals_helper(VALUE obj, struct line_signals *ls)
{
get_line_signals_helper_impl(obj, ls);
}
/*
* Get the state of the CTS line
*
* @return [Integer] the state of the CTS line, 0 or 1
* @see SerialPort#get_signals
*/
static VALUE sp_get_cts(VALUE self)
{
struct line_signals ls;
get_line_signals_helper(self, &ls);
return INT2FIX(ls.cts);
}
/*
* Get the state of the DSR line
*
* @return [Integer] the state of the DSR line, 0 or 1
* @see SerialPort#get_signals
*/
static VALUE sp_get_dsr(VALUE self)
{
struct line_signals ls;
get_line_signals_helper(self, &ls);
return INT2FIX(ls.dsr);
}
/*
* Get the state of the DCD line
*
* @return [Integer] the state of the DCD line, 0 or 1
* @see SerialPort#get_signals
*/
static VALUE sp_get_dcd(VALUE self)
{
struct line_signals ls;
get_line_signals_helper(self, &ls);
return INT2FIX(ls.dcd);
}
/*
* Get the state of the RI line
*
* @return [Integer] the state of the RI line, 0 or 1
* @see SerialPort#get_signals
*/
static VALUE sp_get_ri(VALUE self)
{
struct line_signals ls;
get_line_signals_helper(self, &ls);
return INT2FIX(ls.ri);
}
/*
* Return a hash with the state of each line status bit.
* Keys:
* "rts", "dtr", "cts", "dsr", "dcd", and "ri".
*
* @return [Hash] the state line info
* @note (Windows) the rts and dtr values are not included
* @note This method is implemented as both SerialPort#signals and SerialPort#get_signals
*/
static VALUE sp_signals(VALUE self)
{
struct line_signals ls;
VALUE hash;
get_line_signals_helper(self, &ls);
hash = rb_hash_new();
#if !(defined(OS_MSWIN) || defined(OS_BCCWIN) || defined(OS_MINGW))
rb_hash_aset(hash, sRts, INT2FIX(ls.rts));
rb_hash_aset(hash, sDtr, INT2FIX(ls.dtr));
#endif
rb_hash_aset(hash, sCts, INT2FIX(ls.cts));
rb_hash_aset(hash, sDsr, INT2FIX(ls.dsr));
rb_hash_aset(hash, sDcd, INT2FIX(ls.dcd));
rb_hash_aset(hash, sRi, INT2FIX(ls.ri));
return hash;
}
/**
* Flush data received but not read.
*
* @return [Boolean] true on success or false if an error occurs.
*/
static VALUE sp_flush_input_data(VALUE self)
{
return sp_flush_input_data_impl(self);
}
/**
* Flush data written but not transmitted.
*
* @return [Boolean] true on success or false if an error occurs.
*/
static VALUE sp_flush_output_data(VALUE self)
{
return sp_flush_output_data_impl(self);
}
void Init_serialport(void)
{
sBaud = rb_str_new2("baud");
sDataBits = rb_str_new2("data_bits");
sStopBits = rb_str_new2("stop_bits");
sParity = rb_str_new2("parity");
sRts = rb_str_new2("rts");
sDtr = rb_str_new2("dtr");
sCts = rb_str_new2("cts");
sDsr = rb_str_new2("dsr");
sDcd = rb_str_new2("dcd");
sRi = rb_str_new2("ri");
rb_gc_register_address(&sBaud);
rb_gc_register_address(&sDataBits);
rb_gc_register_address(&sStopBits);
rb_gc_register_address(&sParity);
rb_gc_register_address(&sRts);
rb_gc_register_address(&sDtr);
rb_gc_register_address(&sCts);
rb_gc_register_address(&sDsr);
rb_gc_register_address(&sDcd);
rb_gc_register_address(&sRi);
cSerialPort = rb_define_class("SerialPort", rb_cIO);
rb_define_singleton_method(cSerialPort, "create", sp_create, 1);
rb_define_method(cSerialPort, "get_modem_params", sp_get_modem_params, 0);
rb_define_method(cSerialPort, "set_modem_params", sp_set_modem_params, -1);
rb_define_method(cSerialPort, "modem_params", sp_get_modem_params, 0);
rb_define_method(cSerialPort, "modem_params=", sp_set_modem_params, -1);
rb_define_method(cSerialPort, "baud", sp_get_data_rate, 0);
rb_define_method(cSerialPort, "baud=", sp_set_data_rate, 1);
rb_define_method(cSerialPort, "data_bits", sp_get_data_bits, 0);
rb_define_method(cSerialPort, "data_bits=", sp_set_data_bits, 1);
rb_define_method(cSerialPort, "stop_bits", sp_get_stop_bits, 0);
rb_define_method(cSerialPort, "stop_bits=", sp_set_stop_bits, 1);
rb_define_method(cSerialPort, "parity", sp_get_parity, 0);
rb_define_method(cSerialPort, "parity=", sp_set_parity, 1);
rb_define_method(cSerialPort, "flow_control=", sp_set_flow_control, 1);
rb_define_method(cSerialPort, "flow_control", sp_get_flow_control, 0);
rb_define_method(cSerialPort, "read_timeout", sp_get_read_timeout, 0);
rb_define_method(cSerialPort, "read_timeout=", sp_set_read_timeout, 1);
rb_define_method(cSerialPort, "write_timeout", sp_get_write_timeout, 0);
rb_define_method(cSerialPort, "write_timeout=", sp_set_write_timeout, 1);
rb_define_method(cSerialPort, "break", sp_break, 1);
rb_define_method(cSerialPort, "signals", sp_signals, 0);
rb_define_method(cSerialPort, "get_signals", sp_signals, 0);
rb_define_method(cSerialPort, "rts", sp_get_rts, 0);
rb_define_method(cSerialPort, "rts=", sp_set_rts, 1);
rb_define_method(cSerialPort, "dtr", sp_get_dtr, 0);
rb_define_method(cSerialPort, "dtr=", sp_set_dtr, 1);
rb_define_method(cSerialPort, "cts", sp_get_cts, 0);
rb_define_method(cSerialPort, "dsr", sp_get_dsr, 0);
rb_define_method(cSerialPort, "dcd", sp_get_dcd, 0);
rb_define_method(cSerialPort, "ri", sp_get_ri, 0);
rb_define_method(cSerialPort, "flush_input", sp_flush_input_data, 0);
rb_define_method(cSerialPort, "flush_output", sp_flush_output_data, 0);
/*
* 0
*/
rb_define_const(cSerialPort, "NONE", INT2FIX(NONE));
/*
* 1
*/
rb_define_const(cSerialPort, "HARD", INT2FIX(HARD));
/*
* 2
*/
rb_define_const(cSerialPort, "SOFT", INT2FIX(SOFT));
/*
* 0
*/
rb_define_const(cSerialPort, "SPACE", INT2FIX(SPACE));
/*
* 1
*/
rb_define_const(cSerialPort, "MARK", INT2FIX(MARK));
/*
* 2
*/
rb_define_const(cSerialPort, "EVEN", INT2FIX(EVEN));
/*
* 3
*/
rb_define_const(cSerialPort, "ODD", INT2FIX(ODD));
}
|