-- Code to control a syringe pump (KD Scientific Gemini 88 LEGACY) over serial -- Responds to calls from macros (M-codes) and screen scripts. -- Installation: put in your Modules folder, add this to screen scripts: --package.path = package.path .. ";./Modules/?.lua;" --package.cpath = package.cpath .. ";./Modules/?.dll;" --package.loaded.SyringePump = nil --sp=require "SyringePump" -- to use in your Macros, put these lines in any one of your macro Lua scripts (they are cross-compiled) --package.path = package.path .. ";./Modules/?.lua;" --package.cpath = package.cpath .. ";./Modules/?.dll;" --sp=require "SyringePump" -- You will need to define the COM address in the registers for this script to find. -- TODO: make a screen drop-down box for selecting COM port if/when Mach ever supports that wxWidget :( :( local SyringePump = {} rs232 = require("luars232"); e=nil p=nil local inst = mc.mcGetInstance(); local port_name = mc.mcRegGetValueString(mc.mcRegGetHandle(inst, "iRegs0/syringePumpCOM")); local address = mc.mcRegGetValueString(mc.mcRegGetHandle(inst, "iRegs0/syringePumpAddress")); function SyringePump.EnableSerial() if (p~=nil) then wx.wxMessageBox("port is already open, quitting!\n") end e, p = rs232.open(port_name) -- wx.wxMilliSleep(100) if (p==nil or e ~= rs232.RS232_ERR_NOERROR) then -- handle error wx.wxMessageBox(string.format("can't open serial port '%s', error: '%s'\n", port_name, rs232.error_tostring(e))); return end assert(p:set_baud_rate(rs232.RS232_BAUD_9600) == rs232.RS232_ERR_NOERROR); assert(p:set_data_bits(rs232.RS232_DATA_8) == rs232.RS232_ERR_NOERROR); assert(p:set_parity(rs232.RS232_PARITY_NONE) == rs232.RS232_ERR_NOERROR); assert(p:set_stop_bits(rs232.RS232_STOP_2) == rs232.RS232_ERR_NOERROR); assert(p:set_flow_control(rs232.RS232_FLOW_OFF) == rs232.RS232_ERR_NOERROR); end function SyringePump.StopPump() SyringePump.EnableSerial() if (p==nil ) then mc.mcCntlSetLastError(inst ,"Port is closed, nothing sent") return -1 end err, len_written = p:write("\r", 10) SyringePump.DisableSerial() end function SyringePump.SendCommand(command) SyringePump.EnableSerial() if (p==nil ) then mc.mcCntlSetLastError(inst, "Port is closed, nothing sent") return -1 end err, len_written = p:write(address..command .."\r", 10) local read_len = 2 -- read 2 bytes local err, data_read, size = p:read(read_len) --mc.mcCntlSetLastError(inst, string.gsub(tostring(data_read),"\n"," ")) --RUN THE PUMP assert(e == rs232.RS232_ERR_NOERROR) SyringePump.DisableSerial() end function SyringePump.DisableSerial() if (p~=nil) then p:close() p=nil e=nil else p=nil e=nil mc.mcCntlSetLastError(inst,"Port already closed") end end return SyringePump