0

Hi everyOne it's my first topic on this website; and sorry for my bad english i'am not a native english speaker

So i wanna save my data received from my embedded card into an array

here is my fonctions

public void serialEvent(SerialPortEvent evt) {

    if (evt.getEventType() == SerialPortEvent.DATA_AVAILABLE)
    {

       System.out.println("Data available event received");
        try
        {
            byte singleData = (byte)input.read();

            if (singleData >= Debut_caracter) 
            {     

              System.out.println("Reçu : "+singleData); 
              logText =new String(new byte[] {singleData});
              window.txtLog.append(logText);

              System.out.println("le LogText est : "+logText+"\n");    
            }   

              else { 
                window.txtLog.append(" ");    
                   }
             if (singleData < 0) 
            {

                window.txtLog.append("\n");

            }
        }

        catch (Exception e)
        {
            logText = "Failed to read data. (" + e.toString() + ")";
            window.txtLog.setForeground(Color.red);
            window.txtLog.append(logText + "\n");
        }
    }
   // waiting(1);

}

i received from my card data like this :


le LogText est : R

Data available event received Reçu : 70 le LogText est : F

Data available event received Reçu : 69 le LogText est : E

Data available event received Data available event received Data available event received Data available event received Data available event received Data available event received Data available event received Data available event received Data available event received Data available event received Data available event received Data available event received Reçu : 83 le LogText est : S

Data available event received Reçu : 84 le LogText est : T

Data available event received Reçu : 80 le LogText est : P

Data available event received Reçu : 48 le LogText est : 0

Data available event received Reçu : 49 le LogText est : 1

Data available event received Reçu : 54 le LogText est : 6

Data available event received Reçu : 54 le LogText est : 6

Data available event received Reçu : 48 le LogText est : 0

Data available event received Reçu : 50 le LogText est : 2

Data available event received Data available event received Data available event received


i just want display " STP029393"

2 Answers2

0

You will need to establish the protocol for how the bytes are coming from the device. It looks like you are doing something like that with the Debut_caracter constant. In particular, you need to establish a way of indicating the end of the string of characters you want to display (perhaps with and '\n' character?)

The code you have right now does something with each character immediately as it is received. You need to store the characters temporarily, and then print them when you want to.

The basic idea is to use a StringBuffer object that is declared outside the scope of the serialEvent() method.

  1. When you received the first character, clear the buffer and append the character to it.
  2. Each subsequent character is appended to the StringBuffer.
  3. When the last character is received, append it to the buffer and print out the buffer.
UncleO
  • 8,299
  • 21
  • 29
  • Ok thanks a lot for you answer, i'll try do it it will be my first time using a StringBuffers =) but i would know one thing : my StringBuffer.append(singleData) i ll make it inside the scope of serialEvent right ??? – user3683114 Jun 04 '14 at 08:09
  • and i would know other thing : how can i appends each subsequent caracter to the StringBuffer ? sorry for my questions :( i started java 2mounths ago – user3683114 Jun 04 '14 at 08:30
0

Ok i do something like this

public void serialEvent(SerialPortEvent evt) {

    if (evt.getEventType() == SerialPortEvent.DATA_AVAILABLE)
    {

       //System.out.println("Data available event received");
        try
        {
            byte singleData = (byte)input.read();

            if (singleData != NEW_LINE_ASCII)
            {     
                System.out.println("Reçu :" +singleData);
              logText =new String(new byte[] {singleData});
              stringBuffer.append(logText);

            }   
            if (singleData < 0) 
              { 

                System.out.println("le LogText est : "+stringBuffer+"\n");

                window.txtLog.append("\n"); 
                stringBuffer.setLength(0);
  //              waiting(1);
              }
          }

        catch (Exception e)
        {
            logText = "Failed to read data. (" + e.toString() + ")";
            window.txtLog.setForeground(Color.red);
            window.txtLog.append(logText + "\n");
        }
    }
}

it's works but i don't know if it is optimal. your opinions please ?

  • StringBuffer has an append method that takes a char, so you don't need logtext. Just do `stringBuffer.append(singleData);`. – UncleO Jun 04 '14 at 15:20