| Class | SerialPort |
| In: |
ext/serialport.c
lib/serialport.rb |
| Parent: | IO |
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, …)
| NONE | = | INT2FIX(NONE) | ||
| HARD | = | INT2FIX(HARD) | ||
| SOFT | = | INT2FIX(SOFT) | ||
| SPACE | = | INT2FIX(SPACE) | ||
| MARK | = | INT2FIX(MARK) | ||
| EVEN | = | INT2FIX(EVEN) | ||
| ODD | = | INT2FIX(ODD) | ||
| VERSION | = | rb_str_new2(RUBY_SERIAL_PORT_VERSION) | the package‘s version as a string "X.Y.Z", beeing major, minor and patch level |
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
# File lib/serialport.rb, line 15
15: def SerialPort::new(port, *params)
16: sp = create(port)
17: begin
18: sp.set_modem_params(*params)
19: rescue
20: sp.close
21: raise
22: end
23: return sp
24: end
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.
# File lib/serialport.rb, line 29
29: def SerialPort::open(port, *params)
30: sp = create(port)
31: begin
32: sp.set_modem_params(*params)
33: if (block_given?)
34: yield sp
35: sp.close
36: return nil
37: end
38: rescue
39: sp.close
40: raise
41: end
42: return sp
43: end
Get the current baud rate, see SerialPort#get_modem_params for details.
/*
* 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);
}
Set the baud rate, see SerialPort#set_modem_params for details.
/*
* 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;
}
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.
/*
* 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 CTS line
/*
* 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 current data bits, see SerialPort#get_modem_params for details.
/*
* 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);
}
Set the data bits, see SerialPort#set_modem_params for details.
/*
* 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;
}
Get the state (0 or 1) of the DCD line
/*
* 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 DSR line
/*
* 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 DTR line (not available on Windows)
/*
* 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);
}
Set the state (0 or 1) of the DTR line
/*
* Set the state (0 or 1) of the DTR line
*/
static VALUE sp_set_dtr(self, val)
{
return sp_set_dtr_impl(self, val);
}
Get the flow control. The result is either NONE, HARD, SOFT or (HARD | SOFT)
/*
* 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);
}
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.
/*
* 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)
{
return sp_set_flow_control_impl(self, val);
}
Get the configure of the serial port.
Returned is a hash with the following keys:
/*
* 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;
}
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.
/*
* 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))
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;
}
Get the configure of the serial port.
Returned is a hash with the following keys:
/*
* 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;
}
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:
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.
/*
* 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);
}
Get the current parity, see SerialPort#get_modem_params for details.
/*
* 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);
}
Set the parity, see SerialPort#set_modem_params for details.
/*
* 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 timeout value (in milliseconds) for reading. See SerialPort#set_read_timeout for details.
/*
* 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);
}
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.
/*
* 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)
{
return sp_set_read_timeout_impl(self, val);
}
Get the state (0 or 1) of the RI line
/*
* 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);
}
Get the state (0 or 1) of the RTS line (not available on Windows)
/*
* 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);
}
Set the state (0 or 1) of the RTS line
/*
* Set the state (0 or 1) of the RTS line
*/
static VALUE sp_set_rts(self, val)
{
return sp_set_rts_impl(self, val);
}
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:
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.
/*
* 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);
}
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.
/*
* 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))
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;
}
Get the current stop bits, see SerialPort#get_modem_params for details.
/*
* 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);
}
Set the stop bits, see SerialPort#set_modem_params for details.
/*
* 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;
}
Get the write timeout (in milliseconds)
Note: Under Posix, write timeouts are not implemented.
/*
* 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);
}