Class: UART::GemSerialport

Inherits:
Object
  • Object
show all
Defined in:
lib/mruby/uart/serialport/uart_serialport.rb

Overview

gem serialport wrapper class rubygems.org/gems/serialport

Instance Method Summary collapse

Constructor Details

#initialize(node, baudrate, baud, data_bits, stop_bits, parity, flow_control, unit) ⇒ GemSerialport

constructor

Parameters:

  • node (String)

    device node.

  • baudrate (Integer)

    baudrate

  • baud (Integer)

    baudrate

  • data_bits (Integer)

    data bits

  • stop_bits (Integer)

    stop bits

  • parity (Constant)

    parity bit (UART::NONE, ODD, EVEN)

  • flow_control (Constant)

    flow control (UART::NONE, RTSCTS)

  • unit (String)

    device node.

See Also:



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_availableInteger

Returns the number of readable bytes in the read buffer.

Returns:

  • (Integer)

    num of readable bytes.



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_writeInteger

Returns the number of bytes of data in the transmission buffer that have not been actually sent.

Returns:

  • (Integer)

    num of bytes.



194
195
196
# File 'lib/mruby/uart/serialport/uart_serialport.rb', line 194

def bytes_to_write()
  return 0
end

#can_read_lineBool

Returns true if reading a line of data is possible.

Returns:

  • (Bool)

    true if a line of data can be read



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_buffervoid

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_buffervoid

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

#flushvoid

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

#getsString

Reads a line of string.

Returns:

  • (String)

    reading data.



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.

Parameters:

  • string (String)

    string to send.



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.

Parameters:

  • read_bytes (Integer)

    read bytes.

Returns:

  • (String)

    reading datas.



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.

Parameters:

  • time (Integer, Float) (defaults to: 0)


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.

Parameters:

  • baudrate (Integer) (defaults to: nil)

    baudrate

  • baud (Integer) (defaults to: nil)

    baudrate

  • data_bits (Integer) (defaults to: nil)

    data bits

  • stop_bits (Integer) (defaults to: nil)

    stop bits

  • parity (Constant) (defaults to: nil)

    parity bit (UART::NONE, ODD, EVEN)

  • flow_control (Constant) (defaults to: nil)

    flow control (UART::NONE, RTSCTS)



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.

Parameters:

  • string (String)

    data to send.

Returns:

  • (Integer)

    the number of bytes sent.



139
140
141
# File 'lib/mruby/uart/serialport/uart_serialport.rb', line 139

def write( string )
  @device.write( string )
end