lundi 13 février 2017

Splitting a String from a String Buffer

Vote count: 0

So I have an Arduino connected to my COM port and it's reading the serial port waiting for a request string. In one of the fields of that string is the code '01'. When the Arduino reads the 01 it sends trough the serial port a more complete version of the original request string, this new string contains values I need to insert into a databse. The sending and receiving is done trough Netbeans. The part where I'm stuck is extracting the values from the string and storing them into variables for later sending over HTTP GET requests.

Here is the response string:0001,0004,dd/mm/yy hh:mm:ss,01,value01,value02;

How can I extract/split the value01 and value02 ? Here is my code in NetBeans to read the Serial Port:

 void connect ( String portName ) throws Exception
{
    CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
    if ( portIdentifier.isCurrentlyOwned() )
    {
        System.out.println("Error: Port is currently in use");
    }
    else
    {
        CommPort commPort = portIdentifier.open(this.getClass().getName(),2000);

        if ( commPort instanceof SerialPort )
        {
            SerialPort serialPort = (SerialPort) commPort;
            serialPort.setSerialPortParams(115200,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);

            InputStream in = serialPort.getInputStream();
            OutputStream out = serialPort.getOutputStream();

            (new Thread(new SerialReader(in))).start();
            (new Thread(new SerialWriter(out))).start();

        }
        else
        {
            System.out.println("Error: Only serial ports are handled by this example.");
        }
    }     
}

/** */
public static class SerialReader implements Runnable 
{
    InputStream in;

    public SerialReader ( InputStream in )
    {
        this.in = in;
    }

    public void run ()
    {
        byte[] buffer = new byte[1024];
        int len = -1;
        try
        {
            while ( ( len = this.in.read(buffer)) > -1 )
            {
                System.out.print(new String(buffer,0,len));

                try {
                //"0001,0004,dd/mm/yy hh:mm:ss,01,18,750"
                //String string = new String(buffer,0,len);

                //String[] parts = string.split(",");

                //String temp = parts[0];
                //System.out.print(temp);
                //String lum = parts[5];

                // sendGet(lum, temp, String.valueOf(System.currentTimeMillis() / 1000));
                } catch (Exception ex) {
                    Logger.getLogger(Wasp.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        catch ( IOException e )
        {
            e.printStackTrace();
        }            
    }
}

public static class SerialWriter implements Runnable { OutputStream out;

    public SerialWriter ( OutputStream out )
    {
        this.out = out;
    }

    public void run ()
    {
        try
        {                
            int c = 0;
            while ( ( c = System.in.read()) > -1 )
            {
                this.out.write(c);
            }                
        }
        catch ( IOException e )
        {
            e.printStackTrace();
        }            
    }
}

public static void main ( String[] args )
{
    try
    {
        (new Wasp()).connect("COM3");
    }
    catch ( Exception e )
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

asked 37 secs ago

Let's block ads! (Why?)



Splitting a String from a String Buffer

Aucun commentaire:

Enregistrer un commentaire