Class: SerialPort

Inherits:
IO
  • Object
show all
Defined in:
lib/serialport.rb,
ext/native/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 private and will be called by SerialPort#new or SerialPort#open.



31
32
# File 'ext/native/serialport.c', line 31

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
44
45
46
# File 'lib/serialport.rb', line 29

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

#baudObject

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



252
253
# File 'ext/native/serialport.c', line 252

static VALUE sp_get_data_rate(self)
VALUE self;

#baud=Object

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



192
193
# File 'ext/native/serialport.c', line 192

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.



68
69
# File 'ext/native/serialport.c', line 68

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

#ctsObject

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



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

static VALUE sp_get_cts(self)
VALUE self;

#data_bitsObject

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



265
266
# File 'ext/native/serialport.c', line 265

static VALUE sp_get_data_bits(self)
VALUE self;

#data_bits=Object

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



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

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

#dcdObject

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



364
365
# File 'ext/native/serialport.c', line 364

static VALUE sp_get_dcd(self)
VALUE self;

#dsrObject

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



351
352
# File 'ext/native/serialport.c', line 351

static VALUE sp_get_dsr(self)
VALUE self;

#dtrObject

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



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

static VALUE sp_get_dtr(self)
VALUE self;

#dtr=Object

Set the state (0 or 1) of the DTR line



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

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

#flow_controlObject

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



86
87
# File 'ext/native/serialport.c', line 86

static VALUE sp_get_flow_control(self)
VALUE self;

#flow_control=Object

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.


138
139
# File 'ext/native/serialport.c', line 138

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



310
311
# File 'ext/native/serialport.c', line 310

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.



393
394
# File 'ext/native/serialport.c', line 393

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)



310
311
# File 'ext/native/serialport.c', line 310

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.



54
55
# File 'ext/native/serialport.c', line 54

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

#parityObject

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



291
292
# File 'ext/native/serialport.c', line 291

static VALUE sp_get_parity(self)
VALUE self;

#parity=Object

Set the parity, see SerialPort#set_modem_params for details.



237
238
# File 'ext/native/serialport.c', line 237

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.



96
97
# File 'ext/native/serialport.c', line 96

static VALUE sp_get_read_timeout(self)
VALUE self;

#read_timeout=Object

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.



154
155
# File 'ext/native/serialport.c', line 154

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

#riObject

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



377
378
# File 'ext/native/serialport.c', line 377

static VALUE sp_get_ri(self)
VALUE self;

#rtsObject

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



105
106
# File 'ext/native/serialport.c', line 105

static VALUE sp_get_rts(self)
VALUE self;

#rts=Object

Set the state (0 or 1) of the RTS line



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

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



54
55
# File 'ext/native/serialport.c', line 54

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.



393
394
# File 'ext/native/serialport.c', line 393

static VALUE sp_signals(self)
VALUE self;

#stop_bitsObject

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



278
279
# File 'ext/native/serialport.c', line 278

static VALUE sp_get_stop_bits(self)
VALUE self;

#stop_bits=Object

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



222
223
# File 'ext/native/serialport.c', line 222

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.



116
117
# File 'ext/native/serialport.c', line 116

static VALUE sp_get_write_timeout(self)
VALUE self;

#write_timeout=Object

Set a write timeout (in milliseconds)

Note: Under Posix, write timeouts are not implemented.



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

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