DSP 将原始以太网数据包发送到 PC,而不是 PC 捕获这些数据包并发送 ack 响应。他们正在向 MAC 发送 MAC 消息,因此没有 IP 层。我想每 1ms 进行一次实时消息传递。DSP每1毫秒发送一次消息,但PC无法捕获消息并在1毫秒内发送回复。捕获和发送数据包需要 15 - 30 ms。这个结果对我来说太慢了。我怎样才能让它更快。我正在使用 jpcap 库和我的操作系统 Win XP x32。
捕获代码:
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) {
}
}
}
}
发送代码:
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);
}
}