[Rxtx] Data loss in high ratio while reading event based with SerialPortEvent.DATA_AVAILABLE
Beyefendi
beyefendipass at yahoo.com
Tue Mar 29 02:38:38 MDT 2011
Hi, rxtx experts,
I read a serial connected device. As you know, it produce datas in a specified time intervals. In my configration, length of produced data by the device is 10 characters. Such as; it sends one packet (its length 10 characters) to com port by every 1 seconds. I use device's simulator, it reads data correctly and also never misses, namely simulator shows 10 length of new data in every 1 seconds for this example.
On the other hand, i developed an event based java program that uses rxtx 2.1.7, on os winXP, java 1.6 but i cannot get the correct number of packets. What i am missing?
I've been testing with several different configuratin of device, but I always miss packets with different ratios. My last test shows this statistics;Device produces 900 packetsSimulator read 900 packets, this below code that i implemented read 458. Nearly i lost %50 percent?
I can provide more detail to you who concerned with this problem.I want to take your advice.
// I simplified the code to understand easilypublic class SerialOperations implements SerialPortEventListener {
CommPortIdentifier port; SerialPort serialPort; InputStream is; BufferedReader br; StringBuffer packet = null; StringBuffer lackingPacket = new StringBuffer(""); public boolean openSerialPort() { try {
port = CommPortIdentifier.getPortIdentifier("COM3"); serialPort = (SerialPort) port.open("Berat", 2000); serialPort.setSerialPortParams(48000, 8, //SerialPort.DATABITS_8, 1, //SerialPort.STOPBITS_1, 0); //SerialPort.PARITY_NONE
// Run event based serialPort.addEventListener(this);
serialPort.notifyOnDataAvailable(true);
serialPort.setDTR(true); serialPort.setRTS(true); serialPort.setFlowControlMode(0); // SerialPort.FLOWCONTROL_NONE
is = serialPort.getInputStream(); br = new BufferedReader(new InputStreamReader(is),10); return true; } catch (Exception e) { System.out.println(e.getMessage()); } return false; }
@Override public void serialEvent(SerialPortEvent e) { if (e.getEventType() == SerialPortEvent.DATA_AVAILABLE) { readData(); } }
public void readData() { try { //byte[] buffer = new byte[is.available()]; String buffer; while (is.available() > 0) {
//is.read(buffer); buffer = br.readLine(); packet = new StringBuffer(new String(buffer).trim().replaceAll("\n", "")); StringBuffer temp = new StringBuffer("Data: " + packet + " length: " + packet.length()); System.out.println(temp);
if (packet.length() == 10) { // packet is fetched in one read
// .. controls ... DBOperations.parsePacket(packet.toString()); } else if (packet.length() < 10 && packet.length() > 0){ // for is.read(buffer), it cannot read until \n // .. controls ... lackingPacket.append(packet); }else{ System.out.println("Packet is too long or Packet is not correct format"); }
} }catch (IOException e) { System.out.println("Error in readData(): " + e.getMessage()); } } }
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.qbang.org/pipermail/rxtx/attachments/20110329/abe36124/attachment-0410.htm>
More information about the Rxtx
mailing list