Java串口未知ubuntu中的输出符号

时间:2013-08-03 07:48:31

标签: java ubuntu serial-port rxtx

您好我使用的是Ubuntu最新版本12.X.我想从我的java代码中读取串行输出。问题是我以:�0300(iso 8859 1)代替:10300。 顺便说一句,我使用最新的RXTX2.1和JDK1.7。 谁能让我知道问题是什么?

这是我的代码:

import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.TooManyListenersException;
import javax.comm.*;

public class SimpleRead implements Runnable, SerialPortEventListener {
    static CommPortIdentifier portId;
    static Enumeration portList;

    private Weight weight;


    InputStream inputStream;
    SerialPort serialPort;
    Thread readThread;

    public Weight getWeight() {
        return weight;
    }

    public void setWeight(Weight weight) {
        this.weight = weight;
    }

    public static void main(String[] args) {

               // if (portId.getName().equals("/dev/term/a")) {
                    //SimpleRead reader = new SimpleRead();
         new SimpleRead(new Weight());
    }

    public SimpleRead(Weight weight) {

portList = CommPortIdentifier.getPortIdentifiers();

        while (portList.hasMoreElements()) {
            portId = (CommPortIdentifier) portList.nextElement();
            if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
                 if (portId.getName().equals("COM1")) {
                     System.out.println(portId.getName());
try {
            serialPort = (SerialPort) portId.open("SimpleReadApp", 2000);
        } catch (PortInUseException e) {
        }
        try {
            inputStream = serialPort.getInputStream();
        } catch (IOException e) {}
    try {
            serialPort.addEventListener(this);
    } catch (TooManyListenersException e) {}
        serialPort.notifyOnDataAvailable(true);
        try {
            serialPort.setSerialPortParams(9600,
                SerialPort.DATABITS_7,
                SerialPort.STOPBITS_1,
                SerialPort.PARITY_NONE);
        } catch (UnsupportedCommOperationException e) {}
        readThread = new Thread(this);
        readThread.start();
}}}    }

    public void run() {
        try {
            Thread.sleep(20000);
        } catch (InterruptedException e) {}
    }
int i=0;String a="";


public int getPopularElement(int[] a)
{
  int count = 1, tempCount;
  int popular = a[0];
  int temp = 0;
  for (int i = 0; i < (a.length - 1); i++)
  {
    temp = a[i];
    tempCount = 0;
    for (int j = 1; j < a.length; j++)
    {
      if (temp == a[j])
        tempCount++;
    }
    if (tempCount > count)
    {
      popular = temp;
      count = tempCount;
    }
  }
  return popular;
}

int arr[]= new int[10];
int arr1[]= new int[20];
int arr2[]= new int[10]; int j=0;

    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:
try{
            String line = new String();
    byte[] nextByte = {-1};
    byte[] nextByte1 =new byte[6];
    while (true) {

        nextByte[0] = (byte)inputStream.read();
        //logger.debug("int read: " + nextByte[0]);
        if (nextByte[0] == (byte)-1) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            continue;
        }
        //logger.debug("byte read: " + nextByte[0]);
if(new String(nextByte).equals(":")){
j=6;
}
//System.out.print('A');
if(j !=0 ){
       line = line + new String(nextByte);
       //j--;
}

        if (nextByte[0] == (byte)13) {
            // 13 is carriage return in ASCII
            if(line.contains(":")){
                String [] line1=line.split("\r");
String [] line2=line1[line1.length-1].split(":");
            String a="";
            try{
if(line2.length>1){
               System.out.println(line2[line2.length-1]);  
}
            }catch(Exception e){

           System.out.println(e);
            }
if(a.equals(""))
{a="0";
}
           }
        }
    }
 } catch (IOException e) {
System.out.print(e);
}
 break;
        }
    }
}

0 个答案:

没有答案