Class: SerialPort

Inherits:
IO
  • Object
show all
Defined in:
lib/serialport.rb,
ext/serialport.c

Constant Summary collapse

NONE =
INT2FIX(NONE)
HARD =
INT2FIX(HARD)
SOFT =
INT2FIX(SOFT)
SPACE =
INT2FIX(SPACE)
MARK =
INT2FIX(MARK)
EVEN =
INT2FIX(EVEN)
ODD =
INT2FIX(ODD)
VERSION =

the package’s version as a string “X.Y.Z”, beeing major, minor and patch level

rb_str_new2(RUBY_SERIAL_PORT_VERSION)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.createObject

:nodoc: This method is privat and will be called by SerialPort#new or SerialPort#open.



34
35
# File 'ext/serialport.c', line 34

static VALUE sp_create(class, _port)
VALUE class, _port;

.new(port, *params) ⇒ Object

Creates a serial port object.

port may be a port number or the file name of a defice. The number is portable; so 0 is mapped to “COM1” on Windows, “/dev/ttyS0” on Linux, “/dev/cuaa0” on Mac OS X, etc.

params can be used to configure the serial port. See SerialPort#set_modem_params for details



15
16
17
18
19
20
21
22
23
24
# File 'lib/serialport.rb', line 15

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) ⇒ 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.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/serialport.rb', line 29

def SerialPort::open(port, *params)
   sp = create(port)
   begin
      sp.set_modem_params(*params)
      if (block_given?)
         yield sp
         sp.close
         return nil
      end
   rescue
      sp.close
      raise
   end
   return sp
end

Instance Method Details

#baudObject

Get the current baud rate, see SerialPort#get_modem_params for details.



248
249
# File 'ext/serialport.c', line 248

static VALUE sp_get_data_rate(self)
VALUE self;

#baud=Object

Set the baud rate, see SerialPort#set_modem_params for details.



188
189
# File 'ext/serialport.c', line 188

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

#breakObject

Send a break for the given time.

time is an integer of tenths-of-a-second for the break.

Note: Under Posix, this value is very approximate.



71
72
# File 'ext/serialport.c', line 71

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

#ctsObject

Get the state (0 or 1) of the CTS line



328
329
# File 'ext/serialport.c', line 328

static VALUE sp_get_cts(self)
VALUE self;

#data_bitsObject

Get the current data bits, see SerialPort#get_modem_params for details.



261
262
# File 'ext/serialport.c', line 261

static VALUE sp_get_data_bits(self)
VALUE self;

#data_bits=Object

Set the data bits, see SerialPort#set_modem_params for details.



203
204
# File 'ext/serialport.c', line 203

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

#dcdObject

Get the state (0 or 1) of the DCD line



354
355
# File 'ext/serialport.c', line 354

static VALUE sp_get_dcd(self)
VALUE self;

#dsrObject

Get the state (0 or 1) of the DSR line



341
342
# File 'ext/serialport.c', line 341

static VALUE sp_get_dsr(self)
VALUE self;

#dtrObject

Get the state (0 or 1) of the DTR line (not available on Windows)



80
81
# File 'ext/serialport.c', line 80

static VALUE sp_get_dtr(self)
VALUE self;

#dtr=() ⇒ Object



128
129
130
131
# File 'ext/serialport.c', line 128

static VALUE sp_set_dtr(self, val)
{
   return sp_set_dtr_impl(self, val);
}

#flow_controlObject

Get the flow control. The result is either NONE, HARD, SOFT or (HARD | SOFT)



89
90
# File 'ext/serialport.c', line 89

static VALUE sp_get_flow_control(self)
VALUE self;

#flow_control=() ⇒ Object



140
141
142
143
# File 'ext/serialport.c', line 140

static VALUE sp_set_flow_control(self, val)
{
   return sp_set_flow_control_impl(self, val);
}

#get_modem_paramsObject

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)



307
308
# File 'ext/serialport.c', line 307

static VALUE sp_get_modem_params(self)
VALUE self;

#get_signalsObject

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.



383
384
# File 'ext/serialport.c', line 383

static VALUE sp_signals(self)
VALUE self;

#modem_paramsObject

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)



307
308
# File 'ext/serialport.c', line 307

static VALUE sp_get_modem_params(self)
VALUE self;

#modem_params=Object

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.



57
58
# File 'ext/serialport.c', line 57

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

#parityObject

Get the current parity, see SerialPort#get_modem_params for details.



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

static VALUE sp_get_parity(self)
VALUE self;

#parity=Object

Set the parity, see SerialPort#set_modem_params for details.



233
234
# File 'ext/serialport.c', line 233

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

#read_timeoutObject

Get the timeout value (in milliseconds) for reading. See SerialPort#set_read_timeout for details.



99
100
# File 'ext/serialport.c', line 99

static VALUE sp_get_read_timeout(self)
VALUE self;

#read_timeout=() ⇒ Object



155
156
157
158
# File 'ext/serialport.c', line 155

static VALUE sp_set_read_timeout(self, val)
{
   return sp_set_read_timeout_impl(self, val);
}

#riObject

Get the state (0 or 1) of the RI line



367
368
# File 'ext/serialport.c', line 367

static VALUE sp_get_ri(self)
VALUE self;

#rtsObject

Get the state (0 or 1) of the RTS line (not available on Windows)



108
109
# File 'ext/serialport.c', line 108

static VALUE sp_get_rts(self)
VALUE self;

#rts=() ⇒ Object



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

static VALUE sp_set_rts(self, val)
{
   return sp_set_rts_impl(self, val);
}

#set_modem_paramsObject

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.



57
58
# File 'ext/serialport.c', line 57

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

#signalsObject

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.



383
384
# File 'ext/serialport.c', line 383

static VALUE sp_signals(self)
VALUE self;

#stop_bitsObject

Get the current stop bits, see SerialPort#get_modem_params for details.



274
275
# File 'ext/serialport.c', line 274

static VALUE sp_get_stop_bits(self)
VALUE self;

#stop_bits=Object

Set the stop bits, see SerialPort#set_modem_params for details.



218
219
# File 'ext/serialport.c', line 218

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

#write_timeoutObject

Get the write timeout (in milliseconds)

Note: Under Posix, write timeouts are not implemented.



119
120
# File 'ext/serialport.c', line 119

static VALUE sp_get_write_timeout(self)
VALUE self;

#write_timeout=() ⇒ Object



173
174
175
176
# File 'ext/serialport.c', line 173

static VALUE sp_set_write_timeout(self, val)
{
   return sp_set_write_timeout_impl(self, val);
}