MCError throws new expection for some errors#6
Open
ElectricMolasses wants to merge 1 commit intoNothinRandom:mainfrom
Open
MCError throws new expection for some errors#6ElectricMolasses wants to merge 1 commit intoNothinRandom:mainfrom
ElectricMolasses wants to merge 1 commit intoNothinRandom:mainfrom
Conversation
`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!
0x0051 and 0x0054 inclusive is caught by
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. |
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 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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
MCError, the application throws two exceptions before crashing. The first is thatMCError: <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