### Install pymcprotocol Source: https://github.com/senrust/pymcprotocol/blob/main/docs/index.md Use pip to install the library. ```shell pip install pymcprotocol ``` -------------------------------- ### Start PLC Execution Source: https://context7.com/senrust/pymcprotocol/llms.txt Starts PLC program execution with configurable clear modes. ```python import pymcprotocol pymc3e = pymcprotocol.Type3E() pymc3e.connect("192.168.1.2", 1025) # Run PLC with different clear modes # clear_mode: 0 = do not clear, 1 = clear except latch devices, 2 = clear all devices pymc3e.remote_run(clear_mode=0) # Run without clearing devices pymc3e.remote_run(clear_mode=1) # Run and clear non-latch devices pymc3e.remote_run(clear_mode=2) # Run and clear all devices # Force execution even if another device is controlling pymc3e.remote_run(clear_mode=2, force_exec=True) pymc3e.close() ``` -------------------------------- ### remote_run Source: https://context7.com/senrust/pymcprotocol/llms.txt Starts PLC program execution remotely. ```APIDOC ## remote_run ### Description Start PLC program execution remotely with specified clear mode. ### Parameters - **clear_mode** (int) - Required - 0: do not clear, 1: clear except latch, 2: clear all. - **force_exec** (boolean) - Optional - Force execution even if another device is controlling. ``` -------------------------------- ### Write Consecutive Bit Devices Source: https://context7.com/senrust/pymcprotocol/llms.txt Writes 0 or 1 values to a sequence of bit devices starting from a specified head device. ```python import pymcprotocol pymc3e = pymcprotocol.Type3E() pymc3e.connect("192.168.1.2", 1025) # Write bit values to Y10-Y14 (5 consecutive devices) pymc3e.batchwrite_bitunits(headdevice="Y10", values=[0, 1, 0, 1, 0]) # Write to M devices pymc3e.batchwrite_bitunits(headdevice="M200", values=[1, 1, 0, 0, 1, 1, 0, 0]) # Turn on a single output pymc3e.batchwrite_bitunits(headdevice="Y20", values=[1]) pymc3e.close() ``` -------------------------------- ### Initialize Type3E Connection Source: https://context7.com/senrust/pymcprotocol/llms.txt Create a Type3E connection instance for MC protocol. Specify the PLC series if not using the default Q series. Connect to the PLC using its IP address and port, and close the connection when done. ```python import pymcprotocol # Initialize for Q series PLC (default) pymc3e = pymcprotocol.Type3E() # Initialize for specific PLC series pymc3e_l = pymcprotocol.Type3E(plctype="L") # L series pymc3e_qna = pymcprotocol.Type3E(plctype="QnA") # QnA series pymc3e_iql = pymcprotocol.Type3E(plctype="iQ-L") # iQ-L series pymc3e_iqr = pymcprotocol.Type3E(plctype="iQ-R") # iQ-R series # Connect to PLC pymc3e.connect("192.168.1.2", 1025) # Close connection when done pymc3e.close() ``` -------------------------------- ### Configure Communication Options with setaccessopt Source: https://context7.com/senrust/pymcprotocol/llms.txt Configure MC protocol access options before connecting. Use 'ascii' for ASCII communication mode or 'binary' (default). Network settings like network number, PC station, destination module I/O and station, and timeout can also be configured. ```python import pymcprotocol pymc3e = pymcprotocol.Type3E() # Set to ASCII communication mode (default is "binary") pymc3e.setaccessopt(commtype="ascii") # Configure network options for multidrop connections pymc3e.setaccessopt( commtype="binary", # Communication type: "binary" or "ascii" network=0, # Network number (0-255) pc=0xFF, # PC station number (0-255) dest_moduleio=0x3FF, # Destination module I/O number dest_modulesta=0x0, # Destination module station number timer_sec=4 # Timeout in seconds (socket timeout = timer_sec + 1) ) pymc3e.connect("192.168.1.2", 1025) ``` -------------------------------- ### Connect to PLC Source: https://github.com/senrust/pymcprotocol/blob/main/docs/index.md Initialize the protocol type based on the PLC series and establish a connection. ```python import pymcprotocol #If you use Q series PLC pymc3e = pymcprotocol.Type3E() #if you use L series PLC, pymc3e = pymcprotocol.Type3E(plctype="L") #if you use QnA series PLC, pymc3e = pymcprotocol.Type3E(plctype="L") #if you use iQ-L series PLC, pymc3e = pymcprotocol.Type3E(plctype="iQ-L") #if you use iQ-R series PLC, pymc3e = pymcprotocol.Type3E(plctype="iQ-R") #If you use 4E type pymc4e = pymcprotocol.Type4E() #If you use ascii byte communication. (default is "binary") pymc3e.setaccessopt(commtype="ascii") pymc3e.connect("192.168.1.2", 1025) ``` -------------------------------- ### Type3E - Initialize and Connect Source: https://context7.com/senrust/pymcprotocol/llms.txt Initializes a Type3E connection instance for MC protocol communication and establishes a connection to the PLC. ```APIDOC ## Type3E - Initialize and Connect ### Description Create a Type3E connection instance for MC protocol communication. The constructor accepts a plctype parameter to specify the PLC series being used. ### Method ```python import pymcprotocol # Initialize for Q series PLC (default) pymc3e = pymcprotocol.Type3E() # Initialize for specific PLC series pymc3e_l = pymcprotocol.Type3E(plctype="L") # L series pymc3e_qna = pymcprotocol.Type3E(plctype="QnA") # QnA series pymc3e_iql = pymcprotocol.Type3E(plctype="iQ-L") # iQ-L series pymc3e_iqr = pymcprotocol.Type3E(plctype="iQ-R") # iQ-R series # Connect to PLC pymc3e.connect("192.168.1.2", 1025) # Close connection when done pymc3e.close() ``` ``` -------------------------------- ### Supported PLC Device Types Reference Source: https://context7.com/senrust/pymcprotocol/llms.txt Lists available bit and word device types for read/write operations. ```python # Bit devices (use batchread_bitunits / batchwrite_bitunits) # SM - Special relay # X - Input (hexadecimal addressing) # Y - Output (hexadecimal addressing) # M - Internal relay # L - Latch relay # F - Annunciator # V - Edge relay # B - Link relay (hexadecimal) # TS - Timer contact # TC - Timer coil # CS - Counter contact # CC - Counter coil # SB - Link special relay (hexadecimal) # DX - Direct input (hexadecimal) # DY - Direct output (hexadecimal) # Word devices (use batchread_wordunits / batchwrite_wordunits) # SD - Special register # D - Data register # W - Link register (hexadecimal) # TN - Timer current value # CN - Counter current value # SW - Link special register (hexadecimal) # R - File register # ZR - File register (hexadecimal) # iQ-R series additional devices # LTS, LTC, LTN - Long timer # LSTS, LSTN - Long retentive timer # LCS, LCC, LCN - Long counter # LZ - Long index register # RD - Device ``` -------------------------------- ### Handle MC Protocol Errors Source: https://context7.com/senrust/pymcprotocol/llms.txt Demonstrates exception handling for common MC protocol and connection errors. ```python import pymcprotocol from pymcprotocol.mcprotocolerror import MCProtocolError, UnsupportedComandError from pymcprotocol.type3e import PLCTypeError, CommTypeError try: pymc3e = pymcprotocol.Type3E(plctype="Q") pymc3e.connect("192.168.1.2", 1025) # Attempt to read devices values = pymc3e.batchread_wordunits(headdevice="D100", readsize=10) except PLCTypeError: print("Invalid PLC type specified") except CommTypeError: print("Invalid communication type (must be 'binary' or 'ascii')") except UnsupportedComandError: print("Command not supported - try connecting via E71 module") except MCProtocolError as e: print(f"MC Protocol error: {e.errorcode}") except ConnectionRefusedError: print("Connection refused - check PLC IP and port") except TimeoutError: print("Connection timeout - check network connectivity") finally: pymc3e.close() ``` -------------------------------- ### Initialize Type4E Connection with Subheader Serial Source: https://context7.com/senrust/pymcprotocol/llms.txt Create a Type4E connection instance, which supports subheader serial identification. Set the subheader serial for client identification (0-65535) before connecting. All Type3E methods are available. ```python import pymcprotocol # Initialize Type4E connection pymc4e = pymcprotocol.Type4E() # Set subheader serial for client identification (0-65535) pymc4e.set_subheaderserial(12345) # Connect to PLC pymc4e.connect("192.168.1.2", 1025) # All Type3E methods are available word_values = pymc4e.batchread_wordunits(headdevice="D100", readsize=10) pymc4e.close() ``` -------------------------------- ### Type4E - Initialize with Subheader Serial Source: https://context7.com/senrust/pymcprotocol/llms.txt Initializes a Type4E connection instance for 4E type MC protocol communication, supporting subheader serial identification. ```APIDOC ## Type4E - Initialize with Subheader Serial ### Description Create a Type4E connection instance for 4E type MC protocol communication. Type4E extends Type3E with support for subheader serial identification. ### Method ```python import pymcprotocol # Initialize Type4E connection pymc4e = pymcprotocol.Type4E() # Set subheader serial for client identification (0-65535) pymc4e.set_subheaderserial(12345) # Connect to PLC pymc4e.connect("192.168.1.2", 1025) # All Type3E methods are available word_values = pymc4e.batchread_wordunits(headdevice="D100", readsize=10) pymc4e.close() ``` ``` -------------------------------- ### setaccessopt - Configure Communication Options Source: https://context7.com/senrust/pymcprotocol/llms.txt Configures MC protocol access options including communication type, network settings, and timeout values. This must be called before connecting if using ASCII mode. ```APIDOC ## setaccessopt - Configure Communication Options ### Description Configure MC protocol access options including communication type, network settings, and timeout values. This must be called before connecting if using ASCII mode. ### Method ```python import pymcprotocol pymc3e = pymcprotocol.Type3E() # Set to ASCII communication mode (default is "binary") pymc3e.setaccessopt(commtype="ascii") # Configure network options for multidrop connections pymc3e.setaccessopt( commtype="binary", # Communication type: "binary" or "ascii" network=0, # Network number (0-255) pc=0xFF, # PC station number (0-255) dest_moduleio=0x3FF, # Destination module I/O number dest_modulesta=0x0, # Destination module station number timer_sec=4 # Timeout in seconds (socket timeout = timer_sec + 1) ) pymc3e.connect("192.168.1.2", 1025) ``` ``` -------------------------------- ### MCProtocol Error Handling Source: https://github.com/senrust/pymcprotocol/blob/main/docs/pymcprotocol.md Details on MCProtocol errors and how to check for them. ```APIDOC ## Exception: MCProtocolError ### Description Represents a device code error, indicating that a device does not exist. ### Attributes - **plctype** (str) - The PLC type (e.g., "Q", "L", "iQ"). - **devicename** (str) - The name of the device that caused the error (e.g., "Q", "P"). ### Exception: UnsupportedComandError ### Description Indicates that the requested command is not supported by the connected module. ## Function: check_mcprotocol_error(status) ### Description Checks for MC protocol command errors. If an error is detected (status is not 0), it raises an appropriate error. ### Parameters - **status** (int) - Required - The status code returned from the MC protocol command. ### Example ```python # Assuming 'status_code' is obtained from a PLC operation if status_code != 0: check_mcprotocol_error(status_code) ``` ``` -------------------------------- ### Utility Functions Source: https://github.com/senrust/pymcprotocol/blob/main/docs/pymcprotocol.md Helper functions for text checking and value computation. ```APIDOC ## Utility Functions ### isascii(text) Checks if all characters in the given text are ASCII. * **Parameters**: * **text** (str) - The text to check. * **Note**: Python 3.6 does not support str.isascii(). ### twos_comp(val, mode='short') Computes the 2's complement of an integer value. * **Parameters**: * **val** (int) - The integer value to compute the 2's complement for. * **mode** (str) - Optional. Specifies the mode ('short' or other). Defaults to 'short'. ### get_device_number(device) Extracts the device number from a device string. * **Parameters**: * **device** (str) - The device string (e.g., "D1000", "X0x1A"). * **Returns**: The extracted device number string (e.g., "1000", "0x1A"). ``` -------------------------------- ### Write Word Devices with batchwrite_wordunits Source: https://context7.com/senrust/pymcprotocol/llms.txt Write consecutive word unit devices to PLC memory. Values are written as signed 16-bit integers. Ensure the connection is established before calling this method. ```python import pymcprotocol pymc3e = pymcprotocol.Type3E() pymc3e.connect("192.168.1.2", 1025) # Write values to D10-D14 (5 consecutive devices) pymc3e.batchwrite_wordunits(headdevice="D10", values=[0, 10, 20, 30, 40]) # Write to W devices pymc3e.batchwrite_wordunits(headdevice="W100", values=[1000, 2000, 3000]) # Write negative values (signed) pymc3e.batchwrite_wordunits(headdevice="D500", values=[-100, -50, 0, 50, 100]) pymc3e.close() ``` -------------------------------- ### Custom Exceptions Source: https://github.com/senrust/pymcprotocol/blob/main/docs/pymcprotocol.md Custom exception classes for communication and PLC type errors. ```APIDOC ## Custom Exceptions ### CommTypeError * **Description**: Raised when an invalid communication type is provided. Communication type must be "binary" or "ascii". * **Bases**: `Exception` ### PLCTypeError * **Description**: Raised when an invalid PLC type is provided. PLC type must be "Q", "L", "QnA", "iQ-L", or "iQ-R". ``` -------------------------------- ### Send PLC Commands Source: https://github.com/senrust/pymcprotocol/blob/main/docs/index.md Perform batch read/write operations and random access to PLC memory. ```python #read from D100 to D110 wordunits_values = pymc3e.batchread_wordunits(headdevice="D100", readsize=10) #read from X10 to X20 bitunits_values = pymc3e.batchread_bitunits(headdevice="X10", readsize=10) #write from D10 to D15 pymc3e.batchwrite_wordunits(headdevice="D10", values=[0, 10, 20, 30, 40]) #write from Y10 to Y15 pymc3e.batchwrite_bitunits(headdevice="Y10", values=[0, 1, 0, 1, 0]) #read "D1000", "D2000" and dword "D3000". word_values, dword_values = pymc3e.randomread(word_devices=["D1000", "D2000"], dword_devices=["D3000"]) #write 1000 to "D1000", 2000 to "D2000" and 655362 todword "D3000" pymc3e.randomwrite(word_devices=["D1000", "D1002"], word_values=[1000, 2000], dword_devices=["D1004"], dword_values=[655362]) #write 1(ON) to "X0", 0(OFF) to "X10" pymc3e.randomwrite_bitunits(bit_devices=["X0", "X10"], values=[1, 0]) ``` -------------------------------- ### Execute Remote System Commands Source: https://github.com/senrust/pymcprotocol/blob/main/docs/index.md Control PLC state and query system information when connected via an E71 module. ```python #remote run, clear all device pymc3e.remote_run(clear_mode=2, force_exec=True) #remote stop pymc3e.remote_stop() #remote latch clear. (have to PLC be stopped) pymc3e.remote_latchclear() #remote pause pymc3e.remote_pause(force_exec=False) #remote reset pymc3e.remote_reset() #read PLC type cpu_type, cpu_code = pymc3e.read_cputype() ``` -------------------------------- ### Stop and Reset PLC with pymcprotocol Source: https://context7.com/senrust/pymcprotocol/llms.txt Commands to stop and reset a PLC connection. The connection automatically reconnects upon reset. ```python # Stop PLC first pymc3e.remote_stop() # Reset PLC (connection automatically reconnects) pymc3e.remote_reset() pymc3e.close() ``` -------------------------------- ### batchwrite_wordunits - Write Word Devices Source: https://context7.com/senrust/pymcprotocol/llms.txt Writes consecutive word unit devices to PLC memory. Values are written as signed 16-bit integers. ```APIDOC ## batchwrite_wordunits - Write Word Devices ### Description Write consecutive word unit devices to PLC memory. Values are written as signed 16-bit integers. ### Method ```python import pymcprotocol pymc3e = pymcprotocol.Type3E() pymc3e.connect("192.168.1.2", 1025) # Write values to D10-D14 (5 consecutive devices) pymc3e.batchwrite_wordunits(headdevice="D10", values=[0, 10, 20, 30, 40]) # Write to W devices pymc3e.batchwrite_wordunits(headdevice="W100", values=[1000, 2000, 3000]) # Write negative values (signed) pymc3e.batchwrite_wordunits(headdevice="D500", values=[-100, -50, 0, 50, 100]) pymc3e.close() ``` ``` -------------------------------- ### batchread_wordunits - Read Word Devices Source: https://context7.com/senrust/pymcprotocol/llms.txt Reads consecutive word unit devices from PLC memory and returns a list of signed 16-bit integer values. ```APIDOC ## batchread_wordunits - Read Word Devices ### Description Read consecutive word unit devices from PLC memory. Returns a list of signed 16-bit integer values. ### Method ```python import pymcprotocol pymc3e = pymcprotocol.Type3E() pymc3e.connect("192.168.1.2", 1025) # Read 10 word values starting from D100 (D100 to D109) word_values = pymc3e.batchread_wordunits(headdevice="D100", readsize=10) print(f"D100-D109: {word_values}") # Output: D100-D109: [0, 100, 200, 300, 400, 500, 600, 700, 800, 900] # Read from other word devices w_values = pymc3e.batchread_wordunits(headdevice="W0", readsize=5) r_values = pymc3e.batchread_wordunits(headdevice="R100", readsize=8) pymc3e.close() ``` ``` -------------------------------- ### Batch Write Operations Source: https://github.com/senrust/pymcprotocol/blob/main/docs/pymcprotocol.md Methods for writing data to the PLC in batches, either by word units or bit units. ```APIDOC ## Batch Write Operations ### batchwrite_wordunits(headdevice, values) Writes data in word units to the PLC. * **Parameters**: * **headdevice** (str) - The starting device address for writing (e.g., "D1000"). * **values** (list[int]) - A list of integer values to write. ### batchwrite_bitunits(headdevice, values) Writes data in bit units to the PLC. * **Parameters**: * **headdevice** (str) - The starting device address for writing (e.g., "X10"). * **values** (list[int]) - A list of integers (0 or 1) to write. 0 represents OFF, 1 represents ON. ``` -------------------------------- ### Read Word Devices with batchread_wordunits Source: https://context7.com/senrust/pymcprotocol/llms.txt Read consecutive word unit devices from PLC memory. Returns a list of signed 16-bit integer values. Ensure the connection is established before calling this method. ```python import pymcprotocol pymc3e = pymcprotocol.Type3E() pymc3e.connect("192.168.1.2", 1025) # Read 10 word values starting from D100 (D100 to D109) word_values = pymc3e.batchread_wordunits(headdevice="D100", readsize=10) print(f"D100-D109: {word_values}") # Output: D100-D109: [0, 100, 200, 300, 400, 500, 600, 700, 800, 900] # Read from other word devices w_values = pymc3e.batchread_wordunits(headdevice="W0", readsize=5) r_values = pymc3e.batchread_wordunits(headdevice="R100", readsize=8) pymc3e.close() ``` -------------------------------- ### Batch Read Operations Source: https://github.com/senrust/pymcprotocol/blob/main/docs/pymcprotocol.md Methods for reading data from the PLC in batches, either by word units or bit units. ```APIDOC ## Batch Read Operations ### batchread_wordunits(headdevice, readsize) Reads data in word units from the PLC. * **Parameters**: * **headdevice** (str) - The starting device address for reading (e.g., "D1000"). * **readsize** (int) - The number of word units to read. * **Returns**: A list of integer values read from the specified devices. * **Return type**: list[int] ### batchread_bitunits(headdevice, readsize) Reads data in bit units from the PLC. * **Parameters**: * **headdevice** (str) - The starting device address for reading (e.g., "X1"). * **size** (int) - The number of bit units to read. * **Returns**: A list of integers (0 or 1) representing the bit values. * **Return type**: list[int] ``` -------------------------------- ### Read Bit Devices with batchread_bitunits Source: https://context7.com/senrust/pymcprotocol/llms.txt Read consecutive bit unit devices from PLC memory. Returns a list of 0s (OFF) or 1s (ON). Ensure the connection is established before calling this method. ```python import pymcprotocol pymc3e = pymcprotocol.Type3E() pymc3e.connect("192.168.1.2", 1025) # Read 10 bit values starting from X10 (X10 to X19) bit_values = pymc3e.batchread_bitunits(headdevice="X10", readsize=10) print(f"X10-X19: {bit_values}") # Output: X10-X19: [1, 0, 1, 1, 0, 0, 1, 0, 1, 0] # Read from other bit devices m_values = pymc3e.batchread_bitunits(headdevice="M100", readsize=16) y_values = pymc3e.batchread_bitunits(headdevice="Y0", readsize=8) pymc3e.close() ``` -------------------------------- ### Retrieve CPU Information with read_cputype Source: https://context7.com/senrust/pymcprotocol/llms.txt Reads the CPU model name and a 4-digit hex code from the connected PLC. ```python import pymcprotocol pymc3e = pymcprotocol.Type3E() pymc3e.connect("192.168.1.2", 1025) # Read CPU type information cpu_type, cpu_code = pymc3e.read_cputype() print(f"CPU Type: {cpu_type}") print(f"CPU Code: {cpu_code}") # Output: # CPU Type: Q03UDECPU # CPU Code: 0043 pymc3e.close() ``` -------------------------------- ### Information Retrieval Source: https://github.com/senrust/pymcprotocol/blob/main/docs/pymcprotocol.md Endpoints for reading PLC information. ```APIDOC ## GET /api/read_cputype ### Description Reads the CPU type of the PLC. ### Method GET ### Endpoint /api/read_cputype ### Response #### Success Response (200) - **cpu_type** (str) - The CPU type code (4-digit number). #### Response Example ```json { "cpu_type": "1234" } ``` ``` -------------------------------- ### Perform Connection Echo Test Source: https://context7.com/senrust/pymcprotocol/llms.txt Verifies communication by sending alphanumeric data to the PLC and checking the echoed response. ```python import pymcprotocol pymc3e = pymcprotocol.Type3E() pymc3e.connect("192.168.1.2", 1025) # Send echo test (alphanumeric data only, 1-960 characters) answer_len, answer_data = pymc3e.echo_test("Hello123") print(f"Answer length: {answer_len}") print(f"Answer data: {answer_data}") # Output: # Answer length: 8 # Answer data: Hello123 # Verify communication test_string = "TestConnection" length, response = pymc3e.echo_test(test_string) if response == test_string: print("Communication OK") pymc3e.close() ``` -------------------------------- ### read_cputype Source: https://context7.com/senrust/pymcprotocol/llms.txt Retrieves the CPU model name and a 4-digit hex code from the connected PLC. ```APIDOC ## read_cputype ### Description Read the CPU type and code from the connected PLC. Returns the CPU model name and a 4-digit hex code. ### Method N/A (Library Method) ### Endpoint pymc3e.read_cputype() ### Response - **cpu_type** (string) - The model name of the CPU. - **cpu_code** (string) - The 4-digit hex code of the CPU. ### Response Example CPU Type: Q03UDECPU CPU Code: 0043 ``` -------------------------------- ### batchread_bitunits - Read Bit Devices Source: https://context7.com/senrust/pymcprotocol/llms.txt Reads consecutive bit unit devices from PLC memory and returns a list of values (0 or 1) representing OFF or ON states. ```APIDOC ## batchread_bitunits - Read Bit Devices ### Description Read consecutive bit unit devices from PLC memory. Returns a list of values (0 or 1) representing OFF or ON states. ### Method ```python import pymcprotocol pymc3e = pymcprotocol.Type3E() pymc3e.connect("192.168.1.2", 1025) # Read 10 bit values starting from X10 (X10 to X19) bit_values = pymc3e.batchread_bitunits(headdevice="X10", readsize=10) print(f"X10-X19: {bit_values}") # Output: X10-X19: [1, 0, 1, 1, 0, 0, 1, 0, 1, 0] # Read from other bit devices m_values = pymc3e.batchread_bitunits(headdevice="M100", readsize=16) y_values = pymc3e.batchread_bitunits(headdevice="Y0", readsize=8) pymc3e.close() ``` ``` -------------------------------- ### randomread Source: https://context7.com/senrust/pymcprotocol/llms.txt Reads word and dword devices from non-consecutive memory locations. ```APIDOC ## randomread ### Description Read word and dword (double-word, 32-bit) devices from non-consecutive memory locations. Returns two lists: word values and dword values. ### Parameters - **word_devices** (list) - Required - List of word device addresses. - **dword_devices** (list) - Required - List of dword device addresses. ### Response - **word_values** (list) - List of values read from word_devices. - **dword_values** (list) - List of values read from dword_devices. ``` -------------------------------- ### Reset PLC Source: https://context7.com/senrust/pymcprotocol/llms.txt Resets the PLC remotely. The PLC must be stopped before execution. ```python import pymcprotocol pymc3e = pymcprotocol.Type3E() pymc3e.connect("192.168.1.2", 1025) ``` -------------------------------- ### remote_reset Source: https://context7.com/senrust/pymcprotocol/llms.txt Resets the PLC remotely. ```APIDOC ## remote_reset ### Description Reset the PLC remotely. The PLC must be stopped before using this command. ``` -------------------------------- ### batchwrite_bitunits Source: https://context7.com/senrust/pymcprotocol/llms.txt Writes consecutive bit unit devices to PLC memory. ```APIDOC ## batchwrite_bitunits ### Description Write consecutive bit unit devices to PLC memory. Each value must be 0 (OFF) or 1 (ON). ### Parameters - **headdevice** (string) - Required - The starting device address (e.g., "Y10", "M200"). - **values** (list) - Required - A list of integers (0 or 1) to write to the consecutive devices. ### Request Example ```python pymc3e.batchwrite_bitunits(headdevice="Y10", values=[0, 1, 0, 1, 0]) ``` ``` -------------------------------- ### Type4E Class Source: https://github.com/senrust/pymcprotocol/blob/main/docs/pymcprotocol.md Documentation for the Type4E class used for MC Protocol 4E communication. ```APIDOC ## Class: Type4E ### Description Implements MC Protocol 4E type communication. This class is similar to Type3E but differs in the subheader. ### Attributes - **subheader** (int) - Subheader for MC protocol. Default is 21504. - **subheaderserial** (int) - Subheader serial for MC protocol to identify the client. Default is 0. ### Methods #### set_subheaderserial(subheaderserial) ##### Description Changes the subheader serial. ##### Parameters - **subheaderserial** (int) - Required - The new subheader serial to set. ##### Example ```python # Assuming 'plc' is an instance of Type4E plc.set_subheaderserial(12345) ``` ``` -------------------------------- ### Type3E Class Source: https://github.com/senrust/pymcprotocol/blob/main/docs/pymcprotocol.md The Type3E class handles the mcprotocol 3E communication. It allows configuration of PLC type, communication type, and network settings. ```APIDOC ## Class: Type3E ### Description Implements mcprotocol 3E communication class. ### Parameters * **plctype** (str) - Optional - PLC type. Supported types: "Q", "L", "QnA", "iQ-L", "iQ-R". Defaults to "Q". ### Attributes * **plctype** (str) - PLC type. * **commtype** (str) - Communication type. "binary" or "ascii". Defaults to "binary". * **subheader** (int) - Subheader for mc protocol. Defaults to 20480. * **network** (int) - Network No. of an access target. (0 <= network <= 255). Defaults to 0. * **pc** (int) - Network module station No. of an access target. (0 <= pc <= 255). Defaults to 255. * **dest_moduleio** (int) - Start input/output number for multidrop connections. Defaults to 1023. * **dest_modulesta** (int) - Station No. for multidrop connections. Defaults to 0. * **timer** (int) - Timeout in PLC cycles (250msec). Defaults to 4 (1 sec). * **soc_timeout** (int) - Socket timeout in seconds. Defaults to 2. ### Methods #### __init__(plctype='Q') Constructor for the Type3E class. #### connect(ip, port, timeout) Connect to PLC. * **Parameters**: * **ip** (str) - IP address (IPV4) to connect PLC. * **port** (int) - Port number of connect PLC. * **timeout** (float) - Timeout in seconds for communication. #### close() Close connection. #### setaccessopt(commtype=None, network=None, pc=None, dest_moduleio=None, dest_modulesta=None, timer_sec=None) Set mc protocol access option. * **Parameters**: * **commtype** (str) - Communication type: "binary" or "ascii". * **network** (int) - Network No. of an access target (0-255). * **pc** (int) - Network module station No. of an access target (0-255). * **dest_moduleio** (int) - Start input/output number for multidrop connections. * **dest_modulesta** (int) - Station No. for multidrop connections. * **timer_sec** (int) - Timeout in seconds for PLC response. ``` -------------------------------- ### randomwrite Source: https://context7.com/senrust/pymcprotocol/llms.txt Writes word and dword values to non-consecutive memory locations. ```APIDOC ## randomwrite ### Description Write word and dword values to non-consecutive memory locations. ### Parameters - **word_devices** (list) - Required - List of word device addresses. - **word_values** (list) - Required - List of values to write to word_devices. - **dword_devices** (list) - Required - List of dword device addresses. - **dword_values** (list) - Required - List of values to write to dword_devices. ``` -------------------------------- ### Clear Latch Devices with remote_latchclear Source: https://context7.com/senrust/pymcprotocol/llms.txt Clears all latch device values. The PLC must be in a stopped state before executing this command. ```python import pymcprotocol pymc3e = pymcprotocol.Type3E() pymc3e.connect("192.168.1.2", 1025) # Stop PLC first pymc3e.remote_stop() # Clear latch devices pymc3e.remote_latchclear() # Restart PLC pymc3e.remote_run(clear_mode=0) pymc3e.close() ``` -------------------------------- ### echo_test Source: https://context7.com/senrust/pymcprotocol/llms.txt Performs an echo test to verify communication integrity between the host and the PLC. ```APIDOC ## echo_test ### Description Perform an echo test to verify communication with the PLC. Send data is echoed back for verification. ### Method N/A (Library Method) ### Endpoint pymc3e.echo_test(data) ### Parameters #### Request Body - **data** (string) - Required - Alphanumeric data only, 1-960 characters. ### Response - **answer_len** (int) - Length of the returned data. - **answer_data** (string) - The echoed data string. ``` -------------------------------- ### remote_pause Source: https://context7.com/senrust/pymcprotocol/llms.txt Pauses PLC program execution remotely. ```APIDOC ## remote_pause ### Description Pause PLC program execution remotely. ### Parameters - **force_exec** (boolean) - Optional - Force pause even if another device is controlling. ``` -------------------------------- ### Random Write Operations Source: https://github.com/senrust/pymcprotocol/blob/main/docs/pymcprotocol.md Functions for writing data to word and bit units randomly. ```APIDOC ## POST /api/randomwrite ### Description Writes data to word and dword units randomly. ### Method POST ### Endpoint /api/randomwrite ### Parameters #### Request Body - **word_devices** (list[str]) - Required - List of word devices to write to. - **word_values** (list[int]) - Required - List of integer values for each word device. - **dword_devices** (list[str]) - Required - List of dword devices to write to. - **dword_values** (list[int]) - Required - List of integer values for each dword device. ### Request Example ```json { "word_devices": ["D1000", "D1020"], "word_values": [100, 200], "dword_devices": ["D1000", "D1020"], "dword_values": [100, 200] } ``` ## POST /api/randomwrite_bitunits ### Description Writes data to bit units randomly. ### Method POST ### Endpoint /api/randomwrite_bitunits ### Parameters #### Request Body - **bit_devices** (list[str]) - Required - List of bit devices to write to. - **values** (list[int]) - Required - List of values (0 or 1) for each bit device. 0 is OFF, 1 is ON. ### Request Example ```json { "bit_devices": ["X10", "X20"], "values": [0, 1] } ``` ``` -------------------------------- ### Perform Remote Operations Source: https://github.com/senrust/pymcprotocol/blob/main/docs/index.md Manage PLC lock states for remote operations. ```python #Unlock PLC, #If you set PLC to locked, you need to unlkock to remote operation #Except iQ-R, password is 4 character. pymc3e.remote_unlock(password="1234") #If you want to hide password from program #You can enter passwrod directly pymc3e.remote_unlock(request_input=True) #Lock PLC pymc3e.remote_lock(password="1234") pymc3e.remote_lock(request_input=True) ``` -------------------------------- ### Write Non-Consecutive Devices Source: https://context7.com/senrust/pymcprotocol/llms.txt Writes word and dword values to specific, non-consecutive memory locations. ```python import pymcprotocol pymc3e = pymcprotocol.Type3E() pymc3e.connect("192.168.1.2", 1025) # Write to non-consecutive word devices and dword devices pymc3e.randomwrite( word_devices=["D1000", "D1002"], word_values=[1000, 2000], dword_devices=["D1004"], dword_values=[655362] ) # Write only word devices pymc3e.randomwrite( word_devices=["D100", "D200", "D300"], word_values=[10, 20, 30], dword_devices=[], dword_values=[] ) pymc3e.close() ``` -------------------------------- ### Pause PLC Execution Source: https://context7.com/senrust/pymcprotocol/llms.txt Pauses PLC program execution, which can be resumed later. ```python import pymcprotocol pymc3e = pymcprotocol.Type3E() pymc3e.connect("192.168.1.2", 1025) # Pause PLC execution pymc3e.remote_pause() # Force pause even if another device is controlling pymc3e.remote_pause(force_exec=True) pymc3e.close() ``` -------------------------------- ### PLC Security Operations Source: https://github.com/senrust/pymcprotocol/blob/main/docs/pymcprotocol.md API endpoints for locking and unlocking the PLC. ```APIDOC ## POST /api/remote_unlock ### Description Unlocks the PLC using a password. ### Method POST ### Endpoint /api/remote_unlock ### Parameters #### Request Body - **password** (str) - Optional - The remote password for unlocking. - **request_input** (bool) - Optional - If true, requires password input; otherwise, uses the provided password. ### Request Example ```json { "password": "secure_password", "request_input": false } ``` ## POST /api/remote_lock ### Description Locks the PLC using a password. ### Method POST ### Endpoint /api/remote_lock ### Parameters #### Request Body - **password** (str) - Optional - The remote password for locking. - **request_input** (bool) - Optional - If true, requires password input; otherwise, uses the provided password. ### Request Example ```json { "password": "secure_password", "request_input": false } ``` ``` -------------------------------- ### Random Read Operation Source: https://github.com/senrust/pymcprotocol/blob/main/docs/pymcprotocol.md Reads word and double-word units randomly from the PLC. ```APIDOC ## Random Read Operation ### randomread(word_devices, dword_devices) Reads word units and dword units randomly. Does not support monitor conditions. * **Parameters**: * **word_devices** (list[str]) - A list of word unit device addresses to read (e.g., ["D1000", "D1010"]). * **dword_devices** (list[str]) - A list of dword unit device addresses to read (e.g., ["D1000", "D1012"]). * **Returns**: A tuple containing two lists: the first for word unit values and the second for dword unit values. * **Return type**: tuple[list[int], list[int]] ``` -------------------------------- ### Read Non-Consecutive Devices Source: https://context7.com/senrust/pymcprotocol/llms.txt Reads word and dword values from specific, non-consecutive memory locations. ```python import pymcprotocol pymc3e = pymcprotocol.Type3E() pymc3e.connect("192.168.1.2", 1025) # Read non-consecutive word devices D1000, D2000 and dword device D3000 word_values, dword_values = pymc3e.randomread( word_devices=["D1000", "D2000"], dword_devices=["D3000"] ) print(f"Word values: {word_values}") # [value at D1000, value at D2000] print(f"Dword values: {dword_values}") # [32-bit value at D3000-D3001] # Output: # Word values: [1234, 5678] # Dword values: [655362] # Read only word devices word_values, _ = pymc3e.randomread( word_devices=["D100", "D200", "D300"], dword_devices=[] ) pymc3e.close() ``` -------------------------------- ### Write Non-Consecutive Bit Devices Source: https://context7.com/senrust/pymcprotocol/llms.txt Writes bit values to specific, non-consecutive bit device locations. ```python import pymcprotocol pymc3e = pymcprotocol.Type3E() pymc3e.connect("192.168.1.2", 1025) # Write to non-consecutive bit devices pymc3e.randomwrite_bitunits( bit_devices=["X0", "X10", "Y20", "M100"], values=[1, 0, 1, 1] ) # Turn on specific outputs pymc3e.randomwrite_bitunits( bit_devices=["Y0", "Y5", "Y10"], values=[1, 1, 1] ) pymc3e.close() ``` -------------------------------- ### remote_latchclear Source: https://context7.com/senrust/pymcprotocol/llms.txt Clears all latch device values on the PLC. Requires the PLC to be in a stopped state. ```APIDOC ## remote_latchclear ### Description Clears all latch device values remotely. The PLC must be stopped before using this command. ### Method N/A (Library Method) ### Endpoint pymc3e.remote_latchclear() ### Request Example # Stop PLC first pymc3e.remote_stop() # Clear latch devices pymc3e.remote_latchclear() # Restart PLC pymc3e.remote_run(clear_mode=0) ``` -------------------------------- ### Echo Test Source: https://github.com/senrust/pymcprotocol/blob/main/docs/pymcprotocol.md Performs an echo test by sending data to the PLC and verifying the response. ```APIDOC ## POST /api/echo_test ### Description Performs an echo test by sending data to the PLC. The response data should match the sent data. ### Method POST ### Endpoint /api/echo_test ### Parameters #### Request Body - **echo_data** (str) - Required - The data to send to the PLC for the echo test. ### Request Example ```json { "echo_data": "Hello PLC" } ``` ### Response #### Success Response (200) - **answer_len** (int) - The length of the data received back from the PLC. - **answer_data** (str) - The data received back from the PLC. #### Response Example ```json { "answer_len": 9, "answer_data": "Hello PLC" } ``` ``` -------------------------------- ### Manage PLC Security Source: https://context7.com/senrust/pymcprotocol/llms.txt Unlocks or locks a PLC using a password, supporting standard and iQ-R series requirements. ```python import pymcprotocol pymc3e = pymcprotocol.Type3E() pymc3e.connect("192.168.1.2", 1025) # Unlock PLC with password (4 characters for Q/L/QnA/iQ-L series) pymc3e.remote_unlock(password="1234") # Perform operations while unlocked pymc3e.batchwrite_wordunits(headdevice="D100", values=[999]) # Lock PLC again pymc3e.remote_lock(password="1234") # For iQ-R series (6-32 character password) pymc3e_iqr = pymcprotocol.Type3E(plctype="iQ-R") pymc3e_iqr.connect("192.168.1.3", 1025) pymc3e_iqr.remote_unlock(password="mypassword123") # Interactive password input (hides password from source code) pymc3e.remote_unlock(request_input=True) # Prompts user for password pymc3e.close() ``` -------------------------------- ### remote_stop Source: https://context7.com/senrust/pymcprotocol/llms.txt Stops PLC program execution remotely. ```APIDOC ## remote_stop ### Description Stop PLC program execution remotely. ```