rs_232 2.0.7 → 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,162 +0,0 @@
1
- class Adapter::Dev < Adapter::Generic
2
- attr_reader :interface
3
- # == Top level module ::CommPort constants
4
- #
5
- # :VERSION,
6
- #
7
- # :BAUD_110,
8
- # :BAUD_300,
9
- # :BAUD_600,
10
- # :BAUD_1200,
11
- # :BAUD_2400,
12
- # :BAUD_4800,
13
- # :BAUD_9600,
14
- # :BAUD_19200,
15
- # :BAUD_38400,
16
- # :BAUD_57600,
17
- # :BAUD_115200,
18
- #
19
- # :DATA_BITS_5,
20
- # :DATA_BITS_6,
21
- # :DATA_BITS_7,
22
- # :DATA_BITS_8,
23
- #
24
- # :PAR_NONE,
25
- # :PAR_ODD,
26
- # :PAR_EVEN,
27
- #
28
- # :STOP_BITS_1,
29
- # :STOP_BITS_3,
30
- #
31
- # :FLOW_OFF,
32
- # :FLOW_HARDWARE,
33
- # :FLOW_XONXOFF,
34
- #
35
- # :Impl
36
- #
37
-
38
-
39
- # == constructor with default params
40
- #
41
- def initialize(port, &block)
42
- @interface = CommPort::Rs232.new(port)
43
- super
44
- connect
45
- end
46
-
47
- # Open and configure interface
48
- #
49
- # @return [Bool]
50
- #
51
- def connect
52
- @interface.open
53
-
54
- notify_with '%s has been received call to %s...' % [self, __method__]
55
-
56
- @interface.baud_rate = CommPort::BAUD_115200
57
- @interface.data_bits = CommPort::DATA_BITS_8
58
- @interface.parity = CommPort::PAR_NONE
59
- @interface.stop_bits = CommPort::STOP_BITS_1
60
- @interface.flow_control = CommPort::FLOW_OFF
61
-
62
- @open = open?
63
- end
64
-
65
- # == Write function implementation
66
- #
67
- # @param [String] bytes
68
- # @return [Int]
69
- #
70
- def write(bytes)
71
- @interface.write(bytes)
72
- end
73
-
74
- # == Closing interface and freeing structures
75
- #
76
- # @return [Bool]
77
- #
78
- def close
79
- notify_with '%s has been received call to %s...' % [self, __method__]
80
-
81
- @interface.close
82
- @open = open?
83
- !open?
84
- end
85
-
86
- # == Flashing buffer function
87
- #
88
- def flush
89
- notify_with '%s has been received call to %s...' % [self, __method__]
90
-
91
- @interface.flush
92
- end
93
-
94
- # @return [Bool]
95
- #
96
- def open?
97
- @interface && !@interface.closed?
98
- end
99
-
100
- # == read() implementation example
101
- #
102
- # @param +count+ [Int]
103
- # @param +blocking+ [Bool]
104
- #
105
- # @return [String]
106
- #
107
- # === Alternative implementation:
108
- # @usage:
109
- #
110
- # +timeout+ = blocking_value ? 15000 : 0
111
- # +@interface.timeout+ = +timeout+
112
- # +@interface.read( +count+ )+
113
- #
114
- def read(count, blocking = false)
115
- array = []
116
-
117
- bytes_count = (count == -1) ? @interface.available? : count
118
-
119
- if blocking
120
- bytes = read_io_until(count, count)
121
- array.push bytes if bytes
122
- else
123
- bytes_count.times do
124
- byte = @interface.read(1)
125
- array.push byte if byte
126
- end
127
- end
128
- array.empty? ? nil : array.join
129
- end
130
-
131
- private
132
-
133
- # == simulate blocking function
134
- #
135
- # @param +count+ [Int]
136
- # @param +up_to+ [Int]
137
- #
138
- # no direct ruby usage
139
- #
140
- def block_io_until(count, up_to)
141
- while @interface.available? < count && up_to > 0
142
- up_to -= 1
143
- end
144
- up_to > 0
145
- end
146
-
147
- # == simulate blocking function
148
- #
149
- # @param +count+ [Int]
150
- # @param +up_to+ [Int]
151
- #
152
- # no direct ruby usage
153
- #
154
- def read_io_until(count, up_to)
155
- until block_io_until(count, up_to)
156
- sleep 0.001
157
- end
158
- read(count)
159
- end
160
-
161
- end
162
-
@@ -1,77 +0,0 @@
1
- require 'monitor'
2
-
3
- module Adapter
4
-
5
- class Generic
6
- include MonitorMixin
7
-
8
- DEFAULT_NOTIFIER = ->(message) { $stdout.puts message }
9
-
10
- attr_accessor :notifier
11
-
12
- def initialize(*args, &block)
13
- Thread.abort_on_exception = true
14
- @rxd = true unless instance_variables.include?(:@rxd)
15
- @notifier = block || DEFAULT_NOTIFIER
16
- super()
17
- run
18
- end
19
-
20
- def notifier=(value)
21
- @notifier = value
22
- end
23
-
24
- def notifier
25
- @notifier
26
- end
27
-
28
- def reading_allowed?
29
- @rxd
30
- end
31
-
32
- def defer_reading
33
- @rxd = false
34
- !reading_allowed?
35
- end
36
-
37
- def allow_reading
38
- @rxd = true
39
- end
40
-
41
- def rx(int, blocking = false)
42
- byte = read(int, blocking)
43
- if byte
44
- notify_with "RX [#{byte.length}]: #{byte.inspect}"
45
- end
46
- byte
47
- end
48
-
49
- def tx(bytes)
50
- int = write(bytes)
51
- notify_with "TX [#{int}]: #{bytes.inspect}"
52
- int
53
- end
54
-
55
- def restart_rx_thread
56
- allow_reading
57
- run
58
- end
59
-
60
- def run
61
- Thread.new do
62
- loop do
63
- rx(1, false) if reading_allowed?
64
- sleep(0.005)
65
- end
66
- end
67
- end
68
-
69
- private
70
-
71
- def notify_with(message)
72
- @notifier[message]
73
- end
74
-
75
- end
76
-
77
- end
@@ -1,4 +0,0 @@
1
- module Adapter
2
- require_relative 'adapter/generic'
3
- require_relative 'adapter/dev'
4
- end
@@ -1,23 +0,0 @@
1
- require 'pathname'
2
-
3
- module Fixtures
4
- def fixture_path(name)
5
- File.join '../fixtures', name
6
- end
7
-
8
- def absolute_fixture_path(name)
9
- File.expand_path fixture_path(name), File.dirname(__FILE__)
10
- end
11
-
12
- def fixture(name)
13
- File.read absolute_fixture_path(name)
14
- end
15
-
16
- def binary_fixture(name)
17
- File.open(absolute_fixture_path(name), "rb") { |f| f.read }
18
- end
19
-
20
- def fixtures_path
21
- Pathname(absolute_fixture_path '')
22
- end
23
- end