Class: UART::GemSerialport
- Inherits:
-
Object
- Object
- UART::GemSerialport
- Defined in:
- lib/mruby/uart/serialport/uart_serialport.rb
Overview
gem serialport wrapper class rubygems.org/gems/serialport
Instance Method Summary collapse
-
#bytes_available ⇒ Integer
Returns the number of readable bytes in the read buffer.
-
#bytes_to_write ⇒ Integer
Returns the number of bytes of data in the transmission buffer that have not been actually sent.
-
#can_read_line ⇒ Bool
Returns true if reading a line of data is possible.
-
#clear_rx_buffer ⇒ void
Clears the receive buffer.
-
#clear_tx_buffer ⇒ void
Clears the transmission buffer.
-
#flush ⇒ void
Block until transmission of data accumulated in the transmission buffer is completed.
-
#gets ⇒ String
Reads a line of string.
-
#initialize(node, baudrate, baud, data_bits, stop_bits, parity, flow_control, unit) ⇒ GemSerialport
constructor
constructor.
-
#puts(string) ⇒ Object
Sends one line and sends a newline code at the end of the argument string.
-
#read(read_bytes) ⇒ String
Reads data of the specified number of bytes, read_bytes.
-
#send_break(time = 0) ⇒ void
Sends a break signal.
-
#setmode(baudrate: nil, baud: nil, data_bits: nil, stop_bits: nil, parity: nil, flow_control: nil) ⇒ void
Changes the mode (parameters) of UART.
-
#write(string) ⇒ Integer
Sends data.
Constructor Details
#initialize(node, baudrate, baud, data_bits, stop_bits, parity, flow_control, unit) ⇒ GemSerialport
constructor
57 58 59 60 61 62 63 64 |
# File 'lib/mruby/uart/serialport/uart_serialport.rb', line 57 def initialize( node, baudrate, baud, data_bits, stop_bits, parity, flow_control, unit ) @device = SerialPort.new(node||unit) @readbuf = "".b setmode( baudrate:baudrate, baud:baud, data_bits:data_bits, stop_bits:stop_bits, parity:parity, flow_control:flow_control ) end |
Instance Method Details
#bytes_available ⇒ Integer
Returns the number of readable bytes in the read buffer.
182 183 184 185 186 |
# File 'lib/mruby/uart/serialport/uart_serialport.rb', line 182 def bytes_available() _fill_readbuf() return @readbuf.size end |
#bytes_to_write ⇒ Integer
Returns the number of bytes of data in the transmission buffer that have not been actually sent.
194 195 196 |
# File 'lib/mruby/uart/serialport/uart_serialport.rb', line 194 def bytes_to_write() return 0 end |
#can_read_line ⇒ Bool
Returns true if reading a line of data is possible.
204 205 206 207 208 |
# File 'lib/mruby/uart/serialport/uart_serialport.rb', line 204 def can_read_line() _fill_readbuf() return @readbuf.include?("\n") end |
#clear_rx_buffer ⇒ void
This method returns an undefined value.
Clears the receive buffer.
226 227 228 |
# File 'lib/mruby/uart/serialport/uart_serialport.rb', line 226 def clear_rx_buffer() @readbuf.clear end |
#clear_tx_buffer ⇒ void
This method returns an undefined value.
Clears the transmission buffer.
235 236 237 |
# File 'lib/mruby/uart/serialport/uart_serialport.rb', line 235 def clear_tx_buffer() # nothing to do. end |
#flush ⇒ void
This method returns an undefined value.
Block until transmission of data accumulated in the transmission buffer is completed.
216 217 218 |
# File 'lib/mruby/uart/serialport/uart_serialport.rb', line 216 def flush() @device.flush() end |
#gets ⇒ String
Reads a line of string.
149 150 151 152 153 154 155 156 157 158 |
# File 'lib/mruby/uart/serialport/uart_serialport.rb', line 149 def gets() while true _fill_readbuf() pos = @readbuf.index("\n") break if pos sleep 0.1 end return @readbuf.slice!(0, pos+1) end |
#puts(string) ⇒ Object
Sends one line and sends a newline code at the end of the argument string. The newline code is LF only by this version.
167 168 169 170 171 172 173 174 |
# File 'lib/mruby/uart/serialport/uart_serialport.rb', line 167 def puts( string ) @device.write( string ) if string[-1] != "\n" @device.write( "\n" ) end return nil end |
#read(read_bytes) ⇒ String
Reads data of the specified number of bytes, read_bytes.
124 125 126 127 128 129 130 |
# File 'lib/mruby/uart/serialport/uart_serialport.rb', line 124 def read( read_bytes ) while bytes_available() < read_bytes sleep 0.1 end return @readbuf.slice!(0, read_bytes) end |
#send_break(time = 0) ⇒ void
This method returns an undefined value.
Sends a break signal. The time is optional and specified in seconds.
247 248 249 250 251 252 |
# File 'lib/mruby/uart/serialport/uart_serialport.rb', line 247 def send_break( time = 0 ) t = (time * 10).to_i t = [t, 1].max @device.break( t ) end |
#setmode(baudrate: nil, baud: nil, data_bits: nil, stop_bits: nil, parity: nil, flow_control: nil) ⇒ void
This method returns an undefined value.
Changes the mode (parameters) of UART.
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/mruby/uart/serialport/uart_serialport.rb', line 78 def setmode( baudrate:nil, baud:nil, data_bits:nil, stop_bits:nil, parity:nil, flow_control:nil ) if baud || baudrate @device.baud = baud || baudrate end if data_bits @device.data_bits = data_bits end if stop_bits @device.stop_bits = stop_bits end case parity when UART::NONE @device.parity = SerialPort::NONE when UART::ODD @device.parity = SerialPort::ODD when UART::EVEN @device.parity = SerialPort::EVEN when nil # nothing to do else raise ArgumentError end case flow_control when UART::NONE @device.flow_control = SerialPort::NONE when UART::RTSCTS @device.flow_control = SerialPort::HARD when nil # nothing to do else raise ArgumentError end end |
#write(string) ⇒ Integer
Sends data.
139 140 141 |
# File 'lib/mruby/uart/serialport/uart_serialport.rb', line 139 def write( string ) @device.write( string ) end |