5.9 KiB
5.9 KiB
source_files, generated_at, model, schema_version, sha256
| source_files | generated_at | model | schema_version | sha256 | |
|---|---|---|---|---|---|
|
2026-04-16T11:48:46.182914+00:00 | zai-org/GLM-5-FP8 | 1 | 4992256eaf51da73 |
Documentation: DTS.Common.SerialConnection.SerialConnection
1. Purpose
The SerialConnection class provides a wrapper around System.IO.Ports.SerialPort to implement the IConnection interface. Its primary role is to abstract serial port communication behind an asynchronous, socket-like API (APM pattern). This allows serial devices to be handled uniformly within a system designed around generic connection interfaces, mimicking the behavior of network sockets for operations like connecting, sending, and receiving data.
2. Public Interface
Class: SerialConnection (implements IConnection)
Properties:
bool Connected { get; }: Returns the connection state. Note: Currently hardcoded to returnfalse.bool IsSoftDisconnected { get; }: Returnstrueif the unit is soft disconnected.string ConnectString { get; }: Returns the stored port name (_PortName).System.Net.Sockets.SocketFlags Flags { get; set; }: Gets or sets socket flags.
Events:
EventHandler OnDisconnected: Event raised upon disconnection.
Methods:
void Create(string PortName): Instantiates the internalSerialPortobject.void Create(string connectString, string hostIPAddress): Overload that currently performs no operation.void SoftConnect(): Intended to connect the unit; currently empty.void SoftDisconnect(): Intended to disconnect the unit; currently empty.string GetConnectionData(): Returns an empty string.double GetCurrentDownloadRate(): Returns0.0.double GetCurrentUploadRate(): Returns0.0.
Connection Lifecycle (APM Pattern):
IAsyncResult BeginConnect(AsyncCallback cb, object state): Opens the serial port. ThrowsExceptionifPortis null or_PortNameis empty. Returnsnull.void EndConnect(IAsyncResult ar): ValidatesPortexists. ThrowsExceptionif null.
Data Transfer (APM Pattern):
IAsyncResult BeginSend(byte[] buffer, int offset, int size, AsyncCallback cb, object state): Begins an asynchronous write to the serial port. ThrowsExceptionifPortorcbis null.int EndSend(IAsyncResult ar): Completes the asynchronous write. ReturnsPort.BytesToWrite.Task<int> SendAsync(byte[] sendBuffer, int bufferStartOffset, int bufferSizeToSend): Task-based async wrapper forBeginSend/EndSend.IAsyncResult BeginReceive(byte[] buffer, int offset, int size, AsyncCallback cb, object state): Begins an asynchronous read from the serial port. ThrowsExceptionifPortorcbis null.int EndReceive(IAsyncResult ar): Completes the asynchronous read. Returns the number of bytes read.
Server/Socket Mimicry Methods:
IAsyncResult BeginAccept(AsyncCallback callback, Object state): Returnsnull. Throws ifPortis null.IConnection EndAccept(IAsyncResult asyncResult): Returns a new instance ofSerialConnection. Throws ifPortis null.void Bind(int port): No-op. Throws ifPortis null.void Listen(int backlog): No-op. Throws ifPortis null.IAsyncResult BeginDisconnect(bool reuseSocket, AsyncCallback cb, Object state): Returnsnull. Throws ifPortis null.void EndDisconnect(IAsyncResult asyncResult): No-op. Throws ifPortis null.
IDisposable:
void Dispose(): Disposes theSerialPortand suppresses finalization.
3. Invariants
- Null Checks: Nearly all public methods (except
Createoverloads and disposal methods) throw aSystem.Exceptionif the protectedPortfield is null. - Callback Validation:
BeginSendandBeginReceivethrow aSystem.Exceptionif the providedAsyncCallbackis null. - Connection String:
BeginConnectthrows aSystem.Exceptionif_PortNameis null or empty. - Disposal: Once
Dispose(bool disposing)runs, thePortis closed and set to null; subsequent calls return immediately.
4. Dependencies
- Dependencies (Imports):
DTS.Common.Interface.Connection: Defines theIConnectioninterface this class implements.System.IO.Ports: Provides the underlyingSerialPortfunctionality.System: Basic system types, exceptions, and async patterns.System.Threading.Tasks: Used forTask-based async operations (SendAsync).
- Dependents: Unknown from source alone (consumers of
IConnection).
5. Gotchas
- Critical Bug in
Create(string PortName): The method assigns_PortNameafter attempting to initializePort.This results in thePort = new SerialPort(_PortName); // _PortName is likely null or stale here _PortName = PortName; // Assigned too lateSerialPortbeing instantiated with an incorrect or empty port name, likely causing connection failures. - Hardcoded
ConnectedProperty: TheConnectedproperty is hardcoded to returnfalseand is never updated byBeginConnectorEndConnect. Code relying on this property to verify connectivity will fail. - APM Return Values:
BeginConnect,BeginDisconnect, andBeginAcceptreturnnullinstead of a validIAsyncResult. This may break callers expecting a valid state object from standard APM implementations. EndSendReturn Value:EndSendreturnsPort.BytesToWrite(the number of bytes waiting in the send buffer), not the number of bytes successfully written (which is the standard behavior for socketEndSend).- Soft Disconnect Implementation:
SoftConnectandSoftDisconnectare empty methods. TheIsSoftDisconnectedproperty is initialized tofalsebut is never set by any method in the class. - No-op Methods:
Bind,Listen, andKeepAliveErrorReceivedperform no actions but throw exceptions ifPortis null, making them effectively validation checks with no side effects.