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.3.2"
- 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 ⇒ 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= ⇒ Integer
Set the baud rate.
-
#break ⇒ 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= ⇒ 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= ⇒ Integer
Set the state of the DTR line.
-
#flow_control ⇒ Integer
Get the flow control flag.
-
#flow_control= ⇒ 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= ⇒ Hash
Configure the serial port.
-
#parity ⇒ Integer
Get the current parity.
-
#parity= ⇒ Integer
Set the parity.
-
#read_timeout ⇒ Integer
Get the read timeout value.
-
#read_timeout= ⇒ 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= ⇒ Integer
Set the state of the RTS line.
-
#set_modem_params ⇒ 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= ⇒ Integer
Set the stop bits.
-
#write_timeout ⇒ Integer
Get the write timeout.
-
#write_timeout= ⇒ Integer
Set a write timeout.
Class Method Details
.create ⇒ 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.
29 30 |
# File 'ext/native/serialport.c', line 29
static VALUE sp_create(class, _port)
VALUE 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
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.
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
#baud ⇒ Integer
Get the current baud rate
305 306 |
# File 'ext/native/serialport.c', line 305 static VALUE sp_get_data_rate(self) VALUE self; |
#baud= ⇒ Integer
Set the baud rate
230 231 |
# File 'ext/native/serialport.c', line 230 static VALUE sp_set_data_rate(self, data_rate) VALUE self, data_rate; |
#break ⇒ nil
(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; |
#cts ⇒ Integer
Get the state of the CTS line
403 404 |
# File 'ext/native/serialport.c', line 403 static VALUE sp_get_cts(self) VALUE self; |
#data_bits ⇒ Integer
Get the current data bits
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
249 250 |
# File 'ext/native/serialport.c', line 249 static VALUE sp_set_data_bits(self, data_bits) VALUE self, data_bits; |
#dcd ⇒ Integer
Get the state of the DCD line
435 436 |
# File 'ext/native/serialport.c', line 435 static VALUE sp_get_dcd(self) VALUE self; |
#dsr ⇒ Integer
Get the state of the DSR line
419 420 |
# File 'ext/native/serialport.c', line 419 static VALUE sp_get_dsr(self) VALUE self; |
#dtr ⇒ Integer
(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_control ⇒ Integer
Get the flow control flag
100 101 |
# File 'ext/native/serialport.c', line 100 static VALUE sp_get_flow_control(self) VALUE self; |
#flow_control= ⇒ 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
164 165 |
# File 'ext/native/serialport.c', line 164 static VALUE sp_set_flow_control(self, val) VALUE self, val; |
#flush_input ⇒ Boolean
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_output ⇒ Boolean
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_params ⇒ Hash
Get the configure of the serial port
369 370 |
# File 'ext/native/serialport.c', line 369 static VALUE sp_get_modem_params(self) VALUE self; |
#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".
470 471 |
# File 'ext/native/serialport.c', line 470 static VALUE sp_signals(self) VALUE self; |
#modem_params ⇒ Hash
Get the configure of the serial port
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.
62 63 |
# File 'ext/native/serialport.c', line 62 static VALUE sp_set_modem_params(argc, argv, self) int argc; |
#parity ⇒ Integer
Get the current parity
353 354 |
# File 'ext/native/serialport.c', line 353 static VALUE sp_get_parity(self) VALUE self; |
#parity= ⇒ Integer
Set the parity
287 288 |
# File 'ext/native/serialport.c', line 287 static VALUE sp_set_parity(self, parity) VALUE self, parity; |
#read_timeout ⇒ Integer
Get the read timeout value
112 113 |
# File 'ext/native/serialport.c', line 112 static VALUE sp_get_read_timeout(self) VALUE self; |
#read_timeout= ⇒ 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.
182 183 |
# File 'ext/native/serialport.c', line 182 static VALUE sp_set_read_timeout(self, val) VALUE self, val; |
#ri ⇒ Integer
Get the state of the RI line
451 452 |
# File 'ext/native/serialport.c', line 451 static VALUE sp_get_ri(self) VALUE self; |
#rts ⇒ Integer
(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.
62 63 |
# File 'ext/native/serialport.c', line 62 static VALUE sp_set_modem_params(argc, argv, self) int argc; |
#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".
470 471 |
# File 'ext/native/serialport.c', line 470 static VALUE sp_signals(self) VALUE self; |
#stop_bits ⇒ Integer
Get the current stop bits
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
268 269 |
# File 'ext/native/serialport.c', line 268 static VALUE sp_set_stop_bits(self, stop_bits) VALUE self, stop_bits; |
#write_timeout ⇒ Integer
(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
(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; |