很急,请大家帮忙!
我做了一个SerialComm类,里面有send()和read()方法.send()方法可以实现向串口发数据,但是read()不行.我不知道要怎么去写这个方法.是用void还是String,或者是别的.这个我不是很清楚,这个方法是从串口端读数据的.请问要怎么去定义.
public class SerialComm 
{
static Enumeration        portList;
    static CommPortIdentifier portId;
    static String            messageString;
    static SerialPort        serialPort;
    static OutputStream       outputStream;
    static InputStream        inputStream;
    static boolean            outputBufferEmptyFlag = false;
    SerialPortEvent event;
    
    Thread readThread = null;
    
    
    public SerialComm()
    {
     portList = CommPortIdentifier.getPortIdentifiers();
boolean portFound = false;
     String defaultPort = "COM1";     while (portList.hasMoreElements()) 
     {
         portId = (CommPortIdentifier) portList.nextElement();         if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) 
         {
     if (portId.getName().equals(defaultPort)) 
     {
     portFound = true;
         try 
         {
          serialPort = (SerialPort) portId.open("SerialComm", 2000);
          try 
          {
serialPort.setSerialPortParams(9600,
           SerialPort.DATABITS_8,
   SerialPort.STOPBITS_1,
   SerialPort.PARITY_NONE);   //串口初始化~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
} catch (UnsupportedCommOperationException e) 
{
// TODO 自动生成 catch 块
e.printStackTrace();
}
         } 
         catch (PortInUseException e) {}      
     }
         }
     }
    }
    
/*****************向串口发消息**************************************************************************/
public String send(String message)           
{
try {
outputStream = serialPort.getOutputStream();
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
serialPort.notifyOnOutputEmpty(true);
try {
outputStream.write(message.getBytes());
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
return message;
}
/******************从串口读消息*****************************************************************************************/
public String read(String message)
{
//String message;
try {
inputStream = serialPort.getInputStream();
//serialPort.addEventListener(this);
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
//serialPort.addEventListener(this);
serialPort.notifyOnDataAvailable(true);   //数据可用!
readThread = new Thread();
this.readThread.start();

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:
byte[] readBuffer = new byte[20];
    try 
    {
     while (inputStream.available() > 0) 
     {
     int numBytes = inputStream.read(readBuffer);
     } 
     //message = new String(readBuffer); 
    }catch (IOException e) {}
    break;
   
}

return message;
}
/*************************************************************************************************************/
public void run()
{
try {
Thread.sleep(20000);
} catch (InterruptedException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}

/********************************************************************************************************************/
public void serialEvent(SerialPortEvent event)
{



/*******************关闭串口******************************************************************************************/
public void close()
{
serialPort.close();
}
}

解决方案 »

  1.   

    你用的是javax.comm的串口通讯包吧,既然可以send可见连接已经建立了,之所以read没成功,可能主要是以下原因:
    通讯协议,串口通讯一般都有协议,如xmoden,ymoden,没有发送制定字符,对方是不会对你进行应答的,你不能指望串口一连上对方就会向你发送信息,而且协议中有ack机制,即对方向你发送了信息,但是在规定时间内你未作出应答,对方将视为中断通信,而不继续发送消息,还有就是
    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:
    byte[] readBuffer = new byte[20];
        try 
        {
         while (inputStream.available() > 0) 
         {
         int numBytes = inputStream.read(readBuffer);
         } 
         //message = new String(readBuffer); 
        }catch (IOException e) {}
        break;
       
    } } 
    在这里写read的代码比较合适,作为事件监听串口中可能的信息到来。