Пример #1
0
  private void write(byte[] data) {
    try {
      if (m_out == null) {
        m_PortIdPrinter =
            CommPortIdentifier.getPortIdentifier(
                m_sPortScale); // Tomamos el puerto
        m_CommPortPrinter =
            (SerialPort) m_PortIdPrinter.open("PORTID", 2000); // Abrimos el puerto

        m_out = m_CommPortPrinter.getOutputStream(); // Tomamos el chorro de escritura
        m_in = m_CommPortPrinter.getInputStream();

        m_CommPortPrinter.addEventListener(this);
        m_CommPortPrinter.notifyOnDataAvailable(true);

        m_CommPortPrinter.setSerialPortParams(
            4800,
            SerialPort.DATABITS_8,
            SerialPort.STOPBITS_1,
            SerialPort.PARITY_ODD); // Configuramos el puerto
      }
      m_out.write(data);
    } catch (NoSuchPortException e) {
      e.printStackTrace();
    } catch (PortInUseException e) {
      e.printStackTrace();
    } catch (UnsupportedCommOperationException e) {
      e.printStackTrace();
    } catch (TooManyListenersException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
  /** 通过串口向单片机写入数据 */
  public boolean portWrite() {
    boolean notFinished = true;
    try {
      // get port ID
      CommPortIdentifier portId = CommPortIdentifier.getPortIdentifier(this.portName);

      // open port(port holder, timeout),打开端口(串口所有者,超时等待时间:ms)
      try {
        this.sPort = (SerialPort) portId.open("可爱的皮卡丘", this.timeout);

        // set port(BaudRate,Databits,Stopbits,Parity),设置串口(波特率,数据位,停止位,校验位)
        try {
          this.sPort.setSerialPortParams(
              9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
        } catch (UnsupportedCommOperationException e) {
          e.printStackTrace();
        }

        // 写入数据
        OutputStream OS = null;
        try {
          OS = new BufferedOutputStream(sPort.getOutputStream());
        } catch (IOException e) {
          e.printStackTrace();
        }
        // 选择操作
        System.out.println("选择一下操作: 静止按1; 前进按2; 后退按3; 结束操作按4.");
        int command = s.nextInt();

        switch (command) {
          case 1:
            try {
              OS.write(this.stop);
            } catch (IOException e) {
              e.printStackTrace();
            }
            break;
          case 2:
            try {
              OS.write(this.forward);
            } catch (IOException e) {
              e.printStackTrace();
            }
            break;
          case 3:
            try {
              OS.write(this.backward);
            } catch (IOException e) {
              e.printStackTrace();
            }
            break;
          case 4:
            notFinished = false;
            break;
        }

        try {
          OS.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
        this.sPort.close();

      } catch (PortInUseException e) {
        e.printStackTrace();
      }
    } catch (NoSuchPortException e) {
      e.printStackTrace();
    }
    return notFinished;
  }