用Python采用Modbus-Tcp的方式读取PLC模块数据

使用计算器得到需要的寄存器地址

这里PLC地址是83,对应的程序16进制读取地址是53

实际上由于PLC地址从1开始,所以这里实际地址应该是52,因为计算机从0开始

在这里插入图片描述

使用网络调试助手生成报文

在这里插入图片描述

使用Python中的内置函数int()。以下是将人员卡号’b’3b44’'转换为十进制的示例代码:

card_number = '3b44'
decimal_number = int(card_number, 16)
print(decimal_number)

使用response[-4:]获取了响应数据的后4个字节作为value96。然后,通过struct.unpack(‘>f’, value96)[0]将4字节的二进制字符串解包为单精度浮点数,并将其打印出来。

#实时电量
request = bytes.fromhex("00 20 00 00 00 06 01 03 00 5F 00 02 ")
client_socket.send(request)
response = client_socket.recv(1024)value96 = response[-4:]
value96 = struct.unpack('>f', value96)[0]
value96=value96*10.00
value96=round(value96,2)
print("实时电量 单精度浮点数: {:.2f}".format(value96))

value40 是一个包含两个字节的字节串,即 b’\x00\x00’,将其转换为二进制,并保留8位。

以下是将字节串转换为二进制并保留8位的示例代码:

value40 = b'\x00\x00'
binary_value = bin(int.from_bytes(value40, byteorder='big'))[2:].zfill(8)
print("状态:", binary_value)

DEMO代码

#!D:/workplace/python
# -*- coding: utf-8 -*-
# @File  : main0523_04.py
# @Author:Romulushe
# @Time    : 2023/5/23 10:38
# @Software: PyCharm
# @Use: PyCharm
import socket
import struct
import time
import binasciiinterval = 5
ip_address = ''#根据实际情况自定义
port_number = 502
polling_interval = float(interval)def print_binary_value(value, name):binary_value = bin(int.from_bytes(value, byteorder='big'))[2:].zfill(8)[::-1]print(f"{name}: {binary_value}")with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as client_socket:try:client_socket.settimeout(3)client_socket.connect((ip_address, port_number))while True:try:##电压request = bytes.fromhex("00 22 00 00 00 06 01 03 00 53 00 02 ")client_socket.send(request)response = client_socket.recv(1024)value84 = response[-4:]value84= struct.unpack('>f', value84)[0]# value84 = value84 * 10.00value84 = round(value84, 2)print("实时电压 单精度浮点数: {:.2f}".format(value84))##电流request = bytes.fromhex("00 24 00 00 00 06 01 03 00 4F 00 02  ")client_socket.send(request)response = client_socket.recv(1024)value80 = response[-4:]value80 = struct.unpack('>f', value80)[0]# value84 = value80 * 10.00value80 = round(value80, 2)print("实时电流 单精度浮点数: {:.2f}".format(value80))##实时温度1request = bytes.fromhex("00 2E 00 00 00 06 01 03 00 57 00 02 ")client_socket.send(request)response = client_socket.recv(1024)value88 = response[-4:]value88 = struct.unpack('>f', value88)[0]# value88 = value88 * 10.00value88 = round(value88, 2)print("实时温度1 单精度浮点数: {:.2f}".format(value88))#实时温度2request = bytes.fromhex("00 2C 00 00 00 06 01 03 00 5B 00 02 ")client_socket.send(request)response = client_socket.recv(1024)value92 = response[-4:]value92 = struct.unpack('>f', value92)[0]# value92 = value92 * 10.00value92 = round(value92, 2)print("实时温度2 单精度浮点数: {:.2f}".format(value92))##车速request = bytes.fromhex("00 31 00 00 00 06 01 03 00 3B 00 01 ")client_socket.send(request)response = client_socket.recv(1024)speed= binascii.hexlify(response)[-4:]speed = int(speed, 16)print("车速:", speed)# #实时电量request = bytes.fromhex("00 20 00 00 00 06 01 03 00 5F 00 02 ")client_socket.send(request)response = client_socket.recv(1024)value96 = response[-4:]value96 = struct.unpack('>f', value96)[0]value96=value96*10.00value96=round(value96,2)print("实时电量 单精度浮点数: {:.2f}".format(value96))#充放电状态#超速报警#低电量报警#温度过高1#温度过高2#00 18 00 00 00 06 01 03 00 28 00 01request = bytes.fromhex("00 33 00 00 00 06 01 03 00 28 00 01 ")client_socket.send(request)response = client_socket.recv(1024)value40 = response[-2:]value40= bin(int.from_bytes(value40, byteorder='big'))[2:].zfill(8)[::-1]print("充放电状态:",value40[4])print("超速报警:", value40[0])print("低电量报警:", value40[2])print("温度1过高:", value40[3])print("温度2过高:", value40[7])#车牌号#request = bytes.fromhex("00 00 00 00 00 06 01 03 00 1E 00 02 ")  # 车牌号client_socket.send(request)response = client_socket.recv(1024)car_num= binascii.hexlify(response)[-8:-4]car_num = int(car_num, 16)print("车牌号:", car_num)#人员卡号request = bytes.fromhex("00 00 00 00 00 06 01 03 00 42 00 02 ")  # 人员卡号client_socket.send(request)response = client_socket.recv(1024)card = binascii.hexlify(response)[-8:-4]card = int(card, 16)print("人员卡号:", card)except socket.timeout:print('TIMEOUT ERROR: 服务器未及时响应')except Exception as e:print('CONNECT ERROR:', e)

DEMO结果

E:\software\python\python.exe E:/projects/Forklift/t2.py实时电压 单精度浮点数: 48.85实时电流 单精度浮点数: 0.26实时温度1 单精度浮点数: 31.10实时温度2 单精度浮点数: 30.85车速: 0实时电量 单精度浮点数: 68.52充放电状态: 0超速报警: 0低电量报警: 0温度1过高: 0温度2过高: 0车牌号: 15172人员卡号: 10763

其他:地址表

在这里插入图片描述

附带数据库&log存储的代码:

#!D:/workplace/python
# -*- coding: utf-8 -*-
# @File  : main0523_04.py
# @Author:Romulushe
# @Time    : 2023/5/23 10:38
# @Software: PyCharm
# @Use: PyCharm
import socket
import struct
import time
import binascii
import pymysql
import os,sys,datetime,logginginterval = 5
ip_address = ''#根据实际情况自定义
port_number = ''#根据实际情况自定义
polling_interval = float(interval)base_path = os.path.dirname(os.path.realpath(sys.argv[0]))def get_log_path():return os.path.join(base_path, 'logs')def cleanup_logs():log_path = get_log_path()current_time = time.time()for file_name in os.listdir(log_path):file_path = os.path.join(log_path, file_name)if os.path.isfile(file_path):creation_time = os.path.getctime(file_path)if current_time - creation_time > (3 * 24 * 60 * 60):os.remove(file_path)def configure_logging():log_path = get_log_path()os.makedirs(log_path, exist_ok=True)log_filename = get_log_filename()log_file = os.path.join(log_path, log_filename)logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s', filename=log_file)def get_log_filename():now = datetime.datetime.now()return now.strftime("%Y-%m-%d_%H-%M.log")def create_new_log():log_path = get_log_path()log_files = os.listdir(log_path)if len(log_files) >= 20:oldest_file = min(log_files)os.remove(os.path.join(log_path, oldest_file))log_filename = get_log_filename()log_filepath = os.path.join(log_path, log_filename)return log_filepathdef check_log_size(log_filepath):log_size = os.path.getsize(log_filepath)if log_size > 2 * 1024 * 1024:# 创建新的日志文件new_log_filepath = create_new_log()try:shutil.move(log_filepath, new_log_filepath)return new_log_filepathexcept PermissionError:insert_log(logger, f'{log_filepath} {PermissionError}', log_filepath)time.sleep(0.1)return log_filepathreturn log_filepathdef insert_log(logger, log_message, log_filepath):log_filepath = check_log_size(log_filepath)# 创建文件处理器file_handler = RotatingFileHandler(log_filepath, maxBytes=2 * 1024 * 1024, backupCount=1)file_handler.setLevel(logging.DEBUG)formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')file_handler.setFormatter(formatter)# 添加文件处理器到日志记录器logger.addHandler(file_handler)try:logger.debug(log_message)except PermissionError:insert_log(logger, f'{log_message} {PermissionError}', log_filepath)time.sleep(0.1)  # 延迟0.1秒# 移除文件处理器logger.removeHandler(file_handler)
# 连接数据库
def connect_database(host, port, user, password, db_name):try:conn = pymysql.connect(host=host, port=port, user=user, password=password, db=db_name)# print("成功连接到数据库")return connexcept pymysql.Error as e:insert_log(logger, f'DATABASE CONNECT FAILED', log_filepath)# print(f"数据库连接失败: {e}")# 插入数据
def insert_data(conn, types,table_name, create_time, location_no, parameter_desc, location, param_value):try:cursor = conn.cursor()sql = f"INSERT INTO {table_name} (TYPE, CREATE_TIME, LOCATION_NO, PARAMETER_DESC, LOCATION, PARAMVALUE) " \f"VALUES (%s, %s, %s, %s, %s, %s)"cursor.execute(sql, (types,create_time, location_no, parameter_desc, location, param_value))conn.commit()# print(f"数据插入成功: {create_time}, {parameter_desc}: {param_value}")cursor.close()except pymysql.Error as e:insert_log(logger, f'DATABASE INSERT DATA FAILED', log_filepath)# print(f"数据插入失败: {e}")with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as client_socket:configure_logging()cleanup_logs()log_filepath = create_new_log()logger = logging.getLogger()logger.setLevel(logging.DEBUG)try:client_socket.settimeout(3)client_socket.connect((ip_address, port_number))# 连接数据库host = ''#根据实际情况自定义port = 3306user = ''#根据实际情况自定义password = ''#根据实际情况自定义db_name = ''#根据实际情况自定义table_name =''#根据实际情况自定义try:# 连接数据库conn = connect_database(host, port, user, password, db_name)except Exception as e:print("COON ERROR:",e)while True:try:types ='叉车'location ='F2堆02'##电压request = bytes.fromhex("00 22 00 00 00 06 01 03 00 53 00 02 ")client_socket.send(request)response = client_socket.recv(1024)value84 = response[-4:]value84= struct.unpack('>f', value84)[0]# value84 = value84 * 10.00value84 = round(value84, 2)print("实时电压 单精度浮点数: {:.2f}".format(value84))location_no ='F01-1'parameter_desc ='实时电压'param_value=value84insert_data(conn, types, table_name, create_time, location_no, parameter_desc, location, param_value)##电流request = bytes.fromhex("00 24 00 00 00 06 01 03 00 4F 00 02  ")client_socket.send(request)response = client_socket.recv(1024)value80 = response[-4:]value80 = struct.unpack('>f', value80)[0]# value84 = value80 * 10.00value80 = round(value80, 2)print("实时电流 单精度浮点数: {:.2f}".format(value80))location_no = 'F01-2'parameter_desc = '实时电流'param_value=value80insert_data(conn, types, table_name, create_time, location_no, parameter_desc, location, param_value)##实时温度1request = bytes.fromhex("00 2E 00 00 00 06 01 03 00 57 00 02 ")client_socket.send(request)response = client_socket.recv(1024)value88 = response[-4:]value88 = struct.unpack('>f', value88)[0]# value88 = value88 * 10.00value88 = round(value88, 2)print("实时温度1 单精度浮点数: {:.2f}".format(value88))location_no = 'F01-3'parameter_desc = '实时温度1'param_value = value88insert_data(conn, types, table_name, create_time, location_no, parameter_desc, location, param_value)#实时温度2request = bytes.fromhex("00 2C 00 00 00 06 01 03 00 5B 00 02 ")client_socket.send(request)response = client_socket.recv(1024)value92 = response[-4:]value92 = struct.unpack('>f', value92)[0]# value92 = value92 * 10.00value92 = round(value92, 2)print("实时温度2 单精度浮点数: {:.2f}".format(value92))location_no = 'F01-4'parameter_desc = '实时温度2'param_value = value92insert_data(conn, types, table_name, create_time, location_no, parameter_desc, location, param_value)##车速request = bytes.fromhex("00 31 00 00 00 06 01 03 00 3B 00 01 ")client_socket.send(request)response = client_socket.recv(1024)speed= binascii.hexlify(response)[-4:]speed = int(speed, 16)print("车速:", speed)location_no = 'F01-5'parameter_desc = '车速'param_value = speedinsert_data(conn, types, table_name, create_time, location_no, parameter_desc, location, param_value)# #实时电量request = bytes.fromhex("00 20 00 00 00 06 01 03 00 5F 00 02 ")client_socket.send(request)response = client_socket.recv(1024)value96 = response[-4:]value96 = struct.unpack('>f', value96)[0]value96=value96*10.00value96=round(value96,2)print("实时电量 单精度浮点数: {:.2f}".format(value96))location_no = 'F01-6'parameter_desc = '实时电量'param_value = value96insert_data(conn, types, table_name, create_time, location_no, parameter_desc, location, param_value)#充放电状态#超速报警#低电量报警#温度过高1#温度过高2#00 18 00 00 00 06 01 03 00 28 00 01request = bytes.fromhex("00 33 00 00 00 06 01 03 00 28 00 01 ")client_socket.send(request)response = client_socket.recv(1024)value40 = response[-2:]value40= bin(int.from_bytes(value40, byteorder='big'))[2:].zfill(8)[::-1]print("充放电状态:",value40[4])print("超速报警:", value40[0])print("低电量报警:", value40[2])print("温度1过高:", value40[3])print("温度2过高:", value40[7])location_no = 'F01-7'parameter_desc = '充放电状态'param_value = value40[4]insert_data(conn, types, table_name, create_time, location_no, parameter_desc, location, param_value)location_no = 'F01-8'parameter_desc = '超速报警'param_value = value40[0]insert_data(conn, types, table_name, create_time, location_no, parameter_desc, location, param_value)location_no = 'F01-9'parameter_desc = '低电量报警'param_value = value40[2]insert_data(conn, types, table_name, create_time, location_no, parameter_desc, location, param_value)location_no = 'F01-10'parameter_desc = '温度1过高'param_value = value40[3]insert_data(conn, types, table_name, create_time, location_no, parameter_desc, location, param_value)location_no = 'F01-11'parameter_desc = '温度2过高'param_value = value40[7]insert_data(conn, types, table_name, create_time, location_no, parameter_desc, location, param_value)# 车牌号#request = bytes.fromhex("00 00 00 00 00 06 01 03 00 1E 00 02 ")  # 车牌号client_socket.send(request)response = client_socket.recv(1024)car_num = binascii.hexlify(response)[-8:-4]car_num = int(car_num, 16)print("车牌号:", car_num)location_no = 'F01-12'parameter_desc = '车牌号'param_value = car_numinsert_data(conn, types, table_name, create_time, location_no, parameter_desc, location, param_value)# 人员卡号request = bytes.fromhex("00 00 00 00 00 06 01 03 00 42 00 02 ")  # 人员卡号client_socket.send(request)response = client_socket.recv(1024)card = binascii.hexlify(response)[-8:-4]card = int(card, 16)print("人员卡号:", card)location_no = 'F01-13'parameter_desc = '人员卡号'param_value = cardinsert_data(conn, types, table_name, create_time, location_no, parameter_desc, location, param_value)except socket.timeout:print('TIMEOUT ERROR: 服务器未及时响应')except Exception as e:print('CONNECT ERROR:', e)

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/26717.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

webpack基础知识

webpack基础知识 1、定义2、环境安装3、初始化项目4、简单使用 1、定义 webpack的本质是一个第三方模块包,用于分析,并打包代码 支持所有类型的文件打包支持less/sass> css支持ES6/7/8>ES5压缩代码,提高加载速度 2、环境安装 yarn安…

出差在外,远程访问企业局域网象过河ERP系统【内网穿透】

文章目录 概述1.查看象过河服务端端口2.内网穿透3. 异地公网连接4. 固定公网地址4.1 保留一个固定TCP地址4.2 配置固定TCP地址 5. 使用固定地址连接 概述 ERP系统对于企业来说重要性不言而喻,不管是财务、生产、销售还是采购,都需要用到ERP系统来协助。…

cocos2d-js中jsc逆向为js

1.mac系统 2.安装php7以上的版本 ubuntu $ sudo apt install php7.0 mac $ brew install php7.0 windows just google an binary one 查看php安装的版本这里mac电脑为例子: 输入:php -v 只要7以上的版本即可 3.cd到自己的项目位置 cd path/to/project 安装composer,…

【网站建设】HTTP/HTTPS 是什么?有什么区别?

🚀欢迎来到本文🚀 🍉个人简介:陈童学哦,目前学习C/C、算法、Java等方向,一个正在慢慢前行的普通人。 🏀系列专栏:陈童学的日记 💡其他专栏:CSTL,感…

Python应用实例(二)数据可视化(五)

数据可视化(五)制作全球地震散点图:JSON格式 1.地震数据2.查看JSON数据3.创建地震列表4.提取震级5.提取位置数据6.绘制震级散点图7.另一种指定图表数据的方式 下载一个数据集,其中记录了一个月内全球发生的所有地震,再…

字节跳动春招研发部分编程题汇总

状压dp(不会) http://t.csdn.cn/W9Pi2 #include <iostream> #include<string.h> #include<math.h> using namespace std; char a[1005]; char c[1005]; int main() {int n;scanf("%d",&n);for(int i1;i<n;i){scanf("%s",a);int l…

图形编辑器开发:一些会用到的简单几何算法

大家好&#xff0c;我是前端西瓜哥。 开发图形编辑器&#xff0c;你会经常要解决一些算法问题。本文盘点一些我开发图形编辑器时遇到的简单几何算法问题。 矩形碰撞检测 判断两个矩形是否发生碰撞&#xff08;或者说相交&#xff09;&#xff0c;即两个矩形有重合的区域。 …

【Kubernetes运维篇】RBAC认证授权详解(二)

文章目录 一、RBAC认证授权策略1、Role角色2、ClusterRole集群角色3、RoleBinding角色绑定和ClusterRoleBinding集群角色绑定 二、通过API接口授权访问K8S资源三、案例&#xff1a;常见授权策略1、常见的角色授权策略案例2、常见的角色绑定案例3、常见的ServiceAccount授权绑定…

数学专题训练2 组合计数

1. 硬币购物 4 种面值的硬币&#xff0c;第 i 种的面值是 C i C_i Ci​​。 n n n​ 次询问&#xff0c;每次询问给出每种硬币的数量 D i D_i Di​​ 和一个价格 S S S​&#xff0c;问付款方式。 n ≤ 1 0 3 , S ≤ 1 0 5 n\leq 10^3,S\leq 10^5 n≤103,S≤105​. 如果用…

ORCA优化器浅析——​MD Accessor的三级缓存

分析​MD Accessor对元数据的缓存能力 set client_min_messageslog; set optimizer to on; set optimizer_print_optimization_stats to on; --执行SQLoptimizer_print_optimization_stats GUC会打印处ORCA优化器优化流程中的各步骤的统计数据。分析打印日志如下&#xff0c;由…

C++核心编程之函数高级使用

目录 一、函数的默认参数 二、函数占位参数 三、函数重载 四、函数重载-注意事项 一、函数的默认参数 在C中&#xff0c;函数的形参列表中的形参是可以有默认值的 语法&#xff1a;返回值类型 函数名 &#xff08;参数默认值&#xff09;{} 示例1&#xff1a; #includ…

Windows10下ChatGLM2-6B模型本地化安装部署教程图解

随着人工智能技术的不断发展&#xff0c;自然语言处理模型在研究和应用领域备受瞩目。ChatGLM2-6B模型作为其中的一员&#xff0c;以其强大的聊天和问答能力备受关注&#xff0c;并且最突出的优点是性能出色且轻量化。然而&#xff0c;通过云GPU部署安装模型可能需要支付相应的…