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
|
/* 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.
*
* For documentation on serial programming, see the excellent:
* "Serial Programming Guide for POSIX Operating Systems"
* written Michael R. Sweet.
* http://www.easysw.com/~mike/serial/
*/
#include "serialport.h"
VALUE cSerialPort; /* serial port class */
VALUE sBaud, sDataBits, sStopBits, sParity; /* strings */
VALUE sRts, sDtr, sCts, sDsr, sDcd, sRi;
/*
* :nodoc: This method is private and will be called by SerialPort#new or SerialPort#open.
*/
static VALUE sp_create(class, _port)
VALUE class, _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, depends on platform
* ["data_bits"] Integer from 5 to 8 (4 is allowed on Windows too)
* ["stop_bits"] An 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))
*
* Nota: A baudrate of nil will keep the old value. The default parity depends on the
* number of databits configured before this function call.
*/
static VALUE sp_set_modem_params(argc, argv, self)
int argc;
VALUE *argv, self;
{
return sp_set_modem_params_impl(argc, argv, self);
}
/*
* Send a break for the given time.
*
* <tt>time</tt> is an integer of tenths-of-a-second for the break.
*
* Note: Under Posix, this value is very approximate.
*/
static VALUE sp_break(self, time)
VALUE self, time;
{
return sp_break_impl(self, time);
}
/*
* Get the state (0 or 1) of the DTR line (not available on Windows)
*/
static VALUE sp_get_dtr(self)
VALUE self;
{
return sp_get_dtr_impl(self);
}
/*
* Get the flow control. The result is either NONE, HARD, SOFT or (HARD | SOFT)
*/
static VALUE sp_get_flow_control(self)
VALUE self;
{
return sp_get_flow_control_impl(self);
}
/*
* Get the timeout value (in milliseconds) for reading.
* See SerialPort#set_read_timeout for details.
*/
static VALUE sp_get_read_timeout(self)
VALUE self;
{
return sp_get_read_timeout_impl(self);
}
/*
* Get the state (0 or 1) of the RTS line (not available on Windows)
*/
static VALUE sp_get_rts(self)
VALUE self;
{
return sp_get_rts_impl(self);
}
/*
* Get the write timeout (in milliseconds)
*
* Note: Under Posix, write timeouts are not implemented.
*/
static VALUE sp_get_write_timeout(self)
VALUE self;
{
return sp_get_write_timeout_impl(self);
}
/*
* Set the state (0 or 1) of the DTR line
*/
static VALUE sp_set_dtr(self, val)
VALUE self, val;
{
return sp_set_dtr_impl(self, val);
}
/*
* Set the flow control to either NONE, HARD, SOFT or (HARD | SOFT)
*
* Note: SerialPort::HARD mode is not supported on all platforms.
* SerialPort::HARD uses RTS/CTS handshaking; DSR/DTR is not
* supported.
*/
static VALUE sp_set_flow_control(self, val)
VALUE self, 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.
*
* Note: Read timeouts don't mix well with multi-threading.
*/
static VALUE sp_set_read_timeout(self, val)
VALUE self, val;
{
return sp_set_read_timeout_impl(self, val);
}
/*
* Set the state (0 or 1) of the RTS line
*/
static VALUE sp_set_rts(self, val)
VALUE self, val;
{
return sp_set_rts_impl(self, val);
}
/*
* Set a write timeout (in milliseconds)
*
* Note: Under Posix, write timeouts are not implemented.
*/
static VALUE sp_set_write_timeout(self, val)
VALUE self, val;
{
return sp_set_write_timeout_impl(self, val);
}
/*
*/
static void get_modem_params(self, mp)
VALUE self;
struct modem_params *mp;
{
get_modem_params_impl(self, mp);
}
/*
* Set the baud rate, see SerialPort#set_modem_params for details.
*/
static VALUE sp_set_data_rate(self, data_rate)
VALUE self, 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, see SerialPort#set_modem_params for details.
*/
static VALUE sp_set_data_bits(self, data_bits)
VALUE self, 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, see SerialPort#set_modem_params for details.
*/
static VALUE sp_set_stop_bits(self, stop_bits)
VALUE self, 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, see SerialPort#set_modem_params for details.
*/
static VALUE sp_set_parity(self, parity)
VALUE self, 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, see SerialPort#get_modem_params for details.
*/
static VALUE sp_get_data_rate(self)
VALUE self;
{
struct modem_params mp;
get_modem_params(self, &mp);
return INT2FIX(mp.data_rate);
}
/*
* Get the current data bits, see SerialPort#get_modem_params for details.
*/
static VALUE sp_get_data_bits(self)
VALUE self;
{
struct modem_params mp;
get_modem_params(self, &mp);
return INT2FIX(mp.data_bits);
}
/*
* Get the current stop bits, see SerialPort#get_modem_params for details.
*/
static VALUE sp_get_stop_bits(self)
VALUE self;
{
struct modem_params mp;
get_modem_params(self, &mp);
return INT2FIX(mp.stop_bits);
}
/*
* Get the current parity, see SerialPort#get_modem_params for details.
*/
static VALUE sp_get_parity(self)
VALUE self;
{
struct modem_params mp;
get_modem_params(self, &mp);
return INT2FIX(mp.parity);
}
/*
* Get the configure of the serial port.
*
* Returned is a hash with the following keys:
* ["baud"] Integer with the baud rate
* ["data_bits"] Integer from 5 to 8 (4 is possible on Windows too)
* ["stop_bits"] Integer, 1 or 2 (1.5 is not supported)
* ["parity"] One of the constants NONE, EVEN or ODD (on Windows may also MARK or SPACE)
*/
static VALUE sp_get_modem_params(self)
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;
}
void get_line_signals_helper(obj, ls)
VALUE obj;
struct line_signals *ls;
{
get_line_signals_helper_impl(obj, ls);
}
/*
* Get the state (0 or 1) of the CTS line
*/
static VALUE sp_get_cts(self)
VALUE self;
{
struct line_signals ls;
get_line_signals_helper(self, &ls);
return INT2FIX(ls.cts);
}
/*
* Get the state (0 or 1) of the DSR line
*/
static VALUE sp_get_dsr(self)
VALUE self;
{
struct line_signals ls;
get_line_signals_helper(self, &ls);
return INT2FIX(ls.dsr);
}
/*
* Get the state (0 or 1) of the DCD line
*/
static VALUE sp_get_dcd(self)
VALUE self;
{
struct line_signals ls;
get_line_signals_helper(self, &ls);
return INT2FIX(ls.dcd);
}
/*
* Get the state (0 or 1) of the RI line
*/
static VALUE sp_get_ri(self)
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 are
* "rts", "dtr", "cts", "dsr", "dcd", and "ri".
*
* Note: Under Windows, the rts and dtr values are not included.
*/
static VALUE sp_signals(self)
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;
}
/*
* This class is used for communication over a serial port.
* In addition to the methods here, you can use everything
* Ruby's IO-class provides (read, write, getc, readlines, ...)
*/
void Init_serialport()
{
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_const(cSerialPort, "NONE", INT2FIX(NONE));
rb_define_const(cSerialPort, "HARD", INT2FIX(HARD));
rb_define_const(cSerialPort, "SOFT", INT2FIX(SOFT));
rb_define_const(cSerialPort, "SPACE", INT2FIX(SPACE));
rb_define_const(cSerialPort, "MARK", INT2FIX(MARK));
rb_define_const(cSerialPort, "EVEN", INT2FIX(EVEN));
rb_define_const(cSerialPort, "ODD", INT2FIX(ODD));
/* the package's version as a string "X.Y.Z", beeing major, minor and patch level */
rb_define_const(cSerialPort, "VERSION", rb_str_new2(RUBY_SERIAL_PORT_VERSION));
}
|