分好少啊~以前写的,自己慢慢研究import java.io.*;
import javax.comm.*;
import java.util.*;public class SerialCommunicate
{
     public static void main(String[] agrs)
{
            String []parameters=null;    try
   {
               parameters=getInitializationParameters();
   }
   catch(Exception e)
   {
      System.out.println(e);
      return;
   }    Serial s=null;

            try
   {
       s=new Serial(parameters[0],Integer.parseInt(parameters[1]));
   }
   catch(Exception e)
   {
                System.out.println(e);
       return;
   }

   ArrayList contentArr=new ArrayList();    new Thread(new Read(s)).start(); //启动读串口数据的线程    new Thread(new Write(s,contentArr)).start();  //启动写串口数据的线程
        
   String con=null;            BufferedReader read=new BufferedReader(new InputStreamReader(System.in));            try
  {
      while(!((con=read.readLine()).equals("<end>")))
      {
synchronized(contentArr)
{
                      contentArr.add(con);  //在主线程中将要发送的数据先写到ArrayList中
}
            
       }
   }
   catch(Exception e)
   {
       System.out.println(e);
   }            s.closeSerialPort();
   System.exit(0);
        } private static String[] getInitializationParameters() throws Exception  //得到初始化串口的参数
{
    BufferedReader read=new BufferedReader(new InputStreamReader(System.in));     System.out.print("串口名称(COM1/COM2):   ");     String portName=read.readLine();     System.out.print("设置波特率:   ");     String baudRate=read.readLine();     return new String[]{portName,baudRate};
}};class Serial  //串口类
{
private String portName=null;
private int baudRate=9600;
private BufferedReader in=null;
private OutputStream out=null;
private String appname="SerialCommunication";   //给串口起名字
private int timeout=5000;                       //设置延时
private SerialPort serialPort =null;
public Serial(String portName,int baudRate) 
{
   this.portName=portName;
   this.baudRate=baudRate;
   CommPortIdentifier portIdentifier=null;
   try
   {
               portIdentifier=CommPortIdentifier.getPortIdentifier(portName);
   }
   catch(Exception e)
   {
       System.out.println("get error when method getPortIdentifier was called");
   }
   try
   {
                serialPort = (SerialPort)portIdentifier.open(appname, timeout);
   }
   catch(Exception e)
   {
       System.out.println("get error when method open was called");
   }
   try
   {
      in=new BufferedReader(new InputStreamReader(serialPort.getInputStream()));
       out=serialPort.getOutputStream();
   }
   catch(Exception e)
   {
               System.out.println("get error when  get stream");
   }
   try
   {
               serialPort.setSerialPortParams(baudRate,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);  //设置串口的波特率,数据长度,数据的停止位长度,数据效验类型 , 在此例中设置数据长度为8位,停止位长度为一位,不进行数据效验
   }
   catch(Exception e)
   {
               System.out.println("get error when method setSerialPortParams was called");
   } } public BufferedReader getReader()
{
return in;
} public OutputStream getOutputStream()
{
return out;
} public void closeSerialPort()
{
        serialPort.close();
}
};
class Read implements Runnable    //从串口读取数据
{
private Serial s=null; public Read(Serial s)
{
    this.s=s;

} public void run()
{
   BufferedReader read=s.getReader();
            while(true)
   {
String con=null;
    
try
{
    con=read.readLine(); //读取对方数据
}
catch(Exception e)
{
    System.out.println(e);
}
     System.out.println("对方数据: "+con);  //打印对方发送的数据
    }
}
};class Write implements Runnable  //往串口写数据
{
    private Serial s=null;
    private ArrayList contentArr=null; public Write(Serial s,ArrayList contentArr)
{
    this.s=s;
             this.contentArr=contentArr;
} public void run()
{
    OutputStream out=s.getOutputStream();
        
    while(true)
    {


                if((contentArr.size()!=0))
       {
                     System.out.println(contentArr.size());

   String con=null;
   synchronized(contentArr)
   {
     con=(String)contentArr.get(0); //每次只取第一个数据
System.out.println(con);

   }               
   try
   {
                  out.write((con+System.getProperty("line.separator")).getBytes());  //将数据写入串口
   }
   catch(Exception e)
   {
                         System.out.println("here"+e);
   }
    
        
                
   synchronized(contentArr)
   {
                        contentArr.remove(0);  //将已经写入了串口的数据移去
   }
            

}

     }
}};

解决方案 »

  1.   

    大哥,谢谢了。我是最近才知道这个网站的,我也不知道怎么设分数。请多包涵。
    请问javax.comm这个包为什么我的JBuilder找不到啊?我该怎么操作?谢了。
      

  2.   

    我用你给的程序试了一下,JBuilder报了下面的异常,请问怎么回事呀?是不是我的javax.comm包有问题呢?串口名称(COM1/COM2):   COM1
    设置波特率:   8000
    javax.comm:  Can't find javax.comm.properties!java.io.IOException: javax.comm: platform driver class name = null
                         (Check 'driver' property in javax.comm.properties)
    at javax.comm.CommPortIdentifier.loadDriver(CommPortIdentifier.java:243) at javax.comm.CommPortIdentifier.<clinit>(CommPortIdentifier.java:109) at serial.Serial.<init>(SerialCommunicate.java:70) at serial.SerialCommunicate.main(SerialCommunicate.java:19)java.lang.UnsatisfiedLinkError: isSessionActive at com.sun.comm.SunrayInfo.isSessionActive(Native Method) at com.sun.comm.Portmapping.registerCommPorts(Portmapping.java:155) at com.sun.comm.Portmapping.refreshPortDatabase(Portmapping.java:100) at javax.comm.CommPortIdentifier.<clinit>(CommPortIdentifier.java:138) at serial.Serial.<init>(SerialCommunicate.java:70) at serial.SerialCommunicate.main(SerialCommunicate.java:19)