Pages: [1]   Go Down
Author Topic: Blank serial char[] from Java  (Read 391 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am using a Arduino Uno.  I am experiencing weird behavior with Serial.readbytes();

My currently Arduino code looks like this, the delays are so I can start and stop my java service and look at the serial window in the Arduino IDE, in is a char array;
Code:
void loop() {

       //Get Command
        if (Serial.available() > 0) {
                // read the incoming byte:
               
               Serial.readBytes(in, 10);
               delay(5000);
               Serial.print("I got this ->");
               Serial.print(in);
               Serial.println("<-");
                //newin = true;
               
        }
        delay(5000);
        Serial.println("delay");
       
        Serial.println(in);
       
}

Here is the relevant code for my java side. I modified it from, http://rxtx.qbang.org/wiki/index.php/Two_way_communcation_with_the_serial_port
Code:
public static class SerialWriter implements Runnable
    {
        OutputStream out1;
        DatagramSocket socket = null;
        public byte[] buf = new byte[4];
        public DatagramPacket packet = new DatagramPacket(buf, buf.length);
       
       
        public SerialWriter ( OutputStream out )
        {
            this.out1 = out;
        }
       
        public void run ()
        {
       
        try
        {     
        socket = new DatagramSocket(9876);
        socket.setSoTimeout(1000);
        int c = 5;
        //System.out.print("before");
        byte[] test = ("H567").getBytes();
        this.out1.write(test);
                       
        }
        catch ( IOException e )
        {
        e.printStackTrace();
        }
        }
    }

When I run it my arduino just prints out a blank line. If I prep the arduino with "hiya" using the Serial window first, the when the java is executed it will return back to a blank line. Since the char array is just over-written each time I sent "H567\r\n" and then in the Serial window typed sent "hiya" where the extra newline was still being executed so I know characters are being stored somehow. Another test was to change the last Serial.prinln() to "Serial.println(in[0], DEC)". Using the Serial Window results happen as expected, but from java it just prints out "0"
Serial communication works wonderful coming from the arduino talking to the java, just not from java to the arduino. Any insight would be wonderful!
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Per suggestion of a friend. I changed the read from (in, 10) to (in, Serial.avialable()) and has not changed any results.
Logged

Pages: [1]   Go Up
Jump to: