ito_oti 2011-03-21 15:01
浏览 184
已采纳

求解决方案 - 涉及到串口设备的问题

有一个要完成的任务:

用Swing开发一个窗口在linux系统上运行,linux系统通过串口与一个IC读写器连接.在IC读写器上插入IC卡时,IC卡的信息能在窗口上显示,同时通过这个窗口也能将信息写到IC卡上.

请问谁有过这方面的经验,告诉我应该从哪里着手?

如果需要,我会提供更具体的情况说明.

  • 写回答

1条回答 默认 最新

  • hellostory 2011-03-21 17:11
    关注

    使用SUN公司有提供一个java的串口开发包[color=red]comm.jar[/color]下面提供一段读写串口的代码,可以参考下:
    [code="java"]
    package serial;
    import java.io.*;
    import javax.comm.*;
    /**
    *

    • This bean provides some basic functions to implement full dulplex
    • information exchange through the srial port.
      *
      */
      public class SerialBean{

      /* PortName="COM1","COM2" etc. */
      String PortName;

      CommPortIdentifier portId;
      SerialPort serialPort;
      static OutputStream out;
      static InputStream in;
      SerialBuffer serialBuffer;
      ReadSerialThread readSerialThread;
      /**

      • Constructor
      • @param PortID the ID of the serial to be used. 1 for COM1,
      • 2 for COM2, etc. */ public SerialBean(String PortName){ this.PortName = PortName; }

      /**

      • This function initialize the serial port for communication. It startss a
      • thread which consistently monitors the serial port. Any signal capturred
      • from the serial port is stored into a buffer area.
        *
        */
        public int Initialize(){
        final int InitSuccess = 1;
        final int InitFail = -1;
        try{
        portId = CommPortIdentifier.getPortIdentifier(PortName);
        try{
        serialPort = (SerialPort)

            //timeout = 2000mis
            portId.open("Serial_Communication", 2000);
        } catch (PortInUseException e){
                return InitFail;
        }
        
        //Use InputStream in to read from the serial port, and OutputStream
        //out to write to the serial port.
        try{
            in  = serialPort.getInputStream();
            out = serialPort.getOutputStream();
        } catch (IOException e){
            return InitFail;
        }
        
        //Initialize the communication parameters to 9600, 8, 1, none.
        try{
            serialPort.setSerialPortParams(9600,
                        SerialPort.DATABITS_8,
                        SerialPort.STOPBITS_1,
                        SerialPort.PARITY_NONE);
        } catch (UnsupportedCommOperationException e){
            return InitFail;
        }
        

        } catch (NoSuchPortException e){
        return InitFail;
        }

        // when successfully open the serial port, create a new serial buffer,
        // then create a thread that consistently accepts incoming signals from
        // the serial port. Incoming signals are stored in the serial buffer.
        serialBuffer = new SerialBuffer();
        readSerialThread = new ReadSerialThread(serialBuffer, in);
        readSerialThread.start();

        // return success information
        return InitSuccess;
        }

      /**
      *

      • This function returns a string with a certain length from the incomin
      • messages. *
      • @param Length The length of the string to be returned. * */ public String ReadPort(int Length){ String Msg; Msg = serialBuffer.GetMsg(Length); return Msg; }

      /**
      *

      • This function sends a message through the serial port. *
      • @param Msg The string to be sent. * */ public void WritePort(String Msg){ try{ for (int i = 0; i < Msg.length(); i++) out.write(Msg.charAt(i)); } catch (IOException e) {} }

      /**
      *

      • This function closes the serial port in use. * / public void ClosePort(){ /如何正常关闭线程?*/ //readSerial.stop(); serialPort.close(); } }

    [/code]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办