Dreamine.Communication.Serial 1.0.2
Dreamine.Communication.Serial 통신 기능과 관련 API를 제공합니다.
로딩중...
검색중...
일치하는것 없음
Dreamine.Communication.Serial.Ports.SerialPortTransport 클래스 참조sealed

더 자세히 ...

Dreamine.Communication.Serial.Ports.SerialPortTransport에 대한 상속 다이어그램 :
Dreamine.Communication.Serial.Ports.SerialPortTransport에 대한 협력 다이어그램:

Public 멤버 함수

 SerialPortTransport (SerialPortTransportOptions options)
 SerialPortTransport (SerialPortTransportOptions options, IMessageProtocolAdapter protocolAdapter, IMessageFrameCodec frameCodec)
Task ConnectAsync (CancellationToken cancellationToken=default)
async Task DisconnectAsync (CancellationToken cancellationToken=default)
async Task SendAsync (MessageEnvelope message, CancellationToken cancellationToken=default)
async ValueTask DisposeAsync ()

속성

ConnectionState State [get]
TransportKind Kind [get]

이벤트

EventHandler< MessageEnvelope >? MessageReceived

Private 멤버 함수

async Task ReceiveLoopAsync (CancellationToken cancellationToken)
void CleanupSerialPort ()
void SetState (ConnectionState state)

정적 Private 멤버 함수

static void ValidateOptions (SerialPortTransportOptions options)

Private 속성

readonly SerialPortTransportOptions _options
readonly IMessageProtocolAdapter _protocolAdapter
readonly IMessageFrameCodec _frameCodec
SerialPort? _serialPort
CancellationTokenSource? _receiveLoopCts
Task? _receiveLoopTask
int _state = (int)ConnectionState.Disconnected

상세한 설명

.NET T:System.IO.Ports.SerialPort와 구성 가능한 프레임·프로토콜 어댑터를 사용하는 RS-232 메시지 전송 계층입니다.

SerialPortTransport.cs 파일의 20 번째 라인에서 정의되었습니다.

생성자 & 소멸자 문서화

◆ SerialPortTransport() [1/2]

Dreamine.Communication.Serial.Ports.SerialPortTransport.SerialPortTransport ( SerialPortTransportOptions options)
inline

기본 Dreamine JSON 프로토콜과 길이 접두사 프레임으로 전송 계층을 초기화합니다.

매개변수
options시리얼 회선 및 버퍼 설정입니다.

SerialPortTransport.cs 파일의 103 번째 라인에서 정의되었습니다.

104 : this(
105 options,
106 new DreamineEnvelopeProtocolAdapter(),
107 new LengthPrefixedMessageFrameCodec())
108 {
109 }

◆ SerialPortTransport() [2/2]

Dreamine.Communication.Serial.Ports.SerialPortTransport.SerialPortTransport ( SerialPortTransportOptions options,
IMessageProtocolAdapter protocolAdapter,
IMessageFrameCodec frameCodec )
inline

시리얼 설정과 사용자 지정 프로토콜 및 프레임 코덱으로 전송 계층을 초기화합니다.

매개변수
options시리얼 회선 및 버퍼 설정입니다.
protocolAdapter메시지와 외부 페이로드를 변환할 어댑터입니다.
frameCodec스트림의 메시지 경계를 처리할 코덱입니다.
예외
ArgumentNullExceptionoptions , protocolAdapter 또는 frameCodecnull인 경우 발생합니다.

SerialPortTransport.cs 파일의 151 번째 라인에서 정의되었습니다.

155 {
156 _options = options ?? throw new ArgumentNullException(nameof(options));
157 _protocolAdapter = protocolAdapter ?? throw new ArgumentNullException(nameof(protocolAdapter));
158 _frameCodec = frameCodec ?? throw new ArgumentNullException(nameof(frameCodec));
159
160 ValidateOptions(_options);
161 }

다음을 참조함 : _frameCodec, _options, _protocolAdapter, ValidateOptions().

이 함수 내부에서 호출하는 함수들에 대한 그래프입니다.:

멤버 함수 문서화

◆ CleanupSerialPort()

void Dreamine.Communication.Serial.Ports.SerialPortTransport.CleanupSerialPort ( )
inlineprivate

예외를 외부로 전파하지 않고 시리얼 포트를 닫고 해제합니다.

SerialPortTransport.cs 파일의 533 번째 라인에서 정의되었습니다.

534 {
535 try
536 {
537 if (_serialPort is not null && _serialPort.IsOpen)
538 {
539 _serialPort.Close();
540 }
541 }
542 catch
543 {
544 }
545
546 try
547 {
548 _serialPort?.Dispose();
549 }
550 catch
551 {
552 }
553
554 _serialPort = null;
555 }

다음을 참조함 : _serialPort.

다음에 의해서 참조됨 : ReceiveLoopAsync(), SendAsync().

이 함수를 호출하는 함수들에 대한 그래프입니다.:

◆ ConnectAsync()

Task Dreamine.Communication.Serial.Ports.SerialPortTransport.ConnectAsync ( CancellationToken cancellationToken = default)
inline

시리얼 포트를 열고 백그라운드 수신 루프를 시작합니다. 포트 열기 자체는 동기 API입니다.

매개변수
cancellationToken포트를 열기 전 취소 요청을 확인하는 토큰입니다.
반환값
연결 시작 작업입니다.

SerialPortTransport.cs 파일의 217 번째 라인에서 정의되었습니다.

218 {
219 if (State is ConnectionState.Connected or ConnectionState.Connecting)
220 {
221 return Task.CompletedTask;
222 }
223
224 cancellationToken.ThrowIfCancellationRequested();
225
226 SetState(ConnectionState.Connecting);
227
228 try
229 {
230 _serialPort = new SerialPort(
231 _options.PortName,
232 _options.BaudRate,
233 _options.Parity,
234 _options.DataBits,
235 _options.StopBits)
236 {
237 Handshake = _options.Handshake,
238 ReadTimeout = _options.ReadTimeoutMs,
239 WriteTimeout = _options.WriteTimeoutMs,
240 ReadBufferSize = _options.ReadBufferSize,
241 WriteBufferSize = _options.WriteBufferSize
242 };
243
244 _serialPort.Open();
245
246 SetState(ConnectionState.Connected);
247
248 _receiveLoopCts = new CancellationTokenSource();
249 _receiveLoopTask = Task.Run(
250 () => ReceiveLoopAsync(_receiveLoopCts.Token),
251 _receiveLoopCts.Token);
252
253 return Task.CompletedTask;
254 }
255 catch
256 {
257 SetState(ConnectionState.Faulted);
258
259 _serialPort?.Dispose();
260 _serialPort = null;
261
262 throw;
263 }
264 }

다음을 참조함 : _options, _receiveLoopCts, _receiveLoopTask, _serialPort, ReceiveLoopAsync(), SetState(), State.

이 함수 내부에서 호출하는 함수들에 대한 그래프입니다.:

◆ DisconnectAsync()

async Task Dreamine.Communication.Serial.Ports.SerialPortTransport.DisconnectAsync ( CancellationToken cancellationToken = default)
inline

수신 루프를 중지하고 시리얼 포트와 관련 리소스를 닫습니다.

매개변수
cancellationToken정리 후 연결 해제 취소 여부를 확인하는 토큰입니다.
반환값
비동기 연결 해제 작업입니다.

SerialPortTransport.cs 파일의 290 번째 라인에서 정의되었습니다.

291 {
292 if (State == ConnectionState.Disconnected)
293 {
294 return;
295 }
296
297 SetState(ConnectionState.Disconnecting);
298
299 _receiveLoopCts?.Cancel();
300
301 if (_serialPort is not null)
302 {
303 try
304 {
305 if (_serialPort.IsOpen)
306 {
307 _serialPort.Close();
308 }
309 }
310 finally
311 {
312 _serialPort.Dispose();
313 }
314 }
315
316 if (_receiveLoopTask is not null)
317 {
318 try
319 {
320 await _receiveLoopTask.ConfigureAwait(false);
321 }
322 catch (OperationCanceledException)
323 {
324 }
325 catch (ObjectDisposedException)
326 {
327 }
328 catch (InvalidOperationException)
329 {
330 }
331 catch (IOException)
332 {
333 }
334 }
335
336 _receiveLoopCts?.Dispose();
337 _receiveLoopCts = null;
338 _receiveLoopTask = null;
339 _serialPort = null;
340
341 cancellationToken.ThrowIfCancellationRequested();
342
343 SetState(ConnectionState.Disconnected);
344 }

다음을 참조함 : _receiveLoopCts, _receiveLoopTask, _serialPort, SetState(), State.

다음에 의해서 참조됨 : DisposeAsync().

이 함수 내부에서 호출하는 함수들에 대한 그래프입니다.:
이 함수를 호출하는 함수들에 대한 그래프입니다.:

◆ DisposeAsync()

async ValueTask Dreamine.Communication.Serial.Ports.SerialPortTransport.DisposeAsync ( )
inline

시리얼 포트 연결과 수신 루프 리소스를 비동기적으로 해제합니다.

반환값
비동기 리소스 해제 작업입니다.

SerialPortTransport.cs 파일의 434 번째 라인에서 정의되었습니다.

435 {
436 await DisconnectAsync().ConfigureAwait(false);
437 }

다음을 참조함 : DisconnectAsync().

이 함수 내부에서 호출하는 함수들에 대한 그래프입니다.:

◆ ReceiveLoopAsync()

async Task Dreamine.Communication.Serial.Ports.SerialPortTransport.ReceiveLoopAsync ( CancellationToken cancellationToken)
inlineprivate

시리얼 스트림에서 프레임을 계속 읽어 메시지로 디코딩하고 수신 이벤트를 발생시킵니다.

매개변수
cancellationToken수신 루프 종료를 요청하는 토큰입니다.
반환값
백그라운드 수신 루프 작업입니다.

SerialPortTransport.cs 파일의 463 번째 라인에서 정의되었습니다.

464 {
465 if (_serialPort is null)
466 {
467 return;
468 }
469
470 try
471 {
472 while (!cancellationToken.IsCancellationRequested &&
473 State == ConnectionState.Connected)
474 {
475 var payload = await _frameCodec.ReadFrameAsync(
476 _serialPort.BaseStream,
477 cancellationToken)
478 .ConfigureAwait(false);
479
480 if (payload is null)
481 {
482 break;
483 }
484
485 var message = _protocolAdapter.Decode(payload);
486 MessageReceived?.Invoke(this, message);
487 }
488
489 if (!cancellationToken.IsCancellationRequested &&
490 State == ConnectionState.Connected)
491 {
492 SetState(ConnectionState.Disconnected);
493 }
494 }
495 catch (OperationCanceledException)
496 {
497 }
498 catch (ObjectDisposedException)
499 {
500 }
501 catch (InvalidOperationException)
502 {
503 if (!cancellationToken.IsCancellationRequested)
504 {
505 SetState(ConnectionState.Faulted);
506 }
507 }
508 catch (IOException)
509 {
510 if (!cancellationToken.IsCancellationRequested)
511 {
512 SetState(ConnectionState.Faulted);
513 }
514 }
515 catch
516 {
517 if (!cancellationToken.IsCancellationRequested)
518 {
519 SetState(ConnectionState.Faulted);
520 CleanupSerialPort();
521 }
522 }
523 }

다음을 참조함 : _frameCodec, _protocolAdapter, _serialPort, CleanupSerialPort(), MessageReceived, SetState(), State.

다음에 의해서 참조됨 : ConnectAsync().

이 함수 내부에서 호출하는 함수들에 대한 그래프입니다.:
이 함수를 호출하는 함수들에 대한 그래프입니다.:

◆ SendAsync()

async Task Dreamine.Communication.Serial.Ports.SerialPortTransport.SendAsync ( MessageEnvelope message,
CancellationToken cancellationToken = default )
inline

메시지를 외부 프로토콜과 프레임 형식으로 인코딩해 시리얼 포트로 전송합니다.

매개변수
message전송할 메시지입니다.
cancellationToken프레임 쓰기 취소 요청을 감시하는 토큰입니다.
반환값
비동기 메시지 전송 작업입니다.
예외
InvalidOperationException시리얼 포트가 열려 있지 않거나 전송 계층이 연결 상태가 아닌 경우 발생합니다.

SerialPortTransport.cs 파일의 386 번째 라인에서 정의되었습니다.

389 {
390 ArgumentNullException.ThrowIfNull(message);
391
392 if (_serialPort is null ||
393 !_serialPort.IsOpen ||
394 State != ConnectionState.Connected)
395 {
396 throw new InvalidOperationException("Serial port is not connected.");
397 }
398
399 try
400 {
401 var payload = _protocolAdapter.Encode(message);
402
403 await _frameCodec.WriteFrameAsync(
404 _serialPort.BaseStream,
405 payload,
406 cancellationToken)
407 .ConfigureAwait(false);
408 }
409 catch
410 {
411 SetState(ConnectionState.Faulted);
412 CleanupSerialPort();
413
414 throw;
415 }
416 }

다음을 참조함 : _frameCodec, _protocolAdapter, _serialPort, CleanupSerialPort(), SetState(), State.

이 함수 내부에서 호출하는 함수들에 대한 그래프입니다.:

◆ SetState()

void Dreamine.Communication.Serial.Ports.SerialPortTransport.SetState ( ConnectionState state)
inlineprivate

원자적 연산으로 현재 연결 상태를 설정합니다.

매개변수
state저장할 새 연결 상태입니다.

SerialPortTransport.cs 파일의 573 번째 라인에서 정의되었습니다.

574 {
575 Interlocked.Exchange(ref _state, (int)state);
576 }

다음을 참조함 : _state.

다음에 의해서 참조됨 : ConnectAsync(), DisconnectAsync(), ReceiveLoopAsync(), SendAsync().

이 함수를 호출하는 함수들에 대한 그래프입니다.:

◆ ValidateOptions()

void Dreamine.Communication.Serial.Ports.SerialPortTransport.ValidateOptions ( SerialPortTransportOptions options)
inlinestaticprivate

포트 이름, 회선 속성, 제한 시간 및 버퍼 크기의 유효성을 검사합니다.

매개변수
options검증할 시리얼 포트 설정입니다.
예외
ArgumentException포트 이름이 비어 있는 경우 발생합니다.
ArgumentOutOfRangeException수치 설정이 허용 범위를 벗어난 경우 발생합니다.

SerialPortTransport.cs 파일의 610 번째 라인에서 정의되었습니다.

611 {
612 ArgumentException.ThrowIfNullOrWhiteSpace(options.PortName);
613
614 if (options.BaudRate <= 0)
615 {
616 throw new ArgumentOutOfRangeException(nameof(options.BaudRate));
617 }
618
619 if (options.DataBits is < 5 or > 8)
620 {
621 throw new ArgumentOutOfRangeException(nameof(options.DataBits));
622 }
623
624 if (options.ReadTimeoutMs < System.IO.Ports.SerialPort.InfiniteTimeout)
625 {
626 throw new ArgumentOutOfRangeException(nameof(options.ReadTimeoutMs));
627 }
628
629 if (options.WriteTimeoutMs < System.IO.Ports.SerialPort.InfiniteTimeout)
630 {
631 throw new ArgumentOutOfRangeException(nameof(options.WriteTimeoutMs));
632 }
633
634 if (options.ReadBufferSize <= 0)
635 {
636 throw new ArgumentOutOfRangeException(nameof(options.ReadBufferSize));
637 }
638
639 if (options.WriteBufferSize <= 0)
640 {
641 throw new ArgumentOutOfRangeException(nameof(options.WriteBufferSize));
642 }
643 }

다음을 참조함 : Dreamine.Communication.Serial.Options.SerialPortTransportOptions.BaudRate, Dreamine.Communication.Serial.Options.SerialPortTransportOptions.DataBits, Dreamine.Communication.Serial.Options.SerialPortTransportOptions.PortName, Dreamine.Communication.Serial.Options.SerialPortTransportOptions.ReadBufferSize, Dreamine.Communication.Serial.Options.SerialPortTransportOptions.ReadTimeoutMs, Dreamine.Communication.Serial.Options.SerialPortTransportOptions.WriteBufferSize, Dreamine.Communication.Serial.Options.SerialPortTransportOptions.WriteTimeoutMs.

다음에 의해서 참조됨 : SerialPortTransport().

이 함수를 호출하는 함수들에 대한 그래프입니다.:

멤버 데이터 문서화

◆ _frameCodec

readonly IMessageFrameCodec Dreamine.Communication.Serial.Ports.SerialPortTransport._frameCodec
private

frame Codec 값을 보관합니다.

SerialPortTransport.cs 파일의 48 번째 라인에서 정의되었습니다.

다음에 의해서 참조됨 : ReceiveLoopAsync(), SendAsync(), SerialPortTransport().

◆ _options

readonly SerialPortTransportOptions Dreamine.Communication.Serial.Ports.SerialPortTransport._options
private

options 값을 보관합니다.

SerialPortTransport.cs 파일의 30 번째 라인에서 정의되었습니다.

다음에 의해서 참조됨 : ConnectAsync(), SerialPortTransport().

◆ _protocolAdapter

readonly IMessageProtocolAdapter Dreamine.Communication.Serial.Ports.SerialPortTransport._protocolAdapter
private

protocol Adapter 값을 보관합니다.

SerialPortTransport.cs 파일의 39 번째 라인에서 정의되었습니다.

다음에 의해서 참조됨 : ReceiveLoopAsync(), SendAsync(), SerialPortTransport().

◆ _receiveLoopCts

CancellationTokenSource? Dreamine.Communication.Serial.Ports.SerialPortTransport._receiveLoopCts
private

receive Loop Cts 값을 보관합니다.

SerialPortTransport.cs 파일의 67 번째 라인에서 정의되었습니다.

다음에 의해서 참조됨 : ConnectAsync(), DisconnectAsync().

◆ _receiveLoopTask

Task? Dreamine.Communication.Serial.Ports.SerialPortTransport._receiveLoopTask
private

receive Loop Task 값을 보관합니다.

SerialPortTransport.cs 파일의 76 번째 라인에서 정의되었습니다.

다음에 의해서 참조됨 : ConnectAsync(), DisconnectAsync().

◆ _serialPort

SerialPort? Dreamine.Communication.Serial.Ports.SerialPortTransport._serialPort
private

serial Port 값을 보관합니다.

SerialPortTransport.cs 파일의 58 번째 라인에서 정의되었습니다.

다음에 의해서 참조됨 : CleanupSerialPort(), ConnectAsync(), DisconnectAsync(), ReceiveLoopAsync(), SendAsync().

◆ _state

int Dreamine.Communication.Serial.Ports.SerialPortTransport._state = (int)ConnectionState.Disconnected
private

state 값을 보관합니다.

SerialPortTransport.cs 파일의 85 번째 라인에서 정의되었습니다.

다음에 의해서 참조됨 : SetState().

속성 문서화

◆ Kind

TransportKind Dreamine.Communication.Serial.Ports.SerialPortTransport.Kind
get

시리얼 전송 방식을 가져옵니다.

SerialPortTransport.cs 파일의 181 번째 라인에서 정의되었습니다.

◆ State

ConnectionState Dreamine.Communication.Serial.Ports.SerialPortTransport.State
get

스레드 안전하게 현재 시리얼 연결 상태를 가져옵니다.

SerialPortTransport.cs 파일의 171 번째 라인에서 정의되었습니다.

다음에 의해서 참조됨 : ConnectAsync(), DisconnectAsync(), ReceiveLoopAsync(), SendAsync().

이벤트 문서화

◆ MessageReceived

EventHandler<MessageEnvelope>? Dreamine.Communication.Serial.Ports.SerialPortTransport.MessageReceived

완전한 프레임을 메시지로 디코딩했을 때 발생합니다.

SerialPortTransport.cs 파일의 191 번째 라인에서 정의되었습니다.

다음에 의해서 참조됨 : ReceiveLoopAsync().


이 클래스에 대한 문서화 페이지는 다음의 파일로부터 생성되었습니다.: