Created
December 22, 2017 19:14
-
-
Save cpfarher/585fbeaee9dffe5e587c7f7983622cf7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'colorize' | |
| require 'rmodbus' | |
| require 'timers' | |
| ip="10.0.0.2" | |
| sl1 = ModBus::TCPClient.new(ip).with_slave(1) | |
| timers = Timers::Group.new | |
| now_and_every_five_seconds = timers.now_and_every(0.001) { | |
| begin | |
| sl1.holding_registers[0..1000]; puts "read #{Time.now.to_i}".green | |
| rescue Exception =>e | |
| puts "read exception".blue | |
| end | |
| } | |
| now_and_every_five_seconds_1 = timers.now_and_every(0.01) { | |
| begin | |
| sl1.holding_registers[0..10]=(0..10).to_a.map{|e| e*rand(10)}; puts "write #{Time.now.to_i}".red | |
| rescue Exception =>e | |
| puts ('write exception').yellow | |
| sleep(2) | |
| end | |
| } | |
| loop { timers.wait } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment