1. TOP
  2. Python
  3. PLC communication
  4. MITSUBISHI MC Protocol Communication

日本語 English

Mitsubishi MELSEC MC Protocol Communication with Python

Overview

Reads and writes device values from Python using the MC protocol, the communication protocol of Mitsubishi PLCs

Operating environment

Tested environment: Win10
python3.9
MITSUBISHI MC Protocol Frame3E UDP/IP Connection

Configuration

Configuration diagram of MC protocol communication between Python and a Mitsubishi MELSEC PLC

Installation


 > pip install mcprotocol-udp

Method list

MethodsDescription
readRead memory area
writeWrite memory area
RandomReadRead non-consecutive devices
MonitorSetRegister devices to monitor
MonitorGetRead the values of registered devices

Creating the object

MCProtcol3E(IPAddress, Port)

Parameters
IPAddressDestination IP address
PortDestination port number

Return value
Return valueMCProtcol3E object

Example


mcp = MCProtcol3E('192.168.0.41', 4999)

Methods

read(memAddres, size, unitOfBit)

Read memory area

Parameters
memAddresMemory address
sizeNumber of points to read
unitOfBitTrue = bit units, False = word units

Return value
Return valueRead data (byte array)

Convert the read data with the data conversion methods

Supported memory addresses

X, Y, M, L, F, V, B, D, W, TS, TC, TN, SS, SC, SN, CS, CC, CN, SB, SW, S ,DX, DY, SM, SD

Example


# bit units
data = mcp.read('M0', 8, True)
# word units
data = mcp.read('D0', 1, False)


write(memAddres, data, bitSize)

Write memory area

Parameters
memAddresMemory address
dataWrite data (byte array)
bitSizeNumber of bits (specify only when writing in bit units)

Return value
Return value00 00 (normal completion)

Supported memory addresses

X, Y, M, L, F, V, B, D, W, TS, TC, TN, SS, SC, SN, CS, CC, CN, SB, SW, S ,DX, DY, SM, SD

Example


# Write data from D0
rcv = mcp.write('D0', data)


RandomRead(worddevice, dworddevice)

Read non-consecutive devices

Parameters
worddeviceWord read addresses (comma-separated)
dworddeviceDouble word read addresses (comma-separated)

Return value
Return valueRead data (byte array)

Supported memory addresses

X, Y, M, L, F, V, B, D, W, TN, SN, CN, SB, SW, S ,DX, DY, SM, SD

Example


rcv = mcp.RandomRead('D0,TN0,M100,X20', 'D1500,Y160,M1111')


MonitorSet(worddevice, dworddevice)

Register devices to monitor

Parameters
worddeviceWord read addresses (comma-separated)
dworddeviceDouble word read addresses (comma-separated)

Return value
Return value00 00 (normal completion)

Supported memory addresses

X, Y, M, L, F, V, B, D, W, TN, SN, CN, SB, SW, S ,DX, DY, SM, SD

Example


rcv = mcp.MonitorSet('D50, D55', 'D60, D64')


MonitorGet()

Read the values of registered devices

Return value
Return valueRead data (byte array)

Supported memory addresses

X, Y, M, L, F, V, B, D, W, TN, SN, CN, SB, SW, S ,DX, DY, SM, SD

Example


rcv = mcp.MonitorGet()


Data conversion methods

Example


print(mcp.toInt16(data))

Methods for converting the received bytes
The method to use depends on what kind of data the bytes contain

Method nameLength of data to convert
toBinBit string
WordtoBinBit string in 16-bit units
toInt1616-bit number
toUInt1616-bit unsigned
toInt3232-bit number
toUInt3232-bit unsigned
toInt6464-bit number
toUInt6464-bit unsigned
toFloatFloating point
toDoubleDouble precision
toStringString

Example

from mcprotocol import MCProtcol3E
import struct

'''
# Uncomment if you need the MCProtocol send/receive log
import logging

logging.basicConfig(
	filename="mcp.log",
	level=logging.DEBUG,
	format="%(asctime)s [%(levelname)s] %(name)s: %(message)s"
)
'''

try:
	# set IPAddress,Port
	mcp = MCProtcol3E('192.168.250.41', 4999)

	# words
	data = mcp.read('D10000', 1)
	print(mcp.toInt16(data))        # convert int16    out> [0]
	rcv = mcp.write('D10', data)
	print(rcv)                      # normal recieve  out> b'\x00\x00'

	# bits
	#data = mcp.read('M8000', 8, True)
	data = mcp.read('M0', 16, True)
	print(data)                     # out> b'\x00\x00\x00\x00\x00\x00\x00\x00
	print(mcp.toBin(data))          # out> 0000000000000000
	rcv = mcp.write('M100', data, 8)
	print(rcv)                      # out> b'\x00\x00'

	data = mcp.read('D0', 1, False)
	print(mcp.WordToBin(data))      # out> 0000000000000000

	# numeric
	data = struct.pack('hhh', 123, 456, 789)
	rcv = mcp.write('D20', data)
	print(rcv)                      # out> b'\x00\x00'
	# float
	data = struct.pack('f', 1.23)
	rcv = mcp.write('D10020', data)
	print(rcv)                      # out> b'\x00\x00'
	# bits
	data = [0x11, 0x11, 0x10]
	rcv = mcp.write('M8100', data, 5)

	# RandomRead
	rcv = mcp.RandomRead('D0,TN0,M100,X20', 'D1500,Y160,M1111')
	print(rcv)                      # out> b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
	print(mcp.toInt16(rcv[:8]))     # out> [0, 0, 0, 0]
	print(mcp.toInt32(rcv[8:]))     # out> [0, 0, 0]

	# Monitor
	data = mcp.MonitorSet('D50, D55', 'D60, D64')
	rcv = mcp.MonitorGet()
	print(mcp.toInt16(rcv[:4]))    # out> [50, 55]
	print(mcp.toInt32(rcv[4:]))    # out> [3997756, 4259904]
	
except Exception as e:
    print(e)

Related pages

Qiita article (Japanese)

Sending the Mitsubishi MELSEC MC protocol with Python

GitHub

GitHub MC-Protocol


Comments