Read from serial problem

Hi, i have problem with my reading serial from arduino. Output from arduino is by parts, but i need output to be together
(“Hlasi se: Centrala”). Is there any solution for this problem (sorry for my english)?

 public class Communicator implements jssc.SerialPortEventListener 
{
    //passed from main GUI
    GUI window = null;

    //for containing the ports that will be found
    private Enumeration ports = null;
    //map the port names to CommPortIdentifiers
    private HashMap portMap = new HashMap();

    //this is the object that contains the opened port
    private CommPortIdentifier selectedPortIdentifier = null;
    private gnu.io.SerialPort serialPort = null;
    SerialPort serialPort2=null;
    //input and output streams for sending and receiving data
    private InputStream input = null;
    private OutputStream output = null;

    //just a boolean flag that i use for enabling
    //and disabling buttons depending on whether the program
    //is connected to a serial port or not
    private boolean bConnected = false;

    //the timeout value for connecting with the port
    final static int TIMEOUT = 2000;

    //some ascii values for for certain things
    final static int SPACE_ASCII = 32;
    final static int DASH_ASCII = 45;
    final static int NEW_LINE_ASCII = 10;

    //a string for recording what goes on in the program
    //this string is written to the GUI
    String logText = "";

    public Communicator(GUI window)
    {
        this.window = window;
    }

    //search for all the serial ports
    //pre: none
    //post: adds all the found ports to a combo box on the GUI
    public void searchForPorts()
    {
        ports = CommPortIdentifier.getPortIdentifiers();

        while (ports.hasMoreElements())
        {
            CommPortIdentifier curPort = (CommPortIdentifier)ports.nextElement();

            //get only serial ports
            if (curPort.getPortType() == CommPortIdentifier.PORT_SERIAL)
            {
                window.cboxPorts.addItem(curPort.getName());
                portMap.put(curPort.getName(), curPort);
            }
        }
    }

    public void connect()
    {
        String selectedPort = (String)window.cboxPorts.getSelectedItem();
        selectedPortIdentifier = (CommPortIdentifier)portMap.get(selectedPort);

        CommPort commPort = null;

        try
        {
            
            serialPort2 = new SerialPort(selectedPort);
            serialPort2.openPort();
            serialPort2.setParams(9600, 8, 1, 0);
            
            //for controlling GUI elements
            setConnected(true);

            //logging
            logText = selectedPort + " opened successfully.";
            window.log.setForeground(Color.black);
            window.log.append(logText + "\n");

            //CODE ON SETTING BAUD RATE ETC OMITTED
            //XBEE PAIR ASSUMED TO HAVE SAME SETTINGS ALREADY

            //enables the controls on the GUI if a successful connection is made
            window.keybindingController.toggleControls();
        }
       
        catch (Exception e)
        {
            logText = "Failed to open " + selectedPort + "(" + e.toString() + ")";
            window.log.append(logText + "\n");
            window.log.setForeground(Color.RED);
        }
    }

    //open the input and output streams
    //pre: an open port
    //post: initialized intput and output streams for use to communicate data
    

    //starts the event listener that knows whenever data is available to be read
    //pre: an open serial port
    //post: an event listener for the serial port that knows when data is recieved
    public void initListener() throws SerialPortException, TooManyListenersException
    {
        // serialPort.addEventListener(this);
            serialPort2.addEventListener(this);
            //serialPort.notifyOnDataAvailable(true);
    }

    //disconnect the serial port
    //pre: an open serial port
    //post: clsoed serial port
    public void disconnect()
    {
        //close the serial port
        try
        {
            writeData("");

            serialPort2.removeEventListener();
            serialPort2.closePort();
            
            setConnected(false);
            window.keybindingController.toggleControls();

            logText = "Disconnected.";
            window.log.setForeground(Color.red);
            window.log.append(logText + "\n");
        }
        catch (Exception e)
        {
            logText = "Failed to close " + serialPort2.getPortName() + "(" + e.toString() + ")";
            window.log.setForeground(Color.red);
            window.log.append(logText + "\n");
        }
    }

    final public boolean getConnected()
    {
        return bConnected;
    }

    public void setConnected(boolean bConnected)
    {
        this.bConnected = bConnected;
    }

    //what happens when data is received
    //pre: serial event is triggered
    //post: processing on the data it reads
    public void serialEvent(jssc.SerialPortEvent evt) {
        
        	
        	String input;
			
            	if(evt.isRXCHAR() && evt.getEventValue() > 0) {
            		try
                    {	
                       input = serialPort2.readString();
                       System.out.println(input);
            
            		}
            		catch (Exception e)
                    {
                        logText = "Failed to read data. (" + e.toString() + ")";
                        window.log.setForeground(Color.red);
                        window.log.append(logText + "\n");
                    }
            }
            
        }
    
    public void writeData(String leftThrottle)
    {
        try
        {
        	serialPort2.writeBytes(leftThrottle.getBytes());
        	
          
        }
        catch (Exception e)
        {
            logText = "Failed to write data. (" + e.toString() + ")";
            window.log.setForeground(Color.red);
            window.log.append(logText + "\n");
        }
    }

}

Output

Hlas
i se
: Ce
ntra
la

Arduino code

#include "SIM900.h"
#include <SoftwareSerial.h>
#include "sms.h"
SMSGSM sms;

int numdata;
boolean started=false;
char smsbuffer[160];
char n[20];

char sms_position;
char phone_number[20];
char sms_text[100];
int i;


int recv = 0; // byte received on the serial port

 
void setup() {
   Serial.begin(9600);
    Serial.print("Hlasi se: Centrala");
    
  
    /*if (gsm.begin(9600)) 
    {
        Serial.println("\nstatus=READY");
        started=true;
    } 
    else 
        Serial.println("\nstatus=IDLE");
   */
}


String getStringPartByNr(String data, char separator, int index)
{
    // spliting a string and return the part nr index
    // split by separator
    
    int stringData = 0;        //variable to count data part nr 
    String dataPart = "";      //variable to hole the return text
    
    for(int i = 0; i<data.length(); i++) {    //Walk through the text one letter at a time
      
      if(data[i]==separator) {
        //Count the number of times separator character appears in the text
        stringData++;
        
      }else if(stringData==index) {
        //get the text when separator is the rignt one
        dataPart.concat(data[i]);
        
      }else if(stringData>index) {
        //return text and stop if the next separator appears - to save CPU-time
        return dataPart;
        break;
        
      }

    }
    //return text if this is the last part
    return dataPart;
} 
 
void loop()
{
  String chopString1;
  String chopString2;
  String chopString3;
  String chopString4;
 
String prikaz;
  char prikazy[255] = {0};
  // if serial port is available, read incoming bytes
   
   if (Serial.available() > 0) {
   
   
    prikaz = Serial.readString();
    
    Serial.println(prikaz);
 

  chopString1 = getStringPartByNr(prikaz, ';', 0);   //I love
  chopString2 = getStringPartByNr(prikaz, ';', 1);
  chopString3 = getStringPartByNr(prikaz, ';', 2);
  chopString4 = getStringPartByNr(prikaz, ';', 3);
  
  
  //Serial.println(prikazy);
    if (chopString1.equals("Jane")){
      
      
      int str_len = prikaz.length() + 1; 
// Prepare the character array (the buffer) 
      char buffer [str_len];;

// Copy it over 
      prikaz.toCharArray(buffer, str_len);
     
      }
 
  
  }
  
}

Please post all of your code.

econjack: Please post all of your code.

done

Rather than reinvent the wheel, take a look at the standard library function strtok(). Google it for examples on how it is used. It will solve your problem.

It is not a good idea to use Strings (capital S) in the small memory of an Arduino. It can cause memory corruption. It is much safer to use strings (small s).

The examples in Serial Input Basics are simple reliable ways to receive data. They use strings. There is also a parse example - which uses strtok()

I don’t understand what you mean by “Output from arduino is by parts, but i need output to be together” - can you provide examples to show what you mean.

…R

Thanks, I mean, that string from arduino is in PC splitted

Hl
asis
e:Ce
ntra
la

but i need

Hlasim se: Centrala

Try changing this

Serial.println(prikaz);

to

Serial.print(prikaz);

You may then want to put a Serial.println(); when the complete message has been printed.

Did you look at the examples in Serial Input Basics? They receive all the data before trying to display any of it.

...R