Class: SerialPort
- Inherits:
-
IO
- Object
- IO
- SerialPort
- Defined in:
- lib/serialport.rb,
lib/serialport/version.rb,
ext/native/serialport.c
Overview
This class is used for communication over a serial port. In addition to the methods here, you can use Ruby IO methods, e.g. read, write, getc, readlines, etc.
Constant Summary collapse
- VERSION =
"1.4.0"
- NONE =
0
INT2FIX(NONE)
- HARD =
1
INT2FIX(HARD)
- SOFT =
2
INT2FIX(SOFT)
- SPACE =
0
INT2FIX(SPACE)
- MARK =
1
INT2FIX(MARK)
- EVEN =
2
INT2FIX(EVEN)
- ODD =
3
INT2FIX(ODD)
Class Method Summary collapse
- .create(_port) ⇒ Object private
-
.new(port, *params) ⇒ SerialPort
Creates a serial port object.
-
.open(port, *params) {|serial_port| ... } ⇒ Object
This behaves like SerialPort#new, except that you can pass a block to which the new serial port object will be passed.
Instance Method Summary collapse
-
#baud ⇒ Integer
Get the current baud rate.
-
#baud=(data_rate) ⇒ Integer
Set the baud rate.
-
#break(time) ⇒ nil
Send a break for the given time.
-
#cts ⇒ Integer
Get the state of the CTS line.
-
#data_bits ⇒ Integer
Get the current data bits.
-
#data_bits=(data_bits) ⇒ Integer
Set the data bits.
-
#dcd ⇒ Integer
Get the state of the DCD line.
-
#dsr ⇒ Integer
Get the state of the DSR line.
-
#dtr ⇒ Integer
Get the state of the DTR line.
-
#dtr=(val) ⇒ Integer
Set the state of the DTR line.
-
#flow_control ⇒ Integer
Get the flow control flag.
-
#flow_control=(val) ⇒ Integer
Set the flow control.
-
#flush_input ⇒ Boolean
Flush data received but not read.
-
#flush_output ⇒ Boolean
Flush data written but not transmitted.
-
#get_modem_params ⇒ Hash
Get the configure of the serial port.
-
#get_signals ⇒ Hash
Return a hash with the state of each line status bit.
-
#modem_params ⇒ Hash
Get the configure of the serial port.
-
#modem_params=(*args) ⇒ Hash
Configure the serial port.
-
#parity ⇒ Integer
Get the current parity.
-
#parity=(parity) ⇒ Integer
Set the parity.
-
#read_timeout ⇒ Integer
Get the read timeout value.
-
#read_timeout=(val) ⇒ Integer
Set the timeout value (in milliseconds) for reading.
-
#ri ⇒ Integer
Get the state of the RI line.
-
#rts ⇒ Integer
Get the state of the RTS line.
-
#rts=(val) ⇒ Integer
Set the state of the RTS line.
-
#set_modem_params(*args) ⇒ Hash
Configure the serial port.
-
#signals ⇒ Hash
Return a hash with the state of each line status bit.
-
#stop_bits ⇒ Integer
Get the current stop bits.
-
#stop_bits=(stop_bits) ⇒ Integer
Set the stop bits.
-
#write_timeout ⇒ Integer
Get the write timeout.
-
#write_timeout=(val) ⇒ Integer
Set a write timeout.
Class Method Details
.create(_port) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
50 51 52 53 |
# File 'ext/native/serialport.c', line 50
static VALUE sp_create(VALUE class, VALUE _port)
{
return sp_create_impl(class, _port);
}
|
.new(port, *params) ⇒ SerialPort .new(port, *params) ⇒ SerialPort
Creates a serial port object. Accepts the port identifier and a variable list for configuration as paramaters or hash. Please see SerialPort#set_modem_params
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/serialport.rb', line 25 def SerialPort::new(port, *params) sp = create(port) begin sp.set_modem_params(*params) rescue sp.close raise end return sp end |
.open(port, *params) {|serial_port| ... } ⇒ Object
This behaves like SerialPort#new, except that you can pass a block to which the new serial port object will be passed. In this case the connection is automaticaly closed when the block has finished.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/serialport.rb', line 43 def SerialPort::open(port, *params) sp = create(port) begin sp.set_modem_params(*params) rescue sp.close raise end if (block_given?) begin yield sp ensure sp.close end return nil end return sp end |
Instance Method Details
#baud ⇒ Integer
Get the current baud rate
306 307 308 309 310 311 312 313 |
# File 'ext/native/serialport.c', line 306
static VALUE sp_get_data_rate(VALUE self)
{
struct modem_params mp;
get_modem_params(self, &mp);
return INT2FIX(mp.data_rate);
}
|
#baud=(data_rate) ⇒ Integer
Set the baud rate
235 236 237 238 239 240 241 242 243 244 |
# File 'ext/native/serialport.c', line 235
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;
}
|
#break(time) ⇒ nil
(POSIX) this value is very approximate
Send a break for the given time
94 95 96 97 |
# File 'ext/native/serialport.c', line 94
static VALUE sp_break(VALUE self, VALUE time)
{
return sp_break_impl(self, time);
}
|
#cts ⇒ Integer
Get the state of the CTS line
397 398 399 400 401 402 403 404 |
# File 'ext/native/serialport.c', line 397
static VALUE sp_get_cts(VALUE self)
{
struct line_signals ls;
get_line_signals_helper(self, &ls);
return INT2FIX(ls.cts);
}
|
#data_bits ⇒ Integer
Get the current data bits
321 322 323 324 325 326 327 328 |
# File 'ext/native/serialport.c', line 321
static VALUE sp_get_data_bits(VALUE self)
{
struct modem_params mp;
get_modem_params(self, &mp);
return INT2FIX(mp.data_bits);
}
|
#data_bits=(data_bits) ⇒ Integer
Set the data bits
253 254 255 256 257 258 259 260 261 262 |
# File 'ext/native/serialport.c', line 253
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;
}
|
#dcd ⇒ Integer
Get the state of the DCD line
427 428 429 430 431 432 433 434 |
# File 'ext/native/serialport.c', line 427
static VALUE sp_get_dcd(VALUE self)
{
struct line_signals ls;
get_line_signals_helper(self, &ls);
return INT2FIX(ls.dcd);
}
|
#dsr ⇒ Integer
Get the state of the DSR line
412 413 414 415 416 417 418 419 |
# File 'ext/native/serialport.c', line 412
static VALUE sp_get_dsr(VALUE self)
{
struct line_signals ls;
get_line_signals_helper(self, &ls);
return INT2FIX(ls.dsr);
}
|
#dtr ⇒ Integer
(Windows) DTR is not available
Get the state of the DTR line
105 106 107 108 |
# File 'ext/native/serialport.c', line 105
static VALUE sp_get_dtr(VALUE self)
{
return sp_get_dtr_impl(self);
}
|
#dtr=(val) ⇒ Integer
Set the state of the DTR line
160 161 162 163 |
# File 'ext/native/serialport.c', line 160
static VALUE sp_set_dtr(VALUE self, VALUE val)
{
return sp_set_dtr_impl(self, val);
}
|
#flow_control ⇒ Integer
Get the flow control flag
116 117 118 119 |
# File 'ext/native/serialport.c', line 116
static VALUE sp_get_flow_control(VALUE self)
{
return sp_get_flow_control_impl(self);
}
|
#flow_control=(val) ⇒ Integer
SerialPort::HARD mode is not supported on all platforms.
SerialPort::HARD uses RTS/CTS handshaking. DSR/DTR is not supported.
Set the flow control
175 176 177 178 |
# File 'ext/native/serialport.c', line 175
static VALUE sp_set_flow_control(VALUE self, VALUE val)
{
return sp_set_flow_control_impl(self, val);
}
|
#flush_input ⇒ Boolean
Flush data received but not read.
486 487 488 489 |
# File 'ext/native/serialport.c', line 486
static VALUE sp_flush_input_data(VALUE self)
{
return sp_flush_input_data_impl(self);
}
|
#flush_output ⇒ Boolean
Flush data written but not transmitted.
496 497 498 499 |
# File 'ext/native/serialport.c', line 496
static VALUE sp_flush_output_data(VALUE self)
{
return sp_flush_output_data_impl(self);
}
|
#get_modem_params ⇒ Hash
Get the configure of the serial port
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 |
# File 'ext/native/serialport.c', line 366
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;
}
|
#get_signals ⇒ Hash
(Windows) the rts and dtr values are not included
This method is implemented as both SerialPort#signals and SerialPort#get_signals
Return a hash with the state of each line status bit. Keys:
"rts", "dtr", "cts", "dsr", "dcd", and "ri".
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 |
# File 'ext/native/serialport.c', line 460
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;
}
|
#modem_params ⇒ Hash
Get the configure of the serial port
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 |
# File 'ext/native/serialport.c', line 366
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;
}
|
#set_modem_params(baud, data_bits, stop_bits, parity) ⇒ Hash #set_modem_params(hash) ⇒ Hash
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.
82 83 84 85 |
# File 'ext/native/serialport.c', line 82
static VALUE sp_set_modem_params(int argc, VALUE *argv, VALUE self)
{
return sp_set_modem_params_impl(argc, argv, self);
}
|
#parity ⇒ Integer
Get the current parity
351 352 353 354 355 356 357 358 |
# File 'ext/native/serialport.c', line 351
static VALUE sp_get_parity(VALUE self)
{
struct modem_params mp;
get_modem_params(self, &mp);
return INT2FIX(mp.parity);
}
|
#parity=(parity) ⇒ Integer
Set the parity
289 290 291 292 293 294 295 296 297 298 |
# File 'ext/native/serialport.c', line 289
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;
}
|
#read_timeout ⇒ Integer
Get the read timeout value
127 128 129 130 |
# File 'ext/native/serialport.c', line 127
static VALUE sp_get_read_timeout(VALUE self)
{
return sp_get_read_timeout_impl(self);
}
|
#read_timeout=(val) ⇒ Integer
Read timeouts don’t mix well with multi-threading
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.
192 193 194 195 |
# File 'ext/native/serialport.c', line 192
static VALUE sp_set_read_timeout(VALUE self, VALUE val)
{
return sp_set_read_timeout_impl(self, val);
}
|
#ri ⇒ Integer
Get the state of the RI line
442 443 444 445 446 447 448 449 |
# File 'ext/native/serialport.c', line 442
static VALUE sp_get_ri(VALUE self)
{
struct line_signals ls;
get_line_signals_helper(self, &ls);
return INT2FIX(ls.ri);
}
|
#rts ⇒ Integer
(Windows) RTS is not available
Get the state of the RTS line
138 139 140 141 |
# File 'ext/native/serialport.c', line 138
static VALUE sp_get_rts(VALUE self)
{
return sp_get_rts_impl(self);
}
|
#rts=(val) ⇒ Integer
Set the state of the RTS line
203 204 205 206 |
# File 'ext/native/serialport.c', line 203
static VALUE sp_set_rts(VALUE self, VALUE val)
{
return sp_set_rts_impl(self, val);
}
|
#set_modem_params(baud, data_bits, stop_bits, parity) ⇒ Hash #set_modem_params(hash) ⇒ Hash
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.
82 83 84 85 |
# File 'ext/native/serialport.c', line 82
static VALUE sp_set_modem_params(int argc, VALUE *argv, VALUE self)
{
return sp_set_modem_params_impl(argc, argv, self);
}
|
#signals ⇒ Hash
(Windows) the rts and dtr values are not included
This method is implemented as both SerialPort#signals and SerialPort#get_signals
Return a hash with the state of each line status bit. Keys:
"rts", "dtr", "cts", "dsr", "dcd", and "ri".
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 |
# File 'ext/native/serialport.c', line 460
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;
}
|
#stop_bits ⇒ Integer
Get the current stop bits
336 337 338 339 340 341 342 343 |
# File 'ext/native/serialport.c', line 336
static VALUE sp_get_stop_bits(VALUE self)
{
struct modem_params mp;
get_modem_params(self, &mp);
return INT2FIX(mp.stop_bits);
}
|
#stop_bits=(stop_bits) ⇒ Integer
Set the stop bits
271 272 273 274 275 276 277 278 279 280 |
# File 'ext/native/serialport.c', line 271
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;
}
|
#write_timeout ⇒ Integer
(POSIX) write timeouts are not implemented
Get the write timeout
149 150 151 152 |
# File 'ext/native/serialport.c', line 149
static VALUE sp_get_write_timeout(VALUE self)
{
return sp_get_write_timeout_impl(self);
}
|
#write_timeout=(val) ⇒ Integer
(POSIX) write timeouts are not implemented
Set a write timeout
215 216 217 218 |
# File 'ext/native/serialport.c', line 215
static VALUE sp_set_write_timeout(VALUE self, VALUE val)
{
return sp_set_write_timeout_impl(self, val);
}
|