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
- Photoelectric part present — when the workpiece blocks the photoelectric sensor, the PLC sets
Present. No photo without a part. - Capture rising edge — after the stop is stable the PLC pulls
Triggerhigh. The industrial PC only recognizes 0→1, and only whenPresentis set and it is not busy. - Busy mutual exclusion — right after recognizing the edge, clear the previous OK/NG, then set
Ready=0andBusy=1. New edges during busy are dropped, no double capture. - Local image judging — pick a local template by
Recipeand judge the image (for simulation, sleep stands in for the camera). - OK / NG —
Ok=1for pass,Ng=1for fail; thenBusy=0andReady=1. On OK the PLC releases the part; on NG it fires the reject cylinder. - Disconnect — once connected and later disconnected:
Fault=1,Readyis taken away and no more judging. After reconnecting, ready is set again; the currentTriggerlevel is not treated as a new edge.
Process Data
| Direction | Field | Definition of a Workstation |
|---|---|---|
| From PLC (I) | Present | Photoelectric: part present |
| From PLC (I) | Trigger | Photo-triggered; recognizes only rising edges |
| From PLC (I) | Recipe | Formula Number, Select Local Testing Template |
| Send to PLC (Q) | Ready | I can take the next one. |
| Send to PLC (Q) | Busy | Being captured/judged; mutually exclusive |
| Send to PLC (Q) | Ok | This item passes inspection → PLC releases it |
| Send to PLC (Q) | Ng | This item is nonconforming → PLC marks it for rejection |
| Send to PLC (Q) | Fault | Disconnection/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();
}