Note that there are some explanatory texts on larger screens.

plurals
  1. POJava: read serial port (baudrate 921600)
    primarykey
    data
    text
    <p>I'm developing a Java software which has to be able to read some data sent by a device through a serial COM port, the speed of the communication is very important for me and so the baudrate is set to 921600. In the beginning everything works fine (and so it reads the correct data that the device sends through the serial port) but after a while the software starts to read wrong data. It seems like the software is too slow and it actually loses some of the data while they are coming into the input buffer (maybe when the input buffer is fully filled for the first time). In order to speed up the reading operation I'm currently using a reading approach in which the software deals with as much bytes as possible per reading. I've also tried to make the input buffer size bigger (by using the serialPort.setInputBufferSize(byte) method but it didn't solve my problem) So has anyone ever done a fast serial communication with java? Am I missing something? Why everything works fine for a while and then it stops to work properly?</p> <p>This is my code for the reading part (even driven), please skip the part of the data conversion, that's just because every data is actually composed by 2 bytes and so I also have to compose them before the write them into a txt file.</p> <pre><code>/** * * @param evt */ @Override public void serialEvent(SerialPortEvent evt) { switch(evt.getEventType()) { case SerialPortEvent.OUTPUT_BUFFER_EMPTY: System.out.println("THE OUTPUT BUFFER IS EMPTY"); break; case SerialPortEvent.DATA_AVAILABLE: try { while(SerialPortEvent.DATA_AVAILABLE == 1) { num_bytes = input.available(); array = new byte[num_bytes]; bytes_read = input.read(array, 0, num_bytes); dato = new short[bytes_read]; datoc = new int[bytes_read/2]; datos = new String[bytes_read/2]; for(j=0;j&lt;bytes_read;j++){ dato[j] = (short) (((byte) array[j]) &amp; 0xff); } k = 0; for(j=0;j&lt;(bytes_read/2);j++){ datoc[j] = dato[k]; datoc[j] = (datoc[j]&lt;&lt;8) + dato[k+1]; datoc[j] = datoc[j] &amp; 0xffffffff; k = k + 2; } for(j=0;j&lt;(bytes_read/2);j++){ System.out.println(datoc[j]); datos[j] = Integer.toString(datoc[j]); output1.write(datos[j] + " "); } } } catch(IOException ex) { logText = "Failed to read data. (" + ex.toString() + ")"; System.out.println(logText); } break; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload