import win.dlg.message; import win.ui; /*DSG{{*/ var winform = win.form(text="简单串口助手";right=846;bottom=465) winform.add( btnOpen={cls="button";text="打开端口";left=85;top=411;right=183;bottom=445;db=1;dl=1;z=7}; btnR={cls="button";text="R";left=196;top=178;right=307;bottom=241;z=13}; btnT={cls="button";text="T";left=51;top=166;right=143;bottom=225;z=12}; btnTx={cls="button";text="发送";left=582;top=411;right=680;bottom=445;db=1;dr=1;z=6}; button={cls="button";text="Button";left=720;top=419;right=793;bottom=446;z=9}; button2={cls="button";text="Button";left=726;top=351;right=793;bottom=399;z=11}; combobox={cls="combobox";left=30;top=13;right=169;bottom=39;dl=1;dt=1;edge=1;items={};mode="dropdown";z=2}; editRx={cls="edit";left=34;top=64;right=732;bottom=111;db=1;dl=1;dr=1;dt=1;edge=1;multiline=1;z=3}; editTx={cls="edit";left=240;top=414;right=569;bottom=446;db=1;dl=1;dr=1;edge=1;z=5}; lbComPort={cls="static";text="没有找到任何串口,建议安装虚拟串口驱动: Virtual Serial Port Driver";left=199;top=16;right=607;bottom=40;dl=1;dr=1;dt=1;transparent=1;z=4}; openDeviceManager={cls="plus";text="打开设备管理器";left=612;top=14;right=732;bottom=38;align="left";color=8388608;font=LOGFONT(h=-13);notify=1;textPadding={left=5};z=8}; static={cls="static";text="Static";left=71;top=341;right=203;bottom=367;transparent=1;z=10}; trackbar={cls="plus";left=106;top=284;right=548;bottom=299;bgcolor=14265123;border={radius=-1};foreRight=15;forecolor=1865727;paddingBottom=5;paddingTop=5;z=1} ) /*}}*/ //串口列表 import sys.comPort; var comPorts = sys.comPort.list(); winform.combobox.items = comPorts; winform.combobox.selIndex = sys.comPort.find("CP210x")[["index"]] or 1; //获取串口信息 winform.combobox.oncommand = function(id,event){ var item = comPorts[winform.combobox.selIndex] if(item){ winform.lbComPort.text = item.friendlyName; } } winform.combobox.oncommand(); //串口发送 import sio; var sioPort; winform.btnTx.oncommand = function(id,event){ if(!sioPort){ winform.btnOpen.oncommand(); } if( !sioPort.write( winform.editTx.text ) ){ return winform.msgErr("发送失败"); } } //打开串口 winform.btnOpen.oncommand = function(id,event){ if(sioPort){ sioPort.close(); } var err; sioPort,err = sio.port(winform.combobox.selText); if(!sioPort){ return winform.msgErr(err); } sioPort.ioctl(1e6,8,1); //串口接收到数据触发回调 sioPort.termCntIrq(1,function(port){ var r = sioPort.read(); //winform.editRx.appendText(r); if(r == "try"){ //sioPort.write("hello") } } ) winform.msgOk("已打开端口",1000) } winform.onDestroy = function(){ if(sioPort){ sioPort.close(); } } import sys.device; winform.openDeviceManager.oncommand = function(id,event){ sys.device.manager(); } winform.openDeviceManager.skin({ color={ active=0xFF00FF00; default=0xFF000080; disabled=0xFF6D6D6D; hover=0xFFFF0000 } }) var pos=0 update = function(winform,length,total,pos){ import console winform.trackbar.progressPos = math.round(length/total*100,1) winform.static.text = winform.trackbar.progressPos //console.log(winform.trackbar.progressPos) } winform.trackbar.setTrackbarRange(1,100) winform.button.oncommand = function(id,event){ //var rst = sioPort.request("try",,5) //winform.editRx.appendText("rec---"++rst) sioPort.transmitAscii("E:\OneDrive - Cutting-Edge\Desktop\组成.png", function(length,bufSize,buf,total){ if(length >pos){ pos = length thread.invokeAndWait(update,winform,length,total) } } ) winform.editRx.appendText("456"); } winform.button2.oncommand = function(id,event){ sioPort.abortWrite() } winform.btnT.oncommand = function(id,event){ /* sioPort.transmitXmodem1kCrc("E:\OneDrive - Cutting-Edge\Desktop\组成.png", function(length,bufSize,buf,total){ if(length >pos){ pos = length thread.invokeAndWait(update,winform,length,total) } } ) */ //pos=0 sioPort.transmitZmodem("E:\OneDrive - Cutting-Edge\Desktop\组成.png", function(length,bufSize,buf,total){ if(length >pos){ pos = length thread.invokeAndWait(update,winform,length,total) } } ) } winform.btnR.oncommand = function(id,event){ var sta =sioPort.receiveXmodem1kCrc( function(length,bufSize,buf,total){ if(length >pos){ pos = length thread.invokeAndWait(update,winform,length,total) } } ) //pos=0 /** sioPort.receiveAscii(5/*秒*/, function(length,bufSize,buf,total){ if(length >pos){ pos = length thread.invokeAndWait(update,winform,length,total) } } ) **/ console.log("sta---",sta) } winform.show(); win.loopMessage();