aardio串口调试助手工程源码下载地址:
http://www.jianma123.com/download/串口调试助手.zip (工程资源文件内已有Pcomm.dll)
main.aardio源码:
import win.ui;
import win.reg;
import thread.command;
import console
/*DSG{{*/
mainForm = win.form(text="串口调试助手";right=564;bottom=310)
mainForm.add(
baud={cls="combobox";left=69;top=43;right=144;bottom=63;edge=1;items={"50";"75";"110";"134";"150";"300";"600";"1200";"1800";"2400";"4800";"7200";"9600";"19200";"38400";"57600";"115200";"230400";"460800";"921600"};mode="dropdown";z=3};
button={cls="button";text="开启串口";left=20;top=154;right=138;bottom=207;z=1};
button2={cls="button";text="发送";left=426;top=238;right=487;bottom=291;z=8};
button3={cls="button";text="转Hex";left=493;top=237;right=554;bottom=290;z=12};
checkbox={cls="checkbox";text="16进制显示";left=22;top=237;right=115;bottom=260;z=10};
checkbox2={cls="checkbox";text="16进制发送";left=23;top=262;right=116;bottom=285;z=11};
databit={cls="combobox";left=69;top=98;right=144;bottom=118;edge=1;items={"8";"7";"6";"5"};mode="dropdown";z=5};
parity={cls="combobox";left=69;top=71;right=144;bottom=91;edge=1;items={"None";"Even";"Odd";"Mark";"Space"};mode="dropdown";z=4};
port={cls="combobox";left=69;top=15;right=144;bottom=35;edge=1;items={};mode="dropdown";z=2};
richedit={cls="richedit";left=153;top=11;right=555;bottom=223;edge=1;multiline=1;wrap=1;z=9};
richedit2={cls="richedit";left=151;top=238;right=420;bottom=293;edge=1;multiline=1;wrap=1;z=13};
static={cls="static";text="串口号:
波特率:
校验位:
数据位:
停止位:";left=18;top=17;right=80;bottom=152;transparent=1;z=7};
stop={cls="combobox";left=69;top=126;right=144;bottom=146;edge=1;items={"1";"2"};mode="dropdown";z=6}
)
/*}}*/
import fsys.res;
..fsys.res.saveRes("/res/","/")
import bujin;
Serial = bujin.Serial()
posts = Serial.posts()
for(i=1;#posts;1){
mainForm.port.add(posts[i])
}
mainForm.port.selIndex = 1
mainForm.baud.selIndex = 13
mainForm.stop.selIndex = 1
mainForm.parity.selIndex = 1
mainForm.databit.selIndex = 1
port = mainForm.port.selText
baud = mainForm.baud.selText
stop = mainForm.stop.selText
parity = mainForm.parity.selText
databit = mainForm.databit.selText
mainForm.port.oncommand = function(id,event){
port = mainForm.port.selText
}
mainForm.baud.oncommand = function(id,event){
baud = mainForm.baud.selText
}
mainForm.stop.oncommand = function(id,event){
stop = mainForm.stop.selText
}
mainForm.parity.oncommand = function(id,event){
parity = mainForm.parity.selText
}
mainForm.databit.oncommand = function(id,event){
databit = mainForm.databit.selText
}
mainForm.button.oncommand = function(id,event){
var ret = ""
if(mainForm.button.text == "关闭串口"){
ret = Serial.close(port)
if(ret == 0){
mainForm.button.text = "开启串口"
return ;
}
}
ret = Serial.start(port,baud,stop,parity,databit);
if(ret == 0){
mainForm.button.text = "关闭串口"
}
}
mainForm.button2.oncommand = function(id,event){
var str = mainForm.richedit2.text
if(mainForm.checkbox2.checked){
//串口发送16进制的本质就是发送 ascii
//所以发送16进制前 需要先把文本框的16进制转换成 ascii 后发送
str = Serial.HextoStr(str)
Serial.write(port,str)
}else {
Serial.write(port,str)
}
}
Serial.onRead = function(str,len,port){
if(mainForm.checkbox.checked){
mainForm.richedit.text ++= Serial.StrtoHex(str)
}else {
mainForm.richedit.text ++= str
}
}
mainForm.button3.oncommand = function(id,event){
var str = mainForm.richedit2.text
mainForm.richedit2.text = Serial.StrtoHex(str)
}
mainForm.show()
return win.loopMessage();
Pcomm串口DLL封装源码:
//Pcomm 串口
/*
QQ:332133281
有问题在反馈
*/
import win.reg
import console
import Pcomm;
import thread
import thread.command
namespace bujin;
class Serial{
ctor(){
this.BaudHex = {["50"]="0x00";["75"]="0x01";["110"]="0x02";["134"]="0x03";["150"]="0x04";["300"]="0x05";["600"]="0x06";["1200"]="0x07";["1800"]="0x08";["2400"]="0x09";["4800"]="0x0a";["7200"]="0x0b";["9600"]="0x0c";["19200"]="0x0d";["38400"]="0x0e";["57600"]="0x0f";["115200"]="0x10";["230400"]="0x11";["460800"]="0x12";["921600"]="0x13"};
this.StopHex = {["1"]="0x00";["2"]="0x06"};
this.ParityHex = {["None"]="0x00";["Even"]="0x18";["Odd"]="0x08";["Mark"]="0x28";["Space"]="0x38"};
this.DatabitHex ={["8"]="0x03";["7"]="0x02";["6"]="0x01";["5"]="0x00"};
this.Baud = {50;75;110;134;150;300;600;1200;1800;2400;4800;7200;9600;19200;38400;57600;115200;230400;460800;921600}
this.Stop = {1;2};
this.Parity = {"None";"Even";"Odd";"Mark";"Space"};
this.Databit ={8;7;6;5};
..thread.command().read = function(str,len,port){
if(this.onRead){
this.onRead(str,len,port)
}
} };
posts = function(){
var reg = ..win.reg("HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM");//读取注册表串口
var posts = {}
for(name,value,t in reg.eachValue()) {
..table.push(posts,..string.match(value,"(\d+)"))
}
return posts;
}
CntIrq = function(port){
import thread.command;
Pcom = ..raw.loadDll("\res\Pcomm.dll");
Rxdata = ..raw.buffer(1000,0)
var len = ..Pcom.sio_read(port,Rxdata,#Rxdata)
var str = ..string.sub(Rxdata,1,len)
if(len<=0){
return ;
}
str = ..string.fromto(str,936,65001)//GBK 转 utf-8
..thread.command.post("read",str,len,port)
}
CntIrqFuc = ..thread.tostdcall( this.CntIrq ,"void(int)");
start = function(port,baud,stop,parity,databit){
var ret = ..Pcomm.sio_open(port)
if(ret == 0){
..Pcomm.sio_ioctl(port, this.BaudHex[baud], this.StopHex[stop] | this.ParityHex[parity] | this.DatabitHex[databit] )
..Pcomm.sio_cnt_irq(port,owner.CntIrqFuc,1)
}
return ret;
}
close = function(port){
var ret = ..Pcomm.sio_close(port)
return ret;
}
write = function(port,str){
str = ..string.fromto(str,65001,936)
..Pcomm.sio_write(port,str,#str)
}
StrtoHex = function(str){
assert(str,"StrtoHex 参数不能为空")
var _t = ""
str = ..string.fromto(str,65001,936)
for(k,v in {..string.unpack(str)}){
_t ++=..string.format("%X",v )++" "
}
return _t;
}
HextoStr = function(str){
assert(str,"HextoStr 参数不能为空")
var tab = {}
var pattern = "[A-Fa-f\d]{2}" ;
for s in ..string.gmatch(str , pattern ){
..table.push(tab,eval("0x"++s))
}
return ..string.pack(tab);
}
}
/**intellisense()
bujin.Serial() = !Serial.
!Serial.Baud = 返回波特率数组
!Serial.Stop = 返回停止位数组
!Serial.parity = 返回数据位数组
!Serial.databit = 返回数据位数组
!Serial.posts() = 返回可用串口号 数组
!Serial.start(串口号,波特率,停止位,校验位,数据位) = @.start(port,baud,stop,parity,databit)
!Serial.write(串口号,数据) = @.write(port,buf)
!Serial.close(port) = 停止串口
!Serial.StrtoHex(str) = 文本转16进制
!Serial.HextoStr(str) = 16进制转文本
!Serial.onRead = @.onRead = function(str,len,port){
__/*串口返回数据*/
}
end intellisense**/
感谢:
群友 周