lua脚本

local child=channel.GetChild(0); 取得|调用公共模块
channel.GetPublicFromGroup(poolname,channelname);

如:想调用池名poolname="start"中的模块名为channelname="test"的模块
local pCall=channel.GetPublicFromGroup("start","test");
如为ChannelCall类型,则为: pCall:CallChannel();
如为Text 类型,则为 pCall:GetText();
如为Value类型,则为 pCall:GetValue();改变模块的名字 ChannelName (官方未公布)
channel.GetChild(0):SetName("new channel name!");

注意:不能设空名字 如:"" ,另外没有 GetName(),该功能可以在 TextOperator->Get channel name of child 找到.设置Q3D中模块的值(value)
 channel.GetChild(0):SetValue(12);取得Q3D中模块的值(value)
 local v=channel.GetChild(0):GetValue();设置Q3D中模块的文本(Text)
 channel.GetChild(0):SetText("Hello");取得Q3D中模块的文本(Text)
 local v=channel.GetChild(0):GetText(); 调用Q3D中模块执行
 channel.GetChild(1):CallChannel();取得系统的Tick Counter
 local variable = q.GetTick();加载cgr文件
 q.LoadChannelGroup(".//1.cgr", "test1", 0);     --加载当前目录下的1.cgr 载入成Q3D的"test1"的池;卸载cgr文件
 q.RemoveChannelGroup("test1",0);     --卸载Q3D的"test1"的池;


求取文本行数
功能:得到文本和行数
输入:已知待求文本
输出:文本行数 Value


--Get Text row counter
function GetTextRowCounter(txt)
  if txt=="" then return 0; end
 local pos=0;
 local s=1;
 local nLine=1;
 while pos~=nil do
  pos=string.find(txt,"/n",s);
  if pos~=nil then
   nLine=nLine+1;
   s=pos+1; 
  end
 end
 return nLine;
end


移除文本第一行
功能:移除文本第一行,得到余下文本
输入:已知待求文本
输出:移除第一行后的文本


function RemoveFirstLine(txt)
 local pos=string.find(txt,"/n",1);
 if pos==nil then
  return "";
 else
  return string.sub(txt,pos+1,-1);
 end
end


按指定格式连续匹配
msg ="as33das=b5bvvb,xxc56xx;";
_, _, a, b, c = string.find(msg, "(%w+)%s*=(%w+)%s*,(%w+)%s*;");(%w+)指所有字母连续配置
([^=]+)%s*= 这种指匹配非 ”=” 用来以= 来分隔文本


a=“as33das”
b=“b5bvvb”
c=“xxc56xx”


如读取配置文本


[BarNumb]=3;
[Bar1]=测试1;{
     default,0,默认工具;
}
[Bar2]=测试2;{
     InsBucketTruckA,0,绝缘斗臂车;
}用 _,_,c=string.find(txt,"%[BarNumb]=([^;]+)%s*;",1);

c="3";

消除文本特定符号
消除空格

local txt="aabb  cc dd ee XX !";
local out=string.gsub(txt," ","");
 
-- out 变量为 "aabbccddeeXX!"消除回车

local out=string.gsub(txt,"/r/n","");消除Tab缩行

local out=string.gsub(txt,"/t","");

16进制转2进制

function Format16To2(str)
 local N=string.upper(string.sub(str,1,1));
 local r="";
 if N=="0" then r="0000";
 elseif N=="1" then r="0001";
 elseif N=="2" then r="0010";
 elseif N=="3" then r="0011";
 elseif N=="4" then r="0100";
 elseif N=="5" then r="0101";
 elseif N=="6" then r="0110";
 elseif N=="7" then r="0111";
 elseif N=="8" then r="1000";
 elseif N=="9" then r="1001";
 elseif N=="A" then r="1010";
 elseif N=="B" then r="1011";
 elseif N=="C" then r="1100";
 elseif N=="D" then r="1101";
 elseif N=="E" then r="1110";
 elseif N=="F" then r="1111";
 end
 return r;
end10进制转16进制

 local input=channel.GetChild(0):GetValue();
 local b16= string.format("%x",input );
 channel.GetChild(1):SetText(b16);10进制转换为2进制 str = string.format(”%b”,23 );

2进制转换为10进制 n = tonumber(str,2)

10进制转换为8进制 str = string.format(”%o”,23 );

8进制转换为10进制 n = tonumber(str,8)

10进制转换为16进制 str = string.format(”%x”,23 );

16进制转换为10进制 n = tonumber(str,16)


按指定分隔符来分离文本
功能:按指定分隔符来分离文本
输入:已知待求文本
输出:返回数组t[] 及数组的大小 (注:Lua数组下标是从t[1]开始的)


function SpliteToTable(s,splite)
 if s=="" or s==nil then return; end
 
 local e=0;
 local i=0;
 local t={}; --table
 
 while e~=nil do
  if e~=0 then
   local temp=string.sub(s,1,e-1);
 if splite=="/r/n" then
  s=string.sub(s,e+2,-1);
 else
  s=string.sub(s,e+1,-1);
 end
 
   table.insert(t,temp);
   i=i+1;
  end
  e=string.find(s,splite,1);
 end
 if s~="" then
  table.insert(t,s);
  i=i+1;
 end
 
 return t,i;
end  


数学函数库
函数  功能 
math.abs(x)  返回x的绝对值
math.ceil(x)  返回不小于x的最小整数
math.deg(x)  将弧度制的x转化为角度
math.exp(x)  返回e的x次方
math.floor(x)  返回不大于x的最大整数
math.fmod(x, y)  或x % y, 返回x除以y的余数(Q3D不支持)
math.mod(x,y)  x % y,返回x除以y的余数,如:math.mod(3,5); –取3/5的余数(Q3D支持)
math.log(x)  返回x的自然对数值
math.log10(x)  返回x的常用对数值
math.max(x, y, z, …)  返回参数列表中的最大值
math.min(x, y, z, …)  返回参数列表中的最小值
math.modf(x)  返回两个值, 依次为x的整数和小数部分
math.pi  圆周率常量
math.pow(x, y)  计算并返回x的y次方
math.rad(x)  将角度制的x转化为弧度
math.random(m, n)  产生随机数, 参数可选并影响范围: 无参数时为[0, 1),单参数时为[1, m], 双参数时为[m, n]
math.randomseed(x)  重设随机种子
math.sqrt(x)  开平方根运算


数组相关
定义一个数组

  t={0,1,2,3,4};取得数组长度

  local len=table.getn(t) --得到 Table 的长度遍历TABLE

 local x="";
 
 for i, value in ipairs(t) do
  x=x..t[i];
 end
 
 --最后 x=01234


重要串口格式分离
功能:按指定过过滤格式分离文本
输入:已知待求文本,过滤匹配格式
输出:过滤文本,剩余文本


-- commandfilter  "AA BB XX XX XX XX EF BB"  调用这个函数即可以分离
function ClipCommand(str,commandfilter)
 local h,e,offset=DealFilterCommand(commandfilter);
 local doCom,subCom=FliterCommand(str,h,e,offset);
 return doCom,subCom;
end
 
 
-- Do splite the head & end and offset -----
--按指定格式分离出,命令的表头,表尾及头尾偏移值 如:AA BB XX XX XX XX EF  "AA BB"表头,"EF"表尾
function DealFilterCommand(commandfilter)
 if commandfilter=="" or commandfilter==nil then return "",0,""; end
 local splite=" "; --Splite sign
 local s=commandfilter;
 local e=0;
  local i=0;
  local t={}; --table
 while e~=nil do
  if e~=0 then
     local temp=string.sub(s,1,e-1);
     s=string.sub(s,e+1,-1);
     table.insert(t,temp);
     i=i+1;
   end
   e=string.find(s,splite,1);
  end
  if s~="" then
   table.insert(t,s);
   i=i+1;
  end
 
 -- Get The Table
 local cfhead="";
 local cfend="";
 local bWriteHead=1;
 local offsetpos=0;
 e=1;
 while e<=i do
  if bWriteHead==1 then -- write to head
   if t[e]~="XX" then
    if cfhead=="" then
     cfhead=t[e];
    else
     cfhead=cfhead.." "..t[e];
    end
    e=e+1;
   else
    bWriteHead=0;
   end
  else  --write to end
   if t[e]~="XX" then
    if cfend=="" then
     cfend=t[e];
     offsetpos=3*(e-1);
    else
     cfend=cfend.." "..t[e];
    end
   end
   e=e+1;
  end
 end
 
 return cfhead,cfend,offsetpos;
end
 
 
--按表头表尾及偏移值 坐指定文本中,过虑出,要的命令,及余下的命令。
-- filter one command and sub last
function FliterCommand(str,chead,cend,offset)
 local len=string.len(str);
 local doCom="";
 local subCom="";
 if len <6 then return "",str; end
 local substr=str;
 local s;
 local bContinue=1;
 local rs=0;  -- str get the true command pos
 
 while bContinue==1 do
  s=string.find(substr,chead);
  -- if not found-------------
  if s==nil then
   bContinue=0;
   return "",str;
  end
 
  -- found it -----------------
  len=string.len(cend);
  local start=s+offset;
  local getend=string.sub(substr,start,start+len-1);
 
  -- if no end ----------------
  if getend==nil then
   bContinue=0;
   channel.GetChild(3):SetValue(1);
   return "",str;
  end 
 
  if getend~=cend then -- if not the right end continue
   bContinue=1;
   substr=string.sub(substr,s+string.len(chead)+1);
   rs=rs+s;
   channel.GetChild(3):SetValue(3);
 
  else
   bContinue=0;
   doCom=string.sub(substr,s,start+len-1);
   subCom=string.sub(substr,start+len+1,-1);
   local templen=string.len(subCom)+string.len(doCom)+3;
   local headCom=string.sub(str,1,-templen);
   subCom=headCom.." 00 "..subCom;
  end
 
 end
 
 return doCom,subCom;
end

Quest4.3 取得用户WINDOWS版本国家
 local t=os.setlocale("","time");
 channel.GetChild(0):SetText(t);返回:Chinese (Simplified)_People's Republic of China.936

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值