工业4.0通信协议学习之IPC CFX


前言

CFX(Connected Factory Exchange)是工厂通信的“即插即用”型行业标准,本标准规定电子装配制造领域中制造过程与相关系统之间进行全向数据交换的要求,用于记录学习,随便写写,避免忘记。

一、IPC CFX协议适用范围

该协议标准不仅用于SMT相关生产,也支持机械装配、定制化、包装和运输等上游环节,甚至电气、机械子部件等上游环节。

二、如何通信

在IPC CFX的标准下,设备的数据被定义为制造主题(Topic)和消息结构(Message)。设备不太需要关注数据发送到哪里,数据来源于哪里,只需要知道在什么时机下发送什么数据,收到什么数据执行什么操作即可。

1. 与谁通信

机器与机器,机器与企业之间的通信。

2. 如何通信

IPC CFX提供了一个SDK,设该SDK具有以下功能:

  • 将所有CFX消息的用Class/Object表示。
  • 能够将CFX消息对象序列化和反序列化为JSON格式。
  • 能够通过AMQP传输协议将CFX消息发布到一个或多个目的地。
  • 能够通过AMQP传输协议从一个或多个订阅源接收CFX消息。
  • 完全自动化的网络连接故障管理(即使在网络宕机或其他不可靠的情况下保持AMQP连接)。
  • CFX消息“假脱机”。维护由于网络条件错误而无法传输的CFX消息的持久队列。一旦网络服务恢复,消息将自动按原来的顺序传输。
  • 点对点CFX端点命令(请求/响应)支持。
  • 支持AMQP 1.0 SASL认证和TLS加密。

因此,有了这个SDK,设备软件似乎就简单了很多,只需要使用SDK,将采集到的设备数据通过SDK发送给其他设备或者企业管理中心即可。
如何使用SDK可参考官方案例。
该SDK将消息按照AMQP协议传输,每台设备都是一个amqp端点,通过发布和订阅来实现数据的交互。
以下是官方内容,不想写了,实现了消息的发布通道和订阅通道,注意,使用的是如果是amqps就是加密传输,端口号用5671,如果是非加密传输,使用amqp,端口号为5672。

AmqpCFXEndpoint endpoint = new AmqpCFXEndpoint();
endpoint.Open("Vendor1.Model1.Machine34");

// Note the user of "amqps://" instead of "amqp://"
Uri uri = new Uri("amqps://mycfxbroker.mydomain.com");           

// Target exchange on broker (shown here in RabbitMQ compatible format)
string amqpTarget = "/exchange/myexchange";
endpoint.AddPublishChannel(uri, amqpTarget);

// Source queue on broker (shown here in RabbitMQ compatible format)
string amqpSource = "/queue/myqueue";
endpoint.AddSubscribeChannel(uri, amqpSource);

在发布或接收消息时使用代理(或其他目的地)进行身份验证
将用户名和密码编码到目标Uri中。虽然不需要加密通信(AMQPS),但强烈建议使用。如果使用标准AMQP,您的密码将以明文形式在网络上传输。

AmqpCFXEndpoint endpoint = new AmqpCFXEndpoint();
endpoint.Open("Vendor1.Model1.Machine34");

// Encode your username and password into the destination Uri
string username = "myusername";
string password = "mypassword";
string hostname = "mycfxbroker.mydomain.com";

//  eg.  amqps://myusername:mypassword@mycfxbroker.mydomain.com
Uri uri = new Uri(string.Format("amqps://{0}:{1}@{2}", username, password, hostname));

// Target exchange on broker (shown here in RabbitMQ compatible format)
string amqpTarget = "/exchange/myexchange";
endpoint.AddPublishChannel(uri, amqpTarget);

// Source queue on broker (shown here in RabbitMQ compatible format)
string amqpSource = "/queue/myqueue";
endpoint.AddSubscribeChannel(uri, amqpSource);

向另一个CFX端点发送直接、同步请求(请求/响应模式)
使用ExecuteRequest方法直接向其他CFX端点发出请求。这是一个同步(阻塞)方法。与发布/订阅消息一样,您可以将安全通信(AMQPS)用于直接的点对点请求/响应事务。只需在Uri中使用“amqps://”而不是“amqp://”。如果远程CFX端点需要身份验证,您也可以在Uri中传递您的用户名和密码(就像使用标准的pub/sub消息传输一样)。

string myCFXHandle = "Vendor1.Model1.Machine34";

AmqpCFXEndpoint endpoint = new AmqpCFXEndpoint();
endpoint.Open(myCFXHandle);

string targetEndpointHostname = "machine55.mydomain.com";
string targetCFXHandle = "Vendor2.Model2.Machine55";
string remoteUri = string.Format("amqp://{0}", targetEndpointHostname);

// Set a timeout of 20 seconds.  If the target endpoint does not
// respond in this time, the request will time out.
AmqpCFXEndpoint.RequestTimeout = TimeSpan.FromSeconds(20);

// Build a GetEndpointInfomation Request
CFXEnvelope request = CFXEnvelope.FromCFXMessage(new GetEndpointInformationRequest()
{
    CFXHandle = targetCFXHandle
});

CFXEnvelope response = endpoint.ExecuteRequest(remoteUri, request);

接收和处理来自其他CFX端点的直接、同步请求(请求/响应模式)
使用入站请求Uri (Open方法的可选参数)初始化端点。连接一个事件处理程序来处理请求并返回响应。

AmqpCFXEndpoint endpoint;
string myCFXHandle = "Vendor1.Model1.Machine34";
Uri myRequestUri;

public void OpenEndpoint()
{
    endpoint = new AmqpCFXEndpoint();
    myRequestUri = new Uri(string.Format("amqp://{0}", System.Net.Dns.GetHostName()));

    endpoint.OnRequestReceived += Endpoint_OnRequestReceived;
    endpoint.Open(myCFXHandle, myRequestUri);
}

private CFXEnvelope Endpoint_OnRequestReceived(CFXEnvelope request)
{
    // Process request.  Return Result.
    if (request.MessageBody is WhoIsThereRequest)
    {
        CFXEnvelope result = CFXEnvelope.FromCFXMessage(new WhoIsThereResponse()
        { CFXHandle = myCFXHandle, RequestNetworkUri = myRequestUri.ToString(), RequestTargetAddress = "" });
        result.Source = myCFXHandle;
        result.Target = request.Source;
        return result;
    }

    return null;
}

接收和处理来自其他CFX端点的安全直接、同步请求(请求/响应模式)
使用入站请求Uri (Open方法的可选参数)初始化端点。连接一个事件处理程序来处理请求并返回响应。

AmqpCFXEndpoint endpoint;
string myCFXHandle = "Vendor1.Model1.Machine34";
Uri myRequestUri;

public void OpenEndpointSecure()
{
    endpoint = new AmqpCFXEndpoint();
    myRequestUri = new Uri(string.Format("amqps://{0}", System.Net.Dns.GetHostName()));

    // Load certificate from local machine or user certificate store
    X509Certificate2 cert = AmqpUtilities.GetCertificate("MyCertificateName");

    endpoint.OnRequestReceived += Endpoint_OnRequestReceived;
    endpoint.Open(myCFXHandle, myRequestUri, cert);
}

private CFXEnvelope Endpoint_OnRequestReceived(CFXEnvelope request)
{
    // Process request.  Return Result.
    if (request.MessageBody is WhoIsThereRequest)
    {
        CFXEnvelope result = CFXEnvelope.FromCFXMessage(new WhoIsThereResponse()
        { CFXHandle = myCFXHandle, RequestNetworkUri = myRequestUri.ToString(), RequestTargetAddress = "" });
        result.Source = myCFXHandle;
        result.Target = request.Source;
        return result;
    }

    return null;
}

因此,使用该SDK就是添加发布订阅通道:

theEndpoint.AddPublishChannel(new Uri(myPubBroker), myExchange);
theEndpoint.AddSubscribeChannel(new Uri(mySubBroker), myExchange);

实现theEndpoint的事件:

 theEndpoint.OnRequestReceived += TheEndpoint_OnRequestReceived;
theEndpoint.OnCFXMessageReceived += TheEndpoint_OnCFXMessageReceived;
theEndpoint.OnValidateCertificate += TheEndpoint_OnValidateCertificate;
theEndpoint.OnConnectionEvent += TheEndpoint_OnConnectionEvent;
theEndpoint.OnCFXMessageReceivedFromListener += TheEndpoint_OnCFXMessageReceivedFromListener;

在适当的时机将机器产生的数据发出去,譬如我们一个工序完成了:

 CFXEnvelope env = new CFXEnvelope(new CFX.Production.WorkCompleted()
            {
                PrimaryIdentifier = "Barcode",
                Result = WorkResult.Completed,
                TransactionID = Guid.NewGuid()
            });
            theEndpoint.Publish(env);

总结

CFX标准有多种类型的用户,包括设备供应商,工厂解决方案供应商、企业内部的IT管理等,因此,在CFX标准定义下的数据,可以被应用在各种系统中,例如,现场看板系统、闭环返回系统、MES控制、精益供应链管理、主动质量管理和生产控制等。因此,使用该协议收集收据不是工作重点,使用这些数据更好地管控生产,提升生产质量等,才是协议的中心思想。

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值