大家好!刚接触java读串口,网上才了两天资料,说下载sun的comm .jar,但又说sun的comm .jar只能读rs232,我现在想读rs485该怎么吧呀?谁给帮忙解决一下啊?谢谢

解决方案 »

  1.   

    RS232怎么读啊,先教教我读RS232吧,嘿嘿
      

  2.   

    使用RXTX提供的串口和并口通信的开源java类库,可解决你的问题!http://rxtx.qbang.org/wiki/index.php/Main_Page
      

  3.   

      RXTX是个提供串口和并口通信的开源java类库,由该项目发布的文档均遵循LGPL协议。该项目的主页位于http://users.frii.com/jarvi/rxtx/index.html。
      RXTX项目提供了Windows,Linux,Mac os X,Solaris操作系统下的兼容javax.comm串口通讯包API的实现,为其他研发人员在此类系统下研发串口应用提供了相当的方便。
      针对x86体系结构的Linux操作系统平台,RXTX的部署包括下面几个文档:
      * RXTXcomm.jar RXTX自己的javax.comm实现
      * librxtxSerial.so 由RXTXcomm.jar调用的底层串口库文档
      * librxtxParallel.so 由RXTXcomm.jar调用的底层并口库文档
      * javax.comm.properties RXTX驱动的类配置文档,内容是Driver=gnu.io.RXTXCommDriver
      三.RXTX的配置方法及部分源代码(Linux环境)
      为了使我们的程式使用RXTX作为串口通讯的底层API,需要配置他的环境。仍然以Linux系统平台为例:
      1.复制librxtxSerial.so,librxtxParallel.so到$JAVA_HOME/lib/$(ARCH)/
      2.复制RXTXcomm.jar到$JAVA_HOME/ext/,或在应用程式启动的CLASSPATH中包含RXTXcomm.jar
      3.定义驱动类后将javax.comm.properties放在应用程式的根目录下
      RXTX的使用上和sun提供的comm.jar基本相同,编程时最明显的不同是要包含的包名由javax.comm.*改成了gnu.io.*。下面是我们环境监测系统中封装的一个232串口驱动类部分源代码,使用RXTX作为串口通讯类库。
      Windows系统配置如下:
      1. 将RXTXcomm.jar放到<jre_home>\lib\ext\下
      2. 把rxtxParallel.dll,rxtxSerial.dll这两个放到你java的当前目录下(选windows下的) 
      3. 例子仍然采用原来SUN官方<ReadSimple.java>实例(change all references from 'javax.comm' to 'gnu.io' )
      4. 参考网址:http://users.frii.com/jarvi/rxtx/download.html
      =============================
      以下为RXTX的简单应用代码,仅参考。
      import java.io.*;
      import java.util.*;
      import gnu.io.*;
      public class RFIDReader implements Runnable,SerialPortEventListener{
      static CommPortIdentifier portId;
      static Enumeration portList;
      InputStream inputStream;
      SerialPort serialPort;
      Thread readThread;
      public static void main(String[] args){
      init(); 
      }
      public static void init(){
      portList=CommPortIdentifier.getPortIdentifiers();
      while(portList.hasMoreElements()){
      portId=(CommPortIdentifier)portList.nextElement();
      if(portId.getPortType()==CommPortIdentifier.PORT_SERIAL){
      if(portId.getName().equals("COM1")){
      RFIDReader reader=new RFIDReader();
      System.out.println("COM1 start!");
      }
      }
      }
      }
      public RFIDReader(){
      try{
      serialPort=(SerialPort)portId.open("Sunder",2000);
      }catch(PortInUseException e){System.out.println(e);}
      try{
      inputStream=serialPort.getInputStream();
      }catch(IOException e){System.out.println(e);}
      try{
      serialPort.addEventListener(this);
      }catch(TooManyListenersException e){System.out.println(e);}
      serialPort.notifyOnDataAvailable(true);
      try{
      serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
      }catch(UnsupportedCommOperationException e){System.out.println(e);}
      readThread=new Thread(this);
      readThread.start();
      }
      public void run(){
      try{
      Thread.sleep(20000);
      }catch(InterruptedException e){System.out.println(e);}
      }
      public void serialEvent(SerialPortEvent event){
      switch(event.getEventType()){
      case SerialPortEvent.BI:
      case SerialPortEvent.OE:
      case SerialPortEvent.FE:
      case SerialPortEvent.PE:
      case SerialPortEvent.CD:
      case SerialPortEvent.CTS:
      case SerialPortEvent.DSR:
      case SerialPortEvent.RI:
      case SerialPortEvent.OUTPUT_BUFFER_EMPTY: break;
      case SerialPortEvent.DATA_AVAILABLE:
      byte[] readBuffer=new byte[20];
      try{
      while(inputStream.available()>0){
      int numBytes=inputStream.read(readBuffer);
      }
      System.out.println(new String(readBuffer));
      }catch(IOException e){System.out.println(e);}
      break;
      }
      }
      }
      =============
      开源万岁~!