コード例 #1
0
 public void run() {
   do {
     try {
       // 1. pickup the message and corresponding request
       byte[] message = (byte[]) m_SendQueue.take();
       DatagramPacket req =
           (DatagramPacket) m_Requests.remove(new Integer(ModbusUtil.registersToInt(message)));
       // 2. create new Package with corresponding address and port
       DatagramPacket res =
           new DatagramPacket(message, message.length, req.getAddress(), req.getPort());
       m_Socket.send(res);
       if (Modbus.debug) System.out.println("Sent package from queue.");
     } catch (Exception ex) {
       ex.printStackTrace();
     }
   } while (m_Continue || !m_SendQueue.isEmpty());
 } // run
コード例 #2
0
 public void run() {
   do {
     try {
       // 1. Prepare buffer and receive package
       byte[] buffer = new byte[256]; // max size
       DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
       m_Socket.receive(packet);
       // 2. Extract TID and remember request
       Integer tid = new Integer(ModbusUtil.registersToInt(buffer));
       m_Requests.put(tid, packet);
       // 3. place the data buffer in the queue
       m_ReceiveQueue.put(buffer);
       if (Modbus.debug) System.out.println("Received package to queue.");
     } catch (Exception ex) {
       ex.printStackTrace();
     }
   } while (m_Continue);
 }