User Tools

Site Tools

한국어

cfnet:cfheader:mode_of_operation:index

CFHEADER Mode of Operation

The CFHEADER serves as a USB device interface between a host PC and the CFNET IO modules.

The host PC communicates with the CFHEADER module over USB while the CFHEADER module communicates with each CFNET IO module over I²C.

Normal Mode of Operation

  1. The program running on the host PC updates the state of the output modules by writing to properties such as Cfdo_16n.Channel.State.
  2. When the Sync() function is called:
    1. The host PC transmits the updated state of the CFNET output modules to the CFHEADER's memory over USB.
    2. The CFHEADER module writes the updated state to the CFNET output modules via I²C.
    3. The CFHEADER module reads the state of the CFNET input modules via I²C.
    4. The CFHEADER module transmits the updated state of the CFNET input modules to the host PC over USB.
    5. The host PC updates the state of the CFNET input modules in the host PC's memory by writing to properties such as Cfdi_16b.Channel.State.
  3. The program running on the host processes the updated state of the CFNET input modules read from the CFHEADER module.

That procedure can be visualized in source code form in the following program.

using Cfnet.Cfheader;
 
// Get CFHEADER module at address 0
var cfheader0 = Cfheader.Instances[0];
 
// Get digital output module at address 0
var cfdo_16n0 = cfheader0.Cfdo_16nModules[0];
 
// Get digital input module at address 0
var cfdi_16b0 = cfheader0.Cfdi_16bModules[0];
 
// Open USB communication with the CFHEADER module
cfheader0.Open();
 
while (true)
{
    // Update the state of the digital output modules
    foreach(var doChannel in cfdo_16n0.Channels)
    {
        doChannel.State = true;
    }
 
    // Transmit the updated state of the output modules to the CFHEADER module, and
    // read the updated input state of the input modules from the CFHEADER module
    cfheader0.Sync();
 
    // Process the new state of the digital input modules
    foreach(var diChannel in cfdi_16b0.Channels)
    {
        // Process the digital input channel's state
        Console.WriteLine(diChannel.State);
    }
}

Error Handling

To program a robust IO solution, all sources of errors must be handled. For the CFHEADER, there 2 primary sources for potential errors.

  1. USB communication errors
  2. I²C communication errors

Handling USB Communication Errors

USB communications errors can occur in either the Cfheader.Open() method or the Cfheader.Sync() method. To handle any errors that may occur in those methods, simply wrap them in a try…catch block, and handle an exceptions that occur in the catch block.

using Cfnet.Cfheader;
 
// Open USB communication with the CFHEADER module
try
{
    cfheader0.Open();
}
catch (Exception ex)
{
    // Handle any errors that occur here.
}
 
while (true)
{
    // Transmit the updated state of the output modules to the CFHEADER module, and
    // read the updated input state of the input modules from the CFHEADER module
    try
    {
        cfheader0.Sync();
    }
    catch (Exception ex)
    {
        // Handle an errors that occur here.
    }
}

For errors that occur in the Sync() method it may be necessary to Cfheader.Close() and Open() the module again.

Handling I²C Communication Errors

If the CFHEADER module encounters an error while communicating with CFNET IO module over I²C, then the error will be reported in the IModule.I2cStatus property after the Sync() method returns.

If such an error occurs, the CFHEADER module will no longer attempt to communicate with the CFNET IO module until the IModule.AcknowledgeI2cFailure() method is called to acknowledge the error. After acknowledging the error, the CFHEADER module will attempt to communicate with the CFNET IO module again in the next call to Sync().

while (true)
{
    // Transmit the updated state of the output modules to the CFHEADER module, and
    // read the updated input state of the input modules from the CFHEADER module
    cfheader0.Sync();
 
    if (cfdo_16n0.I2cStatus != I2cStatus.Success)
    {
        // Handle the I²C communication failure
        Console.Error.WriteLine($"I2C communication with the CFDO-16N module at address {cfdo_16n0.Address} failed.");
 
        // Acknowledge the I²C communication failure
        cfdo_16n0.AcknowledgeI2cFailure();
    }
}
cfnet/cfheader/mode_of_operation/index.txt · Last modified: 2024/10/04 08:49 by COMFILE Technology