axuanup 发表于 2024-2-5 15:22:04

Modbus TCP&modbus rtu协议库 上位机开发

import win.ui;
import sys.comPort;
import modbus.modbustcp;
import wsock.tcp.client;
/*DSG{{*/
mainForm = win.form(text="zmodbus";right=663;bottom=623)
mainForm.add(
button={cls="button";text="01功能码";left=8;top=568;right=96;bottom=600;db=1;dl=1;z=68};
button2={cls="button";text="02功能码";left=128;top=568;right=216;bottom=600;db=1;dl=1;z=69};
button3={cls="button";text="03功能码";left=248;top=568;right=336;bottom=600;db=1;dl=1;z=70};
button4={cls="button";text="04功能码";left=360;top=568;right=448;bottom=600;db=1;dl=1;z=71};
["button_COM_断开"]={cls="button";text="断开连接";left=456;top=208;right=544;bottom=240;dr=1;dt=1;z=67};
["button_COM_连接"]={cls="button";text="连接从机";left=352;top=208;right=440;bottom=240;dr=1;dt=1;z=66};
["button_TCP_断开"]={cls="button";text="断开连接";left=152;top=208;right=240;bottom=240;dl=1;dt=1;z=65};
["button_TCP_连接"]={cls="button";text="连接从机";left=48;top=208;right=136;bottom=240;dl=1;dt=1;z=64};
["button_写单个寄存器"]={cls="button";text="写单个寄存器";left=384;top=488;right=472;bottom=520;db=1;dr=1;z=52};
["button_写单个线圈"]={cls="button";text="写单个线圈";left=64;top=488;right=152;bottom=520;db=1;dl=1;z=34};
["button_写多个寄存器"]={cls="button";text="写多个寄存器";left=544;top=488;right=632;bottom=520;db=1;dr=1;z=59};
["button_写多个线圈"]={cls="button";text="写多个线圈";left=224;top=488;right=312;bottom=520;db=1;dl=1;z=41};
["button_读单个寄存器"]={cls="button";text="读单个寄存器";left=384;top=448;right=472;bottom=480;db=1;dr=1;z=51};
["button_读单个线圈"]={cls="button";text="读单个线圈";left=64;top=448;right=152;bottom=480;db=1;dl=1;z=33};
["button_读多个寄存器"]={cls="button";text="读多个寄存器";left=544;top=448;right=632;bottom=480;db=1;dr=1;z=58};
["button_读多个线圈"]={cls="button";text="读多个线圈";left=224;top=448;right=312;bottom=480;db=1;dl=1;z=40};
["combobox_串口号"]={cls="combobox";left=352;top=96;right=432;bottom=120;dr=1;dt=1;edge=1;items={};mode="dropdown";z=18};
["combobox_停止位"]={cls="combobox";left=352;top=168;right=432;bottom=192;dr=1;dt=1;edge=1;items={};mode="dropdown";z=27};
["combobox_数据位"]={cls="combobox";left=352;top=136;right=432;bottom=160;dr=1;dt=1;edge=1;items={};mode="dropdown";z=26};
["combobox_校验位"]={cls="combobox";left=488;top=136;right=568;bottom=160;dr=1;dt=1;edge=1;items={};mode="dropdown";z=23};
["combobox_波特率"]={cls="combobox";left=488;top=96;right=568;bottom=120;dr=1;dt=1;edge=1;items={};mode="dropdown";z=19};
edit_COM_id={cls="edit";text="1";left=352;top=64;right=432;bottom=88;dr=1;dt=1;edge=1;num=1;z=14};
edit_COM_timer={cls="edit";text="200";left=488;top=168;right=568;bottom=192;dr=1;dt=1;edge=1;num=1;z=25};
edit_TCP_id={cls="edit";text="1";left=72;top=56;right=216;bottom=80;dl=1;dt=1;edge=1;num=1;z=6};
edit_TCP_ip={cls="edit";text="192.168.1.143";left=72;top=96;right=216;bottom=120;dl=1;dt=1;edge=1;z=7};
edit_TCP_port={cls="edit";text="502";left=72;top=136;right=216;bottom=160;dl=1;dt=1;edge=1;num=1;z=8};
edit_TCP_timer={cls="edit";text="200";left=72;top=168;right=216;bottom=192;dl=1;dt=1;edge=1;num=1;z=9};
["edit_单个线圈值"]={cls="edit";text="0";left=88;top=344;right=152;bottom=368;db=1;dl=1;edge=1;num=1;z=32};
["edit_单个线圈地址"]={cls="edit";text="0";left=88;top=312;right=152;bottom=336;db=1;dl=1;edge=1;num=1;z=31};
["edit_单寄存器值"]={cls="edit";text="0";left=408;top=344;right=472;bottom=368;db=1;dr=1;edge=1;num=1;z=50};
["edit_单寄存器地址"]={cls="edit";text="0";left=408;top=312;right=472;bottom=336;db=1;dr=1;edge=1;num=1;z=49};
["edit_多寄存器地址"]={cls="edit";text="0";left=576;top=312;right=632;bottom=336;db=1;dr=1;edge=1;num=1;z=56};
["edit_多寄存器地址个数"]={cls="edit";text="2";left=576;top=344;right=632;bottom=368;db=1;dr=1;edge=1;num=1;z=57};
["edit_多寄存器地址值1"]={cls="edit";text="0";left=576;top=376;right=632;bottom=400;db=1;dr=1;edge=1;num=1;z=62};
["edit_多寄存器地址值2"]={cls="edit";text="0";left=576;top=408;right=632;bottom=432;db=1;dr=1;edge=1;num=1;z=63};
["edit_多线圈值1"]={cls="edit";text="0";left=248;top=376;right=312;bottom=400;db=1;dl=1;edge=1;num=1;z=44};
["edit_多线圈值2"]={cls="edit";text="0";left=248;top=408;right=312;bottom=432;db=1;dl=1;edge=1;num=1;z=45};
["edit_多线圈地址"]={cls="edit";text="0";left=248;top=312;right=312;bottom=336;db=1;dl=1;edge=1;num=1;z=38};
["edit_多线圈数量"]={cls="edit";text="2";left=248;top=344;right=312;bottom=368;db=1;dl=1;edge=1;num=1;z=39};
groupbox={cls="groupbox";text="以太网主机";left=8;top=16;right=256;bottom=248;dl=1;dt=1;edge=1;z=1};
groupbox2={cls="groupbox";text="串口主机";left=272;top=16;right=640;bottom=248;dl=1;dr=1;dt=1;edge=1;z=10};
groupbox3={cls="groupbox";text="单个线圈操作";left=8;top=272;right=160;bottom=528;db=1;dl=1;edge=1;z=28};
groupbox5={cls="groupbox";text="多个线圈操作";left=168;top=272;right=320;bottom=528;db=1;dl=1;edge=1;z=35};
groupbox6={cls="groupbox";text="单个寄存器操作";left=328;top=272;right=480;bottom=528;db=1;dr=1;edge=1;z=46};
groupbox7={cls="groupbox";text="多个寄存器操作";left=488;top=272;right=640;bottom=528;db=1;dr=1;edge=1;z=53};
radiobutton_ASCII={cls="radiobutton";text="ASCII";left=576;top=64;right=632;bottom=88;dr=1;dt=1;z=16};
radiobutton_RTU={cls="radiobutton";text="RTU";left=512;top=64;right=568;bottom=88;dr=1;dt=1;z=15};
static={cls="static";text="ID";left=32;top=56;right=56;bottom=80;center=1;dl=1;dt=1;transparent=1;z=2};
static10={cls="static";text="工作方式";left=440;top=64;right=496;bottom=88;center=1;dr=1;dt=1;transparent=1;z=20};
static11={cls="static";text="波特率";left=440;top=96;right=480;bottom=120;center=1;dr=1;dt=1;transparent=1;z=21};
static12={cls="static";text="校验位";left=440;top=136;right=480;bottom=160;center=1;dr=1;dt=1;transparent=1;z=22};
static13={cls="static";text="超时";left=440;top=168;right=480;bottom=192;center=1;dr=1;dt=1;transparent=1;z=24};
static14={cls="static";text="线圈地址";left=16;top=312;right=72;bottom=336;center=1;db=1;dl=1;transparent=1;z=29};
static15={cls="static";text="线圈值";left=16;top=344;right=64;bottom=368;center=1;db=1;dl=1;transparent=1;z=30};
static18={cls="static";text="线圈地址";left=176;top=312;right=232;bottom=336;center=1;db=1;dl=1;transparent=1;z=36};
static19={cls="static";text="线圈个数";left=176;top=344;right=232;bottom=368;center=1;db=1;dl=1;transparent=1;z=37};
static2={cls="static";text="IP";left=32;top=96;right=56;bottom=120;center=1;dl=1;dt=1;transparent=1;z=3};
static20={cls="static";text="线圈(1)值";left=176;top=376;right=232;bottom=400;center=1;db=1;dl=1;transparent=1;z=42};
static21={cls="static";text="线圈(2)值";left=176;top=408;right=232;bottom=432;center=1;db=1;dl=1;transparent=1;z=43};
static22={cls="static";text="寄存器地址";left=336;top=312;right=400;bottom=336;center=1;db=1;dr=1;transparent=1;z=47};
static23={cls="static";text="寄存器值";left=336;top=344;right=400;bottom=368;center=1;db=1;dr=1;transparent=1;z=48};
static26={cls="static";text="寄存器地址";left=496;top=312;right=568;bottom=336;center=1;db=1;dr=1;transparent=1;z=54};
static27={cls="static";text="寄存器个数";left=496;top=344;right=568;bottom=368;center=1;db=1;dr=1;transparent=1;z=55};
static28={cls="static";text="寄存器(1)值";left=496;top=376;right=568;bottom=400;center=1;db=1;dr=1;transparent=1;z=60};
static29={cls="static";text="寄存器(2)值";left=496;top=408;right=568;bottom=432;center=1;db=1;dr=1;transparent=1;z=61};
static3={cls="static";text="端口号";left=32;top=136;right=56;bottom=160;center=1;dl=1;dt=1;transparent=1;z=4};
static4={cls="static";text="超时";left=32;top=168;right=64;bottom=192;center=1;dl=1;dt=1;transparent=1;z=5};
static5={cls="static";text="ID";left=296;top=64;right=320;bottom=88;center=1;dl=1;dt=1;transparent=1;z=11};
static7={cls="static";text="数据位";left=296;top=136;right=336;bottom=160;center=1;dl=1;dr=1;dt=1;transparent=1;z=12};
static8={cls="static";text="停止位";left=296;top=168;right=336;bottom=192;center=1;dl=1;dr=1;dt=1;transparent=1;z=13};
static9={cls="static";text="串口号";left=296;top=96;right=336;bottom=120;center=1;dl=1;dr=1;dt=1;transparent=1;z=17}
)
/*}}*/

mainForm.combobox_串口号.items = sys.comPort.list()

mainForm.combobox_串口号.selIndex = 1;

mainForm.edit_TCP_ip.text = "127.0.0.1"
var handle;
var m = modbus.modbustcp();

mainForm.button_TCP_连接.oncommand = function(id, event) {
    var ip = mainForm.edit_TCP_ip.text;
    var port = mainForm.edit_TCP_port.text;
    var timeout = mainForm.edit_TCP_timer.text;
   
    var v = wsock.tcp.client.test(ip);
   
    if (type(v) == "string") {
      if handle
      return;
      handle = m.tcpConnect(ip, port, timeout);
      if (handle) {
            win.msgbox("连接成功")
      }
      else {
            win.msgbox("连接失败")
      }
    }
    else {
      win.msgbox("连接失败")
    }
}

mainForm.button_TCP_断开.oncommand = function(id, event) {
   
    if (handle) {
      m.closeConnect();
      handle = null;
      win.msgbox("断开成功")
    }
   
}

mainForm.button_读单个线圈.oncommand = function(id, event) {
    var id = mainForm.edit_TCP_id.text
    var addr = mainForm.edit_单个线圈地址.text;
    var v = m.readCoils(id, addr, 1)
    mainForm.edit_单个线圈值.text = v;
}

mainForm.button_写单个线圈.oncommand = function(id, event) {
    var id = mainForm.edit_TCP_id.text
    var addr = mainForm.edit_单个线圈地址.text;
    var v = mainForm.edit_单个线圈值.text;
    m.writeSingle(id, addr, v)
   
}

mainForm.button_读多个线圈.oncommand = function(id, event) {
   
    var addr = mainForm.edit_多线圈地址.text;
    var num = mainForm.edit_多线圈数量.text;
    var id = mainForm.edit_TCP_id.text
    var v = m.readCoils(id, addr, num)
    console.dump(v)
    mainForm.edit_多线圈值1.text = v;
    mainForm.edit_多线圈值2.text = v;
   
}

mainForm.button_写多个线圈.oncommand = function(id, event) {
   
    var id = mainForm.edit_TCP_id.text
    var addr = mainForm.edit_多线圈地址.text;
    var v1 = mainForm.edit_多线圈值1.text;
    var v2 = mainForm.edit_多线圈值2.text;
    m.writeMultipleCoils(id, addr, 2, {
      v1;v2
    })
   
}

mainForm.button_读单个寄存器.oncommand = function(id, event) {
    var addr = mainForm.edit_单寄存器地址.text;
    var regValue = mainForm.edit_单寄存器值.text;
    var id = mainForm.edit_TCP_id.text
    var v = m.readHoldingRegisters(id, addr, 1)
    mainForm.edit_单寄存器值.text = tonumber(v, 16);
}

mainForm.button_写单个寄存器.oncommand = function(id, event) {
    var addr = mainForm.edit_单寄存器地址.text;
    var regValue = mainForm.edit_单寄存器值.text;
    var id = mainForm.edit_TCP_id.text
    m.writeSingleRegister(id, addr, tonumber(regValue))
}

mainForm.button_读多个寄存器.oncommand = function(id, event) {
    var addr = mainForm.edit_多寄存器地址.text;
    var regValue = mainForm.edit_多寄存器地址个数.text;
    var id = mainForm.edit_TCP_id.text
    var v = m.readHoldingRegisters(id, addr, regValue)
    console.dump(v)
    mainForm.edit_多寄存器地址值1.text = tonumber(v, 16)
    mainForm.edit_多寄存器地址值2.text = tonumber(v, 16)
}

mainForm.button_写多个寄存器.oncommand = function(id, event) {
    var v1 = tonumber(mainForm.edit_多寄存器地址值1.text);
    var v2 = tonumber(mainForm.edit_多寄存器地址值2.text);
    var id = mainForm.edit_TCP_id.text
    m.writeMultipleRegisters(id, 0, 2, {
      v1;v2
    })
}


mainForm.button.oncommand = function(id,event){
    //01功能码读
    console.dump(m.readCoils(1,0,10));
}

mainForm.button2.oncommand = function(id,event){
    //02功能码读
    console.dump(m.readDiscreteInputs(1,0,10))
}

mainForm.button3.oncommand = function(id,event){
    //03功能码读
    console.dump(m.readHoldingRegisters(1,0,10))
}

mainForm.button4.oncommand = function(id,event){
    //04功能码读
    console.dump(m.readInputRegisters(1,0,10))
}

mainForm.show();
return win.loopMessage();

xaardio 发表于 2024-4-1 18:49:44

modbus.modbustcp 库在哪里?
页: [1]
查看完整版本: Modbus TCP&modbus rtu协议库 上位机开发