Hands-On C# Host Computer Project – Interaction and Simulation of Host Computer and PLC Signals [Day 16 of Beginner Series]

Today, we will organize two parts: the interaction protocol between the host computer and PLC signals, and the simulation implementation of various signals on the host computer.1. Interaction Protocol between Host Computer and PLC SignalsWe will use the recently released 18650 sorting machine project as a case study.PLC sends signals to the host computer:1. Start scanning – set M0, the host computer determines that it has received the rising edge of M0 and starts scanning.2. Start testing – set M1, the host computer determines that it has received the rising edge of M1 and starts testing.3. Sorting and unloading completed – set M2, the host computer determines that it has received the rising edge of M2, indicating that sorting and unloading are complete.The host computer sends signals to the PLC:1. Scan OK – set M10, the PLC determines that it has received M10 and records that this battery is scanned OK.2. Scan NG – set M11, the PLC determines that it has received M11 and records that this battery is scanned NG.3. Test OK – set M12, the PLC determines that it has received M12 and records that this battery is tested OK.4. Test NG – set M13, the PLC determines that it has received M13 and records that this battery is tested NG.5. Data saved after sorting and unloading completed – set M14, the PLC determines that it has received M14 and unloading is complete.2. Simulation Menu Implementation on the Host ComputerWe previously discussed an article about Context Menu ContextMenuStrip in the series “30 Days Quick Start C# Winform Host Computer Development Day 6: Menus and Toolbars MenuStrip/ToolStrip, etc.”1. The functionality we want to implementWhen right-clicking on the interface, a menu pops up containing five buttons for the host computer to send signals to the PLC, and synchronously reflecting the changes of the battery from scanning –> testing –> unloading in the production data.2. Menu EditingSelect SkinContextMenuStrip from the toolbar and drag it to the main form.Hands-On C# Host Computer Project - Interaction and Simulation of Host Computer and PLC Signals [Day 16 of Beginner Series]Input the five signals sent from the host computer to the PLC above.Hands-On C# Host Computer Project - Interaction and Simulation of Host Computer and PLC Signals [Day 16 of Beginner Series]3. Writing CodeKeep the default names, double-click the five menu options to generate five corresponding events.Hands-On C# Host Computer Project - Interaction and Simulation of Host Computer and PLC Signals [Day 16 of Beginner Series]Five eventsHands-On C# Host Computer Project - Interaction and Simulation of Host Computer and PLC Signals [Day 16 of Beginner Series]Before implementing the code, we first need to delete a previously added simulation data.Hands-On C# Host Computer Project - Interaction and Simulation of Host Computer and PLC Signals [Day 16 of Beginner Series]Code Implementation:

private void 扫码OK置位M10ToolStripMenuItem_Click(object sender, EventArgs e){    // Implement adding barcode, result column displays -- scan OK    // Generate simulated barcode    string barcode = "BC_" + DateTime.Now.ToString("yyyyMMddHHmmssfff");    // Add scan OK record to production data list    AddRowToListView(barcode, "0.00", "0.00", "扫码OK");    AppendLog($"Executing scan OK operation, barcode: {barcode}");    // Here add the command to send M10 to PLC}
private void 扫码NG置位M11ToolStripMenuItem_Click(object sender, EventArgs e){    // Implement adding barcode, result column displays -- scan NG    string barcode = "BC_" + DateTime.Now.ToString("yyyyMMddHHmmssfff");    AddRowToListView(barcode, "0.00", "0.00", "扫码NG");    AppendLog($"Executing scan NG operation, barcode: {barcode}", true);    // Here you can add the command to send M11 to PLC}
private void 测试OK置位M12ToolStripMenuItem_Click(object sender, EventArgs e){    // Testing completed, result column displays -- test OK    if (slvwData.Items.Count == 0)    {        MessageBox.Show("No data to test!", "Tip", MessageBoxButtons.OK, MessageBoxIcon.Information);        return;    }    // Update the latest record's test status to OK    int lastRowIndex = slvwData.Items.Count - 1;    UpdateRowData(lastRowIndex, 5, "测试OK");    AppendLog("Executing test OK operation, updating the latest record status");    // Here you can add the command to send M12 to PLC}
private void 测试NG置位M13ToolStripMenuItem_Click(object sender, EventArgs e){    // Testing completed, result column displays -- test NG    if (slvwData.Items.Count == 0)    {        MessageBox.Show("No data to test!", "Tip", MessageBoxButtons.OK, MessageBoxIcon.Information);        return;    }    int lastRowIndex = slvwData.Items.Count - 1;    UpdateRowData(lastRowIndex, 5, "测试NG");    AppendLog("Executing test NG operation, updating the latest record status", true);    // Here you can add the command to send M13 to PLC}
private void 已保存数据置位M14ToolStripMenuItem_Click(object sender, EventArgs e){    // Unloading completed, result column displays -- unloading completed    if (slvwData.Items.Count == 0)    {        MessageBox.Show("No data to unload!", "Tip", MessageBoxButtons.OK, MessageBoxIcon.Information);        return;    }    int lastRowIndex = slvwData.Items.Count - 1;    UpdateRowData(lastRowIndex, 5, "出料完成");    AppendLog("Executing save data operation, updating the latest record to unloading completed");    // Here you can add the command to send M14 to PLC}

Using the logo icon for the right-click pop-up menu event:Hands-On C# Host Computer Project - Interaction and Simulation of Host Computer and PLC Signals [Day 16 of Beginner Series]Code Implementation:

private void label1_MouseClick(object sender, MouseEventArgs e){    // Check if it is a right-click    if (e.Button == MouseButtons.Right)    {        // Pop up the right-click menu at the mouse click position        skinContextMenuStrip1.Show(this, e.Location);    }}

Now everything is complete, let’s take a look at the simulation.Hands-On C# Host Computer Project - Interaction and Simulation of Host Computer and PLC Signals [Day 16 of Beginner Series]3. Implementing Simulation Functionality on the Host ComputerFirst, right-click on the logo position, then sequentially scan –> test –> unload, and you will see the changes in production data and logs.Summary:Today’s sharing ends here.The source code can be downloaded by sending “3004” in the public account.There are many related articles in the public account,if you like it, you can follow or share it, thank you for your support.A WeChat technical exchange group has been established, you can add it in the public account menu, and everyone is welcome to communicate and learn together.Beginner Project [Automatic Screw Tightening Machine] C#, PLC, Touch Screen Practical (2/5): C# Winforms Host Computer DesignBeginner Project [OpenCV Visual Sorting Robot Arm] (2/3): Host Computer Design. [C# Winforms, OpenCV Vision, PLC Practical]

Leave a Comment