Question

DSP send raw ethernet packets to PC than PC capturing these packets and send ack response. They are messaging MAC to MAC so there are no ip layer. I want to make real-time messaging in every 1ms. DSP send message every 1 milisecond but PC can not capture the messages and send reply under 1 milisecond. Capturing and sending packet takes 15 - 30 ms. This result too slow for me. How can I make this faster. I am using jpcap library and my operating system Win XP x32.

Capturing Code:

    private void captor() {
                try {
                    captor = JpcapCaptor.openDevice(cf.getDevice(), 100, true, 1);
                } catch (Exception ex) {
                    Logger.getLogger(CapturingPacket.class.getName()).log(Level.SEVERE, null, ex);
                }
            }



    private void capturing() {
            Packet packet = captor.getPacket();
            if (packet != null) {
                if (packet.data.length > 0) {
                    EthernetPacket ethernetPacket = (EthernetPacket) packet.datalink;                
                    String receivedDestMac = Common.byteToMacStringFormat(ethernetPacket.src_mac);

                    if (definedDestMac.equals(receivedDestMac)) {
                        captured(packet.data);
                    }

                }
            }

}

private class captureRunner implements Runnable {

        public void run() {
            captor();
            while (running) {
                capturing();
                try {
                    Thread.sleep(0);
                } catch (InterruptedException err) {
                }
            }
        }
    }

Sending Code:

private void send(byte[] message) {
        try {

            JpcapSender send = JpcapSender.openDevice(cf.getDevice());

            Packet packet = new Packet();
            //ethernet frame
            EthernetPacket ethernetPacket = new EthernetPacket();

            // #dst_mac
            ethernetPacket.dst_mac = getDestMac();
            // #src_mac
            ethernetPacket.src_mac = cf.getDevice().mac_address;
            // #frametype
            ethernetPacket.frametype = Common.ETHERNET_FRAME_TYPE;
            // #data
            packet.data = message;
            // datalink
            packet.datalink = ethernetPacket;
            send.sendPacket(packet);
            send.close();

        } catch (Exception ex) {
            Common.showErrorMessage("Send Error");
            Logger.getLogger(MessagingPacket.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
Was it helpful?

Solution

"Capturing and sending packet takes 15 - 30 ms." -- This is normal. You cannot make this much faster. Windows is not a real-time operating system. Use a microcontroller instead (there are better options, like FPGA and ARM, but I have no experience with these).

OTHER TIPS

Use a libpcap filter, which you can set via JPcap, instead of your Java filter. That way non-matching packets won't even be captured, let alone cause callbacks into JPcap and then your Java code.

Also use a much bigger buffer than 100.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top