### Install HslCommunication Library Source: https://context7.com/dathlin/hslcommunicationpython/llms.txt Installs the HslCommunication library using pip. For GUI demos, PyQt5 and PyQt5-tools are also required. ```bash pip install HslCommunication pip install PyQt5 pip install PyQt5-tools ``` -------------------------------- ### Configure Modbus TCP Connection Source: https://context7.com/dathlin/hslcommunicationpython/llms.txt Shows how to initialize and configure a Modbus TCP client connection. This includes setting the IP address, port, station number, and configuring address starting from zero and byte order for multi-byte data. ```python import HslCommunication from HslCommunication import ModbusTcpNet # Create Modbus TCP connection modbus = ModbusTcpNet('192.168.8.20', 502, 1) # IP, port, station number # Configure addressing modbus.isAddressStartWithZero = True # Address starts from 0 # Configure byte order for multi-byte values modbus.byteTransform.DataFormat = HslCommunication.DataFormat.ABCD # Options: ABCD, BADC, CDAB, DCBA ``` -------------------------------- ### Siemens S7 Protocol Communication Setup Source: https://context7.com/dathlin/hslcommunicationpython/llms.txt Initializes a connection object for Siemens S7 PLCs, supporting various models like S7-1200, S7-1500, S7-300, and S7-400. Demonstrates setting the PLC type, IP address, port, and configuring rack and slot for communication. Uses the SiemensS7Net class. ```python from HslCommunication import SiemensS7Net, SiemensPLCS # Create connection for S7-1200 PLC siemens = SiemensS7Net(SiemensPLCS.S1200, '192.168.8.12') siemens.port = 102 # Default S7 port # Configure rack and slot (for S7-300/400/1200/1500) siemens.SetSlotAndRack(0, 0) # Further operations like reading/writing would follow here... ``` -------------------------------- ### Mitsubishi A-1E Protocol Communication Source: https://context7.com/dathlin/hslcommunicationpython/llms.txt Facilitates communication with older Mitsubishi A-series and Q-series PLCs using the 1E (A-compatible) protocol. This example shows basic connection and reading operations, similar to MC protocol. Uses the MelsecA1ENet class. ```python from HslCommunication import MelsecA1ENet # Create A-1E protocol connection melsec_a1e = MelsecA1ENet('192.168.8.14', 6000) # Connect to PLC connect = melsec_a1e.ConnectServer() if connect.IsSuccess: print('Connected using A-1E Binary protocol') # Read and write operations work the same as MC protocol result = melsec_a1e.ReadInt16('D100') if result.IsSuccess: print(f'Value: {result.Content}') melsec_a1e.ConnectClose() else: print(f'Connection failed: {connect.ToMessageShowString()}') ``` -------------------------------- ### Connect and Communicate with Siemens PLC (Fetch/Write Protocol) Source: https://context7.com/dathlin/hslcommunicationpython/llms.txt Demonstrates connecting to a Siemens PLC using the Fetch/Write protocol, reading an Int16 value from the M area, and writing an Int32 value to the M area. Includes connection status checking and closing the connection. ```python from HslCommunication import SiemensFetchWriteNet # Create Fetch/Write connection siemens_fw = SiemensFetchWriteNet('192.168.8.12', 2000) # Connect to PLC connect = siemens_fw.ConnectServer() if connect.IsSuccess: print('Connected using Fetch/Write protocol') # Read from M area result = siemens_fw.ReadInt16('M100') if result.IsSuccess: print(f'M100 value: {result.Content}') # Write to M area result = siemens_fw.WriteInt32('M200', 12345678) if result.IsSuccess: print('Write successful') siemens_fw.ConnectClose() else: print(f'Connection failed: {connect.ToMessageShowString()}') ``` -------------------------------- ### NetSimplifyClient for HSL Server Communication Source: https://context7.com/dathlin/hslcommunicationpython/llms.txt Provides a simplified client implementation for communicating with HSL-based server applications, including optional authentication and data retrieval. ```python client = NetSimplifyClient('118.24.36.220', 18467) if client.ConnectServer().IsSuccess: result = client.ReadFromServer(600, '1.2.0') client.ConnectClose() ``` -------------------------------- ### Connect and Communicate with OMRON PLC (FINS TCP Protocol) Source: https://context7.com/dathlin/hslcommunicationpython/llms.txt Demonstrates connecting to an OMRON PLC using the FINS TCP protocol, reading and writing Int16 and Float values from D and W memory areas, and reading multiple Int16 values. Includes setting the PLC unit number and displaying source/destination addresses. ```python from HslCommunication import OmronFinsNet # Create FINS TCP connection omron = OmronFinsNet('192.168.8.10', 9600) omron.DA2 = 0 # PLC unit number # Connect to PLC connect = omron.ConnectServer() if connect.IsSuccess: print('Connected to OMRON PLC') print(f'SA1 (Source Address): {omron.SA1}') print(f'DA1 (Destination Address): {omron.DA1}') # Read from D (Data Memory) area result = omron.ReadInt16('D100') if result.IsSuccess: print(f'D100 value: {result.Content}') # Read multiple values result = omron.ReadInt16('D100', 10) if result.IsSuccess: print(f'D100-D109 values: {result.Content}') # Read from W (Work) area result = omron.ReadFloat('W100') if result.IsSuccess: print(f'W100 float: {result.Content}') # Write to D area result = omron.WriteInt16('D200', 5678) if result.IsSuccess: print('Write successful') omron.ConnectClose() else: print(f'Connection failed: {connect.ToMessageShowString()}') ``` -------------------------------- ### SoftBasic Utility Functions Source: https://context7.com/dathlin/hslcommunicationpython/llms.txt Demonstrates common utility functions for byte manipulation, such as converting byte arrays to hex strings and formatting arrays for display. ```python byte_data = bytes([0x01, 0x02, 0x03, 0x04]) hex_string = SoftBasic.ByteToHexString(byte_data) byte_array = SoftBasic.HexStringToBytes("01 02 03 04") formatted = SoftBasic.ArrayFormat([100, 200, 300]) ``` -------------------------------- ### Connect and Communicate with Siemens PLC (S7 Protocol) Source: https://context7.com/dathlin/hslcommunicationpython/llms.txt Demonstrates connecting to a Siemens PLC using the S7 protocol, reading and writing various data types (Int16, Float, Bool) from different memory areas (M, DB, I, Q), and reading the PLC order number. Includes handling connection success and failure. ```python from HslCommunication import SiemensPLCS, SiemensS7Net siemens = SiemensS7Net(SiemensPLCS.S200, '192.168.8.12') connect = siemens.ConnectServer() if connect.IsSuccess: print('Connected to Siemens PLC') # Read from M (Merker) area result = siemens.ReadInt16('M100') if result.IsSuccess: print(f'M100 value: {result.Content}') # Read from DB (Data Block) area result = siemens.ReadFloat('DB1.DBD0') if result.IsSuccess: print(f'DB1.DBD0 float: {result.Content}') # Read boolean from I (Input) area result = siemens.ReadBool('I0.0') if result.IsSuccess: print(f'I0.0 state: {result.Content}') # Write to Q (Output) area result = siemens.WriteBool('Q0.0', True) if result.IsSuccess: print('Output write successful') # Read PLC order number order_result = siemens.ReadOrderNumber() if order_result.IsSuccess: print(f'PLC Order Number: {order_result.Content}') siemens.ConnectClose() else: print(f'Connection failed: {connect.ToMessageShowString()}') # For S7-200 (uses TSAP addressing) siemens_200 = SiemensS7Net(SiemensPLCS.S200, '192.168.8.12') siemens_200.SetTsap(0x4D57, 0x4D57) # Local and remote TSAP ``` -------------------------------- ### Configure Language in HslCommunication (Python) Source: https://context7.com/dathlin/hslcommunicationpython/llms.txt Demonstrates how to set the language for error messages and system strings in HslCommunication. It shows how to switch between default Chinese and English. ```python import HslCommunication # Set to Chinese (default) HslCommunication.StringResources.Language = HslCommunication.DefaultLanguage() # Set to English HslCommunication.StringResources.Language = HslCommunication.English() ``` -------------------------------- ### Network Simplify Client Source: https://context7.com/dathlin/hslcommunicationpython/llms.txt Simplified client-server communication for HSL-based applications. ```APIDOC ## NETSIMPLIFYCLIENT OPERATIONS ### Description Connect to an HSL server to exchange data using a simplified protocol. ### Method N/A ### Endpoint N/A ### Parameters - **protocol** (int) - Required - The protocol identifier. - **data** (string) - Required - The payload to send. ### Request Example client.ReadFromServer(600, '1.2.0') ### Response #### Success Response (200) - **Content** (string) - The response string from the server. ``` -------------------------------- ### Using OperateResult for PLC Operations (Python) Source: https://context7.com/dathlin/hslcommunicationpython/llms.txt Illustrates the common OperateResult pattern used in HslCommunication for PLC communication. It shows how to connect to a MelsecMcNet PLC, read data, and handle success or failure responses. ```python from HslCommunication import MelsecMcNet, OperateResult melsec = MelsecMcNet('192.168.8.14', 6000) connect = melsec.ConnectServer() if connect.IsSuccess: # Read operation returns OperateResult result = melsec.ReadInt16('D100') # Check if operation was successful if result.IsSuccess: # Access the content value = result.Content print(f'Read value: {value}') else: # Get error message error_msg = result.ToMessageShowString() print(f'Read failed: {error_msg}') melsec.ConnectClose() ``` -------------------------------- ### Allen-Bradley PLC Communication via CIP Source: https://context7.com/dathlin/hslcommunicationpython/llms.txt Shows how to interface with Allen-Bradley PLCs using the EtherNet/IP protocol. It includes reading tags by name, accessing array elements, and writing values to tags. ```python ab_plc = AllenBradleyNet('192.168.8.30', 44818) ab_plc.Slot = 0 if ab_plc.ConnectServer().IsSuccess: result = ab_plc.ReadInt16('TagName') ab_plc.WriteInt16('OutputTag', 100) ab_plc.ConnectClose() ``` -------------------------------- ### Mitsubishi Melsec MC Binary Protocol Communication Source: https://context7.com/dathlin/hslcommunicationpython/llms.txt Establishes a connection to a Mitsubishi PLC using the MC 3E Binary protocol. It demonstrates reading and writing Int16, boolean values, and raw bytes, along with connection management. Requires the MelsecMcNet class. ```python from HslCommunication import MelsecMcNet, SoftBasic # Create connection to Mitsubishi PLC melsec = MelsecMcNet('192.168.8.14', 6000) melsec.receiveTimeOut = 5000 # Set timeout to 5 seconds # Connect to PLC connect = melsec.ConnectServer() if connect.IsSuccess: print('Connected to Mitsubishi PLC') # Read a single Int16 value from D100 result = melsec.ReadInt16('D100') if result.IsSuccess: print(f'D100 value: {result.Content}') # Read multiple Int16 values (10 registers starting from D100) result = melsec.ReadInt16('D100', 10) if result.IsSuccess: print(f'Values: {SoftBasic.ArrayFormat(result.Content)}') # Read boolean from M100 result = melsec.ReadBool('M100') if result.IsSuccess: print(f'M100 state: {result.Content}') # Write Int16 value to D200 result = melsec.WriteInt16('D200', 1234) if result.IsSuccess: print('Write successful') # Write boolean to M200 result = melsec.WriteBool('M200', True) if result.IsSuccess: print('Boolean write successful') # Bulk read raw bytes result = melsec.Read('D100', 10) if result.IsSuccess: print(f'Raw bytes: {SoftBasic.ByteToHexString(result.Content)}') # Close connection melsec.ConnectClose() else: print(f'Connection failed: {connect.ToMessageShowString()}') ``` -------------------------------- ### Modbus TCP and RTU Communication Source: https://context7.com/dathlin/hslcommunicationpython/llms.txt Demonstrates how to connect to Modbus devices using TCP or RTU over TCP protocols. It covers reading/writing registers, coils, and float values with success checking. ```python connect = modbus.ConnectServer() if connect.IsSuccess: result = modbus.ReadInt16('100') if result.IsSuccess: print(f'Register 100 value: {result.Content}') modbus.ConnectClose() ``` ```python modbus_rtu = ModbusRtuOverTcp('192.168.8.20', 502, 1) modbus_rtu.isAddressStartWithZero = True connect = modbus_rtu.ConnectServer() if connect.IsSuccess: result = modbus_rtu.ReadInt16('100') modbus_rtu.ConnectClose() ``` -------------------------------- ### Allen-Bradley CIP/EIP Protocol Source: https://context7.com/dathlin/hslcommunicationpython/llms.txt Communication methods for Allen-Bradley/Rockwell PLCs using the CIP protocol. ```APIDOC ## ALLEN-BRADLEY CIP OPERATIONS ### Description Interact with CompactLogix and ControlLogix PLCs by reading/writing tags by name. ### Method N/A ### Endpoint N/A ### Parameters - **tagName** (string) - Required - The name of the PLC tag. - **value** (any) - Required for Write operations - The value to assign to the tag. ### Request Example ab_plc.ReadInt16('TagName') ### Response #### Success Response (200) - **Content** (any) - The value of the requested tag. #### Response Example { "IsSuccess": true, "Content": 100 } ``` -------------------------------- ### Mitsubishi Melsec MC ASCII Protocol Communication Source: https://context7.com/dathlin/hslcommunicationpython/llms.txt Connects to a Mitsubishi PLC using the MC ASCII protocol, suitable for environments where binary mode is unavailable. Demonstrates reading and writing float and 32-bit integer values, and managing the connection. Uses the MelsecMcAsciiNet class. ```python from HslCommunication import MelsecMcAsciiNet # Create ASCII mode connection melsec_ascii = MelsecMcAsciiNet('192.168.8.14', 6000) # Connect to PLC connect = melsec_ascii.ConnectServer() if connect.IsSuccess: print('Connected using MC ASCII protocol') # Read float value from D100 result = melsec_ascii.ReadFloat('D100') if result.IsSuccess: print(f'Float value: {result.Content}') # Write float value result = melsec_ascii.WriteFloat('D100', 123.456) if result.IsSuccess: print('Float write successful') # Read 32-bit integer result = melsec_ascii.ReadInt32('D200') if result.IsSuccess: print(f'Int32 value: {result.Content}') melsec_ascii.ConnectClose() else: print(f'Connection failed: {connect.ToMessageShowString()}') ``` -------------------------------- ### Modbus TCP Communication Source: https://context7.com/dathlin/hslcommunicationpython/llms.txt Operations for reading and writing registers and coils using the Modbus TCP protocol. ```APIDOC ## MODBUS TCP OPERATIONS ### Description Allows reading/writing of holding registers, input registers, and coils on Modbus-enabled devices. ### Method N/A (Class-based library methods) ### Endpoint N/A (Socket-based communication) ### Parameters - **address** (string) - Required - The register or coil address (e.g., '100', 'x=4;100'). - **length** (int) - Optional - Number of registers to read. - **value** (any) - Required for Write operations - Data to be written. ### Request Example modbus.ReadInt16('100') ### Response #### Success Response (200) - **Content** (any) - The data returned from the device. #### Response Example { "IsSuccess": true, "Content": 1234 } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.