暂时只封装了modbus tcp 的库后期有时间了会完善


modbus.zip 是协议库
zmodbus工程案例
以下为aardio工程源码
Code AardioLine:192复制
1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.25.26.27.28.29.30.31.32.33.34.35.36.37.38.39.40.41.42.43.44.45.46.47.48.49.50.51.52.53.54.55.56.57.58.59.60.61.62.63.64.65.66.67.68.69.70.71.72.73.74.75.76.77.78.79.80.81.82.83.84.85.86.87.88.89.90.91.92.93.94.95.96.97.98.99.100.101.102.103.104.105.106.107.108.109.110.111.112.113.114.115.116.117.118.119.120.121.122.123.124.125.126.127.128.129.130.131.132.133.134.135.136.137.138.139.140.141.142.143.144.145.146.147.148.149.150.151.152.153.154.155.156.157.158.159.160.161.162.163.164.165.166.167.168.169.170.171.172.173.174.175.176.177.178.179.180.181.182.183.184.185.186.187.188.189.190.191.192.import
win
.ui;import
sys.comPort;import
modbus.modbustcp;import
wsock.tcp.client;- mainForm =
win
.form(text="zmodbus"
;right=647
;bottom=543
) - mainForm.add(
- [
"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
; 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[
1
]; - }
- 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[1
]; - mainForm.edit_多线圈值
2
.text = v[2
]; -
- }
- 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[1
], 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[1
], 16
) - mainForm.edit_多寄存器地址值
2
.text = tonumber
(v[2
], 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.show();
return
win
.loopMessage();
以下是用其它语言封装的,我用aardio 封装了一个 哈哈
