GXSerial is an open-source, high-performance media communication library developed by Gurux Ltd. It provides developers with a lightweight, multi-threaded interface to manage serial port communication without relying on heavy external dependencies. It is widely utilized in industrial and automation fields, particularly for establishing communication with DLMS/COSEM smart meters, sensors, and PLCs. Prerequisites & Installation
GXSerial is highly cross-platform, featuring official support for C#, Python, Java, and Android.
To install the library for Python, run the following command in your terminal: pip install gurux-serial Use code with caution.
(For C# or Java implementations, you can download the respective packages via NuGet or the Gurux GitHub Repositories). Step 1: Initialize Required Parameters
Before opening a connection, you must define five core serial communication properties to match your target physical device:
PortName: The physical identifier of your connection (e.g., “COM1” on Windows or ”/dev/ttyUSB0” on Linux). BaudRate: Transmission speed, typically set to 9600. DataBits: Length of data packets, usually set to 8.
Parity: Error-checking parameter (e.g., None, Even, or Odd). StopBits: Signals the end of a byte, usually set to 1. Step 2: Establish the Media Listeners
GXSerial operates asynchronously by default. To handle background traffic smoothly, you must attach an event listener to intercept critical behaviors:
# Essential event listener setup (Python example format) class GXMediaListener: def onError(self, sender, ex): # Always monitor this; traps post-connection failures print(f”Error encountered: {ex}“) def onReceived(self, sender, e): # Triggers automatically whenever incoming data arrives print(f”Data received: {e.Data}“) def onMediaStateChange(self, sender, e): # Tracks connection state changes (Open/Closed) print(“Connection status changed.”) Use code with caution.
Note: Listening to the onError event is mandatory for managing mid-session line drops safely. Step 3: Connect and Transmit Data
Once configured, opening the port and sending messages can be achieved synchronously or asynchronously in just a few lines of code. C# Implementation Example:
using Gurux.Serial; GXSerial serialClient = new GXSerial(); // Configuration serialClient.PortName = “COM1”; serialClient.BaudRate = 9600; serialClient.DataBits = 8; serialClient.Parity = System.IO.Ports.Parity.None; serialClient.StopBits = System.IO.Ports.StopBits.One; // Establish Connection serialClient.Open(); // Transmit Data serialClient.Send(“Hello World!”); Use code with caution. Multi-Threaded Locking (For Shared Connections):
If your application uses multiple threads to talk to the same serial device, wrap your synchronous payloads using the component’s internal sync engine to prevent collision data corruption:
lock(serialClient.SyncRoot) { lock (serialClient.Synchronous) { serialClient.Send(“Secure Command Payload”, null); } } Use code with caution. Step 4: Closing the Connection
To release system resources and prevent “invalid handle” or port blocking bugs during your next session, always close your port safely when execution finishes: serialClient.Close(); Use code with caution. Gurux Serial port component for C# · GitHub
Leave a Reply