mruby-serialport-uart 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE +28 -0
- data/README.md +45 -0
- data/lib/mruby/uart/serialport/uart_serialport.rb +264 -0
- data/lib/mruby/uart/serialport/version.rb +5 -0
- data/lib/mruby/uart/serialport.rb +4 -0
- data/lib/mruby/uart.rb +3 -0
- metadata +65 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 8acf7450ce5e36ab1afd64bd9a7a56e62666bdd5737cb70bca5125ef4ea13439
|
4
|
+
data.tar.gz: b2fc87101262329fa83b8df0791a9e2ec119b3a4e3339352232b9dd47c317647
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a851cbbb52bace4822139aa72af3f0dfc3bc26548daf4fa9a2afee1ce51a684b959605d2eec5b76d32860c0ad1300d2d43cd402553807fd638464c4082df75c1
|
7
|
+
data.tar.gz: 25f1955388f8915b6bc0b800a55fba116cc8b7ff498c9d6b8da7fbb84ad1a7b1a15181e9d71d8c984a8b83953f50d403fd0c387ccb6ec3429ad38c76dc27b9fa
|
data/LICENSE
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
BSD 3-Clause License
|
2
|
+
|
3
|
+
Copyright (c) 2023, Shimane IT Open-Innovation Center.
|
4
|
+
|
5
|
+
Redistribution and use in source and binary forms, with or without
|
6
|
+
modification, are permitted provided that the following conditions are met:
|
7
|
+
|
8
|
+
1. Redistributions of source code must retain the above copyright notice, this
|
9
|
+
list of conditions and the following disclaimer.
|
10
|
+
|
11
|
+
2. Redistributions in binary form must reproduce the above copyright notice,
|
12
|
+
this list of conditions and the following disclaimer in the documentation
|
13
|
+
and/or other materials provided with the distribution.
|
14
|
+
|
15
|
+
3. Neither the name of the copyright holder nor the names of its
|
16
|
+
contributors may be used to endorse or promote products derived from
|
17
|
+
this software without specific prior written permission.
|
18
|
+
|
19
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
20
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
21
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
22
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
23
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
24
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
25
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
26
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
27
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
28
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
data/README.md
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
# UART class using serialport gem.
|
2
|
+
|
3
|
+
## Overview
|
4
|
+
|
5
|
+
This is an implementation of the UART class library.
|
6
|
+
Follows [mruby, mruby/c common I/O API guidelines.](https://github.com/mruby/microcontroller-peripheral-interface-guide)
|
7
|
+
|
8
|
+
This is a wrapper for the [serialport gem](https://rubygems.org/gems/serialport).
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
$ gem install mruby-serialport-uart
|
13
|
+
|
14
|
+
|
15
|
+
## Features
|
16
|
+
|
17
|
+
* Read and write by serialport.
|
18
|
+
* Communication parameters can be changed at any time.
|
19
|
+
* It has linewise methods and binary methods.
|
20
|
+
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
about RaspberryPi...
|
25
|
+
|
26
|
+
```
|
27
|
+
# echo server
|
28
|
+
require "mruby/uart" # or "mruby/uart/serialport"
|
29
|
+
|
30
|
+
uart = UART.new("/dev/serial0")
|
31
|
+
while true
|
32
|
+
s = uart.read(1)
|
33
|
+
uart.write s
|
34
|
+
print s
|
35
|
+
end
|
36
|
+
```
|
37
|
+
|
38
|
+
Other case, see original guidelines.
|
39
|
+
|
40
|
+
https://github.com/mruby/microcontroller-peripheral-interface-guide/blob/main/mruby_io_UART_en.md
|
41
|
+
|
42
|
+
|
43
|
+
## Licence
|
44
|
+
|
45
|
+
BSD 3-Clause License. see LICENSE file.
|
@@ -0,0 +1,264 @@
|
|
1
|
+
#
|
2
|
+
# UART class using serialport gem.
|
3
|
+
#
|
4
|
+
# Copyright (c) 2023 Shimane IT Open-Innovation Center.
|
5
|
+
#
|
6
|
+
# see: LICENSE file.
|
7
|
+
# see: https://github.com/mruby/microcontroller-peripheral-interface-guide/blob/main/mruby_io_UART_en.md
|
8
|
+
#
|
9
|
+
# frozen_string_literal: true
|
10
|
+
#
|
11
|
+
|
12
|
+
require "serialport"
|
13
|
+
|
14
|
+
|
15
|
+
##
|
16
|
+
# UART namespace
|
17
|
+
#
|
18
|
+
module UART
|
19
|
+
# Constants
|
20
|
+
NONE = 0
|
21
|
+
ODD = 1
|
22
|
+
EVEN = 2
|
23
|
+
RTSCTS = 4
|
24
|
+
|
25
|
+
##
|
26
|
+
# constructor
|
27
|
+
#
|
28
|
+
#@see UART::GemSerialport.initialize
|
29
|
+
#
|
30
|
+
def self.new( node=nil, baudrate:9600, baud:nil, data_bits:8, stop_bits:1,
|
31
|
+
parity:NONE, flow_control:NONE, unit:nil )
|
32
|
+
UART::GemSerialport.new( node, baudrate, baud, data_bits, stop_bits,
|
33
|
+
parity, flow_control, unit )
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
|
38
|
+
##
|
39
|
+
# gem serialport wrapper class
|
40
|
+
# https://rubygems.org/gems/serialport
|
41
|
+
#
|
42
|
+
class UART::GemSerialport
|
43
|
+
|
44
|
+
##
|
45
|
+
# constructor
|
46
|
+
#
|
47
|
+
#@param [String] node device node.
|
48
|
+
#@param [Integer] baudrate baudrate
|
49
|
+
#@param [Integer] baud baudrate
|
50
|
+
#@param [Integer] data_bits data bits
|
51
|
+
#@param [Integer] stop_bits stop bits
|
52
|
+
#@param [Constant] parity parity bit (UART::NONE, ODD, EVEN)
|
53
|
+
#@param [Constant] flow_control flow control (UART::NONE, RTSCTS)
|
54
|
+
#@param [String] unit device node.
|
55
|
+
#@see UART.new
|
56
|
+
#
|
57
|
+
def initialize( node, baudrate, baud, data_bits,
|
58
|
+
stop_bits, parity, flow_control, unit )
|
59
|
+
@device = SerialPort.new(node||unit)
|
60
|
+
@readbuf = "".b
|
61
|
+
|
62
|
+
setmode( baudrate:baudrate, baud:baud, data_bits:data_bits,
|
63
|
+
stop_bits:stop_bits, parity:parity, flow_control:flow_control )
|
64
|
+
end
|
65
|
+
|
66
|
+
|
67
|
+
##
|
68
|
+
# Changes the mode (parameters) of UART.
|
69
|
+
#
|
70
|
+
#@param [Integer] baudrate baudrate
|
71
|
+
#@param [Integer] baud baudrate
|
72
|
+
#@param [Integer] data_bits data bits
|
73
|
+
#@param [Integer] stop_bits stop bits
|
74
|
+
#@param [Constant] parity parity bit (UART::NONE, ODD, EVEN)
|
75
|
+
#@param [Constant] flow_control flow control (UART::NONE, RTSCTS)
|
76
|
+
#@return [void]
|
77
|
+
#
|
78
|
+
def setmode( baudrate:nil, baud:nil, data_bits:nil, stop_bits:nil,
|
79
|
+
parity:nil, flow_control:nil )
|
80
|
+
if baud || baudrate
|
81
|
+
@device.baud = baud || baudrate
|
82
|
+
end
|
83
|
+
|
84
|
+
if data_bits
|
85
|
+
@device.data_bits = data_bits
|
86
|
+
end
|
87
|
+
|
88
|
+
if stop_bits
|
89
|
+
@device.stop_bits = stop_bits
|
90
|
+
end
|
91
|
+
|
92
|
+
case parity
|
93
|
+
when UART::NONE
|
94
|
+
@device.parity = SerialPort::NONE
|
95
|
+
when UART::ODD
|
96
|
+
@device.parity = SerialPort::ODD
|
97
|
+
when UART::EVEN
|
98
|
+
@device.parity = SerialPort::EVEN
|
99
|
+
when nil
|
100
|
+
# nothing to do
|
101
|
+
else
|
102
|
+
raise ArgumentError
|
103
|
+
end
|
104
|
+
|
105
|
+
case flow_control
|
106
|
+
when UART::NONE
|
107
|
+
@device.flow_control = SerialPort::NONE
|
108
|
+
when UART::RTSCTS
|
109
|
+
@device.flow_control = SerialPort::HARD
|
110
|
+
when nil
|
111
|
+
# nothing to do
|
112
|
+
else
|
113
|
+
raise ArgumentError
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
|
118
|
+
##
|
119
|
+
# Reads data of the specified number of bytes, read_bytes.
|
120
|
+
#
|
121
|
+
#@param [Integer] read_bytes read bytes.
|
122
|
+
#@return [String] reading datas.
|
123
|
+
#
|
124
|
+
def read( read_bytes )
|
125
|
+
while bytes_available() < read_bytes
|
126
|
+
sleep 0.1
|
127
|
+
end
|
128
|
+
|
129
|
+
return @readbuf.slice!(0, read_bytes)
|
130
|
+
end
|
131
|
+
|
132
|
+
|
133
|
+
##
|
134
|
+
# Sends data.
|
135
|
+
#
|
136
|
+
#@param [String] string data to send.
|
137
|
+
#@return [Integer] the number of bytes sent.
|
138
|
+
#
|
139
|
+
def write( string )
|
140
|
+
@device.write( string )
|
141
|
+
end
|
142
|
+
|
143
|
+
|
144
|
+
##
|
145
|
+
# Reads a line of string.
|
146
|
+
#
|
147
|
+
#@return [String] reading data.
|
148
|
+
#
|
149
|
+
def gets()
|
150
|
+
while true
|
151
|
+
_fill_readbuf()
|
152
|
+
pos = @readbuf.index("\n")
|
153
|
+
break if pos
|
154
|
+
sleep 0.1
|
155
|
+
end
|
156
|
+
|
157
|
+
return @readbuf.slice!(0, pos+1)
|
158
|
+
end
|
159
|
+
|
160
|
+
|
161
|
+
##
|
162
|
+
# Sends one line and sends a newline code at the end of the argument string.
|
163
|
+
# The newline code is LF only by this version.
|
164
|
+
#
|
165
|
+
#@param [String] string string to send.
|
166
|
+
#
|
167
|
+
def puts( string )
|
168
|
+
@device.write( string )
|
169
|
+
if string[-1] != "\n"
|
170
|
+
@device.write( "\n" )
|
171
|
+
end
|
172
|
+
|
173
|
+
return nil
|
174
|
+
end
|
175
|
+
|
176
|
+
|
177
|
+
##
|
178
|
+
# Returns the number of readable bytes in the read buffer.
|
179
|
+
#
|
180
|
+
#@return [Integer] num of readable bytes.
|
181
|
+
#
|
182
|
+
def bytes_available()
|
183
|
+
_fill_readbuf()
|
184
|
+
|
185
|
+
return @readbuf.size
|
186
|
+
end
|
187
|
+
|
188
|
+
|
189
|
+
##
|
190
|
+
# Returns the number of bytes of data in the transmission buffer that have not been actually sent.
|
191
|
+
#
|
192
|
+
#@return [Integer] num of bytes.
|
193
|
+
#
|
194
|
+
def bytes_to_write()
|
195
|
+
return 0
|
196
|
+
end
|
197
|
+
|
198
|
+
|
199
|
+
##
|
200
|
+
# Returns true if reading a line of data is possible.
|
201
|
+
#
|
202
|
+
#@return [Bool] true if a line of data can be read
|
203
|
+
#
|
204
|
+
def can_read_line()
|
205
|
+
_fill_readbuf()
|
206
|
+
|
207
|
+
return @readbuf.include?("\n")
|
208
|
+
end
|
209
|
+
|
210
|
+
|
211
|
+
##
|
212
|
+
# Block until transmission of data accumulated in the transmission buffer is completed.
|
213
|
+
#
|
214
|
+
#@return [void]
|
215
|
+
#
|
216
|
+
def flush()
|
217
|
+
@device.flush()
|
218
|
+
end
|
219
|
+
|
220
|
+
|
221
|
+
##
|
222
|
+
# Clears the receive buffer.
|
223
|
+
#
|
224
|
+
#@return [void]
|
225
|
+
#
|
226
|
+
def clear_rx_buffer()
|
227
|
+
@readbuf.clear
|
228
|
+
end
|
229
|
+
|
230
|
+
##
|
231
|
+
# Clears the transmission buffer.
|
232
|
+
#
|
233
|
+
#@return [void]
|
234
|
+
#
|
235
|
+
def clear_tx_buffer()
|
236
|
+
# nothing to do.
|
237
|
+
end
|
238
|
+
|
239
|
+
|
240
|
+
##
|
241
|
+
# Sends a break signal.
|
242
|
+
# The time is optional and specified in seconds.
|
243
|
+
#
|
244
|
+
#@param [Integer,Float] time
|
245
|
+
#@return [void]
|
246
|
+
#
|
247
|
+
def send_break( time = 0 )
|
248
|
+
t = (time * 10).to_i
|
249
|
+
t = [t, 1].max
|
250
|
+
|
251
|
+
@device.break( t )
|
252
|
+
end
|
253
|
+
|
254
|
+
|
255
|
+
private
|
256
|
+
def _fill_readbuf()
|
257
|
+
while true
|
258
|
+
s = @device.read_nonblock( 1024 ) rescue nil
|
259
|
+
break if !s
|
260
|
+
@readbuf << s
|
261
|
+
end
|
262
|
+
end
|
263
|
+
|
264
|
+
end
|
data/lib/mruby/uart.rb
ADDED
metadata
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mruby-serialport-uart
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.9.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- HirohitoHigashi
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-10-13 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: serialport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
description:
|
28
|
+
email:
|
29
|
+
- higashi@s-itoc.jp
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- LICENSE
|
35
|
+
- README.md
|
36
|
+
- lib/mruby/uart.rb
|
37
|
+
- lib/mruby/uart/serialport.rb
|
38
|
+
- lib/mruby/uart/serialport/uart_serialport.rb
|
39
|
+
- lib/mruby/uart/serialport/version.rb
|
40
|
+
homepage: https://github.com/HirohitoHigashi/mruby-mio/tree/main/mruby-serialport-uart
|
41
|
+
licenses:
|
42
|
+
- BSD 3-CLAUSE
|
43
|
+
metadata:
|
44
|
+
homepage_uri: https://github.com/HirohitoHigashi/mruby-mio/tree/main/mruby-serialport-uart
|
45
|
+
post_install_message:
|
46
|
+
rdoc_options: []
|
47
|
+
require_paths:
|
48
|
+
- lib
|
49
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: 2.6.0
|
54
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: '0'
|
59
|
+
requirements: []
|
60
|
+
rubygems_version: 3.4.13
|
61
|
+
signing_key:
|
62
|
+
specification_version: 4
|
63
|
+
summary: UART class library using serialport gem. compliant with mruby, mruby/c common
|
64
|
+
I/O API guidelines.
|
65
|
+
test_files: []
|