CFHEADER 오류 처리

CFHEADER 통신 오류는 크게 두 가지로 구분됩니다. 하나는 USB 통신 오류,
다른 하나는 CFNET IO 모듈 간의 I²C통신 오류입니다. 이 문서에서는 이 두 가지 오류가 발생했을 때의 처리 방법에 대해 설명합니다.

USB 통신 오류 처리

Cfheader.Open() 또는 Cfheader.Sync() 메서드에서 발생할 수 있는 USB 통신 오류를 처리하려면, 아래의 코드와 같이 try…catch 블록으로 해당 코드를 감싸고, 오류가 발생했을 때 catch 블록에서 예외를 처리하면 됩니다.

using ComfileTech.Cfnet.Cfheader;
 
// Open USB communication with the CFHEADER module
try
{
    cfheader0.Open();
}
catch (Exception ex)
{
    // Handle any USB communication errors that occur here.
}
 
// Capture any errors that occur when calling `Sync` in the background thread.
cfheader0.BackgroundSync.ExceptionThrown += (bs, ex) =>
{
    Console.Error.WriteLine($"Exception was through from `BackgroundSync`: {ex.Message}");
}
 
// Start the background sync thread
cfheader0.BackgroundSync.Start();
 
while (true)
{
    Thread.Yield();
}

Sync() 호출 중 오류가 발생한 경우, 문제를 복구하려면 CFHEADER 모듈을 Close() 한 후 다시 Open() 해야 합니다.

BackgroundSync에서 USB 통신 오류 처리

BackgroundSync를 사용할 경우, 백그라운드 스레드가 Cfheader.Sync()를 호출하는 동안 발생할 수 있는 오류를 처리하려면 BackgroundSync.ExceptionThrown 이벤트에서 확인하세요.

using ComfileTech.Cfnet.Cfheader;
 
// Open USB communication with the CFHEADER module
try
{
    cfheader0.Open();
}
catch (Exception ex)
{
    // Handle any USB communication errors that occur here.
}
 
// Capture any errors that occur when calling `Sync` in the background thread.
cfheader0.BackgroundSync.ExceptionThrown += (bs, ex) =>
{
    Console.Error.WriteLine($"Exception was through from `BackgroundSync`: {ex.Message}");
}
 
// Start the background sync thread
cfheader0.BackgroundSync.Start();
 
while (true)
{
    Thread.Yield();
}

오류가 발생한 경우, 문제를 복구하려면 CFHEADER 모듈을 Close() 한 후 다시 Open() 해야 합니다.

I²C 통신 오류 처리

CFHEADER 모듈이 CFNET IO 모듈과 I²C를 통해 통신을 시도하는 중 오류가 발생하면, 해당 오류는 Sync() 메서드의 반환값을 통해 감지되며, IIOModule.I2cStatus 속성에 기록됩니다.

이 오류가 발생한 이후에는, IIOModule.AcknowledgeI2cFailure() 메서드가 호출되어 오류가 확인되기 전까지 CFHEADER 모듈은 CFNET IO 모듈과의 통신을 중단합니다. 오류를 확인한 후에는, 다음 Sync() 호출 시 CFHEADER 모듈이 CFNET IO 모듈과의 통신을 다시 시도하게 됩니다.

while (true)
{
    // Write the updated state of the output modules to the CFHEADER module, and
    // read the updated state of the input modules from the CFHEADER module
    cfheader0.Sync();
 
    if (do0.I2cStatus != I2cStatus.Success)
    {
        // Handle the I²C communication failure
        Console.Error.WriteLine($"I²C communication with the digital output module at address {do0.Address} failed.");
 
        // Acknowledge the I²C communication failure
        do0.AcknowledgeI2cFailure();
    }
}

BackgroundSync에서 I²C 통신 오류 처리

BackgroundSync를 사용하는 경우, 해당 모듈과의 I²C 통신 중 발생하는 오류를 감지하려면 IO 모듈의 I2cFailed 이벤트를 확인 합니다.

do0.I2cFailed += module =>
{
    Console.WriteLine($"I²C communication with digital output module at address {module.Address} failed.");
}

이전 페이지로 이동