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 用verilog实现tanh函数和softplus函数
  • ¥15 求京东批量付款能替代天诚
  • ¥15 slaris 系统断电后,重新开机后一直自动重启
  • ¥15 51寻迹小车定点寻迹
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题