Sample 04: creating hardware
The following sample creates a hardware configuration with controllers, PLC tasks, bus couplers and Bus Terminals.
Sample C#
public partial class MainWindow : Window
{
BAProjectBuilder projectBuilder = null;
public MainWindow(AddInEntryPoint addInEntryPoint)
{
InitializeComponent();
// Code here
this.projectBuilder = new BAProjectBuilder();
}
private void buttonCreateHardware_Click(object sender, RoutedEventArgs e)
{
// add a CX2000 with the IO devices of the controller description to the solution
/////////////////////////////////////////////////////////////////////////////////
ControllerDescription descriptionControllerCX2000 = this.projectBuilder.HardwareDescriptionProvider.GetControllerDescription("CX20x0");
IController controllerCX2000 = this.projectBuilder.Solution.HardwareConfig.CreateController(descriptionControllerCX2000);
controllerCX2000.Name = "my CX2020";
// the CX20x0 comes with a terminal device, terminal coupler and two tasks
// get the task and change the name and the cycle time
ITask task01 = controllerCX2000.Tasks[0];
task01.Name = "Main";
task01.Interval = 45;
ITask task02 = controllerCX2000.Tasks[1];
task02.Name = "Background";
task02.Interval = 15;
// get the terminal device
IBoxDevice terminalDeviceCX2000 = controllerCX2000.Devices[0] as IBoxDevice;
terminalDeviceCX2000.Name = "my CX2000/CX5000 Terminal Device";
// get the terminal coupler
ITerminalCouplerBox terminalCouplerBoxCX2000 = terminalDeviceCX2000.Boxes[0] as ITerminalCouplerBox;
terminalCouplerBoxCX2000.Name = "my CX2000/CX5000 Terminal Coupler";
// add terminals to the terminal coupler
ITerminal terminalKL1012 = terminalCouplerBoxCX2000.CreateTerminal(this.projectBuilder.HardwareDescriptionProvider.GetTerminalDescription("KL1012"));
ITerminal terminalKL1404 = terminalCouplerBoxCX2000.CreateTerminal(this.projectBuilder.HardwareDescriptionProvider.GetTerminalDescription("KL1404"));
ITerminal terminalKL2404 = terminalCouplerBoxCX2000.CreateTerminal(this.projectBuilder.HardwareDescriptionProvider.GetTerminalDescription("KL2404"));
// link the channels of the terminals to the Main task
task01.CreateLink(terminalKL1012.Channels[0]);
task01.CreateLink(terminalKL1012.Channels[1]);
task01.CreateLink(terminalKL1404.Channels[0]);
task01.CreateLink(terminalKL1404.Channels[1]);
task01.CreateLink(terminalKL1404.Channels[2]);
task01.CreateLink(terminalKL1404.Channels[3]);
// link the channels of the KL2404 to the Background task
task02.CreateLink(terminalKL2404.Channels[0]);
task02.CreateLink(terminalKL2404.Channels[1]);
task02.CreateLink(terminalKL2404.Channels[2]);
task02.CreateLink(terminalKL2404.Channels[3]);
// add a BACnet Device
BACnetEthernetDevice bacnetDevice = controllerCX2000.CreateDevice(this.projectBuilder.HardwareDescriptionProvider.GetDeviceDescription("BACnet Ethernet Device Rev 12")) as BACnetEthernetDevice;
bacnetDevice.Name = "my BACnet Device";
// add a CP without(!) the IO devices of the controller description to the solution
///////////////////////////////////////////////////////////////////////////////////
ControllerDescription controllerDescriptionCP = this.projectBuilder.HardwareDescriptionProvider.GetControllerDescription("IPC/CP ARM");
IController controllerCP = this.projectBuilder.Solution.HardwareConfig.CreateController(controllerDescriptionCP, false);
controllerCP.Name = "my CP";
// add the task and change the name and the cycle time
ITask task03 = controllerCP.CreateTask(this.projectBuilder.HardwareDescriptionProvider.GetTaskDescription("Standard"));
task03.Name = "Main";
task03.Interval = 20;
// add the RT ethernet device
IBoxDevice ethernetRTDeviceCP = controllerCP.CreateDevice(this.projectBuilder.HardwareDescriptionProvider.GetDeviceDescription("Real Time Ethernet Protocol")) as IBoxDevice;
ethernetRTDeviceCP.Name = "my RT Ethernet Device";
// add the BK9000 coupler
ITerminalCouplerBox terminalCouplerBK9000 = ethernetRTDeviceCP.CreateBox(this.projectBuilder.HardwareDescriptionProvider.GetBoxDescription("BK9000 Ethernet Fieldbus Coupler")) as ITerminalCouplerBox;
terminalCouplerBK9000.Name = "my BK9000 Ethernet Coupler";
// add terminals to the terminal coupler
ITerminal terminalKL3204 = terminalCouplerBK9000.CreateTerminal(this.projectBuilder.HardwareDescriptionProvider.GetTerminalDescription("KL3204"));
// link the channels of the terminals to the Main task
task03.CreateLink(terminalKL3204.Channels[0]);
task03.CreateLink(terminalKL3204.Channels[1]);
task03.CreateLink(terminalKL3204.Channels[2]);
task03.CreateLink(terminalKL3204.Channels[3]);
this.Close();
}
}