235 lines
6.7 KiB
Plaintext
235 lines
6.7 KiB
Plaintext
|
import fonts.fontAwesome;
|
||
|
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=751;top=549;right=866;bottom=576;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="发送";left=213;top=435;right=371;bottom=483;z=21};
|
||
|
combobox={cls="combobox";left=154;top=61;right=332;bottom=87;edge=1;font=LOGFONT(h=-20);items={};mode="dropdown";z=1};
|
||
|
edChangeCur={cls="edit";text="0";left=146;top=389;right=246;bottom=422;center=1;edge=1;font=LOGFONT(h=-19);z=19};
|
||
|
edOutputCur={cls="edit";text="0";left=146;top=217;right=246;bottom=250;center=1;edge=1;font=LOGFONT(h=-19);readonly=1;z=13};
|
||
|
edOutputVol={cls="edit";text="0";left=146;top=170;right=246;bottom=203;center=1;edge=1;font=LOGFONT(h=-19);readonly=1;z=9};
|
||
|
edTargetCur={cls="edit";text="0";left=146;top=263;right=246;bottom=296;center=1;edge=1;font=LOGFONT(h=-19);readonly=1;z=14};
|
||
|
editRx={cls="edit";left=476;top=33;right=859;bottom=538;edge=1;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="V1.0 20250707";left=16;top=563;right=134;bottom=582;font=LOGFONT(h=-16);notify=1;transparent=1;z=7};
|
||
|
static10={cls="static";text="mA";left=256;top=389;right=314;bottom=423;align="center";center=1;font=LOGFONT(h=-19);transparent=1;z=20};
|
||
|
static2={cls="static";text="输出电压";left=30;top=172;right=123;bottom=206;align="center";center=1;font=LOGFONT(h=-19);transparent=1;z=10};
|
||
|
static3={cls="static";text="输出电流";left=30;top=216;right=123;bottom=250;align="center";center=1;font=LOGFONT(h=-19);transparent=1;z=11};
|
||
|
static4={cls="static";left=117;top=118;right=316;bottom=147;center=1;font=LOGFONT(h=-20);transparent=1;z=8};
|
||
|
static5={cls="static";text="目标电流";left=31;top=263;right=124;bottom=297;align="center";center=1;font=LOGFONT(h=-19);transparent=1;z=12};
|
||
|
static6={cls="static";text="mV";left=253;top=167;right=311;bottom=201;align="center";center=1;font=LOGFONT(h=-19);transparent=1;z=15};
|
||
|
static7={cls="static";text="mA";left=253;top=211;right=311;bottom=245;align="center";center=1;font=LOGFONT(h=-19);transparent=1;z=16};
|
||
|
static8={cls="static";text="mA";left=253;top=257;right=311;bottom=291;align="center";center=1;font=LOGFONT(h=-19);transparent=1;z=17};
|
||
|
static9={cls="static";text="修改电流";left=31;top=388;right=124;bottom=422;align="center";center=1;font=LOGFONT(h=-19);transparent=1;z=18}
|
||
|
)
|
||
|
/*}}*/
|
||
|
|
||
|
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();
|
||
|
|
||
|
RxPrint = function(str){
|
||
|
var timenow = time.now();
|
||
|
mainForm.editRx.print(tostring(timenow) ++ str);
|
||
|
}
|
||
|
TxPrint = function(str){
|
||
|
mainForm.editRx.print(str);
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
//获取串口信息
|
||
|
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 data = {};
|
||
|
var buf = {};
|
||
|
|
||
|
var runtime = 0;
|
||
|
var current_offset = 0;
|
||
|
var output_Cur = 0;
|
||
|
var output_Vol = 0;
|
||
|
var target_Cur = 0;
|
||
|
|
||
|
display = function(){
|
||
|
|
||
|
mainForm.edOutputVol.text = output_Vol;
|
||
|
mainForm.edOutputCur.text = output_Cur/100;
|
||
|
mainForm.edTargetCur.text = target_Cur/100;
|
||
|
|
||
|
}
|
||
|
import console;
|
||
|
MsgProcess = function(data){
|
||
|
//console.dumpJson(data);
|
||
|
if(#data == 10){
|
||
|
runtime = (data[1]<<8) + data[2];
|
||
|
current_offset = (data[3]<<8) + data[4];
|
||
|
output_Vol = (data[5]<<8) + data[6];
|
||
|
output_Cur = (data[7]<<8) + data[8];
|
||
|
target_Cur = (data[9]<<8) + data[10];
|
||
|
var str = "rx:"
|
||
|
for(i=1;#data;1){
|
||
|
str += string.format(" %02X",data[i]);
|
||
|
}
|
||
|
RxPrint(str);
|
||
|
|
||
|
}
|
||
|
}
|
||
|
|
||
|
DataPrecess = function(str){
|
||
|
data = string.split(str,' ');
|
||
|
var newdata = {};
|
||
|
if(#data > 0){
|
||
|
//table.append(buf,data);
|
||
|
//console.log("data");
|
||
|
//console.dumpJson(data);
|
||
|
for(i=1;#data;1){
|
||
|
newdata[i] = tonumber(data[i],16);
|
||
|
}
|
||
|
//console.log("newdata");
|
||
|
//console.dumpJson(newdata);
|
||
|
table.append(buf,newdata);
|
||
|
}
|
||
|
|
||
|
|
||
|
}
|
||
|
|
||
|
uartdataProcess = function(){
|
||
|
if(#buf > 13){
|
||
|
var len = #buf;
|
||
|
var str = "";
|
||
|
//console.dumpJson(buf);
|
||
|
//console.log("len = " ++ len);
|
||
|
for(i=1;len-13;1){
|
||
|
if(buf[i] == 0x55 && buf[i+1] == 0xAA && buf[i+12] == 0x5A){
|
||
|
MsgProcess(table.slice(buf,i+2,i+11));
|
||
|
//buf = table.slice(buf,14);
|
||
|
table.clear(buf);
|
||
|
return ;
|
||
|
}
|
||
|
}
|
||
|
buf = table.slice(buf,len-13);
|
||
|
//table.clear(buf);
|
||
|
//RxPrint(str);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
var timer = win.timer(mainForm,100);
|
||
|
timer.onTimer = function(hwnd,msg,id,tick){
|
||
|
uartdataProcess();
|
||
|
display();
|
||
|
//mainForm.editRx.print("test");
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
mainForm.btnOpen.oncommand = function(id,event){
|
||
|
if(sioPort){
|
||
|
sioPort.close();
|
||
|
}
|
||
|
|
||
|
var err;
|
||
|
sioPort,err = sio.port(mainForm.combobox.selText);
|
||
|
if(!sioPort){
|
||
|
return mainForm.msgErr(err,300);
|
||
|
}
|
||
|
sioPort.ioctl(115200,8,1,"none");
|
||
|
//串口接收到数据触发回调
|
||
|
sioPort.termCntIrq(1,function(port){
|
||
|
var ret = sioPort.readHex()
|
||
|
if(ret != null){
|
||
|
DataPrecess(string.trim(ret));
|
||
|
}
|
||
|
|
||
|
} )
|
||
|
sioPort.setBaudRate(115200);
|
||
|
mainForm.msgOk("已打开端口",200);
|
||
|
mainForm.combobox.disabled = 1;
|
||
|
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;
|
||
|
}
|
||
|
|
||
|
msg_tx = function(){
|
||
|
var data = (tf_state<<2) | (jr_state);
|
||
|
var str = tostring(data,16);
|
||
|
str = string.right(str,1);
|
||
|
TX_str = str ++ "0 FC"
|
||
|
//mainForm.editRx.print(TX_str);
|
||
|
}
|
||
|
|
||
|
|
||
|
mainForm.btnClear.oncommand = function(id,event){
|
||
|
mainForm.editRx.text = ""
|
||
|
}
|
||
|
|
||
|
mainForm.button.oncommand = function(id,event){
|
||
|
var new_cur = tonumber(mainForm.edChangeCur.text);
|
||
|
if(new_cur >= 1 && new_cur<=100){
|
||
|
new_cur = math.floor(new_cur * 100)
|
||
|
txbuf = {0x5A,0xA5,0x02,new_cur>>8,new_cur&0xff};
|
||
|
var str = "tx:"
|
||
|
var datastr = "";
|
||
|
for(i=1;#txbuf;1){
|
||
|
datastr = datastr ++ string.format("%02X",txbuf[i]) ++ " ";
|
||
|
}
|
||
|
datastr = string.trim(datastr);
|
||
|
TxPrint(str ++ datastr);
|
||
|
|
||
|
TxMsg(datastr);
|
||
|
//sioPort.write(sioPort);
|
||
|
//TxMsg("5A A5 02 01 F4");
|
||
|
}
|
||
|
else {
|
||
|
TxPrint("电流值范围1-100mA");
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
mainForm.combobox.oncommand();
|
||
|
display();
|
||
|
mainForm.show();
|
||
|
return win.loopMessage();
|