luars232串口连接
2016-04-21 14:02
891 查看
<span style="font-size:14px;">rs232 = require("luars232") local delays = require("time_utils").delay local bord_port_name = "/dev/tty.usbserial-141A" local IA_port_name = "/dev/cu.usbserial-A102PPO2" local read_len = 100 local timeout = 100--msec local port = nil local function SerialPortConnect(portname,bt) -- print("< SerialPort Connect to > : ",portname) local err = nil err, port = rs232.open(portname) print("< SerialPort Connect > : ",portname) if err ~= rs232.RS232_ERR_NOERROR then print("< SerialPort Connect Fail > :-( ",err) return err end -- set port settings if bt == 1 then assert(port:set_baud_rate(rs232.RS232_BAUD_115200) == rs232.RS232_ERR_NOERROR) else assert(port:set_baud_rate(rs232.RS232_BAUD_9600) == rs232.RS232_ERR_NOERROR) end assert(port:set_data_bits(rs232.RS232_DATA_8) == rs232.RS232_ERR_NOERROR) assert(port:set_parity(rs232.RS232_PARITY_NONE) == rs232.RS232_ERR_NOERROR) assert(port:set_stop_bits(rs232.RS232_STOP_1) == rs232.RS232_ERR_NOERROR) assert(port:set_flow_control(rs232.RS232_FLOW_OFF) == rs232.RS232_ERR_NOERROR) print("<SerialPort connect success >:",port) end local function SerialPortRead(readlen, _timeout) if readlen == nil then readlen = read_len end if _timeout==nil then _timeout = timeout end local err, data_read, size = port:read(readlen, _timeout) -- assert(e == rs232.RS232_ERR_NOERROR) print("bord port-read:",err,tostring(data_read),size) return err, data_read, size end local function SerialPortWrite(str, _timeout) if _timeout==nil then _timeout = timeout end local err, len_written = port:write(str, _timeout) -- assert(e == rs232.RS232_ERR_NOERROR) print("port-write:",err,len_written,str) return err, len_written end local function SerialPortClose() local err = port:close() return err end local function ReadCurrent(portname) SerialPortConnect(portname) SerialPortWrite("SYSTEM:Remote\r\n") --切换电流表工作模式 delays(500) SerialPortWrite("MEAS:CURR:DC?\r\n"); --读取电流表电流 delays(1000) local err,ret = SerialPortRead(50,999) ; local numret = string.match(tostring(ret),"([+-]%S%S%S%S%S).-[\r\n]"); local cunt = string.match(tostring(ret),"E-(%d%d)[\r\n]"); print("Agilent 34401A read current:",tostring(numret),cunt); SerialPortClose() local result = tonumber(numret)*0.1^cunt*1000 --mA return result end local table ={x1=0,y1=0,x2=0,y2=0,z1=0,z2=0} local function Factor_math(x1,y1,x2,y2) print("Factor :",x1,y1,x2,y2) local gain,offset gain = (y1-y2)/(x1-x2) offset = y1-(gain*x1) print("factor math done,gain="..gain..";offset="..offset) return gain,offset end local function SerialSend(portname,cmd) SerialPortConnect(portname,1) --波特率115200 port:flush() SerialPortWrite(cmd) delays(2000) --read的长度越长,delays的时间越长 local err,ret,size = SerialPortRead(200,999) local result = string.match(tostring(ret),"I=(%S+)[\r\n]") SerialPortClose() return result end local function Factor_write(gain,offset,wtype,hl) print("Factor_write:",gain,offset,wtype,hl) local wcmd = "" if wtype == "set" then wcmd = "@ISET_" elseif wtype == "read" then wcmd = "@IREAD_" end if hl==1 then SerialSend(bord_port_name,wcmd.."LK="..gain) delays(100) SerialSend(bord_port_name,wcmd.."LE="..offset) delays(100) elseif hl==0 then SerialSend(bord_port_name,wcmd.."HK="..gain) delays(100) SerialSend(bord_port_name,wcmd.."HE="..offset) delays(100) end end function hly(q1,q2) local p1 = q1 local p2 = q2 local hl = nil if p1<1 and p2<1 then hl = 1 else hl = 0 end print("dac set:",p1,p2) if(0.01<tonumber(p1) and tonumber(p1)<105 and 0.01<tonumber(p2) and tonumber(p2)<105)then SerialSend(bord_port_name,"@SET_DAC="..tostring(p1)) y1 = tonumber(ReadCurrent(IA_port_name)) SerialSend(bord_port_name,"@SET_DAC="..tostring(p2)) y2 = tonumber(ReadCurrent(IA_port_name)) local gain1 , offset1 = Factor_math(p1,y1,p2,y2) print("gain:"..gain1.."offset"..offset1.."档位"..hl) Factor_write(gain1,offset1,"set",hl) SerialSend(bord_port_name,"@SET_DAC="..tostring(p1)) y1 = tonumber(ReadCurrent(IA_port_name)) z1 = tonumber(SerialSend(bord_port_name,"@READ_I?")) SerialSend(bord_port_name,"@SET_DAC="..tostring(p2)) y2 = tonumber(ReadCurrent(IA_port_name)) z2 = tonumber(SerialSend(bord_port_name,"@READ_I?")) local gain2 , offset2 = Factor_math(z1,y1,z2,y2) Factor_write(gain2,offset2,"read",hl) else return "error code -1 ,wrong IA range number" end end </span>
相关文章推荐
- Lua中判断table是否为空
- Lua应用——tables应用,查找是否为保留字
- Lua从入门到放弃--文件I/O
- 极客学院lua文档
- Lua协程(coroutine)的基本知识
- 基于 lua-resty-upload 实现简单的文件上传服务
- Lua的闭包
- Lua中的元表和元方法
- UVA_327_Evaluating Simple C Expressions
- VS 2013 编译 Lua 5.3
- 字符估值器CharEvaluator
- Notepad++中调试用心lua程序
- Lua从入门到放弃--打印表Demo
- Lua中的操作系统库
- Lua从入门到放弃--元表(操作符)
- 浅析 Android 动画:自定义 Interpolator 与 TypeEvaluator
- Lua从入门到放弃--元表
- Lua math库
- lua C++对象内存管理
- Lua根据秒数返回时间串00:00:00