Phyton Socket 송수신 Modbus 배열

Phyton으로Modbus 패키지를 보내야 하기 때문에 인터넷의 자료를 찾았습니다. 예를 들어 다음과 같습니다. 기능은 서버의 포트를 끊임없이 열고 데이터를 읽어서 서버의 성능을 테스트하는 데 사용됩니다.
 
# This is a sample Python script.

# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.


def print_hi(name):
    # Use a breakpoint in the code line below to debug your script.
    print(f'Hi, {name}')  # Press Ctrl+F8 to toggle the breakpoint.


# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    print_hi('PyCharm')

import socket
import time
import struct
MaxBytes=1024*1024
host ='192.168.0.61'
port = 502
arr = [0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x04, 0x00, 0x00, 0x00, 0x20]
data=struct.pack("%dB"%(len(arr)),*arr)

num = 0
while True:
    num=num+1
    client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    client.settimeout(30)
    client.connect((host, port))

    sendBytes = client.send(data)
    if sendBytes<=0:
        break;
    recvData = client.recv(MaxBytes)
    if not recvData:
        print(' , ')
        break
    print(" :", num)
    localTime = time.asctime( time.localtime(time.time()))
    print(localTime, '  :',len(recvData))
    '''print(recvData.decode()) '''
client.close()
print(" , ")
# See PyCharm help at https://www.jetbrains.com/help/pycharm/

좋은 웹페이지 즐겨찾기