2011-11-16 58 views
-2

我写这个Java J2SE代码:java使用的端口是什么?

import gnu.io.CommPort; 
import gnu.io.CommPortIdentifier; 
import gnu.io.SerialPort; 
import gnu.io.SerialPortEvent; 
import gnu.io.SerialPortEventListener; 

import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 
public class TwoWaySerialComm 
{ 
    public TwoWaySerialComm() 
    { 
     super(); 
    } 
    void connect (String portName) throws Exception 
    { 
     CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName); 
     if (portIdentifier.isCurrentlyOwned()) 
     { 
      System.out.println("Error: Port is currently in use"); 
     } 
     else 
     { 
      CommPort commPort = portIdentifier.open(this.getClass().getName(),2000); 

      if (commPort instanceof SerialPort) 
      { 
       SerialPort serialPort = (SerialPort) commPort; 
       serialPort.setSerialPortParams(57600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE); 

       InputStream in = serialPort.getInputStream(); 
       OutputStream out = serialPort.getOutputStream(); 

       (new Thread(new SerialWriter(out))).start(); 

       serialPort.addEventListener(new SerialReader(in)); 
       serialPort.notifyOnDataAvailable(true); 

      } 
      else 
      { 
       System.out.println("Error: Only serial ports are handled by this example."); 
      } 
     }  
    } 
    public static class SerialReader implements SerialPortEventListener 
    { 
     private InputStream in; 
     private byte[] buffer = new byte[1024]; 

     public SerialReader (InputStream in) 
     { 
      this.in = in; 
     } 

     public void serialEvent(SerialPortEvent arg0) { 
      int data; 

      try 
      { 
       int len = 0; 
       while ((data = in.read()) > -1) 
       { 
        if (data == '\n') { 
         break; 
        } 
        buffer[len++] = (byte) data; 
       } 
       System.out.print(new String(buffer,0,len)); 
      } 
      catch (IOException e) 
      { 
       e.printStackTrace(); 
       System.exit(-1); 
      }    
     } 
    } 
    public static class SerialWriter implements Runnable 
    { 
     OutputStream out; 

     public SerialWriter (OutputStream out) 
     { 
      this.out = out; 
     } 

     public void run() 
     { 
      try 
      {     
       int c = 0; 
       while ((c = System.in.read()) > -1) 
       { 
        this.out.write(c); 
       }     
      } 
      catch (IOException e) 
      { 
       e.printStackTrace(); 
       System.exit(-1); 
      }    
     } 
    } 
    /** 
    * @param args 
    */ 
    public static void main(String[] args) { 
     // TODO Auto-generated method stub 
     try 
     { 
      (new TwoWaySerialComm()).connect("COM4"); 
     } 
     catch (Exception e) 
     { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 
} 

我创造了在同一时间,系统将推出一个批处理文件:

java TwoWaySerialComm 

control panelscheduled tasks我创建一个任务每次系统启动时启动批处理文件。

那么这个java程序在这种情况下使用的socket port是什么?

+0

此Java程序没有使用套接字端口。 – EJP

回答

0

您的示例使用JavaComm API(gnu.io名称空间)访问串行端口(在您的示例中,您正在访问COM4)。

这与TCP/IP套接字API无关。您的示例中没有涉及TCP/IP网络。

+0

我在套接字方面不太好:是否使用套接字表示TCP/IP网络? – pheromix

+1

大部分是。套接字用于任何类型的进程间通信(unix套接字,TCP套接字...)。实际上,在Java中,您不使用“套接字”,而是使用“java.net”API(此上下文中的术语“套接字”指的是TCP套接字)。在你的例子中,你根本不使用'java.net'。您正在使用与“套接字”无关的其他通信库。 – jjmontes