⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 linkpacketconnection.java

📁 基于Java的地图数据管理软件。使用MySQL数据库管理系统。
💻 JAVA
字号:
package net.aetherial.gis.garmin;

import java.io.*;
import java.util.*;
import javax.comm.*;
import net.aetherial.gis.garmin.*;
import net.aetherial.gis.surface.ItemValue;

public class LinkPacketConnection
{
  public static boolean portBeenUsed = false;
  public int recieveCount = -1;
        public LinkPacketConnection ()
        {
                ConsoleConfig cc = new ConsoleConfig ();

                // Open connection to Comm Port
               // TimeOutOfReceieve toor = new TimeOutOfReceieve(this);
               // toor.start();
                try
                {
                        ItemValue.ttyS = (SerialPort) CommPortIdentifier.getPortIdentifier (cc.getSerialPort ()).open (new String ("Garmin GPS"), 100);
                        ItemValue.ttyS.close();
                        ItemValue.ttyS = null;
                        ItemValue.ttyS = (SerialPort) CommPortIdentifier.getPortIdentifier (cc.getSerialPort ()).open (new String ("Garmin GPS"), 100);
                        ItemValue.ttyS.setSerialPortParams (9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);

                }
                catch (NoSuchPortException e)
                {
                        System.err.println ("The specified port does not exist.");

                }
                catch (PortInUseException e)
                {
                        System.err.println ("That port is already in use.");
                        this.portBeenUsed = true;
                        Thread.currentThread().stop();
                        this.destroy();
                }
                catch (UnsupportedCommOperationException e)
                {
                        System.err.println ("That port operation is not supported on this platform.");
                }

                try
                {
                        in = null;
                        out = null;
                        in = new BufferedInputStream (ItemValue.ttyS.getInputStream ());
                        out = ItemValue.ttyS.getOutputStream ();
                }
                catch (IOException e)
                {
                        System.err.println ("Error opening serial port.");
                }
                catch (NullPointerException e)
                {
                        System.err.println (e);
                }
                try {
                  ItemValue.ttyS.enableReceiveTimeout(100);
                }
                catch (UnsupportedCommOperationException ex) {
                  System.out.println("timeout in class linkPacketConnection");
                }
        }

        public  void destroy ()
        {
                ItemValue.ttyS.close ();

                try
                {
                        in.close ();
                        out.close ();
                }
                catch (IOException e)
                {

                }
        }

        public void send (LinkPacket packet)
        {
          ItemValue.send();
                boolean acked = false;

                while (! acked)
                {
                        ByteArrayOutputStream baos = new ByteArrayOutputStream ();
                        byte single = (byte) 255;

                        try
                        {
                                out.write (packet.toByteArray ());
                        }
                        catch (IOException e)
                        {
                                System.err.println (e);
                        }
                        catch (NullPointerException e)
                        {
                                System.err.println (e);
                        }

// Need to implement proper DLE behavior...

                        try
                        {
                                while (single != (byte) Garmin.EOT)
                                {
                                  //ItemValue.data++;
                                        single = (byte) in.read ();
                                        baos.write (single);
                                }
                        }
                        catch (IOException e)
                        {
                                System.err.println (e);
                        }
                        catch (NullPointerException e)
                        {
                                System.err.println (e);
                        }

                        LinkPacket ack = new LinkPacket (baos.toByteArray ());

                        if (ack.getPacketType () == Garmin.ACK)
                        {
                                acked = true;
                        }
                }
        }

        LinkPacket receive ()
        {
          ItemValue.recieve();
          this.recieveCount++;

                boolean vrfyRcpt = false;
                LinkPacket rcpt = new LinkPacket (0, null);

                while (!vrfyRcpt)
                {
                        byte[] header = new byte[3];
                        byte[] nullByte = {0, 0};
                        int payloadsize;

                        try
                        {
                                while (header[0] != (byte) 16)
                                {
                                        header[0] = (byte) in.read ();
                                }
                                header[1] = (byte) in.read ();
                                header[2] = (byte) in.read ();

                                // System.out.println ("Packet size: "+header[2]);

                                if (header[2] >= 0)
                                        payloadsize = (int) header[2];
                                else
                                        payloadsize = 256 - (int) header[2];

                                // System.out.println ("Payload size: "+payloadsize);


                                byte[] packet = new byte[payloadsize + 6];

                                packet[0] = header[0];
                                packet[1] = header[1];
                                packet[2] = header[2];

                                for (int i = 3; i < (payloadsize + 6); i++)
                                {
                                        packet[i] = (byte) in.read ();

                                        if (packet[i] == (byte) 16)
                                        {
                                                byte b = (byte) in.read ();

                                                if (b == (byte) 3)
                                                        break;
                                                else if (b != (byte) 16)
                                                {
                                                        System.out.println ("DLE anomaly! b = "+b);
                                                }
                                        }
                                }
                                if(packet.length==0)
                                  return null;
                                rcpt = new LinkPacket (packet);
                        }
                        catch (IOException e)
                        {
                                System.err.println (e);
                        }

                        nullByte[0] = (byte) rcpt.getPacketType ();

                        LinkPacket ack;

                //	if (rcpt.goodChecksum ())
                        if (true)
                        {
                                vrfyRcpt = true;
                                ack = new LinkPacket (Garmin.ACK, nullByte);
                        }
                        else
                        {
                //		rcpt.printPacket ();
                                ack = new LinkPacket (Garmin.NAK, nullByte);
                        }

                        try
                        {
                                out.write (ack.toByteArray ());
                        }
                        catch (IOException e)
                        {
                                System.err.println ("Error writing to serial port.");
                        }
                }

                return rcpt;
        }

        //public  SerialPort ttyS;

        public  BufferedInputStream in;
        public  OutputStream out;
}
      /*
class TimeOutOfReceieve extends Thread{
  LinkPacketConnection lpc = null;
  TimeOutOfReceieve(LinkPacketConnection lpc){
    this.lpc = lpc;
  }
  public void run(){
    try {
      Thread.sleep(1000);
      if(lpc.recieveCount<=0){
        System.out.println("Close port");
        lpc.destroy();
      }
    }
    catch (InterruptedException ex) {
    }
  }
}*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -