去 java.sun.com  找一下 java comm api

解决方案 »

  1.   

    好象挺难,不过用JNI可以的。
      

  2.   

    =======================================================================
    使用 Java 读取条形码和维护库存数据库
    http://www-900.cn.ibm.com/developerWorks/cn/java/j-i-barcd/index.shtml
    =======================================================================
    多功能串口设备服务器 
    http://www-900.cn.ibm.com/developerWorks/cn/java/multi-port/index2.shtml
    =======================================================================
      

  3.   

    我用javacomm试了一下,但是不成功,没有数据读出来,不知道为什么.
      

  4.   

    在执行的时候出现以下异常.
    java.lang.NullPointerException at read.Frame1.jButton1_actionPerformed(Frame1.java:99) at read.Frame1$Frame1_jButton1_actionAdapter.actionPerformed(Frame1.java:174) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1764) at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(AbstractButton.java:1817) at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:419) at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:257) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:245) at java.awt.Component.processMouseEvent(Component.java:5134) at java.awt.Component.processEvent(Component.java:4931) at java.awt.Container.processEvent(Container.java:1566) at java.awt.Component.dispatchEventImpl(Component.java:3639) at java.awt.Container.dispatchEventImpl(Container.java:1623) at java.awt.Component.dispatchEvent(Component.java:3480) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:3450) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3165) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3095) at java.awt.Container.dispatchEventImpl(Container.java:1609) at java.awt.Window.dispatchEventImpl(Window.java:1590) at java.awt.Component.dispatchEvent(Component.java:3480) at java.awt.EventQueue.dispatchEvent(EventQueue.java:450) at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:197) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:144) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:136) at java.awt.EventDispatchThread.run(EventDispatchThread.java:99)
      

  5.   

    补充一下,执行到以下语句出现异常
    serialPort.notifyOnDataAvailable(true);
      

  6.   

    是不是你的配置错了啊
     1. 把 comm.jar 檔案複製到 C:\jdk\jre\lib\ext 與 C:\Program Files\JavaSoft\JRE\1.3.0_1\lib\ext 這兩個目錄下。
        2. 把 javax.comm.properties 檔案複製到 C:\jdk\jre\lib 與 C:\Program Files\JavaSoft\JRE\1.3.0_1\lib 這兩個目錄下。
        3. 把 win32com.dll 複製到 C:\jdk\bin 目錄下。
        4. 不用改變 CLASSPATH
      

  7.   

    继承接口就可以了,implements java.io.Serializable
      

  8.   

    我运行了一下javacom自带的BlackBox, 执行结果No serial ports found!在javacom的安装文档中我看到有一下一段话The javax.comm.properties file must be installed. If it is not, no ports will be found by the system. 这个javax.comm.properties 不是只要放到jdk\lib下面就行了吗.还怎么安装.
      

  9.   

    或许可能是因为javacomm20版本太低,好象这个版本只能在jdk1.2以下版本运行,不知道是不是这个原因,请高人指点, 多谢!
      

  10.   

    按以下方法绝对正确,我已经作成功了。
    win32com.dll放置在C:\Program Files\JBuilder9\jdk1.4\bin目录下
    comm.jar放置在C:\Program Files\JBuilder9\jdk1.4\lib目录下
    javax.comm.properties放置在C:\Program Files\JBuilder9\jdk1.4\jre\lib目录下
    在系统中加载comm.jar类,即在配置系统JDK中加载
    串口发送程序如下:
    package testwrite;import java.io.*;
    import java.util.*;
    import javax.comm.*;public class SimpleWrite{
        static Enumeration portList;
        static CommPortIdentifier portId;
        static SerialPort serialPort;
        static BufferedWriter Output;    public static void main(String[] args)
        {
            portList=CommPortIdentifier.getPortIdentifiers();        while(portList.hasMoreElements())
            {
                portId=(CommPortIdentifier)portList.nextElement();
                if(portId.getPortType()==CommPortIdentifier.PORT_SERIAL)
                {
                    // if (portId.getName().equals("COM1")) {
                    if(portId.getName().equalsIgnoreCase("COM1"))
                    {
                        try
                        {
                            serialPort=(SerialPort)portId.open("SimpleWriteApp",30000);
                            serialPort.setOutputBufferSize(1024);
                            Output=new BufferedWriter(new OutputStreamWriter(serialPort.getOutputStream()));
                            serialPort.setSerialPortParams(9600,serialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
                            sendMessage message=new sendMessage(Output);
                            message.start();
                        }
                        catch(PortInUseException e)
                        {}
                        catch(IOException e)
                        {}
                        catch(UnsupportedCommOperationException e)
                        {}
                        break;
                        //     outputStream.write(messageString.getBytes());
                    }
                }
            }
        }
    }//------------------------------------------------------------------------------
    class sendMessage extends Thread{
        private BufferedWriter Output;
        private int intCount=0;
        private String strMessage=" Hello,广告信息管理系统2.0源程序!";
        public sendMessage(BufferedWriter outputStream) throws IOException
        {
            this.Output=outputStream;
        }    public void run()
        {
            while(true)
            {
                intCount++;            try
                {
                    Output.write((Integer.toString(intCount)+strMessage+"\n\r"));
                    Output.flush();
                }
                catch(IOException ex)
                {
                }            try
                {
                    this.sleep(4000);
                }
                catch(InterruptedException ex1)
                {
                }        }
        }
    }
    串口读程序如下:
    package testwrite;import java.io.*;
    import java.util.*;
    import javax.comm.*;public class SimpleRead
        implements SerialPortEventListener{
        static CommPortIdentifier portId;
        static Enumeration portList;    static BufferedReader inputStream;
        SerialPort serialPort;
        Thread readThread;    public static void main(String[] args)
        {
            portList=CommPortIdentifier.getPortIdentifiers();        while(portList.hasMoreElements())
            {
                portId=(CommPortIdentifier)portList.nextElement();
                if(portId.getPortType()==CommPortIdentifier.PORT_SERIAL)
                {
                    if(portId.getName().equalsIgnoreCase("COM2"))
                    {
                        SimpleRead reader=new SimpleRead();
                        break;
                    }
                }
            }
        }    public SimpleRead()
        {
            try
            {
                serialPort=(SerialPort)portId.open("SimpleReadApp",30000);
                serialPort.setInputBufferSize(1024);
                inputStream=new BufferedReader(new InputStreamReader(serialPort.getInputStream()));
                serialPort.addEventListener(this);
                serialPort.notifyOnDataAvailable(true);
                serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);        }
            catch(PortInUseException e)
            {}
            catch(IOException e)
            {}
            catch(TooManyListenersException e)
            {}
            catch(UnsupportedCommOperationException e)
            {}
        }    public void serialEvent(SerialPortEvent event)
        {
            String inputBuffer="";
            int newData=0;
            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:
                    while(newData!=-1)
                    {
                        try
                        {
                            newData=inputStream.read();
                            if(newData==-1)
                            {
                                break;
                            }
                            if('\r'==(char)newData)
                            {
                                inputBuffer+="\n";
                                System.out.println(inputBuffer.toString().trim());
                                inputBuffer="";
                            }
                            else
                            {
                                inputBuffer+=((char)newData);
                            }
                        }
                        catch(IOException ex)
                        {
                            System.err.println(ex);
                            return;
                        }
                    }
                    break;
            }
        }
    }
    詃给多少分,你看作办
      

  11.   

    爽,
    看到虎子兄的提醒,我把javax.comm.properties 放到jb的jdk下,终于搞定了.
    但是还是不知道为什么我开始放到单独安装的jdk下面就不行.
    不管怎样,终究还是弄出来了.
    谢谢,准备接分吧.
      

  12.   

    在工程属性中重新引用你的JDK就可以的