使用串口读取写入FX3UPLC数据
By
cuiqbo
at 2020-12-30 • 0人收藏 • 1877人看过
大神看看那些放需要优化的
import console; //获取FXPLC,寄存器地址 var RWAddr = function(address){ var addr; var str = string.match(address,"\a"); var fnc; //读取地址定义 select(str) { case "X" { addr = 0x0080; fnc=1;} case "Y" { addr = 0x00a0; fnc=1;} case "S" { addr = 0x0000; fnc=2;} case "M" { addr = 0x0100; fnc=2;} case "C" { addr = 0x01C0; fnc=2;} case "T" { addr = 0x00C0; fnc=2;} case "D" { addr = 0x1000; fnc=3;} else {return error("地址错误!");} } select(fnc) { case 1 { if(string.find(address,"[89]") )error("地址错误!"); var a = eval(string.replace(address,"\a","8#") ; return string.format("%04x",addr + a/8); } case 2 { return string.format("%04x",addr + tonumber(string.match(address,"\d+")/8,16));} case 3 { return string.format("%4x",addr + tonumber(string.match(address,"\d+")*2,16));} } } //强制FXPLC,定时器,计数器,中间继电器等地址 var QZAddr = function(address){ var addr,s; var str = string.match(Addr,"\a"); var fnc; select(str) { case "X" { addr = 0x400; fnc=1;} case "Y" { addr = 0x500; fnc=1;} case "S" { addr = 0x000; fnc=2;} case "M" { addr = 0x800; fnc=2;} case "C" { addr = 0x0C0; fnc=2;} case "T" { addr = 0x600; fnc=2;} else {return error("地址错误!");} } select(fnc) { case 1 { if(string.find(address,"[89]") )error("地址错误!"); s = string.format("%04x",addr + eval(string.replace(address,"\a","8#"))); } case 2 { s = string.format("%04x",addr + string.match(address,"\d+")); } } return string.format("%04x",string.right(s,2)++string.left(s,2)); } var cmdStr = class { cmdHead = 2;//命令起始位 cmdEnd = 3;//结束位 cmdRead = 30;//读取PLC cmdWrite = 31;//写入PLC数据 cmdON = 37;//强制ON cmdOFF = 38;//强制OFF } //整理命令数据 var sendFNC = function(cmdfun,address,len=0,text){ var sum1=0; var addr = string.upper(adr); var s; if(len>0){ addr = addr ++ string.format("%02d", len); } for(i=1;#addr;1){ sum1 += addr[i] } sum1 += 0x3 + tostring("0x"++cmdfun); if(text){ var a = string.format("%04x",text ) var b = string.right(a,2)++string.left(a,2); for(i=1;#a;1){ sum1 += string.hex(a[i]); } crcSum = string.upper(string.right(string.format("%x", sum1),2)) s = b++crcSum; }else { crcSum = string.upper(string.right(string.format("%x", sum1),2)) s =crcSum; } return string.format("%02d %d%s %02d%s", 0x02,cmdfun,string.hex(addr," "),0x03,string.hex(s," ")) } //高低位转换 var trHL = function(ad,data){ var str =string.sub(data,2,#Data-3); var pattern = "[\dA-F]{4,4}"; var tab ={}; var num = string.match(ad,"\d+") //console.log(string.match(ad,"\d+") ) for s in string.gmatch( str, pattern ){ var d ="D"; d=d ++num; tab[d] = string.right(s,2)++string.left(s,2); num++; } return tab; } //读写高低位 var tranRW =function(reg,str,len=1){ var r =string.upper(reg); if(string.find(r,"[SMTC]") && eval(len)=1) return tonumber("0x"++string.sub(str,2,#str-3)) & 2**(tonumber(r)%8) ? "on":"off"; if(string.find(r,"[XY]") && eval(len)=1) return tonumber("0x"++string.sub(str,2,#str-3)) & 2**tonumber(string.right(r,1)) ? "on":"off"; if(string.find(r,"D")) return trHL(r,str); } var readReg =function(address ,len){ } //写入D或置位STMXY weriteReg("D0",10) 暂时只能写一个地址 var writeReg = function(address ,s){ var cmd; var len =0; var a = string.upper(address); if(string.find(a,"[XYCDSMT]"){ var s1 = string.upper(s?"on":"off"); select(s1) { case "ON" { cmd = 37;} case "OFF"{ cmd = 38;} else {error("参数2错误!")} } }else { if(string.match(s,"[\dA-F]"){ cmd = 31; len =2; }else { return error("参数2错误"); } } return len>0 ? sendFNC(cmd,RWAddr(address),len,s) : sendFNC(cmd,QZAddr(address),len,s); } import sio; var sport = sio.port("COM5"); var cmdPlc = cmdStr(); //设置串口(波特率,数据位,停止位,校验位) sport.ioctl(9600,7,1,"even"); sport.writeHex(sendFNC(cmdPlc.cmdRead,RWAddr("D10"),6)); sleep(100); var revcread = sport.read() var tab = tranRW("D10",revcread) for(k,v in tab){ console.log(k,"0x"++ v) } console.pause(true);
6 个回复 | 最后更新于 2021-03-24
登录后方可回帖
先点赞