问题:1.serial中的getInputString与getOutputString的具体用法。
      2.例子:
public void WritePort(String Msg){
try{
for(int i=0;i<Msg.length();i++)
{
writer.write(Msg.charAt(i));
System.out.println("输出2");
}
}catch(IOException e)
{
System.out.println("输出");
}
}//WritePort endpublic void actionPerformed(ActionEvent e){ 
 
  JButton but = (JButton)e.getSource();  if(but == send){
     WritePort(sendA.getText());
     System.out.println("3");
       }
}
用串口线连接COM1,成功连接,然后把串口线弄成自连接。就是发送与接收内容一致,可是,就是看不到效果。接收域里没有数据。汗…………难道写法错误。

解决方案 »

  1.   

    补充:接收程序:
    class commListener implements SerialPortEventListener
    {
    public void serialEvent(SerialPortEvent se)
    {
    byte[] readBuffer = new byte[1024];
    if(se.getEventType() == SerialPortEvent.DATA_AVAILABLE)
    {
    //串口有数据
    try
    {
    while(in.available()>0)
      {
    numBytes = reader.read(readBuffer);
    System.out.println("接收");
      }
    ReadString += new String(readBuffer,0,numBytes);
    System.out.println("接收2");
    recevieA.append(ReadString+"\n");//接收
    //处理自己的字符串
    }catch(IOException e){}
    }
    }
    }//commListener end
    public void ReadPort(){
    try{
    //向SerialPort对象new commListener()添加串口事件监听器
    serialPort.addEventListener(new commListener());
    }catch(Exception e){}
    //设置端口有数据事件有效,是应用程序能监听串口是否有数据产生,
    //有就调用事件处理函数自定义的。
    //serialEvent(SerialPortEvent event)处理,件数据接收。
    serialPort.notifyOnDataAvailable(true);
    }//readPort end