KMtronic Modbus LAN TCP/IP to Modbus RS485 RTU Serial Converter: testing MODBUS LED Display

 

Python examples KMtronic Modbus LAN TCP/IP to Modbus RS485 RTU Serial Converter: testing MODBUS LED Display

 

Python examples KMtronic Modbus LAN TCP/IP to Modbus RS485 RTU Serial Converter: testing MODBUS LED Display
Python examples KMtronic Modbus LAN TCP/IP to Modbus RS485 RTU Serial Converter: testing MODBUS LED Display

 

Pymodbus is a full Modbus protocol implementation using twisted for its asynchronous communications core.

 

import struct

from struct import *
from pymodbus.client.sync import ModbusTcpClient
# from pymodbus.client.sync import ModbusSerialClient as ModbusClient

import logging
logging.basicConfig()
log = logging.getLogger()
log.setLevel(logging.DEBUG)

// IP KMtronic Modbus LAN TCP/IP to Modbus RS485 RTU Serial Converter
host = '192.168.1.199'
port = 502

client = ModbusTcpClient(host, port)
# client = ModbusClient(method='rtu', port='/dev/ttyUSB0', timeout=1)
client.connect()


# First digital input address
address = 0
# Written value
value = 1221

# It will send '1221' to the output 
client.write_register(address, value, unit=1)

print "-----------------------------"

 

Output

kr@ubuntu-server:/var/www/html/python-test$ python ModBus-LED-Display.py
DEBUG:pymodbus.transaction:Current transaction state - IDLE
DEBUG:pymodbus.transaction:Running transaction 1
DEBUG:pymodbus.transaction:SEND: 0x0 0x1 0x0 0x0 0x0 0x6 0x1 0x6 0x0 0x0 0x4 0xc5
DEBUG:pymodbus.client.sync:New Transaction state 'SENDING'
DEBUG:pymodbus.transaction:Changing transaction state from 'SENDING' to 'WAITING FOR REPLY'
DEBUG:pymodbus.transaction:Changing transaction state from 'WAITING FOR REPLY' to 'PROCESSING REPLY'
DEBUG:pymodbus.transaction:RECV: 0x0 0x1 0x0 0x0 0x0 0x6 0x1 0x6 0x0 0x0 0x4 0xc5
DEBUG:pymodbus.framer.socket_framer:Processing: 0x0 0x1 0x0 0x0 0x0 0x6 0x1 0x6 0x0 0x0 0x4 0xc5
DEBUG:pymodbus.factory:Factory Response[WriteSingleRegisterResponse: 6]
DEBUG:pymodbus.transaction:Adding transaction 1
DEBUG:pymodbus.transaction:Getting transaction 1
DEBUG:pymodbus.transaction:Changing transaction state from 'PROCESSING REPLY' to 'TRANSACTION_COMPLETE'
-----------------------------
kr@ubuntu-server:/var/www/html/python-test$

 

Python examples KMtronic Modbus LAN TCP/IP to Modbus RS485 RTU Serial Converter: testing MODBUS LED Display
Python examples KMtronic Modbus LAN TCP/IP to Modbus RS485 RTU Serial Converter: testing MODBUS LED Display

 

Python read SDM230 Modbus Single Phase Energy Meter with Pymodbus

 

Python read SDM230 Modbus 220/230V Single Phase Energy Meter with Pymodbus
Python read SDM230 Modbus 220/230V Single Phase Energy Meter with Pymodbus

 

import struct
import binascii
import time

from pymodbus.constants import Endian
from pymodbus.payload import BinaryPayloadDecoder
from pymodbus.payload import BinaryPayloadBuilder


from struct import *
from pymodbus.client.sync import ModbusTcpClient

import logging
logging.basicConfig()
log = logging.getLogger()
#log.setLevel(logging.DEBUG)

host = '192.168.1.204'
port = 502

client = ModbusTcpClient(host, port)
client.connect()


for x in range(0, 300):

rr = client.read_input_registers(0x0000,2,unit=1)

assert(rr.function_code < 0x80) # test that we are not an error

print "-----------------------------"
 print rr
 print rr.registers
 print "-----------------------------"
 print format(rr.registers[0], '02x')
 print format(rr.registers[1], '02x')
 print "-----------------------------"
 # PYTHON FLOAT IEEE 754
 decoder = BinaryPayloadDecoder.fromRegisters(rr.registers, endian=Endian.Big)
 print ("Voltage is ", decoder.decode_32bit_float(), "V")
 print "-----------------------------"
 time.sleep(1)

 

Python read SDM230 Modbus 220/230V Single Phase Energy Meter with Pymodbus
Python read SDM230 Modbus 220/230V Single Phase Energy Meter with Pymodbus

 

Python read SDM230 Modbus Single Phase Energy Meter with Pymodbus
Python read SDM230 Modbus Single Phase Energy Meter with Pymodbus

 

KMtronic Modbus LAN TCP/IP to Modbus RS485 RTU Serial Converter
KMtronic Modbus LAN TCP/IP to Modbus RS485 RTU Serial Converter

 

KMtronic Modbus LAN TCP/IP to Modbus RS485 RTU Serial Converter
KMtronic Modbus LAN TCP/IP to Modbus RS485 RTU Serial Converter
KMtronic Modbus LAN TCP/IP to Modbus RS485 RTU Serial Converter
KMtronic Modbus LAN TCP/IP to Modbus RS485 RTU Serial Converter

 

 

KMtronic Modbus RS485 RTU Serial to Modbus LAN TCP/IP Module Converter

 

KMtronic RS485 8 Channel ModBus RTU Relay Controller

 

 

Ethernet Modbus IP 8 channels Modbus/TCP LAN Relay board

 

 

How to send bytes to serial port in Python | Control KMtronic USB Relay board example

KMtronic USB Relay Controller - One Channel - BOX
KMtronic USB Relay Controller – One Channel – BOX
KMtronic USB Eight Channel Relay Controller
KMtronic USB Eight Channel Relay Controller

 

FIRST channel commands:
OFF command : FF 01 00 (HEX) or 255 1 0 (DEC)
ON command : FF 01 01 (HEX) or 255 1 1 (DEC)

 

(1) Verify Python Interpreter Availability with ‘helloworld.py’

#!/usr/bin/python

# Hello world python program

print "Hello World!";

 

(2) Create ‘kmtronic_usb_relay_test.py” file

(3) Copy and paste code:

#!/usr/bin/python
# Test KMtronic WEB Relay board

print "Test KMtronic USB Relay board";

import os
import time
import serial
import subprocess

from struct import pack, unpack, calcsize

# configure the serial connections 
# (the parameters differs on the device 
# you are connecting to)
ser = serial.Serial(
 port='/dev/ttyUSB0',
 baudrate=9600,
 stopbits=serial.STOPBITS_ONE,
 bytesize=serial.EIGHTBITS,
 parity=serial.PARITY_NONE
)

ser.isOpen()

channel = 0x01;

print "RELAY1_ON_COMMAND: 0xFF 0x01 0x01"
state = 0x01;
int_msg = [0xFF, channel, state]
byte_msg = map(chr, int_msg)
packet = pack('=ccc', *byte_msg)
ser.write(packet)

time.sleep(1)

print "RELAY1_OFF_COMMAND: 0xFF 0x01 0x00"
state = 0;
int_msg = [0xFF, channel, state]
byte_msg = map(chr, int_msg)
packet = pack('=ccc', *byte_msg)
 
ser.write(packet)
 
ser.close()

 

How to send bytes to serial port in Python | Control KMtronic USB Relay board example
How to send bytes to serial port in Python | Control KMtronic USB Relay board example

 

How to Send a GET Packet in Python | Control KMtronic WEB Relay board example

Python socket in Python | Control KMtronic WEB Relay board example
Python socket in Python | Control KMtronic WEB Relay board example

 

(1) Verify Python Interpreter Availability with ‘helloworld.py’

#!/usr/bin/python

# Hello world python program

print "Hello World!";

 

(2) Create ‘kmtronic_web_relay_test.py” file

(3) Copy and paste code:

#!/usr/bin/python

# Test KMtronic WEB Relay board

print "Test KMtronic WEB Relay board";

import socket
import time

print ""

HOST = "192.168.1.199" 
PORT = 80 # Arbitrary non-privileged port
RELAY1_ON_COMMAND = "GET /FF0101 HTTP/1.1\r\n\r\n"
RELAY1_OFF_COMMAND = "GET /FF0100 HTTP/1.1\r\n\r\n"

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))

print "RELAY1_ON_COMMAND:", RELAY1_ON_COMMAND
s.sendall(RELAY1_ON_COMMAND)

s.close()

time.sleep(1)

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))

print "RELAY1_OFF_COMMAND:", RELAY1_OFF_COMMAND
s.sendall(RELAY1_OFF_COMMAND)

s.close()

 

 

 

KMtronic WEB LAN Two channels Relay Ethernet IP Controller - BOX
KMtronic WEB LAN Two channels Relay Ethernet IP Controller – BOX

 

Python socket in Python | Control KMtronic WEB Relay board example
Python socket in Python | Control KMtronic WEB Relay board example

python kmtronic_web_relay_test.py

 

How to Send a UDP Packet in Python | Control KMtronic UDP Relay board

(1) Verify Python Interpreter Availability with ‘helloworld.py’

#!/usr/bin/python

# Hello world python program

print "Hello World!";

 

(2) Create ‘kmtronic_up_relay_test.py” file

(3) Copy and paste code:

#!/usr/bin/python
# Test KMtronic UDP Relay board

print "Test KMtronic UDP Relay board";

import socket
import time

UDP_IP = "192.168.1.199"
UDP_PORT = 12345

RELAY1_ON_COMMAND = "FF0101"
RELAY1_OFF_COMMAND = "FF0100"

print "UDP target IP:", UDP_IP
print "UDP target port:", UDP_PORT

print "RELAY1_ON_COMMAND:", RELAY1_ON_COMMAND

sock = socket.socket(socket.AF_INET, # Internet
 socket.SOCK_DGRAM) # UDP
sock.sendto(RELAY1_ON_COMMAND, (UDP_IP, UDP_PORT))

time.sleep(1)

print "RELAY1_OFF_COMMAND:", RELAY1_OFF_COMMAND
sock.sendto(RELAY1_OFF_COMMAND, (UDP_IP, UDP_PORT))

 

UDP LAN Two channels Relay Ethernet IP Controller - BOX
UDP LAN Two channels Relay Ethernet IP Controller – BOX

 

 

"kmtronic_up_relay_test.py" file
python kmtronic_up_relay_test.py