카테고리 없음

EasyModbusTCP

황기하 2022. 11. 25.

1. 설치

  pip install EasyModbus

요구 사항:

파이썬 2.7
파이썬 3.6

pyserial(Modbus RTU에만 해당)

2. 지원되는 기능 코드

 

  • Read Coils (FC1)
  • Read Discrete Inputs (FC2)
  • Read Holding Registers (FC3)
  • Read Input Registers (FC4)
  • Write Single Coil (FC5)
  • Write Single Register (FC6)
  • Write Multiple Coils (FC15)
  • Write Multiple Registers (FC16)

3. 기본 사용법

3.1  ModbusClient  클래스 인스턴스화

인스턴스화 중에 전달되는 인수는
Modbus RTU와 Modbus TCP를 구별하기 위해 중요합니다.


인스턴스화 중에

한 개의 인수가 전달되면 Modbus RTU와 함께 사용되는 직렬 인터페이스입니다
(예: 'COM1' 또는 '/dev/ttyUSB0'). 


두 개의 인수를 전달되면. Modbus TCP 이며
이는 IP 주소와 포트(보통 502)입니다.

 

Modbus RTU 의 예

import easymodbus.modbusClient
modbus_client = easymodbus.modbusClient.ModbusClient('/dev/ttyUSB0')

Modbus TCP 의 예

 

import easymodbus.modbusClient
modbus_client = easymodbus.modbusClient.ModbusClient('192.168.178.52', 502)

 3.2 연결  Connect 메서드

Modbus-TCP 서버 또는 Modbus-RTU 슬레이브에 연결하려면 connect Method 을 사용하십시오. 
Modbus-RTU를 사용하는 경우 "connect" Method를 실행 하기 전에 
직렬 연결을 위한 속성(Baudrate, Parity, Stopbits)을 설정 해야 합니다.

modbus_client.connect()

 3.3 연결 종료 Close 메서드

Modbus-TCP 서버 또는 Modbus-RTU 슬레이브에 대한 연결을 닫으려면 "close" Method 를 사용하십시오.

modbus_client.close()

 3.4 값 읽기  Read Values

다음 기능 코드는 원격 장치(Modbus-TCP 서버 또는 Modbus-RTU 클라이언트)에서 값을 읽는 데 사용됩니다.

  • Read Coils (FC1)                      - Method "read_coils"
  • Read Discrete Inputs (FC2)      - Method "read_discreteinputs"
  • Read Holding Registers (FC3)  - Method "read_holdingregisters"
  • Read Input Registers (FC4)       - Method "read_inputregisters"

일반적으로 요청과 서버 주소 범위 사이에 레지스터 이동이 있습니다.
서버 주소 범위는 주소 "1"로 시작하지만 전송되는 요청은 "0"으로 시작합니다.
예제 메서드 호출에서 우리는 레지스터 1과 2를 읽습니다
(왜냐하면 레지스터 "0"이 존재하지 않음) 장치 설명서를 확인하거나 사용해 보십시오.

import easymodbus.modbusClient

#create an instance of a Modbus-TCP class (Server IP-Address and Port) and connect

modbus_client = easymodbus.modbusClient.ModbusClient('192.168.178.110', 502)
modbus_client.connect()

#The first argument is the starting address, the second argument is the quantity.

coils             = modbus_client.read_coils(0, 2)	            #Read coils 1 and 2 from server 
discrete_inputs   = modbus_client.read_discreteinputs(10, 10)	#Read discrete inputs 11 to 20 from server 
input_registers   = modbus_client.read_inputregisters(0, 10)	#Read input registers 1 to 10 from server 
holding_registers = modbus_client.read_holdingregisters(0, 5)	#Read holding registers 1 to 5 from server 

modbus_client.close()

 

3.5 단일 값 쓰기  Write single Values

다음 기능 코드는
단일 값(Holding Registers or Coils)을 쓰는 데 사용됩니다.

  • Write Single Coil (FC5)         - Method "write_single_coil"
  • Write Single Register (FC6)  - Method "write_single_register"

 

import easymodbus.modbusClient

#create an instance of a Modbus-TCP class (Server IP-Address and Port) and connect

modbus_client = easymodbus.modbusClient.ModbusClient('192.168.178.110', 502)
modbus_client.connect()

holding_register_value = 115
coil_value = True

#The first argument is the address, the second argument is the value.

modbus_client.write_single_register(0, holding_register_value)	#Write value "115" to Holding Register 1 
modbus_client.write_single_coil(10, coil_value)	                 #Set Coil 11 to True

modbus_client.close()

 

4. 시작하기 - 예시

모든 예제는 Git-Repository의 "examples" 폴더에서 사용할 수 있습니다.

4.1 예시 1 (Modbus-TCP 서버에서 2개의 Holding Register 읽기)

먼저 Modbus-TCP 클래스(서버 IP 주소 및 포트)의 인스턴스를 생성한 다음
서버에서 2개의 보유 레지스터를 읽습니다.

import easymodbus.modbusClient

#create an instance of a Modbus-TCP class (Server IP-Address and Port) and connect

modbus_client = easymodbus.modbusClient.ModbusClient('192.168.178.110', 502)
modbus_client.connect()

#The first argument is the starting registers, the second argument is the quantity.

register_values = modbus_client.read_holdingregisters(0, 2)

print("Value of Register #1:" + str(register_values[0]))
print("Value of Register #2:" + str(register_values[1])) 

#Close the Port

modbus_client.close()

 

5. 라이브러리 문서화

Class: ModbusClient

5.1  Methods

Constructor def __init__(self, *params)  생성자

Modbus RTU용 생성자(serial line)
modbusClient = ModbusClient.ModbusClient('COM1')  첫 번째 매개변수는 직렬 포트입니다.

Modbus TCP 생성자

modbusClient = ModbusClient.ModbusClient('127.0.0.1', 502)
첫 번째 매개변수는 연결할 서버의 IP 주소입니다.
두 번째 매개변수는 서버가 수신하는 포트입니다.

 

connect(self)

주어진 매개변수로 Modbus-TCP 서버 또는 Modbus-RTU 슬레이브에 연결

close(self)

직렬 포트 또는 TCP 소켓 연결을 닫습니다.

read_discreteinputs(self, starting_address, quantity)

서버 장치 에서 Discrete Inputs 읽기((Function code 2)
starting_address  :  First discrete input to read
quantity                :  Number of discrete Inputs to read
returns                  : List of bool values [0..quantity-1] which contains the discrete Inputs

read_coils(self, starting_address, quantity)

서버 장치에서 Read Coils(기능 코드 1)
starting_address  : 수량 을 읽을 첫 번째 코일
quantity                : 읽을 코일 수
returns                  : 코일을 포함하는 bool 값 목록 [0..quantity-1]

read_holdingregisters(self, starting_address, quantity)

서버 장치에서 홀딩 레지스터 읽기(기능 코드 3)
starting_address : 수량을 읽을 첫 번째 홀딩 레지스터
quantity               :  읽을 홀딩 레지스터 수
returns                 : 레지스터를 포함하는 int 값 목록 [0..quantity-1]

read_inputregisters(self, starting_address, quantity)

서버 장치에서 입력 레지스터 읽기(기능 코드 4)
starting_address: 읽을 첫 번째 입력 레지스터
수량: 읽을 입력 레지스터 수
반환: 레지스터를 포함하는 int 값 목록 [0..quantity-1]

def write_single_coil(자신, 시작_주소, 값)

Write single Coil to Server device (Function code 5)
starting_address: 쓸 코일
값: 쓸 부울 값

def write_single_register(자신, 시작_주소, 값)

서버 장치에 단일 홀딩 레지스터 쓰기(기능 코드 6)
starting_address: 값을 쓸 홀딩 레지스터
: 쓸 int 값

def write_multiple_coils(self, starting_address, values)

Write multiple Coils to Server device (Function code 15)
starting_address: 쓸 첫 번째 코일
value: 쓸 Boolean 값 목록

def write_multiple_registers(self, starting_address, values)

서버 장치에 여러 홀딩 레지스터 쓰기(기능 코드 16)
starting_address: 값을 쓸 첫 번째 홀딩 레지스터
: 쓸 int 값 목록

 

5.2 속성  Properties

 

 

연번 속성 설명
1 port Datatype: int  
포트는 Modbus-TCP 서버에 연결할 수 있는 포트(표준은 502)
2 ipaddress Datatype: str
IP-Address of the Server to be connected
3 unitidentifier Datatype: int
Unit identifier in case of serial connection (Default = 1)
4 baudrate Datatype: int
Baudrate for serial connection (Default = 9600)
5 parity Datatype: int
Parity in case of serial connection The Helper class "Parity" can be used to define the parity (Default = Parity.even)
For example: modbus_client.parity = easymodbus.modbusClient.Parity.odd
Possible values are: even = 0
odd = 1
none = 2
6 stopbits Datatype: int
Stopbits in case of serial connection (Default = Stopbits.one) The Helper class "Stopbits" can be used to define the number of stopbits
For example: modbus_client.stopbits = easymodbus.modbusClient.Stopbits.one
Possible values are: one = 0
two = 1
onePointFive = 2
7 timeout Datatype: int
Max. time before an Exception is thrown (Default = 5)
8 is_connected Datatype: bool
Returns true if a connection has been established (only read)
9 debug Datatype: bool
Enables/disables debug mode - In debug mode Request and Response and depending in the logging level the RAW-Data are displayed at the console output and stored in a logfile "logdata.txt"
Disabled at default
10 logging_level Sets or gets the logging level. Default value is logging.INFO. In this Request and Response including arguments are logged (if debug is enabled)
if the level is set to logging.DEBUG also the RAW data transmitted and received are logged for debugging purposes.
The are logged at the console output and stored in logfile.txt

 

5.3  Helper functions

convert_double_to_two_registers(doubleValue, register_order=RegisterOrder.lowHigh)

32비트 값을 2개의 16비트 값으로 변환하여 Modbus 레지스터로 전송합니다
. doubleValue: 변환할 값
register_order: 원하는 단어 순서(낮은 레지스터 우선 또는 높은 레지스터 우선 - 기본값: RegisterOrder.lowHigh
반환: 16비트 레지스터 값 int[]

convert_float_to_two_registers(floatValue, register_order=RegisterOrder.lowHigh)

32비트 실제 값을 2개의 16비트 값으로
변환하여 Modbus 레지스터로 전송 floatValue: 변환할 값
register_order: 원하는 단어 순서(낮은 레지스터 우선 또는 높은 레지스터 우선 - 기본값: RegisterOrder.lowHigh
반환: 16비트 레지스터 값 int[]

convert_registers_to_double(레지스터, register_order=RegisterOrder.lowHigh)

두 개의 16비트 레지스터를 32비트 긴 값으로 변환 - Modbus(Modbus 레지스터는 16비트 길이임) 레지스터에서 32비트 값을 수신하는 데 사용됨
레지스터: 16비트 레지스터
register_order: 원하는 단어 순서(낮은 레지스터 먼저 또는 높은 레지스터 먼저 - 기본값: RegisterOrder. lowHigh
반환: 32비트 값

convert_registers_to_float(레지스터, register_order=RegisterOrder.lowHigh)

두 개의 16비트 레지스터를 32비트 실제 값으로 변환 - Modbus(Modbus 레지스터는 16비트 길이)
레지스터에서 부동 값을 수신하는 데 사용됨 레지스터: 16비트 레지스터
register_order: 원하는 단어 순서(낮은 레지스터 우선 또는 높은 레지스터 우선 - 기본값: RegisterOrder.lowHigh
반환: 32비트 값 실수

 

 

 

# 오토닉스 TK4S, ID:6

import easymodbus.modbusClient

MODBUS_COMPORT = 'COM5'
modbus_client = easymodbus.modbusClient.ModbusClient(MODBUS_COMPORT)
modbus_client.debug         = True
modbus_client.logging_level = easymodbus.modbusClient.logging.DEBUG
modbus_client.stopbits      = easymodbus.modbusClient.Stopbits.two
modbus_client.parity        = easymodbus.modbusClient.Parity.none

modbus_client.connect()

modbus_client.unitidentifier = 6
currentTemp = modbus_client.read_inputregisters(1000, 1)

print(currentTemp)

댓글