Skip to main content

Small-Scale Visual Quality Inspection

TIA Device Name = Configuration Tool Site Name (e.g., vis-qc-01).

Hardware Specifications​

  • Industrial PC: Windows × 1
  • Camera × 1
  • PLC × 1
  • Photoelectric Sensor × 1 + Ejection Cylinder × 1

Performance Metrics​

  • Cycle rate:500 ms / piece (120 pieces/min)
  • Testing time: Less than 150 ms

Use Cases​

Small production-line vision inspection station. The PLC handles incoming material, stopping steady, release, and NG rejection; the industrial PC only runs the camera and the judgement, and on the network it is one IO Device. Images, dimensions, and archives stay on the industrial PC and do not enter process data. The recipe number is used only to pick the inspection template locally.

Workstation Sequence​

  1. Photoelectric part present — when the workpiece blocks the photoelectric sensor, the PLC sets Present. No photo without a part.
  2. Capture rising edge — after the stop is stable the PLC pulls Trigger high. The industrial PC only recognizes 0→1, and only when Present is set and it is not busy.
  3. Busy mutual exclusion — right after recognizing the edge, clear the previous OK/NG, then set Ready=0 and Busy=1. New edges during busy are dropped, no double capture.
  4. Local image judging — pick a local template by Recipe and judge the image (for simulation, sleep stands in for the camera).
  5. OK / NG — Ok=1 for pass, Ng=1 for fail; then Busy=0 and Ready=1. On OK the PLC releases the part; on NG it fires the reject cylinder.
  6. Disconnect — once connected and later disconnected: Fault=1, Ready is taken away and no more judging. After reconnecting, ready is set again; the current Trigger level is not treated as a new edge.

Process Data​

DirectionFieldDefinition of a Workstation
From PLC (I)PresentPhotoelectric: part present
From PLC (I)TriggerPhoto-triggered; recognizes only rising edges
From PLC (I)RecipeFormula Number, Select Local Testing Template
Send to PLC (Q)ReadyI can take the next one.
Send to PLC (Q)BusyBeing captured/judged; mutually exclusive
Send to PLC (Q)OkThis item passes inspection → PLC releases it
Send to PLC (Q)NgThis item is nonconforming → PLC marks it for rejection
Send to PLC (Q)FaultDisconnection/Outage

Once connected, do the reading and writing with the handshake described above. The block below fakes OK/NG by part count (1 NG every 5 parts). On real hardware, replace Thread.Sleep with image capture and the local algorithm, and point the template path at your own.

using System;
using System.Runtime.InteropServices;
using System.Threading;
using Darra.PnS;

[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct FromPlc
{
public bool Present;
public bool Trigger;
public short Recipe;
}

[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct ToPlc
{
public bool Ready;
public bool Busy;
public bool Ok;
public bool Ng;
public bool Fault;
}

using (var device = new DarraPnS())
{
PnSErrorCode rc = device.Start();
if (rc != PnSErrorCode.Success)
return;

string[] templates = { "default.bin", "sku-a.bin", "sku-b.bin" };
ToPlc output = default;
bool lastTrigger = true;
bool wasConnected = false;
bool busy = false;
int piece = 0;
bool running = true;

while (running)
{
if (!device.Connected)
{
if (wasConnected)
{
output = new ToPlc { Fault = true };
device.ProcessData.Write(ref output);
lastTrigger = true;
busy = false;
}
wasConnected = false;
Thread.Sleep(20);
continue;
}

if (!wasConnected)
{
output = new ToPlc { Ready = true };
device.ProcessData.Write(ref output);
lastTrigger = true;
busy = false;
wasConnected = true;
}

FromPlc input = new FromPlc();
device.ProcessData.Read(ref input);

bool rising = input.Trigger && !lastTrigger;
lastTrigger = input.Trigger;

if (rising && input.Present && !busy)
{
busy = true;
output = new ToPlc { Busy = true };
device.ProcessData.Write(ref output);

string template = templates[0];
int recipe = input.Recipe;
if (recipe >= 0 && recipe < templates.Length)
template = templates[recipe];

Thread.Sleep(120);

piece++;
bool ok = (piece % 5) != 0;

output = new ToPlc { Ready = true, Ok = ok, Ng = !ok };
device.ProcessData.Write(ref output);
busy = false;

Console.WriteLine(
"第 " + piece + " 件 模板 " + template + " → " + (ok ? "OK 放行" : "NG 剔除"));
}

Thread.Sleep(10);
}

device.Stop();
}