千分表自动采集程序
This commit is contained in:
parent
3814d163c5
commit
4925d899b4
1
.gitignore
vendored
1
.gitignore
vendored
@ -13,3 +13,4 @@ aio/assets/templates/durable/
|
|||||||
aio/assets/templates/.__c_msg.lock
|
aio/assets/templates/.__c_msg.lock
|
||||||
aio/code/commons/__pycache__/
|
aio/code/commons/__pycache__/
|
||||||
aio/assets/templates/debug.log
|
aio/assets/templates/debug.log
|
||||||
|
dial_gauge/results.xlsx
|
||||||
|
2
dial_gauge/README.md
Normal file
2
dial_gauge/README.md
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
## 千分表数据自动采集
|
||||||
|
|
6
dial_gauge/conf.ini
Normal file
6
dial_gauge/conf.ini
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
[md_rtu]
|
||||||
|
port = COM10
|
||||||
|
|
||||||
|
[md_tcp]
|
||||||
|
addr = 192.168.0.160
|
||||||
|
port = 502
|
99
dial_gauge/gauge.py
Normal file
99
dial_gauge/gauge.py
Normal file
@ -0,0 +1,99 @@
|
|||||||
|
from configparser import ConfigParser
|
||||||
|
from pymodbus.client import ModbusSerialClient, ModbusTcpClient
|
||||||
|
from time import sleep, strftime, localtime, time
|
||||||
|
from openpyxl import load_workbook, Workbook
|
||||||
|
from os import rename
|
||||||
|
# import pymodbus
|
||||||
|
# pymodbus.pymodbus_apply_logging_config("DEBUG")
|
||||||
|
|
||||||
|
|
||||||
|
def initializations():
|
||||||
|
try:
|
||||||
|
now = strftime("%Y%m%d%H%M%S", localtime(time()))
|
||||||
|
rename('results.xlsx', f'results_{now}.xlsx')
|
||||||
|
except FileNotFoundError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
wb = Workbook()
|
||||||
|
ws = wb.active
|
||||||
|
ws.title = 'results'
|
||||||
|
ws['A1'].value = '时间'
|
||||||
|
ws['B1'].value = '次数'
|
||||||
|
ws['C1'].value = '结果'
|
||||||
|
wb.save('results.xlsx')
|
||||||
|
wb.close()
|
||||||
|
|
||||||
|
|
||||||
|
def do_connections():
|
||||||
|
configs = ConfigParser()
|
||||||
|
configs.read('conf.ini')
|
||||||
|
|
||||||
|
# ================ 配置Modbus TCP客户端 ================
|
||||||
|
tcp_host = configs.get('md_tcp', 'addr')
|
||||||
|
tcp_port = configs.getint('md_tcp', 'port')
|
||||||
|
|
||||||
|
client_tcp = ModbusTcpClient(tcp_host, tcp_port)
|
||||||
|
if client_tcp.connect():
|
||||||
|
print(f"Modbus TCP已连接到{tcp_host}:{tcp_port}...")
|
||||||
|
else:
|
||||||
|
raise Exception(f"Modbus TCP无法连接到{tcp_host}:{tcp_port}...")
|
||||||
|
|
||||||
|
# ================ 配置Modbus RTU客户端 ================
|
||||||
|
rtu_port = configs.get('md_rtu', 'port')
|
||||||
|
|
||||||
|
client_rtu = ModbusSerialClient(
|
||||||
|
method='rtu',
|
||||||
|
port=rtu_port, # 根据实际情况调整端口,Windows上可能是 'COM3'
|
||||||
|
baudrate=38400, # 根据协议设定波特率为38400
|
||||||
|
timeout=1,
|
||||||
|
parity='N', # 无奇偶校验
|
||||||
|
stopbits=2, # 2个停止位
|
||||||
|
bytesize=8 # 8个数据位
|
||||||
|
)
|
||||||
|
if client_rtu.connect():
|
||||||
|
print(f"Modbus RTU已连接到系统{rtu_port}端口...")
|
||||||
|
else:
|
||||||
|
raise Exception(f"Modbus RTU无法连接到系统{rtu_port}端口...")
|
||||||
|
|
||||||
|
return client_tcp, client_rtu
|
||||||
|
|
||||||
|
|
||||||
|
def get_gauge_data(client_tcp, client_rtu):
|
||||||
|
count = 2
|
||||||
|
client_tcp.write_register(41000, 1) # 将 act 信号置为 True
|
||||||
|
# ================ 功能实现 ================
|
||||||
|
while True:
|
||||||
|
while True:
|
||||||
|
res_tcp = client_tcp.read_holding_registers(41001, 1) # 获取 time_to_go 的值
|
||||||
|
sleep(0.5)
|
||||||
|
if not res_tcp.registers[0]:
|
||||||
|
continue
|
||||||
|
else:
|
||||||
|
break
|
||||||
|
try:
|
||||||
|
res_rtu = client_rtu.read_holding_registers(0x0000, 2, slave=1)
|
||||||
|
plus_or_minus = 1 if res_rtu.registers[0] == 0 else -1
|
||||||
|
result = res_rtu.registers[1] * plus_or_minus / 10000
|
||||||
|
now = strftime("%Y-%m-%d %H:%M:%S", localtime(time()))
|
||||||
|
wb = load_workbook('results.xlsx')
|
||||||
|
ws = wb['results']
|
||||||
|
ws[f'A{count}'].value = now
|
||||||
|
ws[f'B{count}'].value = count-1
|
||||||
|
ws[f'C{count}'].value = float(result)
|
||||||
|
wb.save('results.xlsx')
|
||||||
|
wb.close()
|
||||||
|
count += 1
|
||||||
|
except:
|
||||||
|
client_tcp.write_register(41000, 0) # 将 act 信号置为 False
|
||||||
|
|
||||||
|
sleep(2) #
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
initializations()
|
||||||
|
client_tcp, client_rtu = do_connections()
|
||||||
|
get_gauge_data(client_tcp, client_rtu)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
43
dial_gauge/gauge.xml
Normal file
43
dial_gauge/gauge.xml
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<m>
|
||||||
|
<l>
|
||||||
|
<c name="addr" type="2" value="41000"/>
|
||||||
|
<c name="addr_1st" type="2" value="0"/>
|
||||||
|
<c name="addr_2nd" type="2" value="0"/>
|
||||||
|
<c name="bit_bias" type="2" value="0"/>
|
||||||
|
<c name="byte_bias" type="4" value="0"/>
|
||||||
|
<c name="description" type="10" value="pc to robot"/>
|
||||||
|
<c name="dev_name" type="10" value="autotest"/>
|
||||||
|
<c name="dev_type" type="10" value="MODBUS"/>
|
||||||
|
<c name="end_addr" type="2" value="41000"/>
|
||||||
|
<c name="function" type="10" value=""/>
|
||||||
|
<c name="len" type="2" value="1"/>
|
||||||
|
<c name="name" type="10" value="act"/>
|
||||||
|
<c name="retain" type="1" value="false"/>
|
||||||
|
<c name="rw" type="10" value="rd"/>
|
||||||
|
<c name="type" type="10" value="bool"/>
|
||||||
|
<c name="value"/>
|
||||||
|
<c name="value_single" type="10" value=""/>
|
||||||
|
<c name="bias" type="2" value="0"/>
|
||||||
|
</l>
|
||||||
|
<l>
|
||||||
|
<c name="addr" type="2" value="41001"/>
|
||||||
|
<c name="addr_1st" type="2" value="0"/>
|
||||||
|
<c name="addr_2nd" type="2" value="0"/>
|
||||||
|
<c name="bit_bias" type="2" value="0"/>
|
||||||
|
<c name="byte_bias" type="4" value="0"/>
|
||||||
|
<c name="description" type="10" value="robot to pc"/>
|
||||||
|
<c name="dev_name" type="10" value="autotest"/>
|
||||||
|
<c name="dev_type" type="10" value="MODBUS"/>
|
||||||
|
<c name="end_addr" type="2" value="41001"/>
|
||||||
|
<c name="function" type="10" value=""/>
|
||||||
|
<c name="len" type="2" value="1"/>
|
||||||
|
<c name="name" type="10" value="time_to_go"/>
|
||||||
|
<c name="retain" type="1" value="false"/>
|
||||||
|
<c name="rw" type="10" value="rdwr"/>
|
||||||
|
<c name="type" type="10" value="bool"/>
|
||||||
|
<c name="value"/>
|
||||||
|
<c name="value_single" type="10" value=""/>
|
||||||
|
<c name="bias" type="2" value="0"/>
|
||||||
|
</l>
|
||||||
|
</m>
|
BIN
dial_gauge/target_dial_gauge.zip
Normal file
BIN
dial_gauge/target_dial_gauge.zip
Normal file
Binary file not shown.
Reference in New Issue
Block a user