关于java串口通信的问题 java 发送数据,另一端,有时会正常接收到 有时只会接收前8个字符,,经常数据接收不完整,请问这是怎么回事啊 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 发送数据 public void WritePortData(byte [] aa){ try{ outputStream = serialPort.getOutputStream(); outputStream.write(aa); outputStream.flush(); }catch(Exception ee){ return ; } } 串口连接:public void connectPort() { if (this.serialPort != null) { this.serialPort.close(); } try { portId = CommPortIdentifier.getPortIdentifier(this.portName); serialPort = (SerialPort) portId.open("ReadComm", 2000); this.p3f2.setText(this.portName + " Success\n"); } catch (Exception ee) { JOptionPane.showMessageDialog(this, ee.toString(), "Message", JOptionPane.INFORMATION_MESSAGE); this.p3f2.setText(this.portName + " failure\n"); } try { serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); } catch (Exception ee) { JOptionPane.showMessageDialog(this, "Please connect serial port first!", "Message", JOptionPane.INFORMATION_MESSAGE); } try { this.inputStream = serialPort.getInputStream(); this.serialPort.addEventListener(this); } catch (Exception ee) {} serialPort.notifyOnDataAvailable(true); }接收数据代码: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[21]; try{ while(inputStream.available()>0){ int numBytes=inputStream.read(readBuffer); } String comStr = new String(readBuffer).trim(); } } catch (Exception e) { } break; } } 系统异常处理 求一套用于练习的java编程习题 内部类有错误 @@@@@@@@@@@@@@@请教关于jbuilder2005 如何获取本机所设置的子网掩码和网关?(不赞同从ipconfig解析的方法) 用java mail发送邮件,出现以下错误,给了源码,大家看看~~~,马上结帐 有关用JAVA写的程序的界面美化问题 一个等差数列输出,程序该怎么编? 如何在Win98下设置skd1.4的环境变量,请高手执教!在线. eclipse改包名后为什么包有个加号? ECLIPSE单步调试时,如何设置让他不跑进java自己的方法里面。 Eclipse中启动weblogic报错
发送数据 public void WritePortData(byte [] aa){
try{
outputStream = serialPort.getOutputStream();
outputStream.write(aa);
outputStream.flush(); }catch(Exception ee){
return ;
}
}
串口连接:public void connectPort() {
if (this.serialPort != null) {
this.serialPort.close();
}
try {
portId = CommPortIdentifier.getPortIdentifier(this.portName);
serialPort = (SerialPort) portId.open("ReadComm", 2000);
this.p3f2.setText(this.portName + " Success\n"); } catch (Exception ee) { JOptionPane.showMessageDialog(this, ee.toString(), "Message",
JOptionPane.INFORMATION_MESSAGE);
this.p3f2.setText(this.portName + " failure\n");
}
try {
serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8,
SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); } catch (Exception ee) { JOptionPane.showMessageDialog(this,
"Please connect serial port first!", "Message",
JOptionPane.INFORMATION_MESSAGE);
}
try { this.inputStream = serialPort.getInputStream();
this.serialPort.addEventListener(this); } catch (Exception ee) {}
serialPort.notifyOnDataAvailable(true);
}
接收数据代码:
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[21];
try{
while(inputStream.available()>0){
int numBytes=inputStream.read(readBuffer);
}
String comStr = new String(readBuffer).trim();
}
} catch (Exception e) { }
break;
}
}