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.
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); } }
To program a robust IO solution, all sources of errors must be handled. For the CFHEADER, there 2 primary sources for potential 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.
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(); } }