如何通过Sharppcap获取连续数据包?

时间:2019-07-09 08:21:55

标签: c# sharppcap

我当前正在尝试捕获通过名为TwinCAT的程序发送和接收的EtherCAT数据包。 TwinCAT是用于Windows上EtherCAT通信的实时控制软件。该程序用于每4ms与从站进行一次通信。

顺便说一句,我捕获了数据包并观察到数据在分析期间不是连续的。 因此,我在捕获数据包的部分插入了代码以检查时间差,并确认某些数据包是比前一个数据包晚20ms的数据包。

因为我可以在TwinCAT XAE工具中检查丢失的帧,所以我认为数据包实际上并没有丢失,我认为程序有问题。

下面是我的代码。

public class EtherCATPacketCaptureService
{
    private const int PacketQueueSize = 1024;

    private object locker;  //<-Mutex
    private Queue<RawCapture> PacketQueue;  //<-패킷을 저장할 큐
    private WinPcapDevice _etherCATDevice;  //<-EtherCAT 통신 네트워크 장치

    public int PacketsCount { get => PacketQueue.Count; }

    //생성자 : _etherCATDevice 객체와 EtherCAT통신장치 매칭 
    public EtherCATPacketCaptureService(string etherCATNICAddr)
    {
        CaptureDeviceList devices = CaptureDeviceList.Instance;

        if (devices.Count() < 1)
        {
            throw new Exception("Not exist network interface");
        }

        foreach (WinPcapDevice dev in devices)
        {
            if (dev.Addresses.Count > 0)
            {
                foreach (PcapAddress addr in dev.Addresses)
                {
                    if (addr.Addr.hardwareAddress != null)
                    {
                        string HWAddr = addr.Addr.hardwareAddress.ToString();
                        if (HWAddr == etherCATNICAddr)  // EtherCAT NIC MAC주소를 EtherCATNICAddr파라미터로 넘겨받아 설정
                        {
                            _etherCATDevice = dev;
                        }
                    }
                }
            }
        }

        if (_etherCATDevice == null)
            throw new NullReferenceException("Can't find EtherCAT NIC");
        else
        {
            PacketQueue = new Queue<RawCapture>(PacketQueueSize);
            locker = new object();
            _etherCATDevice.OnPacketArrival += Device_OnPacketArrival;
            _etherCATDevice.Open(OpenFlags.Promiscuous, 1000);
        }
    }

    ~EtherCATPacketCaptureService()
    {
        _etherCATDevice.Close();
    }

    //패킷 캡쳐 시작
    public void StartCapture(int timeout)
    {
        if(_etherCATDevice != null)
        {
            _etherCATDevice.StartCapture();
        }
    }

    //패킷 캡쳐 종료
    public void StopCapture()
    {
        if (_etherCATDevice != null)
        {
            _etherCATDevice.StopCapture();
        }
    }

    //패킷캡쳐 이벤트 발생 시 패킷을 큐메모리에 저장
    private void Device_OnPacketArrival(object sender, SharpPcap.CaptureEventArgs e)
    {
        if (_etherCATDevice != null)
        {
            lock (locker)
            {
                if (PacketQueue != null)
                {
                    if (PacketQueue.Count > 0)
                    {
                        if((e.Packet.Timeval.Date.Ticks - PacketQueue.Peek().Timeval.Date.Ticks) > 200000)
                            throw new Exception("Packet Droped");
                    }
                    PacketQueue.Enqueue(e.Packet);
                }
            }
        }
    }

    //저장된 패킷을 리턴(Dequeue)
    public RawCapture[] GetPackets(int count)
    {
        RawCapture[] PacketArray;
        if (_etherCATDevice != null)
        {
            lock (locker)
            {
                if (count >= PacketQueue.Count)
                    PacketArray = new RawCapture[PacketQueue.Count];
                else
                    PacketArray = new RawCapture[count];

                for (int i = 0; i < PacketArray.Length; i++)
                {
                    PacketArray[i] = PacketQueue.Dequeue();
                }
            }
            return PacketArray;
        }
        else
            return null;
    }

    public RawCapture[] GetPackets()
    {
        RawCapture[] PacketArray;
        if (_etherCATDevice != null)
        {
            lock (locker)
            {
                PacketArray = new RawCapture[PacketQueue.Count];

                for (int i = 0; i < PacketArray.Length; i++)
                {
                    PacketArray[i] = PacketQueue.Dequeue();
                }
            }
            return PacketArray;
        }
        else
            return null;
    }

    //저장된 패킷 클리어
    public void ClearPackets()
    {
        lock (locker)
        {
            PacketQueue.Clear();
        }
    }
}

OnPacketArrival的事件处理程序是Device_OnPacketArrival,它通过在处理程序中发现与前一个数据包的时间相差20ms或更多的时间时引发异常来检测问题。

这是因为我的表现不好而发生的问题吗? 性能改善能否解决? 如果您有好的意见,请回复。

2 个答案:

答案 0 :(得分:0)

如果您不是出于娱乐目的或出于教育目的而编写程序,而是出于专业目的,建议您使用Wireshark和名为ET2000的附加硬件来分析计算机的Ethercat数据包。

ET2000添加了一个时间戳,最大值为延迟40ns,直到发送到PC的镜像数据包为止,这要归功于Wireshark中的EtherCAT Stack Link解析器。

答案 1 :(得分:0)

我们在 EtherCAT 和 PROFINET 捕获方面遇到了类似的问题。我们从那时起使用 Hilscher netANALYZER 工具,现在一切都很好。这些工具内置了硬件 TAP,因此延迟更小 1 ns。时间戳分辨率也是 1 ns。

相关问题