Skip to content

Conversation

@ElectricMolasses
Copy link

@ElectricMolasses ElectricMolasses commented Jun 13, 2025

MCError, the application throws two exceptions before crashing. The first is that MCError: <exception str() failed> has failed, and the second is the cause of the nested error, TypeError '>=' not supported between instances of 'str' and 'int'`. This is an attempted fix for that user, though I do not have a PLC to test on myself. I believe this will handle all cases, but since I'm uncertain what the real value of errorcode is, I've made a best effort correction based on what I can infer from the code. Hopefully this PR is welcome!

Fixes #5

`MCError`, the application throws two exceptions before crashing. The
first is that `MCError: <exception str() failed> has failed, and the
second is the cause of the nested error, `TypeError '>=' not supported
between instances of 'str' and 'int'`. This is an attempted fix for that
user, though I do not have a PLC to test on myself. I believe this will
handle all cases, but since I'm uncertain what the real value of
errorcode is, I've made a best effort correction based on what I can
infer from the code. Hopefully this PR is welcome!
@ElectricMolasses ElectricMolasses changed the title When an error code between 0x0051 and 0x0054 inclusive is caught by fix #5 Jun 13, 2025
@ElectricMolasses ElectricMolasses changed the title fix #5 MCError throws new expection for some errors Jun 13, 2025
@NothinRandom
Copy link
Owner

@ElectricMolasses Do you know what is needed to replicate this issue? Let me see if I can get some hardware to validate prior to merging.

@ElectricMolasses
Copy link
Author

@NothinRandom Sorry, I completely missed the comment somehow!

I was troubleshooting second-hand for someone that was learning to use the equipment in a lab. I believe their approach was throwing a legitimate error, this library just crashed out within that short range of error codes as well.

They were using a Mitsubishi IQ-R series PLC, and the following code threw the exception for them:

from pymelsec import Type3E
from pymelsec.constants import DT
from pymelsec.tag import Tag

"""
There was no setup needed within GX Works 3 other than setting the IP Address and Subnet Mask
"""

# Here I define two variables that i later use as arguments to assign my PLC Address and TCP/IP Port (Default is 5007)
HOST = "10.51.12.10"
PORT = 5007

"""
I import the Type3E class from pymelsec, which is how you simply are allowed to talk with the PLC from your PC

You possibly can use Type4E for CC-Link use cases but as I am in the office with limited scenarios I am sticking with basic ethernet
"""

#Created the Type3E client (for iQ-R)
plc = Type3E(host="10.51.12.10", port=5007, plc_type="iQ-R")
plc.connect(HOST, PORT)

# I used try, except, and finally concepts to hand the connection and to handle any exceptions that may occur during the connection or reading process. It makes more sense to use try, except, and finally instead of while loops or conditionals
try:
    # Connect to PLC
    print(f"Connecting to PLC at {HOST}:{PORT}...")
    plc.connect(HOST, PORT)
    print("Connected successfully.")

    """
    Read tags from PLC
    READ_TAGS is a list of the device names and their types
    To view the values you must use plc.read() and pass the READ_TAGS as an argument.
    I assigned results to that and used a for loop to print the values.
    As shown below:
    """
    # If you want to read a Label, i.e. Global Label it needs to be assigned to a Device such as (D, M, etc.) in the PLC and then you can read the label tag using the device name instead.
    # Read tags from PLC
READ_TAGS = [
    Tag(device="D100", type=DT.SWORD),  # WORD signed
    Tag(device="D101", type=DT.UWORD),  # WORD unsigned
    Tag(device="D102", type=DT.SDWORD), # DWORD signed
    Tag(device="D104", type=DT.UDWORD), # DWORD unsigned
    Tag(device="M1", type=DT.BIT), #  BIT
    Tag(device="D206", type=DT.FLOAT),  # FLOAT
    ]

    read_results = plc.read(READ_TAGS)

    for tag in read_results:
        print(f"{tag.device}: {tag.value} ({tag.type})")

    print("Read operation successful.")
    print("\n")
    print("\n")



    # Write tags to PLC
    values = [
        int(201),
        float(10.54)

    ]

    write_result1 = plc.batch_write("D200", [201], DT.SWORD)
    for tag in write_result1:
        print(f"{tag.device}: ({tag.type})")



except Exception as e:
    print("❌ Communication failed during write.")
    print("Error Type:", type(e).name)
    print("Raw Error:", e)


finally:
    plc.close()
    print("Read and Write operation successful, closing connection.")

plc.close()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MCError throwing TypeError on errors from 0x0051 to 0x0054

2 participants