Skip to content

Instantly share code, notes, and snippets.

@k-nowicki
Created April 13, 2013 17:20
Show Gist options
  • Save k-nowicki/5379272 to your computer and use it in GitHub Desktop.
Save k-nowicki/5379272 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
@vitiral
Copy link

vitiral commented Sep 12, 2014

Hey, do you have a link to the programming guide where you got these commands? I can't find it anywhere!

@keesj
Copy link

keesj commented Nov 18, 2014

A "python" version also Released under the MIT License.

#!/usr/bin/env python
import serial
from time import sleep

class Korad:
  def __init__(self,port):
     self.ser =serial.Serial(port, 9600, timeout=1)

  def get_status(self):
    self.command("STATUS?")
    return ord(self.ser.read(1))

  def get_idn(self):
    return self.feedback_command("*IDN?")

  def turn_on(self):
    self.command("OUT1")

  def turn_off(self):
    self.command("OUT0")

  def recall_memory(self,bank):
    self.command("RCL%s" % bank)

  def save_memory(self,bank):
    self.command("SAV%s" % bank)

  def turn_on_OCP(self):
    self.command("OCP1")

  def turn_off_OCP(self):
    self.command("OCP0")

  def turn_on_OVP(self):
    self.command("OVP1")

  def turn_off_OVP(self):
    self.command("OVP0")

#  def set_tracking_mode(value = :independent)
#    mode = [:independent, :series, :parallel].index(value)
#    mode ? command("TRACK#{mode}") : raise(Exception.new("UnknownModeException"))
#  end

  def set_current(self,channel,value):
    self.command("ISET%s:%s" % ( channel, value))

  def set_voltage(self,channel, value):
    self.command("VSET%s:%s" % ( channel, value))

  def get_stored_current(self,channel):
    return feedback_command("ISET%s?" % channel)

  def get_stored_voltage(self,channel):
    return feedback_command("VSET%s?" % channel)

  def get_output_current(self,channel):
    return feedback_command("IOUT%s?" % channel)

  def get_output_voltage(self,channel):
    return feedback_command("VOUT%?" % channel)

  def feedback_command(self,text):
      self.command(text)
      return self.filtered_output()

  def command(self,text):
    sleep(0.1)
    self.ser.write(text)

  def filtered_output(self):
    return self.ser.read(200)

# example 
korad =  Korad('/dev/ttyACM0')
korad.set_voltage(1,7)
print korad.get_idn()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment