171 lines
4.5 KiB
Plaintext
171 lines
4.5 KiB
Plaintext
import win.dlg.message;
|
|
import win.ui;
|
|
/*DSG{{*/
|
|
mainForm = win.form(text="串口上位机";right=885;bottom=591;border="dialog frame";max=false)
|
|
mainForm.add(
|
|
btnClear={cls="button";text="清空";left=760;top=555;right=875;bottom=582;z=6};
|
|
btnFresh={cls="button";text="刷新";left=41;top=47;right=138;bottom=101;font=LOGFONT(h=-20);z=3};
|
|
btnOpen={cls="button";text="打开";left=346;top=47;right=442;bottom=101;font=LOGFONT(h=-20);z=4};
|
|
button={cls="button";text='\u2460 设定原点';left=123;top=269;right=294;bottom=315;font=LOGFONT(h=-18);z=12};
|
|
button2={cls="button";text='\u2461 记录当前位置';left=406;top=131;right=594;bottom=191;font=LOGFONT(h=-18);z=15};
|
|
combobox={cls="combobox";left=154;top=61;right=332;bottom=87;edge=1;font=LOGFONT(h=-20);items={};mode="dropdown";z=1};
|
|
edNowloc={cls="edit";text="0";left=176;top=167;right=303;bottom=199;edge=1;multiline=1;z=11};
|
|
edXdloc={cls="edit";text="0";left=176;top=208;right=303;bottom=240;edge=1;multiline=1;z=14};
|
|
edit={cls="edit";left=496;top=35;right=870;bottom=60;disabled=1;edge=1;readonly=1;z=9};
|
|
editRx={cls="edit";left=403;top=200;right=874;bottom=546;edge=1;font=LOGFONT(h=-20);multiline=1;readonly=1;vscroll=1;z=5};
|
|
lbComPort={cls="static";text="串口信息";left=40;top=4;right=413;bottom=40;transparent=1;z=2};
|
|
static={cls="static";text="V0.1 20231215";left=16;top=563;right=134;bottom=582;font=LOGFONT(h=-16);notify=1;transparent=1;z=7};
|
|
static2={cls="static";text="当前位置";left=92;top=165;right=166;bottom=204;align="center";center=1;transparent=1;z=10};
|
|
static3={cls="static";text="报文记录";left=494;top=4;right=587;bottom=36;center=1;transparent=1;z=8};
|
|
static4={cls="static";text="相对位置";left=92;top=207;right=166;bottom=246;align="center";center=1;transparent=1;z=13}
|
|
)
|
|
/*}}*/
|
|
|
|
import sys.comPort;
|
|
import console;
|
|
import sio;
|
|
import win.timer;
|
|
|
|
|
|
var sioPort;
|
|
//串口列表
|
|
FreshHw = function(){
|
|
comPorts = sys.comPort.list();
|
|
mainForm.combobox.items = comPorts;
|
|
mainForm.combobox.selIndex = sys.comPort.find("CP210x")[["index"]] or 1;
|
|
if(#comPorts == 0){
|
|
mainForm.lbComPort.text = "没有找到硬件"
|
|
}
|
|
}
|
|
FreshHw();
|
|
|
|
|
|
//获取串口信息
|
|
mainForm.combobox.oncommand = function(id,event){
|
|
var item = comPorts[mainForm.combobox.selIndex]
|
|
if(item){
|
|
mainForm.lbComPort.text = item.friendlyName;
|
|
}
|
|
}
|
|
|
|
mainForm.btnFresh.oncommand = function(id,event){
|
|
FreshHw();
|
|
mainForm.combobox.oncommand();
|
|
}
|
|
var ProState = 0;
|
|
var LastData = 0;
|
|
var TimerCount = 0;
|
|
var Datalen = 0;
|
|
var data = {};
|
|
var tempstr = "";
|
|
var nowloc = 0;
|
|
var zeroloc = 0x8000;
|
|
var xdloc = 0;
|
|
DataPrecess = function(str){
|
|
data = string.split(str,' ');
|
|
if(data[1] != "AA" || data[2] != "55"){//检测到帧头
|
|
//tempstr += "帧头错误"
|
|
return;
|
|
}
|
|
mainForm.edit.text = str;
|
|
nowloc = ((tonumber(data[3],16)<<8) + tonumber(data[4],16));
|
|
xdloc = zeroloc-nowloc;
|
|
}
|
|
display = function(){
|
|
mainForm.edNowloc.text = nowloc;
|
|
mainForm.edXdloc.text = xdloc;
|
|
}
|
|
|
|
var datastr = "";
|
|
|
|
var timer = win.timer(mainForm,10);
|
|
timer.onTimer = function(hwnd,msg,id,tick){
|
|
display();
|
|
if(ProState == 0){
|
|
TimerCount = 0;
|
|
return ;
|
|
}
|
|
TimerCount += 1;
|
|
if(TimerCount > 3){
|
|
ProState = 0;
|
|
data = {};
|
|
TimerCount = 0;
|
|
DataPrecess(string.trim(datastr));
|
|
//mainForm.editRx.print(tempstr);
|
|
|
|
}
|
|
}
|
|
|
|
|
|
mainForm.btnOpen.oncommand = function(id,event){
|
|
if(sioPort){
|
|
sioPort.close();
|
|
}
|
|
|
|
var err;
|
|
if(#mainForm.combobox.selText > 0){
|
|
sioPort,err = sio.port(mainForm.combobox.selText);
|
|
}
|
|
else {
|
|
return ;
|
|
}
|
|
|
|
|
|
if(!sioPort){
|
|
return mainForm.msgErr(err,300);
|
|
}
|
|
sioPort.ioctl(115200,8,1,"none");
|
|
//串口接收到数据触发回调
|
|
sioPort.termCntIrq(1,function(port){
|
|
var ret = sioPort.readHex()
|
|
var num = tonumber(ret,16,2);
|
|
if(ProState == 0){
|
|
tempstr = tostring(time.now()) + " Rx:"
|
|
datastr = "";
|
|
}
|
|
if(ret != null){
|
|
tempstr += ret;
|
|
datastr += ret;
|
|
}
|
|
|
|
ProState = 1;
|
|
TimerCount = 0;
|
|
/*
|
|
if(num != null){
|
|
DataPrecess(num);
|
|
}
|
|
*/
|
|
} )
|
|
|
|
mainForm.msgOk("已打开端口",200);
|
|
timer.enable();
|
|
}
|
|
|
|
mainForm.onDestroy = function(){
|
|
if(sioPort){
|
|
sioPort.close();
|
|
}
|
|
}
|
|
TxMsg = function(str){
|
|
if(sioPort == null){
|
|
mainForm.msgErr("端口未打开",300);
|
|
return;
|
|
}
|
|
//mainForm.editRx.print(tostring(time.now()) + " Tx: " + string.upper(str));
|
|
sioPort.writeHex(str);
|
|
return;
|
|
}
|
|
|
|
mainForm.button.oncommand = function(id,event){
|
|
zeroloc = nowloc;
|
|
mainForm.editRx.print("设定位置 "++nowloc++" 为原点");
|
|
}
|
|
|
|
mainForm.button2.oncommand = function(id,event){
|
|
mainForm.editRx.print("记录当前位置:"++nowloc++",相对位置:"++xdloc);
|
|
}
|
|
|
|
mainForm.combobox.oncommand();
|
|
|
|
mainForm.show();
|
|
return win.loopMessage(); |