lib/escalator/uploader.rb in escalator-0.2.1 vs lib/escalator/uploader.rb in escalator-0.2.2
- old
+ new
@@ -19,31 +19,26 @@
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+require "escalator/plc_define"
+require "escalator/plc_device"
+
module Escalator
class Uploader
+ include ::Escalator::PlcDefine
attr_accessor :protocol
- attr_accessor :program_area, :interaction_area
attr_accessor :source
attr_accessor :data
def initialize options={}
@protocol = options[:protocol] if options[:protocol]
- @program_area = options[:program_area] if options[:program_area]
- @interaction_area = options[:interaction_area] if options[:interaction_area]
end
- STOP_PLC_FLAG = 2 # bit 1
- CLEAR_PROGRAM_FLAG = 4 # bit 2 require bit 1 on
-
- CYCLE_RUN_FLAG = 2
-
-
def upload
# stop plc
stop_plc
clear_program
write_program
@@ -57,55 +52,47 @@
end
end
def word_data
data.each_slice(2).map do |pair|
+ pair << 0 if pair.size == 1
pair.pack("c*").unpack("n*")
end.flatten
end
private
- def to_plc_status
- @to_plc_status ||= interaction_area
- end
-
- def from_plc_status
- @from_plc_status ||= interaction_area.next_device
- end
-
-
def stop_plc
- @protocol.set_word_to_device STOP_PLC_FLAG, to_plc_status
+ @protocol.set_word_to_device ESC_STATUS_TO_PLC_STOP_PLC_FLAG, EscDevice.status_to_plc_device
Timeout.timeout(5) do
- v = @protocol.get_word_from_device from_plc_status
- break if (v & STOP_PLC_FLAG) != 0
+ v = @protocol.get_word_from_device EscDevice.status_from_plc_device
+ break if (v & ESC_STATUS_TO_PLC_STOP_PLC_FLAG) != 0
sleep 0.1
end
end
def clear_program
- @protocol.set_word_to_device STOP_PLC_FLAG | CLEAR_PROGRAM_FLAG, to_plc_status
+ @protocol.set_word_to_device ESC_STATUS_TO_PLC_STOP_PLC_FLAG | ESC_STATUS_TO_PLC_CLEAR_PROGRAM, EscDevice.status_to_plc_device
Timeout.timeout(5) do
- v = @protocol.get_word_from_device from_plc_status
- break if (v & CLEAR_PROGRAM_FLAG) != 0
+ v = @protocol.get_word_from_device EscDevice.status_from_plc_device
+ break if (v & ESC_STATUS_TO_PLC_CLEAR_PROGRAM) != 0
sleep 0.1
end
- @protocol.set_word_to_device STOP_PLC_FLAG, to_plc_status
+ @protocol.set_word_to_device ESC_STATUS_TO_PLC_STOP_PLC_FLAG, EscDevice.status_to_plc_device
end
def run_plc
- @protocol.set_word_to_device 0, to_plc_status
+ @protocol.set_word_to_device 0, EscDevice.status_to_plc_device
Timeout.timeout(5) do
- v = @protocol.get_word_from_device from_plc_status
- break if (v & CYCLE_RUN_FLAG) != 0
+ v = @protocol.get_word_from_device EscDevice.status_from_plc_device
+ break if (v & ESC_STATUS_FROM_PLC_CYCLE_RUN) != 0
sleep 0.1
end
end
def write_program
word_data.each_slice(2*1024) do |chunk|
- @protocol.set_words_to_device chunk, @program_area
+ @protocol.set_words_to_device chunk, EscDevice.program_area_device
end
end
end