Skip to content

Instantly share code, notes, and snippets.

@ToddG
Forked from k-nowicki/Korad_KA_series_driver.rb
Created December 7, 2021 19:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ToddG/45bde78fc489a75ad600f0f95342975e to your computer and use it in GitHub Desktop.
Save ToddG/45bde78fc489a75ad600f0f95342975e to your computer and use it in GitHub Desktop.
Ruby implementation of KORAD KA series power supplies communication protocol. Verified on KA3005P only, but should work also with all supplies of series, including multichannel ones.
require 'rubygems'
require 'serialport'
class Korad
def initialize(port="/dev/ttyACM0", baud=9600)
@serial = SerialPort::open(port,baud)
@serial.read_timeout = 100
end
def get_status
command("STATUS?")
@serial.read(1).ord.to_s(2)
end
def get_idn
return feedback_command("*IDN?")
end
def turn_on
command("OUT1")
end
def turn_off
command("OUT0")
end
def recall_memory(bank)
command("RCL#{bank}")
end
def save_memory(bank)
command("SAV#{bank}")
end
def turn_on_OCP
command("OCP1")
end
def turn_off_OCP
command("OCP0")
end
def turn_on_OVP
command("OVP1")
end
def turn_off_OVP
command("OVP0")
end
def set_tracking_mode(value = :independent)
mode = [:independent, :series, :parallel].index(value)
mode ? command("TRACK#{mode}") : raise(Exception.new("UnknownModeException"))
end
def set_current(channel, value)
command("ISET#{channel}:#{value}")
end
def set_voltage(channel, value)
command("VSET#{channel}:#{value}")
end
def get_stored_current(channel)
return feedback_command("ISET#{channel}?")
end
def get_stored_voltage(channel)
return feedback_command("VSET#{channel}?")
end
def get_output_current(channel)
return feedback_command("IOUT#{channel}?")
end
def get_output_voltage(channel)
return feedback_command("VOUT#{channel}?")
end
private
def feedback_command(text)
command(text)
filtered_output
end
def command(text)
@serial.write(text)
end
def filtered_output
result = @serial.read(200)
result ? result.each_byte.reject{|i| i== 1}.map{|a| a.chr}.join : result
end
end
#example:
korad = Korad.new
puts korad.set_voltage(1,7)
sleep(0.1)
puts korad.get_idn
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment