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

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

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

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

用Python采用Modbus-Tcp的方式读取PLC模块数据_第1张图片

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

用Python采用Modbus-Tcp的方式读取PLC模块数据_第2张图片

使用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 binascii

interval = 5
ip_address = '10.7.132.27'
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.00
                value84 = 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.00
                value80 = round(value80, 2)
                print("实时电流 单精度浮点数: {:.2f}".format(value80))

                ##实时温度1
                request = 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.00
                value88 = round(value88, 2)
                print("实时温度1 单精度浮点数: {:.2f}".format(value88))

                #实时温度2
                request = 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.00
                value92 = 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.00
                value96=round(value96,2)
                print("实时电量 单精度浮点数: {:.2f}".format(value96))

                #充放电状态

                #超速报警

                #低电量报警

                #温度过高1

                #温度过高2

                #00 18 00 00 00 06 01 03 00 28 00 01
                request = 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

其他:地址表

用Python采用Modbus-Tcp的方式读取PLC模块数据_第3张图片

你可能感兴趣的:(Python学习私人笔记,物联网,python,tcp/ip,物联网,IOT,Modbus)