Skip to content

Instantly share code, notes, and snippets.

@cpfarher
Created December 22, 2017 19:14
Show Gist options
  • Select an option

  • Save cpfarher/585fbeaee9dffe5e587c7f7983622cf7 to your computer and use it in GitHub Desktop.

Select an option

Save cpfarher/585fbeaee9dffe5e587c7f7983622cf7 to your computer and use it in GitHub Desktop.
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