分享一个ZylSerialPortAXControl.ocx串口控件
无聊河蟹了个串口控件, 其实没啥用, 网上瞎逛看到的.
我们都有sio控件和其他一些dll可以使用了, 看看就算了.
这里发出来给喜欢折腾的人吧
此控件支持com.lite免注册调用

import win.ui;
/*DSG{{*/
var winform = win.form(text="aardio form";right=540;bottom=205)
winform.add(
button={cls="button";text="打开COM1";left=19;top=37;right=175;bottom=98;dl=1;dt=1;z=1};
button2={cls="button";text="关闭串口";left=19;top=126;right=175;bottom=187;db=1;dl=1;z=2};
edit={cls="edit";left=192;top=36;right=526;bottom=187;border=1;db=1;dl=1;dr=1;dt=1;multiline=1;z=3};
static={cls="static";text="接受到:";left=192;top=10;right=315;bottom=37;border=1;center=1;dl=1;dr=1;dt=1;font=LOGFONT(h=-16);transparent=1;z=4}
)
/*}}*/
import console
console.open()
import com.lite;
var dll = com.lite.appData("\popdes\ZylSerialPortAXControl.ocx",$"\res\ZylSerialPortAXControl.ocx");
var ZylSerialPortAX = dll.createObject("{F66B5918-ACF6-49D3-86A4-FEF1003E4E22}");
com.Connect(ZylSerialPortAX,{
OnReceive = function(str){
ZylSerialPortAX.OnReceive(str);
}
});
ZylSerialPortAX.OnReceive = function(str){
console.log(str)
winform.edit.log( str,'\r\n' )
}
winform.button.oncommand = function(id,event){
ZylSerialPortAX.Port = 1
ZylSerialPortAX.Open()
}
winform.button2.oncommand = function(id,event){
ZylSerialPortAX.Close();
}
winform.show();
win.loopMessage();最近用了一下,感觉对8位数据位支持不太好(打开串口时用的参数是 8 位数据位)。
比如,我发:
1B 01 01 01 00 00 00 00 00 00 00 00 00 00 00 00 E9 33 AD 8C 9F BE 76 C8 E9 33 AD 8C A0 00 00 00 E9 33 AD 8C A0 00 00 00 E9 33 AD 8C A0 00 00 00
监控到发出的是:
1B 01 01 01 00 00 00 00 00 00 00 00 00 00 00 00 3F 33 3F 3F 3F 3F 76 3F 3F 33 3F 3F 3F 00 00 00 3F 33 3F 3F 3F 00 00 00 3F 33 3F 3F 3F 00 00 00
我发:
01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F 20 21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E 2F 30 31 32 33 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F 40 41 42 43 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F 50 51 52 53 54 55 56 57 58 59 5A 5B 5C 5D 5E 5F 60 61 62 63 64 65 66 67 68 69 6A 6B 6C 6D 6E 6F 70 71 72 73 74 75 76 77 78 79 7A 7B 7C 7D 7E 7F 80 81 82 83 84 85 86 87 88 89 8A 8B 8C 8D 8E 8F 90 91 92 93 94 95 96 97 98 99 9A 9B 9C 9D 9E 9F A0 A1 A2 A3 A4 A5 A6 A7 A8 A9 AA AB AC AD AE AF B0 B1 B2 B3 B4 B5 B6 B7 B8 B9 BA BB BC BD BE BF C0 C1 C2 C3 C4 C5 C6 C7 C8 C9 CA CB CC CD CE CF D0 D1 D2 D3 D4 D5 D6 D7 D8 D9 DA DB DC DD DE DF E0 E1 E2 E3 E4 E5 E6 E7 E8 E9 EA EB EC ED EE EF F0 F1 F2 F3 F4 F5 F6 F7 F8 F9 FA FB FC FD FE FF
监控到发出的是:
01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F 20 21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E 2F 30 31 32 33 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F 40 41 42 43 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F 50 51 52 53 54 55 56 57 58 59 5A 5B 5C 5D 5E 5F 60 61 62 63 64 65 66 67 68 69 6A 6B 6C 6D 6E 6F 70 71 72 73 74 75 76 77 78 79 7A 7B 7C 7D 7E 7F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 3F
登录后方可回帖
感谢: indertust 封装并分享此调用库 , 库代码如下:
class ZylSerial{ ctor(){ this.rxCount = 0; this.txCount = 0; this.rxBytes = 0; this.txBytes = 0; this.eventConnected = false; }; open = function(portName='COM1', baudRate=9600, dataWidth=8, parityBits='n', stopBits='1'){ this.port, this.info = open(portName, baudRate, dataWidth, parityBits, stopBits) //.._p('this.info', this.info) return this.port, this.info } sendString = function(str){ if((this.port != 0) and (type(str) == type.string) and (#str > 0)){ this.txCount++ this.txBytes += #str this.rxData = str return sendString(str) } } sendByte = function(byte){ if((this.port != 0) and (type(byte) == type.number) and (byte >= 0) and (byte <= 255)){ this.txCount++ this.txBytes += 1 return sendByte(byte) } } sendHex = function(str){ if((this.port != 0) and (type(str) == type.string) and (#str > 0)){ var byte for(i=1;#str;1){ byte = str[i] sendByte(byte) } this.txCount++ this.txBytes += #str return #str } } resetCounts = function(){ this.rxCount = 0; this.txCount = 0; this.rxBytes = 0; this.txBytes = 0; } close = function(){ if(this.port != 0){ close() } } about = function(){ if(ready){ ZylSerialPortAX.AboutBox() } } isConnected = function(){ if(ready){ this.port = ZylSerialPortAX.IsConnected() return this.port } } SetOnReceiveCallbackFunction = function(fun){ if(ZylSerialPortAX.IsConnected() and (type(fun) == type.function)){ ZylSerialPortAX.OnReceive = function(str){ fun(str) } if(this.eventConnected == false){ com.Connect(ZylSerialPortAX,{ OnReceive = function(str){ this.txData = str this.rxCount++ this.rxBytes += #str ZylSerialPortAX.OnReceive(str); } }); this.eventConnected = true } } } } namespace ZylSerial{ import com.lite; import io; string = ..string; ParityBitsValue = { None = 0; Odd = 1; Even = 2; Mark = 3; Space = 4; none = 0; odd = 1; even = 2; mark = 3; space = 4; n = 0; o = 1; e = 2; m = 3; s = 4; N = 0; O = 1; E = 2; M = 3; S = 4; }; DataWidthValue = { [0] = -5; [1] = -4; [2] = -3; [3] = -2; [4] = -1; [5] = 0; [6] = 1; [7] = 2; [8] = 3; ['5'] = 0; ['6'] = 1; ['7'] = 2; ['8'] = 3; }; StopBitsValue = { [1] = 0; [2] = 2; ['1'] = 0; ['1.5'] = 1; ['2'] = 2; }; getBaudRateValue = function(baudrate){ var baudRateValue = 6; select(baudrate) { case 110 { baudRateValue = 0 } case 300 { baudRateValue = 1 } case 600 { baudRateValue = 2 } case 1200 { baudRateValue = 3 } case 2400 { baudRateValue = 4 } case 4800 { baudRateValue = 5 } case 9600 { baudRateValue = 6 } case 14400 { baudRateValue = 7 } case 19200 { baudRateValue = 8 } case 38400 { baudRateValue = 9 } case 57600 { baudRateValue = 10 } case 115200 { baudRateValue = 11 } case 128000 { baudRateValue = 12 } case 256000 { baudRateValue = 13 } case 460800 { baudRateValue = 14 } case 921600 { baudRateValue = 15 } } return baudRateValue } getErrorInfo = function(es){ var errInfo = string.match(es, "<@@{Error}:@>([^{]+)") return tostring(errInfo) } parsePortName = function(portName = 'COM1'){ var portNumber = string.match(portName, "\d+") return tonumber(portNumber) } //var dll = com.lite.appData("\ZylSerialPortAXControl.ocx",$"\res\ZylSerialPortAXControl.ocx"); if(io.exist("\ZylSerialPortAXControl.ocx")){ dll = com.lite("\ZylSerialPortAXControl.ocx") if(dll){ ZylSerialPortAX = dll.createObject("{F66B5918-ACF6-49D3-86A4-FEF1003E4E22}"); } if(ZylSerialPortAX){ ready = true } } if(ready){ about = function(){ ZylSerialPortAX.AboutBox() } open = function(portName='COM1', baudRate=9600, dataWidth=8, parityBits='n', stopBits='1'){ ZylSerialPortAX.Port = parsePortName(portName) ZylSerialPortAX.BaudRate = getBaudRateValue(baudRate) ZylSerialPortAX.DataWidth = DataWidthValue[dataWidth] ZylSerialPortAX.Parity = ParityBitsValue[parityBits] ZylSerialPortAX.StopBits = StopBitsValue[stopBits] if(ZylSerialPortAX.IsConnected()){ ZylSerialPortAX.Close() } var info = 'ok' try{ ZylSerialPortAX.Open() }catch(e){ //在这里 return 只会跳出 try 块,并不会从本函数返回,所以需要域外变量传过去返回 //return ZylSerialPortAX.IsConnected(), getErrorInfo(e) info = getErrorInfo(e) } return ZylSerialPortAX.IsConnected(), info } sendString = function(str){ if((type(str) == type.string) and (#str >= 0)){ if(ZylSerialPortAX.IsConnected()){ return ZylSerialPortAX.SendString(str) } } } sendByte = function(byte){ if((type(byte) == type.number) and (byte >= 0) and (byte <= 255)){ if(ZylSerialPortAX.IsConnected()){ return ZylSerialPortAX.SendByte(byte) } } } close = function(){ ZylSerialPortAX.Close() } } } /**intellisense() ZylSerial = ZylSerial 串口库 ZylSerial() = !ZylSerial. end intellisense**/ /**intellisense(!ZylSerial) open(portname, baudrate, datawidth, paritybits, stopbits) = 打开串口 sendString(str) = 发送字符串,返回已发送数据长度 sendHex(str) = 发送 16 进制字符串,返回已发送数据长度 sendByte(byte) = 发送字节(数值: 0~255) SetOnReceiveCallbackFunction(fun) = 设置接收回调函数,必须在调用 open 之后,只能设置一次,内部已禁止多次调用 close() = 关闭串口 about() = 显示关于窗口 resetCounts() = 复位包和字节计数 rxCount = 接收包数 txCount = 发送包数 rxBytes = 接收字节数 txBytes = 发送字节数 rxData = 最后一次接收的数据 txData = 最后一次发送的数据 port = 当前打开的串口 isConnected() = 串口是否已连接,如果是,则返回串口编号,否则,返回 0 end intellisense**/