에서 SDK 계속
이전 단계에서 이미 구성 도구를 통해 테스트를 마쳤으며, 코드를 내보냈습니다. SDK 설치 디렉터리 C:\DARRA\Profinet_Slave\SDK\에 설치할 수 있으며, 공개 저장소를 통해서도 설치할 수 있습니다(C#은 NuGet, Python은 PyPI, Java는 Maven Central, Rust는 crates.io). C#에서는 using Darra.PnS;를 작성하면 됩니다.
최소 순환
역명과 NIC는 구성 도구에서 설정합니다. 프로그램 측: new → Start → 한 번 매핑하기프로세스 데이터(구조체가 프로세스 이미지에 매핑되며, 소스 변수가 필드로 변환됨) → Stop.
주기 전환이 자동으로 이루어지므로, 직접 주기 루프를 작성할 필요가 없으며, 어떤 스레드에서든 동시에 사용할 수 있습니다. Read / Write 이러한 유형은스냅샷(복사한 인스턴스는 저절로 변하지 않습니다); 매핑 엔트리는 각 언어의 InputsMapping / OutputsMapping 와 같은 이름의 엔트리입니다(I 측 읽기 전용, Q 측 쓰기 가능).
using System.Runtime.InteropServices;
using Darra.PnS;
[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct FromPlc // I: 컨트롤러 → 장치
{
public byte Present;
public short Recipe; // 온라인 측: 상단에 배치하기만 하면 되며, 값은 Read(ref rx)를 통해 가져옵니다.
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct ToPlc // Q: 장비 → 컨트롤러
{
public byte Ready;
public byte Busy;
}
using (var device = new DarraPnS())
{
device.Start();
// 한 번 매핑하기
ref readonly var inp = ref device.ProcessData.InputsMapping<FromPlc>();
ref ToPlc outp = ref device.ProcessData.OutputsMapping<ToPlc>();
while (running)
{
if (!device.Connected)
continue;
byte present = inp.Present; // 읽기 I: 버스 리프레시 후 스스로 변함
outp.Ready = 1; // Q: 프로세스 이미지를 직접 저장하기
}
device.Stop();
}
다바이트 필드는 PROFINET 네트워크의 빅엔디안(온라인 바이트 순서)입니다: 호스트에서 직접 읽을 short 경우 바이트 순서가 뒤바뀌므로 int , 호스트 순서 값으로 전송해야 합니다 Read Write(빅엔디안 인코딩/디코딩).
- C++
- C
- Python
- Java
- Rust
#include "pns.hpp"
using namespace darra::pns;
#pragma pack(push, 1)
struct FromPlc // I: 컨트롤러 → 장치
{
uint8_t Present;
uint8_t Trigger;
int16_t Recipe;
};
struct ToPlc // Q: 장비 → 컨트롤러
{
uint8_t Ready;
uint8_t Busy;
uint8_t Ok;
uint8_t Ng;
};
#pragma pack(pop)
PnSService device;
device.Start();
// 한 번 매핑하기
const FromPlc& inp = device.ProcessData().InputsMapping<FromPlc>();
ToPlc& outp = device.ProcessData().OutputsMapping<ToPlc>();
while (running)
{
if (!device.Connected())
continue;
uint8_t present = inp.Present; // 읽기 I: 버스 리프레시 후 스스로 변함
outp.Ready = 1; // Q: 프로세스 이미지를 직접 저장하기
}
device.Stop();
#include "pns.h"
#pragma pack(push, 1)
typedef struct { /* I: 컨트롤러 → 장치 */
uint8_t Present;
uint8_t Trigger;
} from_plc_t;
typedef struct { /* Q: 장비 → 컨트롤러 */
uint8_t Ready;
uint8_t Busy;
uint8_t Ok;
uint8_t Ng;
} to_plc_t;
#pragma pack(pop)
pns_service_handle_t session = NULL;
PnSServiceConnectRealtime(&session);
PnSServiceStart(session);
/* 한 번 매핑하기 */
const uint8_t *i = NULL; /* I 읽기 전용 */
volatile uint8_t *q = NULL; /* Q 작성 가능 */
size_t ni = 0, nq = 0;
if (PnSProcessDataInputsMappingRO(session, &i, &ni) != PNS_OK ||
PnSProcessDataOutputsMapping(session, &q, &nq) != PNS_OK ||
i == NULL || q == NULL || /* 실패 시 뷰가 비어 있음 */
ni < sizeof(from_plc_t) || nq < sizeof(to_plc_t))
{
/* 프로세스 이미지가 사용할 수 없음 */
}
const from_plc_t *inp = (const from_plc_t *)i;
to_plc_t *outp = (to_plc_t *)q;
while (running)
{
if (!PnSServiceIsConnected(session))
continue;
uint8_t present = inp->Present; /* 읽기 I: 버스 리프레시 후 스스로 변함 */
outp->Ready = 1; /* Q: 프로세스 이미지를 직접 저장하기 */
}
PnSServiceStop(session);
PnSServiceClose(session);
import ctypes
from darra_pns import PnSService
class FromPlc(ctypes.BigEndianStructure): # I: 컨트롤러 → 장치
_pack_ = 1
_fields_ = [("present", ctypes.c_uint8),
("recipe", ctypes.c_int16)]
class ToPlc(ctypes.BigEndianStructure): # Q: 장비 → 컨트롤러
_pack_ = 1
_fields_ = [("ready", ctypes.c_uint8),
("busy", ctypes.c_uint8)]
with PnSService() as device:
device.start()
# 한 번 매핑하기
inp = device.process_data.inputs_mapping(FromPlc)
outp = device.process_data.outputs_mapping(ToPlc)
while running:
if not device.connected:
continue
present = inp.present # 읽기 I: 버스 리프레시 후 스스로 변함
outp.ready = 1 # 글 Q: 연승 과정의 인상
device.stop()
스냅샷은 device.read_struct("Hi") / device.write_struct("Bf", 1, 1.5) 와 device.process_data.read_struct / write_struct —— 복사된 인스턴스는 스스로 변하지 않습니다. 주소 기반 읽기/쓰기는 안전 체인와 호환됩니다:
device.write_bool("Q0.0", True)
from_plc = device.read_bool("I0.0")
import com.darra.pns.service.PnSService;
import com.sun.jna.Structure;
import com.sun.jna.Structure.FieldOrder;
@FieldOrder({"present", "recipe"})
class FromPlc extends Structure { // I: 컨트롤러 → 장치
public byte present;
public short recipe;
public FromPlc() { super(ALIGN_NONE); }
}
@FieldOrder({"ready", "busy"})
class ToPlc extends Structure { // Q: 장비 → 컨트롤러
public byte ready;
public byte busy;
public ToPlc() { super(ALIGN_NONE); }
}
try (PnSService device = new PnSService()) {
device.start();
// 한 번 매핑하기
FromPlc inp = device.processData().inputsMapping(FromPlc.class);
ToPlc outp = device.processData().outputsMapping(ToPlc.class);
while (running) {
if (!device.connected())
continue;
inp.read(); // 프로세스 이미지 → 호스팅 캐시
byte present = inp.present; // 읽기 I: 버스 리프레시 후 스스로 변함
outp.ready = 1;
outp.write(); // Q: 캐시 호스팅 → 프로세스 이미지
}
device.stop();
}
use darra_pns::{PnSService, ServiceResult};
#[repr(C, packed)]
struct FromPlc { present: u8, recipe: i16 } // I: 컨트롤러 → 장치
#[repr(C, packed)]
struct ToPlc { ready: u8, busy: u8 } // Q: 장비 → 컨트롤러
fn main() -> ServiceResult<()> {
let device = PnSService::new();
device.start()?;
// 한 번 매핑하기
let inp = unsafe { device.process_data().inputs_mapping::<FromPlc>()? };
let outp = unsafe { device.process_data().outputs_mapping::<ToPlc>()? };
while running {
if !device.connected() {
continue;
}
let present = inp.present; // 읽기 I: 버스 리프레시 후 스스로 변함
outp.ready = 1; // Q: 프로세스 이미지를 직접 저장하기
let _ = present;
}
device.stop()?;
Ok(())
}
주소 기반 읽기/쓰기(Q0.0 / I0.0)는 안전 체인와 호환됩니다.
구성 도구의 상단 메뉴 바에 있는 「내보내기」를 통해 각 언어별 예제를 출력할 수 있습니다.
