Class: SerialPort

Inherits:
IO
  • Object
show all
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

VERSION =
"1.3.1"
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

Instance Method Summary collapse

Class Method Details

.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

See Also:



24
25
26
27
28
29
30
31
32
33
# File 'lib/serialport.rb', line 24

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.

Yields:

  • (serial_port)

    the serial port number or filename

See Also:



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/serialport.rb', line 42

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

#baudInteger

Get the current baud rate

See Also:



305
306
# File 'ext/native/serialport.c', line 305

static VALUE sp_get_data_rate(self)
VALUE self;

#baud=Integer

Set the baud rate

See Also:



230
231
# File 'ext/native/serialport.c', line 230

static VALUE sp_set_data_rate(self, data_rate)
VALUE self, data_rate;

#breaknil

Note:

(POSIX) this value is very approximate

Send a break for the given time



76
77
# File 'ext/native/serialport.c', line 76

static VALUE sp_break(self, time)
VALUE self, time;

#ctsInteger

Get the state of the CTS line

See Also:



403
404
# File 'ext/native/serialport.c', line 403

static VALUE sp_get_cts(self)
VALUE self;

#data_bitsInteger

Get the current data bits

See Also:



321
322
# File 'ext/native/serialport.c', line 321

static VALUE sp_get_data_bits(self)
VALUE self;

#data_bits=Integer

Set the data bits

See Also:



249
250
# File 'ext/native/serialport.c', line 249

static VALUE sp_set_data_bits(self, data_bits)
VALUE self, data_bits;

#dcdInteger

Get the state of the DCD line

See Also:



435
436
# File 'ext/native/serialport.c', line 435

static VALUE sp_get_dcd(self)
VALUE self;

#dsrInteger

Get the state of the DSR line

See Also:



419
420
# File 'ext/native/serialport.c', line 419

static VALUE sp_get_dsr(self)
VALUE self;

#dtrInteger

Note:

(Windows) DTR is not available

Get the state of the DTR line



88
89
# File 'ext/native/serialport.c', line 88

static VALUE sp_get_dtr(self)
VALUE self;

#dtr=Integer

Set the state of the DTR line



148
149
# File 'ext/native/serialport.c', line 148

static VALUE sp_set_dtr(self, val)
VALUE self, val;

#flow_controlInteger

Get the flow control flag

See Also:

  • #set_flow_control


100
101
# File 'ext/native/serialport.c', line 100

static VALUE sp_get_flow_control(self)
VALUE self;

#flow_control=Integer

Note:

SerialPort::HARD mode is not supported on all platforms.

Note:

SerialPort::HARD uses RTS/CTS handshaking. DSR/DTR is not supported.

Set the flow control



164
165
# File 'ext/native/serialport.c', line 164

static VALUE sp_set_flow_control(self, val)
VALUE self, val;

#flush_inputBoolean

Flush data received but not read.



497
498
# File 'ext/native/serialport.c', line 497

static VALUE sp_flush_input_data(self)
VALUE self;

#flush_outputBoolean

Flush data written but not transmitted.



508
509
# File 'ext/native/serialport.c', line 508

static VALUE sp_flush_output_data(self)
VALUE self;

#get_modem_paramsHash

Get the configure of the serial port

See Also:



369
370
# File 'ext/native/serialport.c', line 369

static VALUE sp_get_modem_params(self)
VALUE self;

#get_signalsHash

Note:

(Windows) the rts and dtr values are not included

Note:

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".


470
471
# File 'ext/native/serialport.c', line 470

static VALUE sp_signals(self)
VALUE self;

#modem_paramsHash

Get the configure of the serial port

See Also:



369
370
# File 'ext/native/serialport.c', line 369

static VALUE sp_get_modem_params(self)
VALUE self;

#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.

Raises:

  • (ArgumentError)

    if values are invalide or unsupported



62
63
# File 'ext/native/serialport.c', line 62

static VALUE sp_set_modem_params(argc, argv, self)
int argc;

#parityInteger

Get the current parity

See Also:



353
354
# File 'ext/native/serialport.c', line 353

static VALUE sp_get_parity(self)
VALUE self;

#parity=Integer

Set the parity

See Also:



287
288
# File 'ext/native/serialport.c', line 287

static VALUE sp_set_parity(self, parity)
VALUE self, parity;

#read_timeoutInteger

Get the read timeout value

See Also:

  • #set_read_timeout


112
113
# File 'ext/native/serialport.c', line 112

static VALUE sp_get_read_timeout(self)
VALUE self;

#read_timeout=Integer

Note:

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.



182
183
# File 'ext/native/serialport.c', line 182

static VALUE sp_set_read_timeout(self, val)
VALUE self, val;

#riInteger

Get the state of the RI line

See Also:



451
452
# File 'ext/native/serialport.c', line 451

static VALUE sp_get_ri(self)
VALUE self;

#rtsInteger

Note:

(Windows) RTS is not available

Get the state of the RTS line



124
125
# File 'ext/native/serialport.c', line 124

static VALUE sp_get_rts(self)
VALUE self;

#rts=Integer

Set the state of the RTS line



194
195
# File 'ext/native/serialport.c', line 194

static VALUE sp_set_rts(self, val)
VALUE 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.

Raises:

  • (ArgumentError)

    if values are invalide or unsupported



62
63
# File 'ext/native/serialport.c', line 62

static VALUE sp_set_modem_params(argc, argv, self)
int argc;

#signalsHash

Note:

(Windows) the rts and dtr values are not included

Note:

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".


470
471
# File 'ext/native/serialport.c', line 470

static VALUE sp_signals(self)
VALUE self;

#stop_bitsInteger

Get the current stop bits

See Also:



337
338
# File 'ext/native/serialport.c', line 337

static VALUE sp_get_stop_bits(self)
VALUE self;

#stop_bits=Integer

Set the stop bits

See Also:



268
269
# File 'ext/native/serialport.c', line 268

static VALUE sp_set_stop_bits(self, stop_bits)
VALUE self, stop_bits;

#write_timeoutInteger

Note:

(POSIX) write timeouts are not implemented

Get the write timeout



136
137
# File 'ext/native/serialport.c', line 136

static VALUE sp_get_write_timeout(self)
VALUE self;

#write_timeout=Integer

Note:

(POSIX) write timeouts are not implemented

Set a write timeout



207
208
# File 'ext/native/serialport.c', line 207

static VALUE sp_set_write_timeout(self, val)
VALUE self, val;